blob: d6723b27ef422a9c201155a113ae9e46e3b561ad [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
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020088
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020089#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
91 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020092#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020093#endif
94
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010095/****************************************************************/
96/* Global data, support functions and library management */
97/****************************************************************/
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100{
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102}
103
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100104/* Values for psa_global_data_t::rng_state */
105#define RNG_NOT_INITIALIZED 0
106#define RNG_INITIALIZED 1
107#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100110 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100111 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100112 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100113 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114} psa_global_data_t;
115
116static psa_global_data_t global_data;
117
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100118#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
119mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
120 &global_data.rng.drbg;
121#endif
122
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (global_data.initialized == 0) \
125 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100127int psa_can_do_hash(psa_algorithm_t hash_alg)
128{
129 (void) hash_alg;
130 return global_data.drivers_initialized;
131}
Valerio Setti8bb57632023-05-26 13:48:07 +0200132#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200133 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Setti8bb57632023-05-26 13:48:07 +0200134 defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200135static int psa_is_dh_key_size_valid(size_t bits)
136{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200137 if (bits != 2048 && bits != 3072 && bits != 4096 &&
138 bits != 6144 && bits != 8192) {
139 return 0;
140 }
141
142 return 1;
143}
Valerio Setti8bb57632023-05-26 13:48:07 +0200144#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200145 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Setti8bb57632023-05-26 13:48:07 +0200146 MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100149{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100150 /* Mbed TLS error codes can combine a high-level error code and a
151 * low-level error code. The low-level error usually reflects the
152 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 int low_level_ret = -(-ret & 0x007f);
154 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100155 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100157
158 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
159 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200161 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
162 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
163 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
164 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
165 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200167 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200169 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182
Gilles Peskine26869f22019-05-06 15:25:00 +0200183 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200185
186 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200190
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
207 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100208 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
209 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
213 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100217#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100218
219 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100221
Gilles Peskinee59236f2018-01-27 23:32:46 +0100222 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
223 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
224 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100226
227 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200229 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100236 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
237 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100238 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100240 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
241 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100245#endif
246
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
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_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
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_DIVISION_BY_ZERO:
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_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200296 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100312 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
313 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100334 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100336
Paul Elliott588f8ed2022-12-02 18:10:26 +0000337 case MBEDTLS_ERR_ECP_IN_PROGRESS:
338 return PSA_OPERATION_INCOMPLETE;
339
Janos Follatha13b9052019-11-22 12:48:59 +0000340 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300342
Gilles Peskinee59236f2018-01-27 23:32:46 +0100343 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100345 }
346}
347
Paul Elliottdc42ca82023-02-24 18:11:59 +0000348/**
349 * \brief For output buffers which contain "tags"
350 * (outputs that may be checked for validity like
351 * hashes, MACs and signatures), fill the unused
352 * part of the output buffer (the whole buffer on
353 * error, the trailing part on success) with
354 * something that isn't a valid tag (barring an
355 * attack on the tag and deliberately-crafted
356 * input), in case the caller doesn't check the
357 * return status properly.
358 *
359 * \param output_buffer Pointer to buffer to wipe. May not be NULL
360 * unless \p output_buffer_size is zero.
361 * \param status Status of function called to generate
362 * output_buffer originally
363 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
364 * could be NULL.
365 * \param output_buffer_length Length of data written to output_buffer, must be
366 * less than \p output_buffer_size
367 */
368static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000369 size_t output_buffer_size, size_t output_buffer_length)
370{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000371 size_t offset = 0;
372
373 if (output_buffer_size == 0) {
374 /* If output_buffer_size is 0 then we have nothing to do. We must not
375 call memset because output_buffer may be NULL in this case */
376 return;
377 }
378
379 if (status == PSA_SUCCESS) {
380 offset = output_buffer_length;
381 }
382
383 memset(output_buffer + offset, '!', output_buffer_size - offset);
384}
385
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200386
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200387
388
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100389/****************************************************************/
390/* Key management */
391/****************************************************************/
392
Valerio Setti81d75122023-06-14 14:49:33 +0200393#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200394psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
395 size_t *bits)
396{
397 switch (grpid) {
398 case MBEDTLS_ECP_DP_SECP192R1:
399 *bits = 192;
400 return PSA_ECC_FAMILY_SECP_R1;
401 case MBEDTLS_ECP_DP_SECP224R1:
402 *bits = 224;
403 return PSA_ECC_FAMILY_SECP_R1;
404 case MBEDTLS_ECP_DP_SECP256R1:
405 *bits = 256;
406 return PSA_ECC_FAMILY_SECP_R1;
407 case MBEDTLS_ECP_DP_SECP384R1:
408 *bits = 384;
409 return PSA_ECC_FAMILY_SECP_R1;
410 case MBEDTLS_ECP_DP_SECP521R1:
411 *bits = 521;
412 return PSA_ECC_FAMILY_SECP_R1;
413 case MBEDTLS_ECP_DP_BP256R1:
414 *bits = 256;
415 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
416 case MBEDTLS_ECP_DP_BP384R1:
417 *bits = 384;
418 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
419 case MBEDTLS_ECP_DP_BP512R1:
420 *bits = 512;
421 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
422 case MBEDTLS_ECP_DP_CURVE25519:
423 *bits = 255;
424 return PSA_ECC_FAMILY_MONTGOMERY;
425 case MBEDTLS_ECP_DP_SECP192K1:
426 *bits = 192;
427 return PSA_ECC_FAMILY_SECP_K1;
428 case MBEDTLS_ECP_DP_SECP224K1:
429 *bits = 224;
430 return PSA_ECC_FAMILY_SECP_K1;
431 case MBEDTLS_ECP_DP_SECP256K1:
432 *bits = 256;
433 return PSA_ECC_FAMILY_SECP_K1;
434 case MBEDTLS_ECP_DP_CURVE448:
435 *bits = 448;
436 return PSA_ECC_FAMILY_MONTGOMERY;
437 default:
438 *bits = 0;
439 return 0;
440 }
441}
442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
444 size_t bits,
445 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200446{
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100448 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#endif
454#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#endif
458#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100461#endif
462#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100463 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100465#endif
466#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100467 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (bits_is_sloppy) {
471 return MBEDTLS_ECP_DP_SECP521R1;
472 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100474#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100475 }
476 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100477
Paul Elliott8ff510a2020-06-02 17:19:28 +0100478 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100480#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100481 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100483#endif
484#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100485 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100487#endif
488#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100489 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100491#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100492 }
493 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100494
Paul Elliott8ff510a2020-06-02 17:19:28 +0100495 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100497#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100498 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100500 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if (bits_is_sloppy) {
502 return MBEDTLS_ECP_DP_CURVE25519;
503 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100504 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100505#endif
506#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100507 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100509#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100510 }
511 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100512
Paul Elliott8ff510a2020-06-02 17:19:28 +0100513 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100515#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100516 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100518#endif
519#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100520 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100522#endif
523#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100524 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100526#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100527 }
528 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200529 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100530
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100531 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200533}
Valerio Setti81d75122023-06-14 14:49:33 +0200534#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
537 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200538{
539 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200541 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200542 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200543 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200544 case PSA_KEY_TYPE_PASSWORD:
545 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200546 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100547#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200548 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (bits != 128 && bits != 192 && bits != 256) {
550 return PSA_ERROR_INVALID_ARGUMENT;
551 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200552 break;
553#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200554#if defined(PSA_WANT_KEY_TYPE_ARIA)
555 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (bits != 128 && bits != 192 && bits != 256) {
557 return PSA_ERROR_INVALID_ARGUMENT;
558 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200559 break;
560#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100561#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200562 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (bits != 128 && bits != 192 && bits != 256) {
564 return PSA_ERROR_INVALID_ARGUMENT;
565 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200566 break;
567#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100568#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200569 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (bits != 64 && bits != 128 && bits != 192) {
571 return PSA_ERROR_INVALID_ARGUMENT;
572 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200573 break;
574#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100575#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200576 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (bits != 256) {
578 return PSA_ERROR_INVALID_ARGUMENT;
579 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200580 break;
581#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200582 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200584 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (bits % 8 != 0) {
586 return PSA_ERROR_INVALID_ARGUMENT;
587 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200590}
591
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100592/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100593 *
Steven Cooremancd640932021-03-08 12:00:27 +0100594 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
595 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100596 *
597 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100598 * \param[in] key_type The key type of the key to be used with the
599 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100600 *
601 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100602 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100603 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100604 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100605 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100606MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100607 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100609{
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (PSA_ALG_IS_HMAC(algorithm)) {
611 if (key_type == PSA_KEY_TYPE_HMAC) {
612 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100613 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100614 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
617 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
618 * key. */
619 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
620 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
621 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
622 * the block length (larger than 1) for block ciphers. */
623 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
624 return PSA_SUCCESS;
625 }
626 }
627 }
628
629 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100630}
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
633 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200634{
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (slot->key.data != NULL) {
636 return PSA_ERROR_ALREADY_EXISTS;
637 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 slot->key.data = mbedtls_calloc(1, buffer_length);
640 if (slot->key.data == NULL) {
641 return PSA_ERROR_INSUFFICIENT_MEMORY;
642 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200643
Ronald Cronea0f8a62020-11-25 17:52:23 +0100644 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200646}
647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
649 const uint8_t *data,
650 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200651{
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 psa_status_t status = psa_allocate_buffer_to_slot(slot,
653 data_length);
654 if (status != PSA_SUCCESS) {
655 return status;
656 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 memcpy(slot->key.data, data, data_length);
659 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200660}
661
Ronald Crona0fe59f2020-11-29 11:14:53 +0100662psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100663 const psa_key_attributes_t *attributes,
664 const uint8_t *data, size_t data_length,
665 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100668 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
669 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100670
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200671 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (data_length == 0) {
673 return PSA_ERROR_NOT_SUPPORTED;
674 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (key_type_is_raw_bytes(type)) {
677 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
680 *bits);
681 if (status != PSA_SUCCESS) {
682 return status;
683 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200684
Ronald Crondd04d422020-11-28 15:14:42 +0100685 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100687 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return PSA_SUCCESS;
691 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +0200692#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200693 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100694 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200695 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100696 return PSA_ERROR_INVALID_ARGUMENT;
697 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200698 return mbedtls_psa_ffdh_import_key(attributes,
699 data, data_length,
700 key_buffer, key_buffer_size,
701 key_buffer_length,
702 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100703 }
Valerio Setti8bb57632023-05-26 13:48:07 +0200704#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200705 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti8bb57632023-05-26 13:48:07 +0200706#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
708 if (PSA_KEY_TYPE_IS_ECC(type)) {
709 return mbedtls_psa_ecp_import_key(attributes,
710 data, data_length,
711 key_buffer, key_buffer_size,
712 key_buffer_length,
713 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100714 }
Valerio Setti8bb57632023-05-26 13:48:07 +0200715#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -0800716 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Setti8bb57632023-05-26 13:48:07 +0200717#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
719 if (PSA_KEY_TYPE_IS_RSA(type)) {
720 return mbedtls_psa_rsa_import_key(attributes,
721 data, data_length,
722 key_buffer, key_buffer_size,
723 key_buffer_length,
724 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200725 }
Valerio Setti8bb57632023-05-26 13:48:07 +0200726#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
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 Setti8bb57632023-05-26 13:48:07 +02001254#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
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 Setti8bb57632023-05-26 13:48:07 +02001296#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
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 Setti8bb57632023-05-26 13:48:07 +02001327#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
John Durkop0e005192020-10-31 22:06:54 -07001328 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001329 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001330 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001331 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001332 * is not yet implemented.
1333 * https://github.com/ARMmbed/mbed-crypto/issues/216
1334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001336 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001337
Ronald Cron90857082020-11-25 14:58:33 +01001338 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 slot->attr.type,
1340 slot->key.data,
1341 slot->key.bytes,
1342 &rsa);
1343 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001344 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 status = psa_get_rsa_public_exponent(rsa,
1348 attributes);
1349 mbedtls_rsa_free(rsa);
1350 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001351 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001352 break;
Valerio Setti8bb57632023-05-26 13:48:07 +02001353#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001354 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001355 default:
1356 /* Nothing else to do. */
1357 break;
1358 }
1359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (status != PSA_SUCCESS) {
1361 psa_reset_key_attributes(attributes);
1362 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001367}
1368
Gilles Peskinec8000c02019-08-02 20:15:51 +02001369#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1370psa_status_t psa_get_key_slot_number(
1371 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001373{
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001375 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 return PSA_SUCCESS;
1377 } else {
1378 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001379 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001380}
1381#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1384 size_t key_buffer_size,
1385 uint8_t *data,
1386 size_t data_size,
1387 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001388{
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if (key_buffer_size > data_size) {
1390 return PSA_ERROR_BUFFER_TOO_SMALL;
1391 }
1392 memcpy(data, key_buffer, key_buffer_size);
1393 memset(data + key_buffer_size, 0,
1394 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001395 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001397}
1398
Ronald Cron7285cda2020-11-26 14:46:37 +01001399psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001400 const psa_key_attributes_t *attributes,
1401 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001403{
Ronald Crond18b5f82020-11-25 16:46:05 +01001404 psa_key_type_t type = attributes->core.type;
1405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (key_type_is_raw_bytes(type) ||
1407 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001408 PSA_KEY_TYPE_IS_ECC(type) ||
1409 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 return psa_export_key_buffer_internal(
1411 key_buffer, key_buffer_size,
1412 data, data_size, data_length);
1413 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001414 /* This shouldn't happen in the reference implementation, but
1415 it is valid for a special-purpose implementation to omit
1416 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001418 }
1419}
1420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1422 uint8_t *data,
1423 size_t data_size,
1424 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001425{
1426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1427 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1428 psa_key_slot_t *slot;
1429
Ronald Crone907e552021-01-18 13:22:38 +01001430 /* Reject a zero-length output buffer now, since this can never be a
1431 * valid key representation. This way we know that data must be a valid
1432 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (data_size == 0) {
1434 return PSA_ERROR_BUFFER_TOO_SMALL;
1435 }
Ronald Crone907e552021-01-18 13:22:38 +01001436
Ronald Cron9486f9d2020-11-26 13:36:23 +01001437 /* Set the key to empty now, so that even when there are errors, we always
1438 * set data_length to a value between 0 and data_size. On error, setting
1439 * the key to empty is a good choice because an empty key representation is
1440 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001441 *data_length = 0;
1442
Ronald Cron9486f9d2020-11-26 13:36:23 +01001443 /* Export requires the EXPORT flag. There is an exception for public keys,
1444 * which don't require any flag, but
1445 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1448 PSA_KEY_USAGE_EXPORT, 0);
1449 if (status != PSA_SUCCESS) {
1450 return status;
1451 }
mohammad160306e79202018-03-28 13:17:44 +03001452
Ronald Crond18b5f82020-11-25 16:46:05 +01001453 psa_key_attributes_t attributes = {
1454 .core = slot->attr
1455 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 status = psa_driver_wrapper_export_key(&attributes,
1457 slot->key.data, slot->key.bytes,
1458 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001463}
1464
Ronald Cron7285cda2020-11-26 14:46:37 +01001465psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001466 const psa_key_attributes_t *attributes,
1467 const uint8_t *key_buffer,
1468 size_t key_buffer_size,
1469 uint8_t *data,
1470 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001472{
Ronald Crond18b5f82020-11-25 16:46:05 +01001473 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001474
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001475 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001476 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1477 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001478 /* Exporting public -> public */
1479 return psa_export_key_buffer_internal(
1480 key_buffer, key_buffer_size,
1481 data, data_size, data_length);
1482 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001483#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001484 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1485 return mbedtls_psa_rsa_export_public_key(attributes,
1486 key_buffer,
1487 key_buffer_size,
1488 data,
1489 data_size,
1490 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001491#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001492 /* We don't know how to convert a private RSA key to public. */
1493 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti8bb57632023-05-26 13:48:07 +02001494#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001495 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001496 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001497#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001498 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1499 return mbedtls_psa_ecp_export_public_key(attributes,
1500 key_buffer,
1501 key_buffer_size,
1502 data,
1503 data_size,
1504 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001505#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001506 /* We don't know how to convert a private ECC key to public */
1507 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti8bb57632023-05-26 13:48:07 +02001508#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001509 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001510 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001511#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001512 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001513 return mbedtls_psa_export_ffdh_public_key(attributes,
1514 key_buffer,
1515 key_buffer_size,
1516 data, data_size,
1517 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001518#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001519 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti8bb57632023-05-26 13:48:07 +02001520#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001521 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001523 (void) key_buffer;
1524 (void) key_buffer_size;
1525 (void) data;
1526 (void) data_size;
1527 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001529 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001530}
Ronald Crond18b5f82020-11-25 16:46:05 +01001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1533 uint8_t *data,
1534 size_t data_size,
1535 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001536{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001537 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001538 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001539 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001540
Ronald Crone907e552021-01-18 13:22:38 +01001541 /* Reject a zero-length output buffer now, since this can never be a
1542 * valid key representation. This way we know that data must be a valid
1543 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (data_size == 0) {
1545 return PSA_ERROR_BUFFER_TOO_SMALL;
1546 }
Ronald Crone907e552021-01-18 13:22:38 +01001547
Darryl Greendd8fb772018-11-07 16:00:44 +00001548 /* Set the key to empty now, so that even when there are errors, we always
1549 * set data_length to a value between 0 and data_size. On error, setting
1550 * the key to empty is a good choice because an empty key representation is
1551 * unlikely to be accepted anywhere. */
1552 *data_length = 0;
1553
1554 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1556 if (status != PSA_SUCCESS) {
1557 return status;
1558 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1561 status = PSA_ERROR_INVALID_ARGUMENT;
1562 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001563 }
1564
Ronald Crond18b5f82020-11-25 16:46:05 +01001565 psa_key_attributes_t attributes = {
1566 .core = slot->attr
1567 };
Ronald Cron67227982020-11-26 15:16:05 +01001568 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001569 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001571
1572exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001576}
1577
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001578MBEDTLS_STATIC_ASSERT(
1579 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1580 "One or more key attribute flag is listed as both external-only and dual-use")
1581MBEDTLS_STATIC_ASSERT(
1582 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1583 "One or more key attribute flag is listed as both internal-only and dual-use")
1584MBEDTLS_STATIC_ASSERT(
1585 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1586 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001587
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001588/** Validate that a key policy is internally well-formed.
1589 *
1590 * This function only rejects invalid policies. It does not validate the
1591 * consistency of the policy with respect to other attributes of the key
1592 * such as the key type.
1593 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001594static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001595{
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1597 PSA_KEY_USAGE_COPY |
1598 PSA_KEY_USAGE_ENCRYPT |
1599 PSA_KEY_USAGE_DECRYPT |
1600 PSA_KEY_USAGE_SIGN_MESSAGE |
1601 PSA_KEY_USAGE_VERIFY_MESSAGE |
1602 PSA_KEY_USAGE_SIGN_HASH |
1603 PSA_KEY_USAGE_VERIFY_HASH |
1604 PSA_KEY_USAGE_VERIFY_DERIVATION |
1605 PSA_KEY_USAGE_DERIVE)) != 0) {
1606 return PSA_ERROR_INVALID_ARGUMENT;
1607 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001610}
1611
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001612/** Validate the internal consistency of key attributes.
1613 *
1614 * This function only rejects invalid attribute values. If does not
1615 * validate the consistency of the attributes with any key data that may
1616 * be involved in the creation of the key.
1617 *
1618 * Call this function early in the key creation process.
1619 *
1620 * \param[in] attributes Key attributes for the new key.
1621 * \param[out] p_drv On any return, the driver for the key, if any.
1622 * NULL for a transparent key.
1623 *
1624 */
1625static psa_status_t psa_validate_key_attributes(
1626 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001628{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001629 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1631 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 status = psa_validate_key_location(lifetime, p_drv);
1634 if (status != PSA_SUCCESS) {
1635 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001636 }
1637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 status = psa_validate_key_persistence(lifetime);
1639 if (status != PSA_SUCCESS) {
1640 return status;
1641 }
1642
1643 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1644 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1645 return PSA_ERROR_INVALID_ARGUMENT;
1646 }
1647 } else {
1648 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1649 return PSA_ERROR_INVALID_ARGUMENT;
1650 }
1651 }
1652
1653 status = psa_validate_key_policy(&attributes->core.policy);
1654 if (status != PSA_SUCCESS) {
1655 return status;
1656 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001657
1658 /* Refuse to create overly large keys.
1659 * Note that this doesn't trigger on import if the attributes don't
1660 * explicitly specify a size (so psa_get_key_bits returns 0), so
1661 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1663 return PSA_ERROR_NOT_SUPPORTED;
1664 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001665
Gilles Peskine91e8c332019-08-02 19:19:39 +02001666 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1668 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1669 return PSA_ERROR_INVALID_ARGUMENT;
1670 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001673}
1674
Gilles Peskine4747d192019-04-17 15:05:45 +02001675/** Prepare a key slot to receive key material.
1676 *
1677 * This function allocates a key slot and sets its metadata.
1678 *
1679 * If this function fails, call psa_fail_key_creation().
1680 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001681 * This function is intended to be used as follows:
1682 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001683 * it with the specified attributes, and in case of a volatile key assign it
1684 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001685 * -# Populate the slot with the key material.
1686 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1687 * In case of failure at any step, stop the sequence and call
1688 * psa_fail_key_creation().
1689 *
Ronald Cron5c522922020-11-14 16:35:34 +01001690 * On success, the key slot is locked. It is the responsibility of the caller
1691 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001692 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001693 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001694 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001695 * \param[out] p_slot On success, a pointer to the prepared slot.
1696 * \param[out] p_drv On any return, the driver for the key, if any.
1697 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001698 *
1699 * \retval #PSA_SUCCESS
1700 * The key slot is ready to receive key material.
1701 * \return If this function fails, the key slot is an invalid state.
1702 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001703 */
1704static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001705 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001706 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001707 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001709{
1710 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001711 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001712 psa_key_slot_t *slot;
1713
Gilles Peskinedf179142019-07-15 22:02:14 +02001714 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001715 *p_drv = NULL;
1716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 status = psa_validate_key_attributes(attributes, p_drv);
1718 if (status != PSA_SUCCESS) {
1719 return status;
1720 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001721
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1723 if (status != PSA_SUCCESS) {
1724 return status;
1725 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 slot = *p_slot;
1727
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001728 /* We're storing the declared bit-size of the key. It's up to each
1729 * creation mechanism to verify that this information is correct.
1730 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001731 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001732 * is optional (import, copy). In case of a volatile key, assign it the
1733 * volatile key identifier associated to the slot returned to contain its
1734 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001735
1736 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001738#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1739 slot->attr.id = volatile_key_id;
1740#else
1741 slot->attr.id.key_id = volatile_key_id;
1742#endif
1743 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001744
Gilles Peskine91e8c332019-08-02 19:19:39 +02001745 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001746 * external-only flags, query `attributes`. Thanks to the check
1747 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001748 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001749 * may have set. */
1750 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001751
Gilles Peskinecbaff462019-07-12 23:46:04 +02001752#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001753 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001754 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001755 * create the key file in internal storage, create the
1756 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001757 * persistent data. This is done by starting a transaction that will
1758 * encompass these three actions.
1759 * For registering a volatile key, we just need to find an appropriate
1760 * slot number inside the SE. Since the key is designated volatile, creating
1761 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001762 /* The first thing to do is to find a slot number for the new key.
1763 * We save the slot number in persistent storage as part of the
1764 * transaction data. It will be needed to recover if the power
1765 * fails during the key creation process, to clean up on the secure
1766 * element side after restarting. Obtaining a slot number from the
1767 * secure element driver updates its persistent state, but we do not yet
1768 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001769 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001771 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1773 &slot_number);
1774 if (status != PSA_SUCCESS) {
1775 return status;
1776 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1779 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001780 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001781 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001782 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 status = psa_crypto_save_transaction();
1784 if (status != PSA_SUCCESS) {
1785 (void) psa_crypto_stop_transaction();
1786 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001787 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001788 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001789
1790 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001792 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001795 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001797 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001798#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001801}
Gilles Peskine4747d192019-04-17 15:05:45 +02001802
1803/** Finalize the creation of a key once its key material has been set.
1804 *
1805 * This entails writing the key to persistent storage.
1806 *
1807 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001808 * See the documentation of psa_start_key_creation() for the intended use
1809 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001810 *
Ronald Cron5c522922020-11-14 16:35:34 +01001811 * If the finalization succeeds, the function unlocks the key slot (it was
1812 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1813 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001814 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001815 * \param[in,out] slot Pointer to the slot with key material.
1816 * \param[in] driver The secure element driver for the key,
1817 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001818 * \param[out] key On success, identifier of the key. Note that the
1819 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001820 *
1821 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001822 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001823 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1824 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1825 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1826 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1827 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1828 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001829 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001830 * \return If this function fails, the key slot is an invalid state.
1831 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001832 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001833static psa_status_t psa_finish_key_creation(
1834 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001835 psa_se_drv_table_entry_t *driver,
1836 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001837{
1838 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001839 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001840 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001841
1842#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001844#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001846 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001847 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001849
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001850 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1851 sizeof(data.slot_number),
1852 "Slot number size does not match psa_se_key_data_storage_t");
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1855 status = psa_save_persistent_key(&slot->attr,
1856 (uint8_t *) &data,
1857 sizeof(data));
1858 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001859#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1860 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001861 /* Key material is saved in export representation in the slot, so
1862 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 status = psa_save_persistent_key(&slot->attr,
1864 slot->key.data,
1865 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001866 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001867 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001868#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1869
Gilles Peskinecbaff462019-07-12 23:46:04 +02001870#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001871 /* Finish the transaction for a key creation. This does not
1872 * happen when registering an existing key. Detect this case
1873 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001874 * creation of a persistent key in a secure element requires a transaction,
1875 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (driver != NULL &&
1877 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1878 status = psa_save_se_persistent_data(driver);
1879 if (status != PSA_SUCCESS) {
1880 psa_destroy_persistent_key(slot->attr.id);
1881 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001882 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001884 }
1885#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001888 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 status = psa_unlock_key_slot(slot);
1890 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001891 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001893 }
Ronald Cron50972942020-11-14 11:28:25 +01001894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001896}
1897
1898/** Abort the creation of a key.
1899 *
1900 * You may call this function after calling psa_start_key_creation(),
1901 * or after psa_finish_key_creation() fails. In other circumstances, this
1902 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001903 * See the documentation of psa_start_key_creation() for the intended use
1904 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001905 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001906 * \param[in,out] slot Pointer to the slot with key material.
1907 * \param[in] driver The secure element driver for the key,
1908 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001910static void psa_fail_key_creation(psa_key_slot_t *slot,
1911 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001912{
Gilles Peskine011e4282019-06-26 18:34:38 +02001913 (void) driver;
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001916 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001918
1919#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001920 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001921 * element, and the failure happened later (when saving metadata
1922 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001923 * element.
1924 * https://github.com/ARMmbed/mbed-crypto/issues/217
1925 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001926
Gilles Peskined7729582019-08-05 15:55:54 +02001927 /* Abort the ongoing transaction if any (there may not be one if
1928 * the creation process failed before starting one, or if the
1929 * key creation is a registration of a key in a secure element).
1930 * Earlier functions must already have done what it takes to undo any
1931 * partial creation. All that's left is to update the transaction data
1932 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001934#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001937}
1938
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001939/** Validate optional attributes during key creation.
1940 *
1941 * Some key attributes are optional during key creation. If they are
1942 * specified in the attributes structure, check that they are consistent
1943 * with the data in the slot.
1944 *
1945 * This function should be called near the end of key creation, after
1946 * the slot in memory is fully populated but before saving persistent data.
1947 */
1948static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951{
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 if (attributes->core.type != 0) {
1953 if (attributes->core.type != slot->attr.type) {
1954 return PSA_ERROR_INVALID_ARGUMENT;
1955 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001956 }
1957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if (attributes->domain_parameters_size != 0) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001959#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1961 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001962 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001963 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001964 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001965
Ronald Cron90857082020-11-25 14:58:33 +01001966 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 slot->attr.type,
1968 slot->key.data,
1969 slot->key.bytes,
1970 &rsa);
1971 if (status != PSA_SUCCESS) {
1972 return status;
1973 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 mbedtls_mpi_init(&actual);
1976 mbedtls_mpi_init(&required);
1977 ret = mbedtls_rsa_export(rsa,
1978 NULL, NULL, NULL, NULL, &actual);
1979 mbedtls_rsa_free(rsa);
1980 mbedtls_free(rsa);
1981 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001982 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 }
1984 ret = mbedtls_mpi_read_binary(&required,
1985 attributes->domain_parameters,
1986 attributes->domain_parameters_size);
1987 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 }
1990 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 }
1993rsa_exit:
1994 mbedtls_mpi_free(&actual);
1995 mbedtls_mpi_free(&required);
1996 if (ret != 0) {
1997 return mbedtls_to_psa_error(ret);
1998 }
1999 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02002000#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08002001 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002002 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004 }
2005 }
2006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 if (attributes->core.bits != 0) {
2008 if (attributes->core.bits != slot->attr.bits) {
2009 return PSA_ERROR_INVALID_ARGUMENT;
2010 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011 }
2012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002014}
2015
Gilles Peskine449bd832023-01-11 14:50:10 +01002016psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2017 const uint8_t *data,
2018 size_t data_length,
2019 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002020{
2021 psa_status_t status;
2022 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002023 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002024 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302025 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002026
Ronald Cron81709fc2020-11-14 12:10:32 +01002027 *key = MBEDTLS_SVC_KEY_ID_INIT;
2028
Gilles Peskine0f84d622019-09-12 19:03:13 +02002029 /* Reject zero-length symmetric keys (including raw data key objects).
2030 * This also rejects any key which might be encoded as an empty string,
2031 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 if (data_length == 0) {
2033 return PSA_ERROR_INVALID_ARGUMENT;
2034 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002035
Archana449608b2021-09-08 15:36:05 +05302036 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 if (data_length > SIZE_MAX / 8) {
2038 return PSA_ERROR_NOT_SUPPORTED;
2039 }
Archana6ed4bda2021-08-04 10:47:15 +05302040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2042 &slot, &driver);
2043 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002044 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002046
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002047 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302048 * storage ( thus not in the case of importing a key in a secure element
2049 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2050 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 if (slot->key.data == NULL) {
2052 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302053 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 attributes, data, data_length, &storage_size);
2055 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302056 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 }
Archanad8a83dc2021-06-14 10:04:16 +05302058 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 status = psa_allocate_buffer_to_slot(slot, storage_size);
2060 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002061 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002063 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002064
2065 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 status = psa_driver_wrapper_import_key(attributes,
2067 data, data_length,
2068 slot->key.data,
2069 slot->key.bytes,
2070 &slot->key.bytes, &bits);
2071 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002072 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002076 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002078 status = PSA_ERROR_INVALID_ARGUMENT;
2079 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002080 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002081
Archana6ed4bda2021-08-04 10:47:15 +05302082 /* Enforce a size limit, and in particular ensure that the bit
2083 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302085 status = PSA_ERROR_NOT_SUPPORTED;
2086 goto exit;
2087 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 status = psa_validate_optional_attributes(slot, attributes);
2089 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002090 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002094exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (status != PSA_SUCCESS) {
2096 psa_fail_key_creation(slot, driver);
2097 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002100}
2101
Gilles Peskined7729582019-08-05 15:55:54 +02002102#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2103psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002105{
2106 psa_status_t status;
2107 psa_key_slot_t *slot = NULL;
2108 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002109 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002110
2111 /* Leaving attributes unspecified is not currently supported.
2112 * It could make sense to query the key type and size from the
2113 * secure element, but not all secure elements support this
2114 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2116 return PSA_ERROR_NOT_SUPPORTED;
2117 }
2118 if (psa_get_key_bits(attributes) == 0) {
2119 return PSA_ERROR_NOT_SUPPORTED;
2120 }
Gilles Peskined7729582019-08-05 15:55:54 +02002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2123 &slot, &driver);
2124 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 }
Gilles Peskined7729582019-08-05 15:55:54 +02002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002129
2130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 if (status != PSA_SUCCESS) {
2132 psa_fail_key_creation(slot, driver);
2133 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002134
Gilles Peskined7729582019-08-05 15:55:54 +02002135 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 psa_close_key(key);
2137 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002138}
2139#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2142 const psa_key_attributes_t *specified_attributes,
2143 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002144{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002146 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002147 psa_key_slot_t *source_slot = NULL;
2148 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002149 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002150 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302151 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002152
Ronald Cron81709fc2020-11-14 12:10:32 +01002153 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2154
Archana8a180362021-07-05 02:18:48 +05302155 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2157 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002158 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 status = psa_validate_optional_attributes(source_slot,
2162 specified_attributes);
2163 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002166
Archana9d17bf42021-09-10 06:22:44 +05302167 /* The target key type and number of bits have been validated by
2168 * psa_validate_optional_attributes() to be either equal to zero or
2169 * equal to the ones of the source key. So it is safe to inherit
2170 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302171 * */
Archana9d17bf42021-09-10 06:22:44 +05302172 actual_attributes.core.bits = source_slot->attr.bits;
2173 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302174
2175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 status = psa_restrict_key_policy(source_slot->attr.type,
2177 &actual_attributes.core.policy,
2178 &source_slot->attr.policy);
2179 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2184 &target_slot, &driver);
2185 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002186 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 }
2188 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2189 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302190 /*
Archana9d17bf42021-09-10 06:22:44 +05302191 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302192 * the source key would need to be exported as plaintext and re-imported
2193 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302194 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302195 * appropriate API invocations from the application, if needed.
2196 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002197 status = PSA_ERROR_NOT_SUPPORTED;
2198 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002199 }
Archana8a180362021-07-05 02:18:48 +05302200 /*
2201 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302202 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302203 * - For opaque keys this translates to an invocation of the drivers'
2204 * copy_key entry point through the dispatch layer.
2205 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2207 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2208 &storage_size);
2209 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302210 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 }
Archana374fe5b2021-09-08 15:50:28 +05302212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 status = psa_allocate_buffer_to_slot(target_slot, 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_driver_wrapper_copy_key(&actual_attributes,
2219 source_slot->key.data,
2220 source_slot->key.bytes,
2221 target_slot->key.data,
2222 target_slot->key.bytes,
2223 &target_slot->key.bytes);
2224 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302225 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 }
2227 } else {
2228 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302229 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 source_slot->key.bytes);
2231 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302232 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 }
Archana8a180362021-07-05 02:18:48 +05302234 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (status != PSA_SUCCESS) {
2238 psa_fail_key_creation(target_slot, driver);
2239 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002244}
2245
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002246
2247
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002248/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002249/* Message digests */
2250/****************************************************************/
2251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002253{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002254 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if (operation->id == 0) {
2256 return PSA_SUCCESS;
2257 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002260 operation->id = 0;
2261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002263}
2264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2266 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002268 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002269
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002270 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002272 status = PSA_ERROR_BAD_STATE;
2273 goto exit;
2274 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002277 status = PSA_ERROR_INVALID_ARGUMENT;
2278 goto exit;
2279 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002280
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002281 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2282 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002286
2287exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (status != PSA_SUCCESS) {
2289 psa_hash_abort(operation);
2290 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002291
2292 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002293}
2294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2296 const uint8_t *input,
2297 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002298{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002299 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002302 status = PSA_ERROR_BAD_STATE;
2303 goto exit;
2304 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002305
Steven Cooremanc8288352021-03-04 14:02:19 +01002306 /* Don't require hash implementations to behave correctly on a
2307 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 if (input_length == 0) {
2309 return PSA_SUCCESS;
2310 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002313
2314exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (status != PSA_SUCCESS) {
2316 psa_hash_abort(operation);
2317 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320}
2321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2323 uint8_t *hash,
2324 size_t hash_size,
2325 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002327 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (operation->id == 0) {
2329 return PSA_ERROR_BAD_STATE;
2330 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331
Steven Cooreman1e582352021-02-18 17:24:37 +01002332 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 operation, hash, hash_size, hash_length);
2334 psa_hash_abort(operation);
2335 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336}
2337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2339 const uint8_t *hash,
2340 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341{
Ronald Cron69a63422021-10-18 09:47:58 +02002342 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002344 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 operation,
2346 actual_hash, sizeof(actual_hash),
2347 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002350 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002354 status = PSA_ERROR_INVALID_SIGNATURE;
2355 goto exit;
2356 }
2357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002359 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002361
2362exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2364 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002365 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002369}
2370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371psa_status_t psa_hash_compute(psa_algorithm_t alg,
2372 const uint8_t *input, size_t input_length,
2373 uint8_t *hash, size_t hash_size,
2374 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002375{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002376 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (!PSA_ALG_IS_HASH(alg)) {
2378 return PSA_ERROR_INVALID_ARGUMENT;
2379 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2382 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002383}
2384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385psa_status_t psa_hash_compare(psa_algorithm_t alg,
2386 const uint8_t *input, size_t input_length,
2387 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002388{
Ronald Cron69a63422021-10-18 09:47:58 +02002389 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002390 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 if (!PSA_ALG_IS_HASH(alg)) {
2393 return PSA_ERROR_INVALID_ARGUMENT;
2394 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002395
Steven Cooreman1e582352021-02-18 17:24:37 +01002396 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 alg, input, input_length,
2398 actual_hash, sizeof(actual_hash),
2399 &actual_hash_length);
2400 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002401 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 }
2403 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002404 status = PSA_ERROR_INVALID_SIGNATURE;
2405 goto exit;
2406 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002408 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002410
2411exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2413 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002414}
2415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2417 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002418{
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (source_operation->id == 0 ||
2420 target_operation->id != 0) {
2421 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002422 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2425 target_operation);
2426 if (status != PSA_SUCCESS) {
2427 psa_hash_abort(target_operation);
2428 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002431}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002432
2433
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002434/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435/* MAC */
2436/****************************************************************/
2437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002439{
Steven Cooremane6804192021-03-19 18:28:56 +01002440 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 if (operation->id == 0) {
2442 return PSA_SUCCESS;
2443 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002446 operation->mac_size = 0;
2447 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002448 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002451}
2452
Ronald Cron2dff3b22021-06-17 16:33:22 +02002453static psa_status_t psa_mac_finalize_alg_and_key_validation(
2454 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002455 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002457{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002458 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 psa_key_type_t key_type = psa_get_key_type(attributes);
2460 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (!PSA_ALG_IS_MAC(alg)) {
2463 return PSA_ERROR_INVALID_ARGUMENT;
2464 }
Steven Cooremane6804192021-03-19 18:28:56 +01002465
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002466 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 status = psa_mac_key_can_do(alg, key_type);
2468 if (status != PSA_SUCCESS) {
2469 return status;
2470 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002471
Steven Cooremand1a68f12021-05-10 11:13:41 +02002472 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002476 /* A very short MAC is too short for security since it can be
2477 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2478 * so we make this our minimum, even though 32 bits is still
2479 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002481 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2484 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002485 /* It's impossible to "truncate" to a larger length than the full length
2486 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002488 }
2489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002491 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2492 * that is disabled in the compile-time configuration. The result can
2493 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2494 * configuration into account. In this case, force a return of
2495 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2496 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2497 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2498 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2499 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002501 }
2502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002504}
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2507 mbedtls_svc_key_id_t key,
2508 psa_algorithm_t alg,
2509 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002510{
2511 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2512 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002513 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514
2515 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002517 status = PSA_ERROR_BAD_STATE;
2518 goto exit;
2519 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520
2521 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 key,
2523 &slot,
2524 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2525 alg);
2526 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002527 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529
2530 psa_key_attributes_t attributes = {
2531 .core = slot->attr
2532 };
2533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2535 &operation->mac_size);
2536 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539
2540 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002541 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if (is_sign) {
2543 status = psa_driver_wrapper_mac_sign_setup(operation,
2544 &attributes,
2545 slot->key.data,
2546 slot->key.bytes,
2547 alg);
2548 } else {
2549 status = psa_driver_wrapper_mac_verify_setup(operation,
2550 &attributes,
2551 slot->key.data,
2552 slot->key.bytes,
2553 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002555
Gilles Peskinefbfac682018-07-08 20:51:54 +02002556exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (status != PSA_SUCCESS) {
2558 psa_mac_abort(operation);
2559 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564}
2565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2567 mbedtls_svc_key_id_t key,
2568 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002569{
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002571}
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2574 mbedtls_svc_key_id_t key,
2575 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002576{
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002578}
2579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2581 const uint8_t *input,
2582 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583{
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 if (operation->id == 0) {
2585 return PSA_ERROR_BAD_STATE;
2586 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002588 /* Don't require hash implementations to behave correctly on a
2589 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (input_length == 0) {
2591 return PSA_SUCCESS;
2592 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2595 input, input_length);
2596 if (status != PSA_SUCCESS) {
2597 psa_mac_abort(operation);
2598 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601}
2602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2604 uint8_t *mac,
2605 size_t mac_size,
2606 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002607{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2609 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002612 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002613 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002614 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002617 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002618 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002619 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002620
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002621 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2622 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002624 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002625 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002626 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002629 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002630 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002631 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 status = psa_driver_wrapper_mac_sign_finish(operation,
2634 mac, operation->mac_size,
2635 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002636
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002637exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002638 /* In case of success, set the potential excess room in the output buffer
2639 * to an invalid value, to avoid potentially leaking a longer MAC.
2640 * In case of error, set the output length and content to a safe default,
2641 * such that in case the caller misses an error check, the output would be
2642 * an unachievable MAC.
2643 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002645 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002646 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002647 }
mohammad16036df908f2018-04-02 08:34:15 -07002648
Paul Elliottdc42ca82023-02-24 18:11:59 +00002649 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002654}
2655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2657 const uint8_t *mac,
2658 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002659{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002660 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2661 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002664 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002665 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002666 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002669 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002670 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002671 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002674 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002675 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002676 }
2677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 status = psa_driver_wrapper_mac_verify_finish(operation,
2679 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002680
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002681exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002685}
2686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2688 psa_algorithm_t alg,
2689 const uint8_t *input,
2690 size_t input_length,
2691 uint8_t *mac,
2692 size_t mac_size,
2693 size_t *mac_length,
2694 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002695{
2696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002697 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2698 psa_key_slot_t *slot;
2699 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002700
Ronald Cron51131b52021-06-17 17:17:20 +02002701 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 key,
2703 &slot,
2704 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2705 alg);
2706 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002707 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002709
Ronald Cron51131b52021-06-17 17:17:20 +02002710 psa_key_attributes_t attributes = {
2711 .core = slot->attr
2712 };
2713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2715 &operation_mac_size);
2716 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002717 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002721 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002722 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002723 }
2724
2725 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 &attributes,
2727 slot->key.data, slot->key.bytes,
2728 alg,
2729 input, input_length,
2730 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002731
2732exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002733 /* In case of success, set the potential excess room in the output buffer
2734 * to an invalid value, to avoid potentially leaking a longer MAC.
2735 * In case of error, set the output length and content to a safe default,
2736 * such that in case the caller misses an error check, the output would be
2737 * an unachievable MAC.
2738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002740 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002741 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002742 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002743
2744 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002749}
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002752 psa_algorithm_t alg,
2753 const uint8_t *input,
2754 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 uint8_t *mac,
2756 size_t mac_size,
2757 size_t *mac_length)
2758{
2759 return psa_mac_compute_internal(key, alg,
2760 input, input_length,
2761 mac, mac_size, mac_length, 1);
2762}
2763
2764psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2765 psa_algorithm_t alg,
2766 const uint8_t *input,
2767 size_t input_length,
2768 const uint8_t *mac,
2769 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002770{
2771 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002772 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2773 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 status = psa_mac_compute_internal(key, alg,
2776 input, input_length,
2777 actual_mac, sizeof(actual_mac),
2778 &actual_mac_length, 0);
2779 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002780 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002784 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002785 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002786 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002788 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002789 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002790 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002791
2792exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002796}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002797
Gilles Peskinee59236f2018-01-27 23:32:46 +01002798/****************************************************************/
2799/* Asymmetric cryptography */
2800/****************************************************************/
2801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2803 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002804{
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 if (input_is_message) {
2806 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2807 return PSA_ERROR_INVALID_ARGUMENT;
2808 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2811 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2812 return PSA_ERROR_INVALID_ARGUMENT;
2813 }
2814 }
2815 } else {
2816 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2817 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002818 }
2819 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002822}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2825 int input_is_message,
2826 psa_algorithm_t alg,
2827 const uint8_t *input,
2828 size_t input_length,
2829 uint8_t *signature,
2830 size_t signature_size,
2831 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002832{
2833 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2834 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2835 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002836
2837 *signature_length = 0;
2838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 status = psa_sign_verify_check_alg(input_is_message, alg);
2840 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002841 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002843
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002844 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002845 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002846 * buffer can in principle be empty since it doesn't actually have
2847 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 if (signature_size == 0) {
2849 return PSA_ERROR_BUFFER_TOO_SMALL;
2850 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002851
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002852 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 key, &slot,
2854 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2855 PSA_KEY_USAGE_SIGN_HASH,
2856 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002859 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002863 status = PSA_ERROR_INVALID_ARGUMENT;
2864 goto exit;
2865 }
2866
2867 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002869 };
2870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002872 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002873 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002874 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 signature, signature_size, signature_length);
2876 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002877
2878 status = psa_driver_wrapper_sign_hash(
2879 &attributes, slot->key.data, slot->key.bytes,
2880 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002882 }
2883
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002884
2885exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002886 psa_wipe_tag_output_buffer(signature, status, signature_size,
2887 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002888
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002890
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002892}
2893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2895 int input_is_message,
2896 psa_algorithm_t alg,
2897 const uint8_t *input,
2898 size_t input_length,
2899 const uint8_t *signature,
2900 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002901{
2902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2903 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2904 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 status = psa_sign_verify_check_alg(input_is_message, alg);
2907 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002908 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002910
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002911 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 key, &slot,
2913 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2914 PSA_KEY_USAGE_VERIFY_HASH,
2915 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 if (status != PSA_SUCCESS) {
2918 return status;
2919 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002920
2921 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002923 };
2924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002926 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002927 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002928 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 signature, signature_length);
2930 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002931 status = psa_driver_wrapper_verify_hash(
2932 &attributes, slot->key.data, slot->key.bytes,
2933 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002935 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002940
2941}
2942
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002943psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002944 const psa_key_attributes_t *attributes,
2945 const uint8_t *key_buffer,
2946 size_t key_buffer_size,
2947 psa_algorithm_t alg,
2948 const uint8_t *input,
2949 size_t input_length,
2950 uint8_t *signature,
2951 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002953{
2954 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002957 size_t hash_length;
2958 uint8_t hash[PSA_HASH_MAX_SIZE];
2959
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002960 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 PSA_ALG_SIGN_GET_HASH(alg),
2962 input, input_length,
2963 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002966 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002968
2969 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 attributes, key_buffer, key_buffer_size,
2971 alg, hash, hash_length,
2972 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002973 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002976}
2977
Gilles Peskine449bd832023-01-11 14:50:10 +01002978psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2979 psa_algorithm_t alg,
2980 const uint8_t *input,
2981 size_t input_length,
2982 uint8_t *signature,
2983 size_t signature_size,
2984 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002985{
2986 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002987 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002989}
2990
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002991psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002992 const psa_key_attributes_t *attributes,
2993 const uint8_t *key_buffer,
2994 size_t key_buffer_size,
2995 psa_algorithm_t alg,
2996 const uint8_t *input,
2997 size_t input_length,
2998 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003000{
3001 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003004 size_t hash_length;
3005 uint8_t hash[PSA_HASH_MAX_SIZE];
3006
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003007 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 PSA_ALG_SIGN_GET_HASH(alg),
3009 input, input_length,
3010 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003013 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003015
3016 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 attributes, key_buffer, key_buffer_size,
3018 alg, hash, hash_length,
3019 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003020 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003023}
3024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3026 psa_algorithm_t alg,
3027 const uint8_t *input,
3028 size_t input_length,
3029 const uint8_t *signature,
3030 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003031{
3032 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003033 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003035}
3036
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003037psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003038 const psa_key_attributes_t *attributes,
3039 const uint8_t *key_buffer, size_t key_buffer_size,
3040 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003042{
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3044 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3045 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003046#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3048 return mbedtls_psa_rsa_sign_hash(
3049 attributes,
3050 key_buffer, key_buffer_size,
3051 alg, hash, hash_length,
3052 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003053#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3054 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 } else {
3056 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003057 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3059 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003060#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3062 return mbedtls_psa_ecdsa_sign_hash(
3063 attributes,
3064 key_buffer, key_buffer_size,
3065 alg, hash, hash_length,
3066 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003067#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3068 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 } else {
3070 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003071 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003072 }
Ronald Cron4501c982021-03-19 20:09:32 +01003073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 (void) key_buffer;
3075 (void) key_buffer_size;
3076 (void) hash;
3077 (void) hash_length;
3078 (void) signature;
3079 (void) signature_size;
3080 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003083}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3086 psa_algorithm_t alg,
3087 const uint8_t *hash,
3088 size_t hash_length,
3089 uint8_t *signature,
3090 size_t signature_size,
3091 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003092{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003093 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003094 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003096}
3097
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003098psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003099 const psa_key_attributes_t *attributes,
3100 const uint8_t *key_buffer, size_t key_buffer_size,
3101 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003103{
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3105 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3106 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003107#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3109 return mbedtls_psa_rsa_verify_hash(
3110 attributes,
3111 key_buffer, key_buffer_size,
3112 alg, hash, hash_length,
3113 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003114#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3115 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 } else {
3117 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003118 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3120 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003121#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3123 return mbedtls_psa_ecdsa_verify_hash(
3124 attributes,
3125 key_buffer, key_buffer_size,
3126 alg, hash, hash_length,
3127 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003128#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3129 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 } else {
3131 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003132 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003133 }
Ronald Cron4501c982021-03-19 20:09:32 +01003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 (void) key_buffer;
3136 (void) key_buffer_size;
3137 (void) hash;
3138 (void) hash_length;
3139 (void) signature;
3140 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003143}
3144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3146 psa_algorithm_t alg,
3147 const uint8_t *hash,
3148 size_t hash_length,
3149 const uint8_t *signature,
3150 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003151{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003152 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003153 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003155}
3156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3158 psa_algorithm_t alg,
3159 const uint8_t *input,
3160 size_t input_length,
3161 const uint8_t *salt,
3162 size_t salt_length,
3163 uint8_t *output,
3164 size_t output_size,
3165 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003168 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003169 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003170
Darryl Green5cc689a2018-07-24 15:34:10 +01003171 (void) input;
3172 (void) input_length;
3173 (void) salt;
3174 (void) output;
3175 (void) output_size;
3176
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003177 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3180 return PSA_ERROR_INVALID_ARGUMENT;
3181 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003182
Ronald Cron5c522922020-11-14 16:35:34 +01003183 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3185 if (status != PSA_SUCCESS) {
3186 return status;
3187 }
3188 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3189 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003190 status = PSA_ERROR_INVALID_ARGUMENT;
3191 goto exit;
3192 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003193
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003194 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003196 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003197
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003198 status = psa_driver_wrapper_asymmetric_encrypt(
3199 &attributes, slot->key.data, slot->key.bytes,
3200 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003206}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3209 psa_algorithm_t alg,
3210 const uint8_t *input,
3211 size_t input_length,
3212 const uint8_t *salt,
3213 size_t salt_length,
3214 uint8_t *output,
3215 size_t output_size,
3216 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003217{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003218 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003219 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003220 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003221
Darryl Green5cc689a2018-07-24 15:34:10 +01003222 (void) input;
3223 (void) input_length;
3224 (void) salt;
3225 (void) output;
3226 (void) output_size;
3227
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003228 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003229
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3231 return PSA_ERROR_INVALID_ARGUMENT;
3232 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003233
Ronald Cron5c522922020-11-14 16:35:34 +01003234 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3236 if (status != PSA_SUCCESS) {
3237 return status;
3238 }
3239 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003240 status = PSA_ERROR_INVALID_ARGUMENT;
3241 goto exit;
3242 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003243
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003244 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003246 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003248 status = psa_driver_wrapper_asymmetric_decrypt(
3249 &attributes, slot->key.data, slot->key.bytes,
3250 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003252
3253exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003257}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003258
Paul Elliott9fe12f62022-11-30 19:16:02 +00003259/****************************************************************/
3260/* Asymmetric interruptible cryptography */
3261/****************************************************************/
3262
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003263static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3264
Paul Elliott9fe12f62022-11-30 19:16:02 +00003265void psa_interruptible_set_max_ops(uint32_t max_ops)
3266{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003267 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003268}
3269
3270uint32_t psa_interruptible_get_max_ops(void)
3271{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003272 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003273}
3274
Paul Elliott9fe12f62022-11-30 19:16:02 +00003275uint32_t psa_sign_hash_get_num_ops(
3276 const psa_sign_hash_interruptible_operation_t *operation)
3277{
Paul Elliott296ede92022-12-15 17:00:30 +00003278 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003279}
3280
3281uint32_t psa_verify_hash_get_num_ops(
3282 const psa_verify_hash_interruptible_operation_t *operation)
3283{
Paul Elliott296ede92022-12-15 17:00:30 +00003284 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003285}
3286
Paul Elliott59ad9452022-12-18 15:09:02 +00003287static psa_status_t psa_sign_hash_abort_internal(
3288 psa_sign_hash_interruptible_operation_t *operation)
3289{
3290 if (operation->id == 0) {
3291 /* The object has (apparently) been initialized but it is not (yet)
3292 * in use. It's ok to call abort on such an object, and there's
3293 * nothing to do. */
3294 return PSA_SUCCESS;
3295 }
3296
3297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3298
3299 status = psa_driver_wrapper_sign_hash_abort(operation);
3300
3301 operation->id = 0;
3302
Paul Elliotte6145dc2023-02-07 12:51:21 +00003303 /* Do not clear either the error_occurred or num_ops elements here as they
3304 * only want to be cleared by the application calling abort, not by abort
3305 * being called at completion of an operation. */
3306
Paul Elliott59ad9452022-12-18 15:09:02 +00003307 return status;
3308}
3309
Paul Elliott9fe12f62022-11-30 19:16:02 +00003310psa_status_t psa_sign_hash_start(
3311 psa_sign_hash_interruptible_operation_t *operation,
3312 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3313 const uint8_t *hash, size_t hash_length)
3314{
3315 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3316 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3317 psa_key_slot_t *slot;
3318
Paul Elliottc9774412023-02-06 15:14:07 +00003319 /* Check that start has not been previously called, or operation has not
3320 * previously errored. */
3321 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003322 return PSA_ERROR_BAD_STATE;
3323 }
3324
Paul Elliott9fe12f62022-11-30 19:16:02 +00003325 status = psa_sign_verify_check_alg(0, alg);
3326 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003327 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003328 return status;
3329 }
3330
3331 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3332 PSA_KEY_USAGE_SIGN_HASH,
3333 alg);
3334
3335 if (status != PSA_SUCCESS) {
3336 goto exit;
3337 }
3338
3339 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3340 status = PSA_ERROR_INVALID_ARGUMENT;
3341 goto exit;
3342 }
3343
3344 psa_key_attributes_t attributes = {
3345 .core = slot->attr
3346 };
3347
Paul Elliott296ede92022-12-15 17:00:30 +00003348 /* Ensure ops count gets reset, in case of operation re-use. */
3349 operation->num_ops = 0;
3350
Paul Elliott9fe12f62022-11-30 19:16:02 +00003351 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3352 slot->key.data,
3353 slot->key.bytes, alg,
3354 hash, hash_length);
3355exit:
3356
3357 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003358 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003359 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003360 }
3361
3362 unlock_status = psa_unlock_key_slot(slot);
3363
Paul Elliottc9774412023-02-06 15:14:07 +00003364 if (unlock_status != PSA_SUCCESS) {
3365 operation->error_occurred = 1;
3366 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003367
Paul Elliottc9774412023-02-06 15:14:07 +00003368 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003369}
3370
3371
3372psa_status_t psa_sign_hash_complete(
3373 psa_sign_hash_interruptible_operation_t *operation,
3374 uint8_t *signature, size_t signature_size,
3375 size_t *signature_length)
3376{
3377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3378
3379 *signature_length = 0;
3380
Paul Elliottc9774412023-02-06 15:14:07 +00003381 /* Check that start has been called first, and that operation has not
3382 * previously errored. */
3383 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003384 status = PSA_ERROR_BAD_STATE;
3385 goto exit;
3386 }
3387
Paul Elliott096abc42023-02-03 18:33:23 +00003388 /* Immediately reject a zero-length signature buffer. This guarantees that
3389 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003390 if (signature_size == 0) {
3391 status = PSA_ERROR_BUFFER_TOO_SMALL;
3392 goto exit;
3393 }
3394
3395 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3396 signature_size,
3397 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003398
Paul Elliott296ede92022-12-15 17:00:30 +00003399 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003400 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003401
Paul Elliottebe225c2023-02-10 14:32:53 +00003402exit:
3403
Paul Elliott59200a22023-02-21 15:07:40 +00003404 psa_wipe_tag_output_buffer(signature, status, signature_size,
3405 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003406
Paul Elliott53bb3122023-02-10 14:22:22 +00003407 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003408 if (status != PSA_SUCCESS) {
3409 operation->error_occurred = 1;
3410 }
3411
Paul Elliott59ad9452022-12-18 15:09:02 +00003412 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003413 }
3414
3415 return status;
3416}
3417
3418psa_status_t psa_sign_hash_abort(
3419 psa_sign_hash_interruptible_operation_t *operation)
3420{
Paul Elliott59ad9452022-12-18 15:09:02 +00003421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3422
3423 status = psa_sign_hash_abort_internal(operation);
3424
3425 /* We clear the number of ops done here, so that it is not cleared when
3426 * the operation fails or succeeds, only on manual abort. */
3427 operation->num_ops = 0;
3428
Paul Elliottc9774412023-02-06 15:14:07 +00003429 /* Likewise, failure state. */
3430 operation->error_occurred = 0;
3431
Paul Elliott59ad9452022-12-18 15:09:02 +00003432 return status;
3433}
3434
3435static psa_status_t psa_verify_hash_abort_internal(
3436 psa_verify_hash_interruptible_operation_t *operation)
3437{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003438 if (operation->id == 0) {
3439 /* The object has (apparently) been initialized but it is not (yet)
3440 * in use. It's ok to call abort on such an object, and there's
3441 * nothing to do. */
3442 return PSA_SUCCESS;
3443 }
3444
Paul Elliott59ad9452022-12-18 15:09:02 +00003445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3446
3447 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003448
3449 operation->id = 0;
3450
Paul Elliotte6145dc2023-02-07 12:51:21 +00003451 /* Do not clear either the error_occurred or num_ops elements here as they
3452 * only want to be cleared by the application calling abort, not by abort
3453 * being called at completion of an operation. */
3454
Paul Elliott59ad9452022-12-18 15:09:02 +00003455 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003456}
3457
3458psa_status_t psa_verify_hash_start(
3459 psa_verify_hash_interruptible_operation_t *operation,
3460 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3461 const uint8_t *hash, size_t hash_length,
3462 const uint8_t *signature, size_t signature_length)
3463{
3464 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3465 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3466 psa_key_slot_t *slot;
3467
Paul Elliottc9774412023-02-06 15:14:07 +00003468 /* Check that start has not been previously called, or operation has not
3469 * previously errored. */
3470 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003471 return PSA_ERROR_BAD_STATE;
3472 }
3473
3474 status = psa_sign_verify_check_alg(0, alg);
3475 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003476 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003477 return status;
3478 }
3479
3480 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3481 PSA_KEY_USAGE_VERIFY_HASH,
3482 alg);
3483
3484 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003485 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003486 return status;
3487 }
3488
3489 psa_key_attributes_t attributes = {
3490 .core = slot->attr
3491 };
3492
Paul Elliott296ede92022-12-15 17:00:30 +00003493 /* Ensure ops count gets reset, in case of operation re-use. */
3494 operation->num_ops = 0;
3495
Paul Elliott9fe12f62022-11-30 19:16:02 +00003496 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3497 slot->key.data,
3498 slot->key.bytes,
3499 alg, hash, hash_length,
3500 signature, signature_length);
3501
3502 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003503 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003504 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003505 }
3506
3507 unlock_status = psa_unlock_key_slot(slot);
3508
Paul Elliottc9774412023-02-06 15:14:07 +00003509 if (unlock_status != PSA_SUCCESS) {
3510 operation->error_occurred = 1;
3511 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003512
Paul Elliottc9774412023-02-06 15:14:07 +00003513 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003514}
3515
3516psa_status_t psa_verify_hash_complete(
3517 psa_verify_hash_interruptible_operation_t *operation)
3518{
3519 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3520
Paul Elliottc9774412023-02-06 15:14:07 +00003521 /* Check that start has been called first, and that operation has not
3522 * previously errored. */
3523 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003524 status = PSA_ERROR_BAD_STATE;
3525 goto exit;
3526 }
3527
3528 status = psa_driver_wrapper_verify_hash_complete(operation);
3529
Paul Elliott296ede92022-12-15 17:00:30 +00003530 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003531 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003532 operation);
3533
Paul Elliottebe225c2023-02-10 14:32:53 +00003534exit:
3535
Paul Elliott9fe12f62022-11-30 19:16:02 +00003536 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003537 if (status != PSA_SUCCESS) {
3538 operation->error_occurred = 1;
3539 }
3540
Paul Elliott59ad9452022-12-18 15:09:02 +00003541 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003542 }
3543
3544 return status;
3545}
3546
3547psa_status_t psa_verify_hash_abort(
3548 psa_verify_hash_interruptible_operation_t *operation)
3549{
Paul Elliott59ad9452022-12-18 15:09:02 +00003550 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003551
Paul Elliott59ad9452022-12-18 15:09:02 +00003552 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003553
Paul Elliott59ad9452022-12-18 15:09:02 +00003554 /* We clear the number of ops done here, so that it is not cleared when
3555 * the operation fails or succeeds, only on manual abort. */
3556 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003557
Paul Elliottc9774412023-02-06 15:14:07 +00003558 /* Likewise, failure state. */
3559 operation->error_occurred = 0;
3560
Paul Elliott59ad9452022-12-18 15:09:02 +00003561 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003562}
3563
Paul Elliott588f8ed2022-12-02 18:10:26 +00003564/****************************************************************/
3565/* Asymmetric interruptible cryptography internal */
3566/* implementations */
3567/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003568
Paul Elliott588f8ed2022-12-02 18:10:26 +00003569void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3570{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003571
Paul Elliott588f8ed2022-12-02 18:10:26 +00003572#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3573 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3574 defined(MBEDTLS_ECP_RESTARTABLE)
3575
3576 /* Internal implementation uses zero to indicate infinite number max ops,
3577 * therefore avoid this value, and set to minimum possible. */
3578 if (max_ops == 0) {
3579 max_ops = 1;
3580 }
3581
Paul Elliott588f8ed2022-12-02 18:10:26 +00003582 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003583#else
3584 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003585#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3586 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3587 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3588}
3589
Paul Elliott588f8ed2022-12-02 18:10:26 +00003590uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003591 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003592{
3593#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3594 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3595 defined(MBEDTLS_ECP_RESTARTABLE)
3596
Paul Elliott93d9ca82023-02-15 18:14:21 +00003597 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003598#else
3599 (void) operation;
3600 return 0;
3601#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3602 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3603 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3604}
3605
3606uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003607 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003608{
3609 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3610 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3611 defined(MBEDTLS_ECP_RESTARTABLE)
3612
Paul Elliott93d9ca82023-02-15 18:14:21 +00003613 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003614#else
3615 (void) operation;
3616 return 0;
3617#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3618 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3619 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3620}
3621
3622psa_status_t mbedtls_psa_sign_hash_start(
3623 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3624 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3625 size_t key_buffer_size, psa_algorithm_t alg,
3626 const uint8_t *hash, size_t hash_length)
3627{
3628 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003629 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003630
Paul Elliott068fe072023-01-16 13:59:15 +00003631 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3632 return PSA_ERROR_NOT_SUPPORTED;
3633 }
3634
3635 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003636 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003637 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638
3639#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003640 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3641 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003642
Paul Elliotta1c94092023-02-15 16:38:04 +00003643 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3644
Paul Elliott93d9ca82023-02-15 18:14:21 +00003645 /* Ensure num_ops is zero'ed in case of context re-use. */
3646 operation->num_ops = 0;
3647
Paul Elliott068fe072023-01-16 13:59:15 +00003648 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3649 attributes->core.bits,
3650 key_buffer,
3651 key_buffer_size,
3652 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003653
Paul Elliott068fe072023-01-16 13:59:15 +00003654 if (status != PSA_SUCCESS) {
3655 return status;
3656 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003657
Paul Elliott1bc59df2023-02-05 13:41:57 +00003658 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003659 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660
Paul Elliott068fe072023-01-16 13:59:15 +00003661 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003662 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003663 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003664
Paul Elliott0290a762023-02-09 14:30:24 +00003665 /* We only need to store the same length of hash as the private key size
3666 * here, it would be truncated by the internal implementation anyway. */
3667 required_hash_length = (hash_length < operation->coordinate_bytes ?
3668 hash_length : operation->coordinate_bytes);
3669
Paul Elliottba70ad42023-02-15 18:23:53 +00003670 if (required_hash_length > sizeof(operation->hash)) {
3671 /* Shouldn't happen, but better safe than sorry. */
3672 return PSA_ERROR_CORRUPTION_DETECTED;
3673 }
3674
Paul Elliott0290a762023-02-09 14:30:24 +00003675 memcpy(operation->hash, hash, required_hash_length);
3676 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003677
3678 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003679
3680#else
Paul Elliott068fe072023-01-16 13:59:15 +00003681 (void) operation;
3682 (void) key_buffer;
3683 (void) key_buffer_size;
3684 (void) alg;
3685 (void) hash;
3686 (void) hash_length;
3687 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003688 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689
Paul Elliott068fe072023-01-16 13:59:15 +00003690 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003691#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3692 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3693 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003694}
3695
3696psa_status_t mbedtls_psa_sign_hash_complete(
3697 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3698 uint8_t *signature, size_t signature_size,
3699 size_t *signature_length)
3700{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003701#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3702 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3703 defined(MBEDTLS_ECP_RESTARTABLE)
3704
Paul Elliotta1c94092023-02-15 16:38:04 +00003705 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003706 mbedtls_mpi r;
3707 mbedtls_mpi s;
3708
Paul Elliott46845252023-02-03 14:59:11 +00003709 mbedtls_mpi_init(&r);
3710 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003712 /* Ensure max_ops is set to the current value (or default). */
3713 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3714
Paul Elliotta1c94092023-02-15 16:38:04 +00003715 if (signature_size < 2 * operation->coordinate_bytes) {
3716 status = PSA_ERROR_BUFFER_TOO_SMALL;
3717 goto exit;
3718 }
3719
Paul Elliott588f8ed2022-12-02 18:10:26 +00003720 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003721
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3723 status = mbedtls_to_psa_error(
3724 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003725 &r,
3726 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727 &operation->ctx->d,
3728 operation->hash,
3729 operation->hash_length,
3730 operation->md_alg,
3731 mbedtls_psa_get_random,
3732 MBEDTLS_PSA_RANDOM_STATE,
3733 &operation->restart_ctx));
3734#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003735 status = PSA_ERROR_NOT_SUPPORTED;
3736 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003737#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3738 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003739 status = mbedtls_to_psa_error(
3740 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003741 &r,
3742 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003743 &operation->ctx->d,
3744 operation->hash,
3745 operation->hash_length,
3746 mbedtls_psa_get_random,
3747 MBEDTLS_PSA_RANDOM_STATE,
3748 mbedtls_psa_get_random,
3749 MBEDTLS_PSA_RANDOM_STATE,
3750 &operation->restart_ctx));
3751 }
3752
Paul Elliottf8e5b562023-02-19 18:43:45 +00003753 /* Hide the fact that the restart context only holds a delta of number of
3754 * ops done during the last operation, not an absolute value. */
3755 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3756
Paul Elliott724bd252023-02-08 12:35:08 +00003757 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003758 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003759 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003760 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003761 operation->coordinate_bytes)
3762 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003763
3764 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003765 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003766 }
3767
3768 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003769 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003771 operation->coordinate_bytes,
3772 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
Paul Elliott1bc59df2023-02-05 13:41:57 +00003779 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003780
Paul Elliott724bd252023-02-08 12:35:08 +00003781 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003782 }
Paul Elliott724bd252023-02-08 12:35:08 +00003783
3784exit:
3785
3786 mbedtls_mpi_free(&r);
3787 mbedtls_mpi_free(&s);
3788 return status;
3789
Paul Elliott588f8ed2022-12-02 18:10:26 +00003790 #else
3791
3792 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003793 (void) signature;
3794 (void) signature_size;
3795 (void) signature_length;
3796
3797 return PSA_ERROR_NOT_SUPPORTED;
3798
3799#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3800 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3801 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3802}
3803
3804psa_status_t mbedtls_psa_sign_hash_abort(
3805 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3806{
3807
3808#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3809 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3810 defined(MBEDTLS_ECP_RESTARTABLE)
3811
3812 if (operation->ctx) {
3813 mbedtls_ecdsa_free(operation->ctx);
3814 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003815 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003816 }
3817
3818 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3819
Paul Elliott93d9ca82023-02-15 18:14:21 +00003820 operation->num_ops = 0;
3821
Paul Elliott588f8ed2022-12-02 18:10:26 +00003822 return PSA_SUCCESS;
3823
3824#else
3825
3826 (void) operation;
3827
3828 return PSA_ERROR_NOT_SUPPORTED;
3829
3830#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3831 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3832 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3833}
3834
3835psa_status_t mbedtls_psa_verify_hash_start(
3836 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3837 const psa_key_attributes_t *attributes,
3838 const uint8_t *key_buffer, size_t key_buffer_size,
3839 psa_algorithm_t alg,
3840 const uint8_t *hash, size_t hash_length,
3841 const uint8_t *signature, size_t signature_length)
3842{
3843 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003844 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003845 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846
Paul Elliott068fe072023-01-16 13:59:15 +00003847 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3848 return PSA_ERROR_NOT_SUPPORTED;
3849 }
3850
3851 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003852 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003853 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854
3855#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003856 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3857 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003858
Paul Elliotta1c94092023-02-15 16:38:04 +00003859 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3860 mbedtls_mpi_init(&operation->r);
3861 mbedtls_mpi_init(&operation->s);
3862
Paul Elliott93d9ca82023-02-15 18:14:21 +00003863 /* Ensure num_ops is zero'ed in case of context re-use. */
3864 operation->num_ops = 0;
3865
Paul Elliott068fe072023-01-16 13:59:15 +00003866 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3867 attributes->core.bits,
3868 key_buffer,
3869 key_buffer_size,
3870 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003871
Paul Elliott068fe072023-01-16 13:59:15 +00003872 if (status != PSA_SUCCESS) {
3873 return status;
3874 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003875
Paul Elliottc569fc22023-02-10 13:02:54 +00003876 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003877
Paul Elliott1bc59df2023-02-05 13:41:57 +00003878 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003879 return PSA_ERROR_INVALID_SIGNATURE;
3880 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003881
Paul Elliott068fe072023-01-16 13:59:15 +00003882 status = mbedtls_to_psa_error(
3883 mbedtls_mpi_read_binary(&operation->r,
3884 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003885 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003886
Paul Elliott068fe072023-01-16 13:59:15 +00003887 if (status != PSA_SUCCESS) {
3888 return status;
3889 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003890
Paul Elliott068fe072023-01-16 13:59:15 +00003891 status = mbedtls_to_psa_error(
3892 mbedtls_mpi_read_binary(&operation->s,
3893 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003894 coordinate_bytes,
3895 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003896
Paul Elliott068fe072023-01-16 13:59:15 +00003897 if (status != PSA_SUCCESS) {
3898 return status;
3899 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003900
Paul Elliott2c9843f2023-02-15 17:32:42 +00003901 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003902
Paul Elliott2c9843f2023-02-15 17:32:42 +00003903 if (status != PSA_SUCCESS) {
3904 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003905 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003906
Paul Elliott0290a762023-02-09 14:30:24 +00003907 /* We only need to store the same length of hash as the private key size
3908 * here, it would be truncated by the internal implementation anyway. */
3909 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3910 coordinate_bytes);
3911
Paul Elliottba70ad42023-02-15 18:23:53 +00003912 if (required_hash_length > sizeof(operation->hash)) {
3913 /* Shouldn't happen, but better safe than sorry. */
3914 return PSA_ERROR_CORRUPTION_DETECTED;
3915 }
3916
Paul Elliott0290a762023-02-09 14:30:24 +00003917 memcpy(operation->hash, hash, required_hash_length);
3918 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003919
3920 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003921#else
Paul Elliott068fe072023-01-16 13:59:15 +00003922 (void) operation;
3923 (void) key_buffer;
3924 (void) key_buffer_size;
3925 (void) alg;
3926 (void) hash;
3927 (void) hash_length;
3928 (void) signature;
3929 (void) signature_length;
3930 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003931 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003932 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003933
Paul Elliott068fe072023-01-16 13:59:15 +00003934 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003935#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3936 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3937 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003938}
3939
3940psa_status_t mbedtls_psa_verify_hash_complete(
3941 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3942{
3943
3944#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3945 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3946 defined(MBEDTLS_ECP_RESTARTABLE)
3947
Paul Elliottf8e5b562023-02-19 18:43:45 +00003948 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3949
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003950 /* Ensure max_ops is set to the current value (or default). */
3951 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3952
Paul Elliottf8e5b562023-02-19 18:43:45 +00003953 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003954 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3955 operation->hash,
3956 operation->hash_length,
3957 &operation->ctx->Q,
3958 &operation->r,
3959 &operation->s,
3960 &operation->restart_ctx));
3961
Paul Elliottf8e5b562023-02-19 18:43:45 +00003962 /* Hide the fact that the restart context only holds a delta of number of
3963 * ops done during the last operation, not an absolute value. */
3964 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3965
3966 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003967#else
3968 (void) operation;
3969
3970 return PSA_ERROR_NOT_SUPPORTED;
3971
3972#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3973 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3974 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3975}
3976
3977psa_status_t mbedtls_psa_verify_hash_abort(
3978 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3979{
3980
3981#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3982 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3983 defined(MBEDTLS_ECP_RESTARTABLE)
3984
3985 if (operation->ctx) {
3986 mbedtls_ecdsa_free(operation->ctx);
3987 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003988 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003989 }
3990
3991 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3992
Paul Elliott93d9ca82023-02-15 18:14:21 +00003993 operation->num_ops = 0;
3994
Paul Elliott588f8ed2022-12-02 18:10:26 +00003995 mbedtls_mpi_free(&operation->r);
3996 mbedtls_mpi_free(&operation->s);
3997
3998 return PSA_SUCCESS;
3999
4000#else
4001 (void) operation;
4002
4003 return PSA_ERROR_NOT_SUPPORTED;
4004
4005#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4006 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4007 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4008}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004009
mohammad1603503973b2018-03-12 15:59:30 +02004010/****************************************************************/
4011/* Symmetric cryptography */
4012/****************************************************************/
4013
Gilles Peskine449bd832023-01-11 14:50:10 +01004014static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4015 mbedtls_svc_key_id_t key,
4016 psa_algorithm_t alg,
4017 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004018{
4019 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4020 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004021 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4023 PSA_KEY_USAGE_ENCRYPT :
4024 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004025
4026 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004028 status = PSA_ERROR_BAD_STATE;
4029 goto exit;
4030 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004033 status = PSA_ERROR_INVALID_ARGUMENT;
4034 goto exit;
4035 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004036
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4038 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004041
Ronald Cron49fafa92021-03-10 08:34:23 +01004042 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004043 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004044 * so we only set it (in the driver wrapper) after resources have been
4045 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004046 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004048 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004050 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 }
4052 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004053
Ronald Crona4af55f2020-12-14 14:36:06 +01004054 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004056 };
4057
Ronald Cronab99ac22020-10-01 16:38:28 +02004058 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 if (cipher_operation == MBEDTLS_ENCRYPT) {
4060 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4061 &attributes,
4062 slot->key.data,
4063 slot->key.bytes,
4064 alg);
4065 } else {
4066 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4067 &attributes,
4068 slot->key.data,
4069 slot->key.bytes,
4070 alg);
4071 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004072
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004073exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 if (status != PSA_SUCCESS) {
4075 psa_cipher_abort(operation);
4076 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004077
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004079
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004081}
4082
Gilles Peskine449bd832023-01-11 14:50:10 +01004083psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4084 mbedtls_svc_key_id_t key,
4085 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004086{
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004088}
4089
Gilles Peskine449bd832023-01-11 14:50:10 +01004090psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4091 mbedtls_svc_key_id_t key,
4092 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004093{
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004095}
4096
Gilles Peskine449bd832023-01-11 14:50:10 +01004097psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4098 uint8_t *iv,
4099 size_t iv_size,
4100 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004101{
Ronald Cron6d051732020-10-01 14:10:20 +02004102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004103 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4104 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004105
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004107 status = PSA_ERROR_BAD_STATE;
4108 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004109 }
4110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004112 status = PSA_ERROR_BAD_STATE;
4113 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004114 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004115
Ronald Cron2fb90522021-07-05 12:31:44 +02004116 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004118 status = PSA_ERROR_BUFFER_TOO_SMALL;
4119 goto exit;
4120 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004123 status = PSA_ERROR_GENERIC_ERROR;
4124 goto exit;
4125 }
4126
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 status = psa_generate_random(local_iv, default_iv_length);
4128 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 }
Ronald Cron5618a392021-03-26 09:52:26 +01004131
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 status = psa_driver_wrapper_cipher_set_iv(operation,
4133 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004134
4135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (status == PSA_SUCCESS) {
4137 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004138 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004139 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004141 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004143 }
Ronald Cron6d051732020-10-01 14:10:20 +02004144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004146}
4147
Gilles Peskine449bd832023-01-11 14:50:10 +01004148psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4149 const uint8_t *iv,
4150 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004151{
Ronald Cron6d051732020-10-01 14:10:20 +02004152 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004155 status = PSA_ERROR_BAD_STATE;
4156 goto exit;
4157 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004160 status = PSA_ERROR_BAD_STATE;
4161 goto exit;
4162 }
Ronald Crona0d68172021-03-26 10:15:08 +01004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004165 status = PSA_ERROR_INVALID_ARGUMENT;
4166 goto exit;
4167 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 status = psa_driver_wrapper_cipher_set_iv(operation,
4170 iv,
4171 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004172
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004173exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004175 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 } else {
4177 psa_cipher_abort(operation);
4178 }
4179 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004180}
4181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4183 const uint8_t *input,
4184 size_t input_length,
4185 uint8_t *output,
4186 size_t output_size,
4187 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004188{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004189 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004192 status = PSA_ERROR_BAD_STATE;
4193 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004194 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004197 status = PSA_ERROR_BAD_STATE;
4198 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004199 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 status = psa_driver_wrapper_cipher_update(operation,
4202 input,
4203 input_length,
4204 output,
4205 output_size,
4206 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004207
4208exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 if (status != PSA_SUCCESS) {
4210 psa_cipher_abort(operation);
4211 }
Ronald Cron6d051732020-10-01 14:10:20 +02004212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004214}
4215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4217 uint8_t *output,
4218 size_t output_size,
4219 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004220{
David Saadab4ecc272019-02-14 13:48:10 +02004221 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004224 status = PSA_ERROR_BAD_STATE;
4225 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004226 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004229 status = PSA_ERROR_BAD_STATE;
4230 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004231 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 status = psa_driver_wrapper_cipher_finish(operation,
4234 output,
4235 output_size,
4236 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004237
4238exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 if (status == PSA_SUCCESS) {
4240 return psa_cipher_abort(operation);
4241 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004242 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004244
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004246 }
mohammad1603503973b2018-03-12 15:59:30 +02004247}
4248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004250{
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004252 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004253 * in use. It's ok to call abort on such an object, and there's
4254 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004256 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004259
Ronald Cron49fafa92021-03-10 08:34:23 +01004260 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004261 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004262 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004265}
4266
Gilles Peskine449bd832023-01-11 14:50:10 +01004267psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4268 psa_algorithm_t alg,
4269 const uint8_t *input,
4270 size_t input_length,
4271 uint8_t *output,
4272 size_t output_size,
4273 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004274{
4275 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004276 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004277 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004278 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4279 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004280
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004282 status = PSA_ERROR_INVALID_ARGUMENT;
4283 goto exit;
4284 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4287 PSA_KEY_USAGE_ENCRYPT,
4288 alg);
4289 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004292
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004293 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004295 };
4296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4298 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004299 status = PSA_ERROR_GENERIC_ERROR;
4300 goto exit;
4301 }
4302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 if (default_iv_length > 0) {
4304 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004305 status = PSA_ERROR_BUFFER_TOO_SMALL;
4306 goto exit;
4307 }
4308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 status = psa_generate_random(local_iv, default_iv_length);
4310 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004311 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004313 }
4314
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004315 status = psa_driver_wrapper_cipher_encrypt(
4316 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004317 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004318 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004320
4321exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 unlock_status = psa_unlock_key_slot(slot);
4323 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004324 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004325 }
Ronald Cron23919522021-07-08 19:00:07 +02004326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 if (status == PSA_SUCCESS) {
4328 if (default_iv_length > 0) {
4329 memcpy(output, local_iv, default_iv_length);
4330 }
4331 *output_length += default_iv_length;
4332 } else {
4333 *output_length = 0;
4334 }
4335
4336 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004337}
4338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4340 psa_algorithm_t alg,
4341 const uint8_t *input,
4342 size_t input_length,
4343 uint8_t *output,
4344 size_t output_size,
4345 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004346{
4347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004348 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004349 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004352 status = PSA_ERROR_INVALID_ARGUMENT;
4353 goto exit;
4354 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004355
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4357 PSA_KEY_USAGE_DECRYPT,
4358 alg);
4359 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004360 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004362
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004363 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004365 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4368 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004369 status = PSA_ERROR_INVALID_ARGUMENT;
4370 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004372 status = PSA_ERROR_INVALID_ARGUMENT;
4373 goto exit;
4374 }
4375
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004376 status = psa_driver_wrapper_cipher_decrypt(
4377 &attributes, slot->key.data, slot->key.bytes,
4378 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004380
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004381exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 unlock_status = psa_unlock_key_slot(slot);
4383 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004384 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004388 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 }
Ronald Cron23919522021-07-08 19:00:07 +02004390
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004392}
4393
4394
mohammad16035955c982018-04-26 00:53:03 +03004395/****************************************************************/
4396/* AEAD */
4397/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004398
Paul Elliottbaff51c2021-09-28 17:44:45 +01004399/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004400static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004401{
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004403}
4404
4405/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004406static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4407 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004408{
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004410
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004412#if defined(PSA_WANT_ALG_GCM)
4413 case PSA_ALG_GCM:
4414 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 * arbitrarily large nonces. Please note that we do not generally
4416 * recommend the usage of nonces of greater length than
4417 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4418 * size, which can then lead to collisions if you encrypt a very
4419 * large number of messages.*/
4420 if (nonce_length != 0) {
4421 return PSA_SUCCESS;
4422 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004423 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004424#endif /* PSA_WANT_ALG_GCM */
4425#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004426 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 if (nonce_length >= 7 && nonce_length <= 13) {
4428 return PSA_SUCCESS;
4429 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004430 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004431#endif /* PSA_WANT_ALG_CCM */
4432#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004433 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 if (nonce_length == 12) {
4435 return PSA_SUCCESS;
4436 } else if (nonce_length == 8) {
4437 return PSA_ERROR_NOT_SUPPORTED;
4438 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004439 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004440#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004441 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004442 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004444 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004445
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004447}
4448
Gilles Peskine449bd832023-01-11 14:50:10 +01004449static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004450{
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4452 return PSA_ERROR_INVALID_ARGUMENT;
4453 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004456}
4457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4459 psa_algorithm_t alg,
4460 const uint8_t *nonce,
4461 size_t nonce_length,
4462 const uint8_t *additional_data,
4463 size_t additional_data_length,
4464 const uint8_t *plaintext,
4465 size_t plaintext_length,
4466 uint8_t *ciphertext,
4467 size_t ciphertext_size,
4468 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004469{
Ronald Cron215633c2021-03-16 17:15:37 +01004470 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4471 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004472
mohammad1603f08a5502018-06-03 15:05:47 +03004473 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004474
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 status = psa_aead_check_algorithm(alg);
4476 if (status != PSA_SUCCESS) {
4477 return status;
4478 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004479
Ronald Cron9a986162021-03-26 12:40:07 +01004480 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4482 if (status != PSA_SUCCESS) {
4483 return status;
4484 }
mohammad16035955c982018-04-26 00:53:03 +03004485
Ronald Cron215633c2021-03-16 17:15:37 +01004486 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004488 };
mohammad16035955c982018-04-26 00:53:03 +03004489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 status = psa_aead_check_nonce_length(alg, nonce_length);
4491 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004492 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004494
Ronald Cronde822812021-03-17 16:08:20 +01004495 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004496 &attributes, slot->key.data, slot->key.bytes,
4497 alg,
4498 nonce, nonce_length,
4499 additional_data, additional_data_length,
4500 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4504 memset(ciphertext, 0, ciphertext_size);
4505 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004506
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004507exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004511}
4512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4514 psa_algorithm_t alg,
4515 const uint8_t *nonce,
4516 size_t nonce_length,
4517 const uint8_t *additional_data,
4518 size_t additional_data_length,
4519 const uint8_t *ciphertext,
4520 size_t ciphertext_length,
4521 uint8_t *plaintext,
4522 size_t plaintext_size,
4523 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004524{
Ronald Cron215633c2021-03-16 17:15:37 +01004525 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4526 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004527
Gilles Peskineee652a32018-06-01 19:23:52 +02004528 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004529
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 status = psa_aead_check_algorithm(alg);
4531 if (status != PSA_SUCCESS) {
4532 return status;
4533 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004534
Ronald Cron9a986162021-03-26 12:40:07 +01004535 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4537 if (status != PSA_SUCCESS) {
4538 return status;
4539 }
mohammad16035955c982018-04-26 00:53:03 +03004540
Ronald Cron215633c2021-03-16 17:15:37 +01004541 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004543 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004544
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 status = psa_aead_check_nonce_length(alg, nonce_length);
4546 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004547 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004549
Ronald Cronde822812021-03-17 16:08:20 +01004550 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004551 &attributes, slot->key.data, slot->key.bytes,
4552 alg,
4553 nonce, nonce_length,
4554 additional_data, additional_data_length,
4555 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 if (status != PSA_SUCCESS && plaintext_size != 0) {
4559 memset(plaintext, 0, plaintext_size);
4560 }
mohammad160360a64d02018-06-03 17:20:42 +03004561
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004562exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 return status;
mohammad16035955c982018-04-26 00:53:03 +03004566}
4567
Gilles Peskine449bd832023-01-11 14:50:10 +01004568static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4569{
4570 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004573#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004575 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4577 return PSA_ERROR_INVALID_ARGUMENT;
4578 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004579 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004580#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004581
Przemek Stekiel86679c72022-10-06 17:06:56 +02004582#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004584 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4586 return PSA_ERROR_INVALID_ARGUMENT;
4587 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004588 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004589#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004590
Przemek Stekiel86679c72022-10-06 17:06:56 +02004591#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004593 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 if (tag_len != 16) {
4595 return PSA_ERROR_INVALID_ARGUMENT;
4596 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004597 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004598#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004599
4600 default:
4601 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004603 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004605}
4606
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004607/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004608static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4609 int is_encrypt,
4610 mbedtls_svc_key_id_t key,
4611 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004612{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004613 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4614 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4615 psa_key_slot_t *slot = NULL;
4616 psa_key_usage_t key_usage = 0;
4617
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 status = psa_aead_check_algorithm(alg);
4619 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004620 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004624 status = PSA_ERROR_BAD_STATE;
4625 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004626 }
4627
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 if (operation->nonce_set || operation->lengths_set ||
4629 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004630 status = PSA_ERROR_BAD_STATE;
4631 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004632 }
4633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004635 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004637 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004639
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4641 alg);
4642 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004645
4646 psa_key_attributes_t attributes = {
4647 .core = slot->attr
4648 };
4649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004651 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 if (is_encrypt) {
4655 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4656 &attributes,
4657 slot->key.data,
4658 slot->key.bytes,
4659 alg);
4660 } else {
4661 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4662 &attributes,
4663 slot->key.data,
4664 slot->key.bytes,
4665 alg);
4666 }
4667 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004668 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004670
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004672
4673exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004677 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004679 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 } else {
4681 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004682 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004685}
4686
Paul Elliott302ff6b2021-04-20 18:10:30 +01004687/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004688psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4689 mbedtls_svc_key_id_t key,
4690 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004691{
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004693}
4694
4695/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004696psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4697 mbedtls_svc_key_id_t key,
4698 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004699{
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004701}
4702
4703/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004704psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4705 uint8_t *nonce,
4706 size_t nonce_size,
4707 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004708{
4709 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004710 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004711 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004712
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004713 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004716 status = PSA_ERROR_BAD_STATE;
4717 goto exit;
4718 }
4719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004721 status = PSA_ERROR_BAD_STATE;
4722 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004723 }
4724
Paul Elliotte193ea82021-10-01 13:00:16 +01004725 /* For CCM, this size may not be correct according to the PSA
4726 * specification. The PSA Crypto 1.0.1 specification states:
4727 *
4728 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4729 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4730 *
4731 * However this restriction that L has to be the smallest integer is not
4732 * applied in practice, and it is not implementable here since the
4733 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4735 operation->alg);
4736 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004737 status = PSA_ERROR_BUFFER_TOO_SMALL;
4738 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004739 }
4740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 status = psa_generate_random(local_nonce, required_nonce_size);
4742 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004743 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004745
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004747
Paul Elliott39dc6b82021-05-11 19:16:09 +01004748exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 if (status == PSA_SUCCESS) {
4750 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004751 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 } else {
4753 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004754 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004757}
4758
4759/* Set the nonce for a multipart authenticated encryption or decryption
4760 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004761psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4762 const uint8_t *nonce,
4763 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004764{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004765 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4766
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004768 status = PSA_ERROR_BAD_STATE;
4769 goto exit;
4770 }
4771
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004773 status = PSA_ERROR_BAD_STATE;
4774 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004775 }
4776
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4778 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004779 status = PSA_ERROR_INVALID_ARGUMENT;
4780 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004781 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4784 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004785
Paul Elliott39dc6b82021-05-11 19:16:09 +01004786exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004788 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 } else {
4790 psa_aead_abort(operation);
4791 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004792
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004794}
4795
4796/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004797psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4798 size_t ad_length,
4799 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004800{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004801 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004804 status = PSA_ERROR_BAD_STATE;
4805 goto exit;
4806 }
4807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 if (operation->lengths_set || operation->ad_started ||
4809 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004810 status = PSA_ERROR_BAD_STATE;
4811 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004812 }
4813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004815#if defined(PSA_WANT_ALG_GCM)
4816 case PSA_ALG_GCM:
4817 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 * bits. Without the guard this code will generate warnings on 32bit
4819 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004820#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 if (((uint64_t) ad_length) >> 61 != 0 ||
4822 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004823 status = PSA_ERROR_INVALID_ARGUMENT;
4824 goto exit;
4825 }
Paul Elliott325d3742021-09-27 17:56:28 +01004826#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004827 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004828#endif /* PSA_WANT_ALG_GCM */
4829#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004830 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004832 status = PSA_ERROR_INVALID_ARGUMENT;
4833 goto exit;
4834 }
4835 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004836#endif /* PSA_WANT_ALG_CCM */
4837#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004838 case PSA_ALG_CHACHA20_POLY1305:
4839 /* No length restrictions for ChaChaPoly. */
4840 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004841#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004842 default:
4843 break;
4844 }
Paul Elliott325d3742021-09-27 17:56:28 +01004845
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4847 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004848
Paul Elliott39dc6b82021-05-11 19:16:09 +01004849exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004851 operation->ad_remaining = ad_length;
4852 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004853 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 } else {
4855 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004856 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004859}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004860
4861/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004862psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4863 const uint8_t *input,
4864 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004865{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004866 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004869 status = PSA_ERROR_BAD_STATE;
4870 goto exit;
4871 }
4872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004874 status = PSA_ERROR_BAD_STATE;
4875 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004876 }
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (operation->lengths_set) {
4879 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004880 status = PSA_ERROR_INVALID_ARGUMENT;
4881 goto exit;
4882 }
4883
4884 operation->ad_remaining -= input_length;
4885 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004886#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004888 status = PSA_ERROR_BAD_STATE;
4889 goto exit;
4890 }
4891#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004892
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 status = psa_driver_wrapper_aead_update_ad(operation, input,
4894 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004895
Paul Elliott39dc6b82021-05-11 19:16:09 +01004896exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004898 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 } else {
4900 psa_aead_abort(operation);
4901 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004904}
4905
4906/* Encrypt or decrypt a message fragment in an active multipart AEAD
4907 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004908psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4909 const uint8_t *input,
4910 size_t input_length,
4911 uint8_t *output,
4912 size_t output_size,
4913 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004914{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004915 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004916
4917 *output_length = 0;
4918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004920 status = PSA_ERROR_BAD_STATE;
4921 goto exit;
4922 }
4923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004925 status = PSA_ERROR_BAD_STATE;
4926 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004927 }
4928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004930 /* Additional data length was supplied, but not all the additional
4931 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004933 status = PSA_ERROR_INVALID_ARGUMENT;
4934 goto exit;
4935 }
4936
4937 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004939 status = PSA_ERROR_INVALID_ARGUMENT;
4940 goto exit;
4941 }
4942
4943 operation->body_remaining -= input_length;
4944 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004945#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004947 status = PSA_ERROR_BAD_STATE;
4948 goto exit;
4949 }
4950#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004951
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4953 output, output_size,
4954 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004955
Paul Elliott39dc6b82021-05-11 19:16:09 +01004956exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004958 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 } else {
4960 psa_aead_abort(operation);
4961 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004962
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004964}
4965
Gilles Peskine449bd832023-01-11 14:50:10 +01004966static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004967{
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 if (operation->id == 0 || !operation->nonce_set) {
4969 return PSA_ERROR_BAD_STATE;
4970 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004971
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4973 operation->body_remaining != 0)) {
4974 return PSA_ERROR_INVALID_ARGUMENT;
4975 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004976
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004978}
4979
Paul Elliott302ff6b2021-04-20 18:10:30 +01004980/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004981psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4982 uint8_t *ciphertext,
4983 size_t ciphertext_size,
4984 size_t *ciphertext_length,
4985 uint8_t *tag,
4986 size_t tag_size,
4987 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004988{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004989 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4990
Paul Elliott302ff6b2021-04-20 18:10:30 +01004991 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004992 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004993
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 status = psa_aead_final_checks(operation);
4995 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004996 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005000 status = PSA_ERROR_BAD_STATE;
5001 goto exit;
5002 }
5003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5005 ciphertext_size,
5006 ciphertext_length,
5007 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005008
Paul Elliott39dc6b82021-05-11 19:16:09 +01005009exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005010
5011
Paul Elliottf47b0952021-05-21 18:02:33 +01005012 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005013 * the zero tag size, make sure the tag is set to something implausible.
5014 * Even if the operation succeeds, make sure we clear the rest of the
5015 * buffer to prevent potential leakage of anything previously placed in
5016 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005017 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005022}
5023
5024/* Finish authenticating and decrypting a message in a multipart AEAD
5025 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005026psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5027 uint8_t *plaintext,
5028 size_t plaintext_size,
5029 size_t *plaintext_length,
5030 const uint8_t *tag,
5031 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005032{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005033 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5034
Paul Elliott302ff6b2021-04-20 18:10:30 +01005035 *plaintext_length = 0;
5036
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 status = psa_aead_final_checks(operation);
5038 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005043 status = PSA_ERROR_BAD_STATE;
5044 goto exit;
5045 }
5046
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5048 plaintext_size,
5049 plaintext_length,
5050 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005051
5052exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005056}
5057
5058/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005059psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005060{
5061 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5062
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005064 /* The object has (apparently) been initialized but it is not (yet)
5065 * in use. It's ok to call abort on such an object, and there's
5066 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005068 }
5069
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005075}
5076
Gilles Peskinee59236f2018-01-27 23:32:46 +01005077/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005078/* Generators */
5079/****************************************************************/
5080
Przemek Stekiel69c46792022-06-10 12:59:51 +02005081#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005082 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005083 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305084 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
5085 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
John Durkop07cc04a2020-11-16 22:08:34 -08005086#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005087#endif /* At least one builtin KDF */
5088
Przemek Stekiel69c46792022-06-10 12:59:51 +02005089#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005090 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5091 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5092static psa_status_t psa_key_derivation_start_hmac(
5093 psa_mac_operation_t *operation,
5094 psa_algorithm_t hash_alg,
5095 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005097{
5098 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5099 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5101 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5102 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005103
Steven Cooreman72f736a2021-05-07 14:14:37 +02005104 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 status = psa_driver_wrapper_mac_sign_setup(operation,
5108 &attributes,
5109 hmac_key, hmac_key_length,
5110 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 psa_reset_key_attributes(&attributes);
5113 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005114}
5115#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005116
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005117#define HKDF_STATE_INIT 0 /* no input yet */
5118#define HKDF_STATE_STARTED 1 /* got salt */
5119#define HKDF_STATE_KEYED 2 /* got key */
5120#define HKDF_STATE_OUTPUT 3 /* output started */
5121
Gilles Peskinecbe66502019-05-16 16:59:18 +02005122static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005124{
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5126 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5127 } else {
5128 return operation->alg;
5129 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005130}
5131
Gilles Peskine449bd832023-01-11 14:50:10 +01005132psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005133{
5134 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5136 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005137 /* The object has (apparently) been initialized but it is not
5138 * in use. It's ok to call abort on such an object, and there's
5139 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005141#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5143 mbedtls_free(operation->ctx.hkdf.info);
5144 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5145 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005146#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005147#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5148 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5150 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5151 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5152 if (operation->ctx.tls12_prf.secret != NULL) {
5153 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5154 operation->ctx.tls12_prf.secret_length);
5155 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005156 }
5157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 if (operation->ctx.tls12_prf.seed != NULL) {
5159 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5160 operation->ctx.tls12_prf.seed_length);
5161 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005162 }
5163
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 if (operation->ctx.tls12_prf.label != NULL) {
5165 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5166 operation->ctx.tls12_prf.label_length);
5167 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005168 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005169#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 if (operation->ctx.tls12_prf.other_secret != NULL) {
5171 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5172 operation->ctx.tls12_prf.other_secret_length);
5173 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005174 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005175#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005176 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005177
5178 /* We leave the fields Ai and output_block to be erased safely by the
5179 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005181#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5182 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005183#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5185 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5186 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5187 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005188#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305189#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5190 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305191 if (operation->ctx.pbkdf2.salt != NULL) {
5192 mbedtls_platform_zeroize(operation->ctx.pbkdf2.salt,
5193 operation->ctx.pbkdf2.salt_length);
5194 mbedtls_free(operation->ctx.pbkdf2.salt);
5195 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305196
5197 status = PSA_SUCCESS;
5198 } else
5199#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005200 {
5201 status = PSA_ERROR_BAD_STATE;
5202 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 mbedtls_platform_zeroize(operation, sizeof(*operation));
5204 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005205}
5206
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005209{
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005213 }
5214
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005217}
5218
Gilles Peskine449bd832023-01-11 14:50:10 +01005219psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5220 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005221{
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 if (operation->alg == 0) {
5223 return PSA_ERROR_BAD_STATE;
5224 }
5225 if (capacity > operation->capacity) {
5226 return PSA_ERROR_INVALID_ARGUMENT;
5227 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005228 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005230}
5231
Przemek Stekiel69c46792022-06-10 12:59:51 +02005232#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005233/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005234static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5235 psa_algorithm_t kdf_alg,
5236 uint8_t *output,
5237 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005238{
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5240 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005241 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005242 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005243#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005245#else
5246 const uint8_t last_block = 0xff;
5247#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005248
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 if (hkdf->state < HKDF_STATE_KEYED ||
5250 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005251#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005253#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 )) {
5255 return PSA_ERROR_BAD_STATE;
5256 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005257 hkdf->state = HKDF_STATE_OUTPUT;
5258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005260 /* Copy what remains of the current block */
5261 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005263 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 }
5265 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005266 output += n;
5267 output_length -= n;
5268 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005270 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005272 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005273 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005275 * object was corrupted or if this function is called directly
5276 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 if (hkdf->block_number == last_block) {
5278 return PSA_ERROR_BAD_STATE;
5279 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005280
5281 /* We need a new block */
5282 ++hkdf->block_number;
5283 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5286 hash_alg,
5287 hkdf->prk,
5288 hash_length);
5289 if (status != PSA_SUCCESS) {
5290 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005291 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005292
5293 if (hkdf->block_number != 1) {
5294 status = psa_mac_update(&hkdf->hmac,
5295 hkdf->output_block,
5296 hash_length);
5297 if (status != PSA_SUCCESS) {
5298 return status;
5299 }
5300 }
5301 status = psa_mac_update(&hkdf->hmac,
5302 hkdf->info,
5303 hkdf->info_length);
5304 if (status != PSA_SUCCESS) {
5305 return status;
5306 }
5307 status = psa_mac_update(&hkdf->hmac,
5308 &hkdf->block_number, 1);
5309 if (status != PSA_SUCCESS) {
5310 return status;
5311 }
5312 status = psa_mac_sign_finish(&hkdf->hmac,
5313 hkdf->output_block,
5314 sizeof(hkdf->output_block),
5315 &hmac_output_length);
5316 if (status != PSA_SUCCESS) {
5317 return status;
5318 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005319 }
5320
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005322}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005323#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005324
John Durkop07cc04a2020-11-16 22:08:34 -08005325#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5326 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005327static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5328 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005330{
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5332 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005333 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5334 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005335 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005336
Janos Follath7742fee2019-06-17 12:58:10 +01005337 /* We can't be wanting more output after block 0xff, otherwise
5338 * the capacity check in psa_key_derivation_output_bytes() would have
5339 * prevented this call. It could happen only if the operation
5340 * object was corrupted or if this function is called directly
5341 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 if (tls12_prf->block_number == 0xff) {
5343 return PSA_ERROR_CORRUPTION_DETECTED;
5344 }
Janos Follath7742fee2019-06-17 12:58:10 +01005345
5346 /* We need a new block */
5347 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005348 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005349
5350 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5351 *
5352 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5353 *
5354 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5355 * HMAC_hash(secret, A(2) + seed) +
5356 * HMAC_hash(secret, A(3) + seed) + ...
5357 *
5358 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005359 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005360 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005361 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005362 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005363 * is currently extracted as `output_block` and where i is
5364 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005365 */
5366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 status = psa_key_derivation_start_hmac(&hmac,
5368 hash_alg,
5369 tls12_prf->secret,
5370 tls12_prf->secret_length);
5371 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005372 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005374
5375 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005377 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5378 * the variable seed and in this instance means it in the context of the
5379 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 status = psa_mac_update(&hmac,
5381 tls12_prf->label,
5382 tls12_prf->label_length);
5383 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005384 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 }
5386 status = psa_mac_update(&hmac,
5387 tls12_prf->seed,
5388 tls12_prf->seed_length);
5389 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005390 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 }
5392 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005393 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5395 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005396 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005398 }
5399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 status = psa_mac_sign_finish(&hmac,
5401 tls12_prf->Ai, hash_length,
5402 &hmac_output_length);
5403 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005404 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 }
5406 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005407 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005409
5410 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 status = psa_key_derivation_start_hmac(&hmac,
5412 hash_alg,
5413 tls12_prf->secret,
5414 tls12_prf->secret_length);
5415 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005416 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 }
5418 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5419 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005420 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 }
5422 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5423 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005424 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 }
5426 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5427 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005428 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 }
5430 status = psa_mac_sign_finish(&hmac,
5431 tls12_prf->output_block, hash_length,
5432 &hmac_output_length);
5433 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005434 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005436
Janos Follath7742fee2019-06-17 12:58:10 +01005437
5438cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 cleanup_status = psa_mac_abort(&hmac);
5440 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005441 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005445}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005446
Janos Follath844eb0e2019-06-19 12:10:49 +01005447static psa_status_t psa_key_derivation_tls12_prf_read(
5448 psa_tls12_prf_key_derivation_t *tls12_prf,
5449 psa_algorithm_t alg,
5450 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005452{
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5454 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005455 psa_status_t status;
5456 uint8_t offset, length;
5457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005459 case PSA_TLS12_PRF_STATE_LABEL_SET:
5460 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5461 break;
5462 case PSA_TLS12_PRF_STATE_OUTPUT:
5463 break;
5464 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005466 }
5467
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005469 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 if (tls12_prf->left_in_block == 0) {
5471 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5472 alg);
5473 if (status != PSA_SUCCESS) {
5474 return status;
5475 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005476
5477 continue;
5478 }
5479
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005481 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005483 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005485
5486 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005488 output += length;
5489 output_length -= length;
5490 tls12_prf->left_in_block -= length;
5491 }
5492
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005494}
John Durkop07cc04a2020-11-16 22:08:34 -08005495#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5496 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005497
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005498#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5499static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5500 psa_tls12_ecjpake_to_pms_t *ecjpake,
5501 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005503{
5504 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005505 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 if (output_length != 32) {
5508 return PSA_ERROR_INVALID_ARGUMENT;
5509 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005510
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5512 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5513 &output_size);
5514 if (status != PSA_SUCCESS) {
5515 return status;
5516 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005517
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 if (output_size != output_length) {
5519 return PSA_ERROR_GENERIC_ERROR;
5520 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005521
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005523}
5524#endif
5525
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305526#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5527static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5528 psa_pbkdf2_key_derivation_t *pbkdf2,
5529 psa_algorithm_t prf_alg,
5530 uint8_t prf_output_length,
5531 psa_key_attributes_t *attributes)
5532{
5533 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305534 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305535 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305536 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305537 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305538 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305539 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305540
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305541 mac_operation.is_sign = 1;
5542 mac_operation.mac_size = prf_output_length;
5543 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305544
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305545 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5546 attributes,
5547 pbkdf2->password,
5548 pbkdf2->password_length,
5549 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305550 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305551 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305552 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305553 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5554 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305555 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305556 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305557 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305558 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305559 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305560 }
5561 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5562 &mac_output_length);
5563 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305564 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305565 }
5566
5567 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305568 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305569 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305570 }
5571
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305572 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305573
5574 for (i = 1; i < pbkdf2->input_cost; i++) {
5575 status = psa_driver_wrapper_mac_compute(attributes,
5576 pbkdf2->password,
5577 pbkdf2->password_length,
5578 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305579 U_i, sizeof(U_i),
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305580 &mac_output_length);
5581 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305582 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305583 }
5584
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305585 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305586 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305587
5588cleanup:
5589 /* Zeroise buffers to clear sensitive data from memory. */
5590 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5591 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305592}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305593
5594static psa_status_t psa_key_derivation_pbkdf2_read(
5595 psa_pbkdf2_key_derivation_t *pbkdf2,
5596 psa_algorithm_t kdf_alg,
5597 uint8_t *output,
5598 size_t output_length)
5599{
5600 psa_status_t status;
5601 psa_algorithm_t prf_alg;
5602 uint8_t prf_output_length;
5603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5604 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5605 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5606
5607 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5608 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5609 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5610 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305611 } else {
5612 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305613 }
5614
5615 switch (pbkdf2->state) {
5616 case PSA_PBKDF2_STATE_PASSWORD_SET:
5617 /* Initially we need a new block so bytes_used is equal to block size*/
5618 pbkdf2->bytes_used = prf_output_length;
5619 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5620 break;
5621 case PSA_PBKDF2_STATE_OUTPUT:
5622 break;
5623 default:
5624 return PSA_ERROR_BAD_STATE;
5625 }
5626
5627 while (output_length != 0) {
5628 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5629 if (n > output_length) {
5630 n = (uint8_t) output_length;
5631 }
5632 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5633 output += n;
5634 output_length -= n;
5635 pbkdf2->bytes_used += n;
5636
5637 if (output_length == 0) {
5638 break;
5639 }
5640
5641 /* We need a new block */
5642 pbkdf2->bytes_used = 0;
5643 pbkdf2->block_number++;
5644
5645 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5646 prf_output_length,
5647 &attributes);
5648 if (status != PSA_SUCCESS) {
5649 return status;
5650 }
5651 }
5652
5653 return PSA_SUCCESS;
5654}
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305655#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
5656
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005657psa_status_t psa_key_derivation_output_bytes(
5658 psa_key_derivation_operation_t *operation,
5659 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005661{
5662 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005664
Gilles Peskine449bd832023-01-11 14:50:10 +01005665 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005666 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005668 }
5669
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005671 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005672 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005673 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005674 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005675 goto exit;
5676 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005678 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005679 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005680 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5681 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005683 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005685 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005687
Przemek Stekiel69c46792022-06-10 12:59:51 +02005688#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5690 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5691 output, output_length);
5692 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005693#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005694#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5695 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5697 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5698 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5699 kdf_alg, output,
5700 output_length);
5701 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005702#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5703 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005704#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005706 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5708 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005709#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305710#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5711 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305712 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5713 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305714 } else
5715#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005716
Gilles Peskineeab56e42018-07-12 17:12:33 +02005717 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005718 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005720 }
5721
5722exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005724 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005725 * This allows us to differentiate between exhausted operations and
5726 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5727 * operations. */
5728 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005730 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005732 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005734}
5735
David Brown7807bf72021-02-09 16:28:23 -07005736#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005737static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005738{
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 if (data_size >= 8) {
5740 mbedtls_des_key_set_parity(data);
5741 }
5742 if (data_size >= 16) {
5743 mbedtls_des_key_set_parity(data + 8);
5744 }
5745 if (data_size >= 24) {
5746 mbedtls_des_key_set_parity(data + 16);
5747 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005748}
David Brown7807bf72021-02-09 16:28:23 -07005749#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005750
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005751/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 * ECC keys on a Weierstrass elliptic curve require the generation
5753 * of a private key which is an integer
5754 * in the range [1, N - 1], where N is the boundary of the private key domain:
5755 * N is the prime p for Diffie-Hellman, or the order of the
5756 * curve’s base point for ECC.
5757 *
5758 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5759 * This function generates the private key using the following process:
5760 *
5761 * 1. Draw a byte string of length ceiling(m/8) bytes.
5762 * 2. If m is not a multiple of 8, set the most significant
5763 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5764 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5765 * 4. If k > N - 2, discard the result and return to step 1.
5766 * 5. Output k + 1 as the private key.
5767 *
5768 * This method allows compliance to NIST standards, specifically the methods titled
5769 * Key-Pair Generation by Testing Candidates in the following publications:
5770 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5771 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5772 * Diffie-Hellman keys.
5773 *
5774 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5775 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5776 *
5777 * Note: Function allocates memory for *data buffer, so given *data should be
5778 * always NULL.
5779 */
Valerio Setti8bb57632023-05-26 13:48:07 +02005780#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY) || \
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005781 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005782 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5783 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5784 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005785static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005786 psa_key_slot_t *slot,
5787 size_t bits,
5788 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005789 uint8_t **data
5790 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005791{
Valerio Setti52789862023-04-07 12:13:11 +02005792#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005793 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005794 mbedtls_mpi k;
5795 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005796 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005797 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 mbedtls_mpi_init(&k);
5800 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005801
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005802 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005804 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005808 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005809 goto cleanup;
5810 }
5811
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005812 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005816
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005817 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005818 /* Let m be the bit size of N. */
5819 size_t m = ecp_group.nbits;
5820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005822
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005823 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005825
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005826 /* Note: This function is always called with *data == NULL and it
5827 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 *data = mbedtls_calloc(1, m_bytes);
5829 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005830 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005831 goto cleanup;
5832 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005835 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005837 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005839
5840 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005842 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005843 * (8 * ceiling(m/8) - m) bits of the first byte in
5844 * the string to zero.
5845 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005847 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005848 }
5849
5850 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 * big-endian byte string.
5852 */
5853 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005854
5855 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 * Result of comparison is returned. When it indicates error
5857 * then this function is called again.
5858 */
5859 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005860 }
5861
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005862 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5864 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005865cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 if (ret != 0) {
5867 status = mbedtls_to_psa_error(ret);
5868 }
5869 if (status != PSA_SUCCESS) {
5870 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005871 *data = NULL;
5872 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 mbedtls_mpi_free(&k);
5874 mbedtls_mpi_free(&diff_N_2);
5875 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005876#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005877 (void) slot;
5878 (void) bits;
5879 (void) operation;
5880 (void) data;
5881 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005882#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005883}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005884
5885/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5886 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5887 *
5888 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5889 * draw a 32-byte string and process it as specified in
5890 * Elliptic Curves for Security [RFC7748] §5.
5891 *
5892 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5893 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5894 *
5895 * Note: Function allocates memory for *data buffer, so given *data should be
5896 * always NULL.
5897 */
5898
5899static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5900 size_t bits,
5901 psa_key_derivation_operation_t *operation,
5902 uint8_t **data
5903 )
5904{
5905 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005907
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005909 case 255:
5910 output_length = 32;
5911 break;
5912 case 448:
5913 output_length = 56;
5914 break;
5915 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005917 break;
5918 }
5919
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 if (*data == NULL) {
5923 return PSA_ERROR_INSUFFICIENT_MEMORY;
5924 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005929 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005933 case 255:
5934 (*data)[0] &= 248;
5935 (*data)[31] &= 127;
5936 (*data)[31] |= 64;
5937 break;
5938 case 448:
5939 (*data)[0] &= 252;
5940 (*data)[55] |= 128;
5941 break;
5942 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005944 break;
5945 }
5946
5947 return status;
5948}
Valerio Setti8bb57632023-05-26 13:48:07 +02005949#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY) ||
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005950 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005951 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5952 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5953 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005954
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005955static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005956 psa_key_slot_t *slot,
5957 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005959{
5960 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305962 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005963 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5966 return PSA_ERROR_INVALID_ARGUMENT;
5967 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005968
Valerio Setti8bb57632023-05-26 13:48:07 +02005969#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY) || \
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005970 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005971 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5972 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5973 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5975 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5976 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005977 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5979 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005980 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 }
5982 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005983 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5985 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005986 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005988 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005989 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02005990#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY) ||
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005991 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005992 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5993 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5994 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 if (key_type_is_raw_bytes(slot->attr.type)) {
5996 if (bits % 8 != 0) {
5997 return PSA_ERROR_INVALID_ARGUMENT;
5998 }
5999 data = mbedtls_calloc(1, bytes);
6000 if (data == NULL) {
6001 return PSA_ERROR_INSUFFICIENT_MEMORY;
6002 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006003
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 status = psa_key_derivation_output_bytes(operation, data, bytes);
6005 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006006 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 }
David Brown12ca5032021-02-11 11:02:00 -07006008#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6010 psa_des_set_key_parity(data, bytes);
6011 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006012#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 } else {
6014 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006015 }
Ronald Crondd04d422020-11-28 15:14:42 +01006016
Ronald Cronbf33c932020-11-28 18:06:53 +01006017 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006018 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006020 };
6021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6023 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6024 &storage_size);
6025 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306026 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 }
Archanad8a83dc2021-06-14 10:04:16 +05306028 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 status = psa_allocate_buffer_to_slot(slot, storage_size);
6030 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306031 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 }
Archanad8a83dc2021-06-14 10:04:16 +05306033
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 status = psa_driver_wrapper_import_key(&attributes,
6035 data, bytes,
6036 slot->key.data,
6037 slot->key.bytes,
6038 &slot->key.bytes, &bits);
6039 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006040 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006042
6043exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 mbedtls_free(data);
6045 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006046}
6047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6049 psa_key_derivation_operation_t *operation,
6050 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006051{
6052 psa_status_t status;
6053 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006054 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006055
Ronald Cron81709fc2020-11-14 12:10:32 +01006056 *key = MBEDTLS_SVC_KEY_ID_INIT;
6057
Gilles Peskine0f84d622019-09-12 19:03:13 +02006058 /* Reject any attempt to create a zero-length key so that we don't
6059 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 if (psa_get_key_bits(attributes) == 0) {
6061 return PSA_ERROR_INVALID_ARGUMENT;
6062 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 if (operation->alg == PSA_ALG_NONE) {
6065 return PSA_ERROR_BAD_STATE;
6066 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006067
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 if (!operation->can_output_key) {
6069 return PSA_ERROR_NOT_PERMITTED;
6070 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6073 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006074#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006076 /* Deriving a key in a secure element is not implemented yet. */
6077 status = PSA_ERROR_NOT_SUPPORTED;
6078 }
6079#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (status == PSA_SUCCESS) {
6081 status = psa_generate_derived_key_internal(slot,
6082 attributes->core.bits,
6083 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006084 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 if (status == PSA_SUCCESS) {
6086 status = psa_finish_key_creation(slot, driver, key);
6087 }
6088 if (status != PSA_SUCCESS) {
6089 psa_fail_key_creation(slot, driver);
6090 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006093}
6094
Gilles Peskineeab56e42018-07-12 17:12:33 +02006095
6096
6097/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006098/* Key derivation */
6099/****************************************************************/
6100
Steven Cooreman932ffb72021-02-15 12:14:32 +01006101#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006102static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006103{
6104#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6106 return 1;
6107 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006108#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006109#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6111 return 1;
6112 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006113#endif
6114#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6116 return 1;
6117 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006118#endif
6119#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6121 return 1;
6122 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006123#endif
6124#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6126 return 1;
6127 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006128#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006129#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6131 return 1;
6132 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006133#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306134#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6135 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6136 return 1;
6137 }
6138#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006140}
6141
Gilles Peskine449bd832023-01-11 14:50:10 +01006142static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006143{
6144 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 psa_status_t status = psa_hash_setup(&operation, alg);
6146 psa_hash_abort(&operation);
6147 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006148}
6149
Gilles Peskine969c5d62019-01-16 15:53:06 +01006150static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006151 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006153{
Janos Follath5fe19732019-06-20 15:09:30 +01006154 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6155 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006157
Gilles Peskine969c5d62019-01-16 15:53:06 +01006158 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 if (!is_kdf_alg_supported(kdf_alg)) {
6160 return PSA_ERROR_NOT_SUPPORTED;
6161 }
John Durkop07cc04a2020-11-16 22:08:34 -08006162
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006163 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006164 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6166 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6167 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6168 if (hash_size == 0) {
6169 return PSA_ERROR_NOT_SUPPORTED;
6170 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006171
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006172 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6173 * we might fail later, which is somewhat unfriendly and potentially
6174 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 psa_status_t status = psa_hash_try_support(hash_alg);
6176 if (status != PSA_SUCCESS) {
6177 return status;
6178 }
6179 } else {
6180 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006181 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006182
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6184 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6185 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6186 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006187 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006188#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006189 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6191 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006192 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006194#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6195 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 operation->capacity = 255 * hash_size;
6197 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006198}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006201{
6202#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (alg == PSA_ALG_ECDH) {
6204 return PSA_SUCCESS;
6205 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006206#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006207#if defined(PSA_WANT_ALG_FFDH)
6208 if (alg == PSA_ALG_FFDH) {
6209 return PSA_SUCCESS;
6210 }
6211#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006212 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006214}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006215
6216static int psa_key_derivation_allows_free_form_secret_input(
6217 psa_algorithm_t kdf_alg)
6218{
6219#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6220 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6221 return 0;
6222 }
6223#endif
6224 (void) kdf_alg;
6225 return 1;
6226}
John Durkop07cc04a2020-11-16 22:08:34 -08006227#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006228
Gilles Peskine449bd832023-01-11 14:50:10 +01006229psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6230 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006231{
6232 psa_status_t status;
6233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 if (operation->alg != 0) {
6235 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006236 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6239 return PSA_ERROR_INVALID_ARGUMENT;
6240 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6241#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6242 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6243 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6244 status = psa_key_agreement_try_support(ka_alg);
6245 if (status != PSA_SUCCESS) {
6246 return status;
6247 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006248 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6249 return PSA_ERROR_INVALID_ARGUMENT;
6250 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6252#else
6253 return PSA_ERROR_NOT_SUPPORTED;
6254#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6255 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6256#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6257 status = psa_key_derivation_setup_kdf(operation, alg);
6258#else
6259 return PSA_ERROR_NOT_SUPPORTED;
6260#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6261 } else {
6262 return PSA_ERROR_INVALID_ARGUMENT;
6263 }
6264
6265 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006266 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 }
6268 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006269}
6270
Przemek Stekiel69c46792022-06-10 12:59:51 +02006271#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006272static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6273 psa_algorithm_t kdf_alg,
6274 psa_key_derivation_step_t step,
6275 const uint8_t *data,
6276 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006277{
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006279 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006281 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006282#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6284 return PSA_ERROR_INVALID_ARGUMENT;
6285 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006286#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 if (hkdf->state != HKDF_STATE_INIT) {
6288 return PSA_ERROR_BAD_STATE;
6289 } else {
6290 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6291 hash_alg,
6292 data, data_length);
6293 if (status != PSA_SUCCESS) {
6294 return status;
6295 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006296 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006298 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006299 case PSA_KEY_DERIVATION_INPUT_SECRET:
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)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006302 /* We shouldn't be in different state as HKDF_EXPAND only allows
6303 * two inputs: SECRET (this case) and INFO which does not modify
6304 * the state. It could happen only if the hkdf
6305 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 if (hkdf->state != HKDF_STATE_INIT) {
6307 return PSA_ERROR_BAD_STATE;
6308 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006309
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006310 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6312 return PSA_ERROR_INVALID_ARGUMENT;
6313 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006314
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 memcpy(hkdf->prk, data, data_length);
6316 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006317#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006318 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006319 /* HKDF: If no salt was provided, use an empty salt.
6320 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006322#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6324 return PSA_ERROR_BAD_STATE;
6325 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006326#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6328 hash_alg,
6329 NULL, 0);
6330 if (status != PSA_SUCCESS) {
6331 return status;
6332 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006333 hkdf->state = HKDF_STATE_STARTED;
6334 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 if (hkdf->state != HKDF_STATE_STARTED) {
6336 return PSA_ERROR_BAD_STATE;
6337 }
6338 status = psa_mac_update(&hkdf->hmac,
6339 data, data_length);
6340 if (status != PSA_SUCCESS) {
6341 return status;
6342 }
6343 status = psa_mac_sign_finish(&hkdf->hmac,
6344 hkdf->prk,
6345 sizeof(hkdf->prk),
6346 &data_length);
6347 if (status != PSA_SUCCESS) {
6348 return status;
6349 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006350 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006351
Gilles Peskine2b522db2019-04-12 15:11:49 +02006352 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006353 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006354#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006356 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006358 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006360#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006361 {
6362 /* Block 0 is empty, and the next block will be
6363 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006365 }
6366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006368 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006369#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6371 return PSA_ERROR_INVALID_ARGUMENT;
6372 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006373#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006374#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6376 hkdf->state == HKDF_STATE_INIT) {
6377 return PSA_ERROR_BAD_STATE;
6378 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006379#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 if (hkdf->state == HKDF_STATE_OUTPUT) {
6381 return PSA_ERROR_BAD_STATE;
6382 }
6383 if (hkdf->info_set) {
6384 return PSA_ERROR_BAD_STATE;
6385 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006386 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 if (data_length != 0) {
6388 hkdf->info = mbedtls_calloc(1, data_length);
6389 if (hkdf->info == NULL) {
6390 return PSA_ERROR_INSUFFICIENT_MEMORY;
6391 }
6392 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006393 }
6394 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006396 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006398 }
6399}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006400#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006401
John Durkop07cc04a2020-11-16 22:08:34 -08006402#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6403 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006404static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6405 const uint8_t *data,
6406 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006407{
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6409 return PSA_ERROR_BAD_STATE;
6410 }
Janos Follathf08e2652019-06-13 09:05:41 +01006411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 if (data_length != 0) {
6413 prf->seed = mbedtls_calloc(1, data_length);
6414 if (prf->seed == NULL) {
6415 return PSA_ERROR_INSUFFICIENT_MEMORY;
6416 }
Janos Follathf08e2652019-06-13 09:05:41 +01006417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006419 prf->seed_length = data_length;
6420 }
Janos Follathf08e2652019-06-13 09:05:41 +01006421
Gilles Peskine43f958b2020-12-13 14:55:14 +01006422 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006423
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006425}
6426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6428 const uint8_t *data,
6429 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006430{
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6432 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6433 return PSA_ERROR_BAD_STATE;
6434 }
Janos Follath81550542019-06-13 14:26:34 +01006435
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 if (data_length != 0) {
6437 prf->secret = mbedtls_calloc(1, data_length);
6438 if (prf->secret == NULL) {
6439 return PSA_ERROR_INSUFFICIENT_MEMORY;
6440 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006441
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006443 prf->secret_length = data_length;
6444 }
Janos Follath81550542019-06-13 14:26:34 +01006445
Gilles Peskine43f958b2020-12-13 14:55:14 +01006446 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006449}
6450
Gilles Peskine449bd832023-01-11 14:50:10 +01006451static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6452 const uint8_t *data,
6453 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006454{
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6456 return PSA_ERROR_BAD_STATE;
6457 }
Janos Follath63028dd2019-06-13 09:15:47 +01006458
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 if (data_length != 0) {
6460 prf->label = mbedtls_calloc(1, data_length);
6461 if (prf->label == NULL) {
6462 return PSA_ERROR_INSUFFICIENT_MEMORY;
6463 }
Janos Follath63028dd2019-06-13 09:15:47 +01006464
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006466 prf->label_length = data_length;
6467 }
Janos Follath63028dd2019-06-13 09:15:47 +01006468
Gilles Peskine43f958b2020-12-13 14:55:14 +01006469 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006470
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006472}
6473
Gilles Peskine449bd832023-01-11 14:50:10 +01006474static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6475 psa_key_derivation_step_t step,
6476 const uint8_t *data,
6477 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006478{
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006480 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006482 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006484 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006486 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006488 }
6489}
John Durkop07cc04a2020-11-16 22:08:34 -08006490#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6491 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6492
6493#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6494static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6495 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006496 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006498{
6499 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6501 4 + data_length + prf->other_secret_length :
6502 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006503
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6505 return PSA_ERROR_INVALID_ARGUMENT;
6506 }
John Durkop07cc04a2020-11-16 22:08:34 -08006507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 uint8_t *pms = mbedtls_calloc(1, pms_len);
6509 if (pms == NULL) {
6510 return PSA_ERROR_INSUFFICIENT_MEMORY;
6511 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006512 uint8_t *cur = pms;
6513
6514 /* pure-PSK:
6515 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006516 *
6517 * The premaster secret is formed as follows: if the PSK is N octets
6518 * long, concatenate a uint16 with the value N, N zero octets, a second
6519 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006520 *
6521 * mixed-PSK:
6522 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6523 * follows: concatenate a uint16 with the length of the other secret,
6524 * the other secret itself, uint16 with the length of PSK, and the
6525 * PSK itself.
6526 * For details please check:
6527 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6528 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6529 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006530 */
6531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6533 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6534 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6535 if (prf->other_secret_length != 0) {
6536 memcpy(cur, prf->other_secret, prf->other_secret_length);
6537 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006538 cur += prf->other_secret_length;
6539 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 } else {
6541 *cur++ = MBEDTLS_BYTE_1(data_length);
6542 *cur++ = MBEDTLS_BYTE_0(data_length);
6543 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006544 cur += data_length;
6545 }
6546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 *cur++ = MBEDTLS_BYTE_1(data_length);
6548 *cur++ = MBEDTLS_BYTE_0(data_length);
6549 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006550 cur += data_length;
6551
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006553
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 mbedtls_platform_zeroize(pms, pms_len);
6555 mbedtls_free(pms);
6556 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006557}
Janos Follath6660f0e2019-06-17 08:44:03 +01006558
Przemek Stekiele47201b2022-04-19 13:53:28 +02006559static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6560 psa_tls12_prf_key_derivation_t *prf,
6561 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006563{
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6565 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006566 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006567
6568 if (data_length != 0) {
6569 prf->other_secret = mbedtls_calloc(1, data_length);
6570 if (prf->other_secret == NULL) {
6571 return PSA_ERROR_INSUFFICIENT_MEMORY;
6572 }
6573
6574 memcpy(prf->other_secret, data, data_length);
6575 prf->other_secret_length = data_length;
6576 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006577 prf->other_secret_length = 0;
6578 }
6579
6580 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6581
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006583}
6584
Janos Follath6660f0e2019-06-17 08:44:03 +01006585static psa_status_t psa_tls12_prf_psk_to_ms_input(
6586 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006587 psa_key_derivation_step_t step,
6588 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006590{
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006592 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 return psa_tls12_prf_psk_to_ms_set_key(prf,
6594 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006595 break;
6596 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6598 data,
6599 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006600 break;
6601 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006603 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006604
Przemek Stekiele47201b2022-04-19 13:53:28 +02006605 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006606}
John Durkop07cc04a2020-11-16 22:08:34 -08006607#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006608
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006609#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6610static psa_status_t psa_tls12_ecjpake_to_pms_input(
6611 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006612 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006613 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006615{
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6617 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6618 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006619 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006620
6621 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006622 if (data[0] != 0x04) {
6623 return PSA_ERROR_INVALID_ARGUMENT;
6624 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006625
6626 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006628
Gilles Peskine449bd832023-01-11 14:50:10 +01006629 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006630}
6631#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306632
6633#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6634static psa_status_t psa_pbkdf2_set_input_cost(
6635 psa_pbkdf2_key_derivation_t *pbkdf2,
6636 psa_key_derivation_step_t step,
6637 uint64_t data)
6638{
6639 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6640 return PSA_ERROR_INVALID_ARGUMENT;
6641 }
6642
6643 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6644 return PSA_ERROR_BAD_STATE;
6645 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306646
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306647 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306648 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306649 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306650
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306651 if (data == 0) {
6652 return PSA_ERROR_INVALID_ARGUMENT;
6653 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306654
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306655 pbkdf2->input_cost = data;
6656 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6657
6658 return PSA_SUCCESS;
6659}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306660
6661static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6662 const uint8_t *data,
6663 size_t data_length)
6664{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306665 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6666 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6667 return PSA_ERROR_BAD_STATE;
6668 }
6669
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306670 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6671 pbkdf2->salt = mbedtls_calloc(1, data_length);
6672 if (pbkdf2->salt == NULL) {
6673 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306674 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306675
6676 memcpy(pbkdf2->salt, data, data_length);
6677 pbkdf2->salt_length = data_length;
6678 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306679 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306680
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306681 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6682 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306683 return PSA_ERROR_INSUFFICIENT_MEMORY;
6684 }
6685
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306686 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6687 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306688 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306689 mbedtls_free(pbkdf2->salt);
6690 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306691 }
6692
6693 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6694
6695 return PSA_SUCCESS;
6696}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306697
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306698static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306699 const uint8_t *input,
6700 size_t input_len,
6701 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306702 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306703{
6704 psa_status_t status = PSA_SUCCESS;
6705 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6706 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306707 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306708 } else {
6709 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306710 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306711 }
6712 return status;
6713}
6714
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306715static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306716 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306717 const uint8_t *data,
6718 size_t data_length)
6719{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306720 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306721 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6722 return PSA_ERROR_BAD_STATE;
6723 }
6724
6725 if (data_length != 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306726 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6727 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6728 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6729 pbkdf2->password,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306730 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306731 }
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306732 }
6733
6734 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6735
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306736 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306737}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306738
6739static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306740 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306741 psa_key_derivation_step_t step,
6742 const uint8_t *data,
6743 size_t data_length)
6744{
6745 switch (step) {
6746 case PSA_KEY_DERIVATION_INPUT_SALT:
6747 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6748 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306749 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306750 default:
6751 return PSA_ERROR_INVALID_ARGUMENT;
6752 }
6753}
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306754#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6755
Gilles Peskineb8965192019-09-24 16:21:10 +02006756/** Check whether the given key type is acceptable for the given
6757 * input step of a key derivation.
6758 *
6759 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6760 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6761 * Both secret and non-secret inputs can alternatively have the type
6762 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6763 * that the input was passed as a buffer rather than via a key object.
6764 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006765static int psa_key_derivation_check_input_type(
6766 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006768{
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006770 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 if (key_type == PSA_KEY_TYPE_DERIVE) {
6772 return PSA_SUCCESS;
6773 }
6774 if (key_type == PSA_KEY_TYPE_NONE) {
6775 return PSA_SUCCESS;
6776 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006777 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006778 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 if (key_type == PSA_KEY_TYPE_DERIVE) {
6780 return PSA_SUCCESS;
6781 }
6782 if (key_type == PSA_KEY_TYPE_NONE) {
6783 return PSA_SUCCESS;
6784 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006785 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006786 case PSA_KEY_DERIVATION_INPUT_LABEL:
6787 case PSA_KEY_DERIVATION_INPUT_SALT:
6788 case PSA_KEY_DERIVATION_INPUT_INFO:
6789 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6791 return PSA_SUCCESS;
6792 }
6793 if (key_type == PSA_KEY_TYPE_NONE) {
6794 return PSA_SUCCESS;
6795 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006796 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306797 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6798 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6799 return PSA_SUCCESS;
6800 }
6801 if (key_type == PSA_KEY_TYPE_DERIVE) {
6802 return PSA_SUCCESS;
6803 }
6804 if (key_type == PSA_KEY_TYPE_NONE) {
6805 return PSA_SUCCESS;
6806 }
6807 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006808 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006809 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006810}
6811
Janos Follathb80a94e2019-06-12 15:54:46 +01006812static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006813 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006814 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006815 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006816 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006818{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006819 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006821
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 status = psa_key_derivation_check_input_type(step, key_type);
6823 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006824 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006826
Przemek Stekiel69c46792022-06-10 12:59:51 +02006827#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006828 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6829 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6830 step, data, data_length);
6831 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006832#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006833#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6835 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6836 step, data, data_length);
6837 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006838#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6839#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6841 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6842 step, data, data_length);
6843 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006844#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006845#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006847 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6849 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006850#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306851#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6852 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306853 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6854 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306855 } else
6856#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006857 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006858 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006859 (void) data;
6860 (void) data_length;
6861 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006863 }
6864
Gilles Peskine224b0d62019-09-23 18:13:17 +02006865exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 if (status != PSA_SUCCESS) {
6867 psa_key_derivation_abort(operation);
6868 }
6869 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006870}
6871
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306872static psa_status_t psa_key_derivation_input_integer_internal(
6873 psa_key_derivation_operation_t *operation,
6874 psa_key_derivation_step_t step,
6875 uint64_t value)
6876{
6877 psa_status_t status;
6878 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6879
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306880#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6881 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6882 status = psa_pbkdf2_set_input_cost(
6883 &operation->ctx.pbkdf2, step, value);
6884 } else
6885#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306886 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306887 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306888 (void) value;
6889 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306890 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306891 }
6892
6893 if (status != PSA_SUCCESS) {
6894 psa_key_derivation_abort(operation);
6895 }
6896 return status;
6897}
6898
Janos Follath51f4a0f2019-06-14 11:35:55 +01006899psa_status_t psa_key_derivation_input_bytes(
6900 psa_key_derivation_operation_t *operation,
6901 psa_key_derivation_step_t step,
6902 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006904{
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 return psa_key_derivation_input_internal(operation, step,
6906 PSA_KEY_TYPE_NONE,
6907 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006908}
6909
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306910psa_status_t psa_key_derivation_input_integer(
6911 psa_key_derivation_operation_t *operation,
6912 psa_key_derivation_step_t step,
6913 uint64_t value)
6914{
6915 return psa_key_derivation_input_integer_internal(operation, step, value);
6916}
6917
Janos Follath51f4a0f2019-06-14 11:35:55 +01006918psa_status_t psa_key_derivation_input_key(
6919 psa_key_derivation_operation_t *operation,
6920 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006922{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006923 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006924 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006925 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006926
Ronald Cron5c522922020-11-14 16:35:34 +01006927 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6929 if (status != PSA_SUCCESS) {
6930 psa_key_derivation_abort(operation);
6931 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006932 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006933
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306934 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6935 * permission to output to a key object. */
6936 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6937 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006938 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 status = psa_key_derivation_input_internal(operation,
6942 step, slot->attr.type,
6943 slot->key.data,
6944 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006945
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006947
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006949}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006950
6951
6952
6953/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006954/* Key agreement */
6955/****************************************************************/
6956
Gilles Peskine449bd832023-01-11 14:50:10 +01006957psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6958 const uint8_t *key_buffer,
6959 size_t key_buffer_size,
6960 psa_algorithm_t alg,
6961 const uint8_t *peer_key,
6962 size_t peer_key_length,
6963 uint8_t *shared_secret,
6964 size_t shared_secret_size,
6965 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006966{
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006968#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006969 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6971 key_buffer_size, alg,
6972 peer_key, peer_key_length,
6973 shared_secret,
6974 shared_secret_size,
6975 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006976#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006977
6978#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6979 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006980 return mbedtls_psa_key_agreement_ffdh(attributes,
6981 peer_key,
6982 peer_key_length,
6983 key_buffer,
6984 key_buffer_size,
6985 shared_secret,
6986 shared_secret_size,
6987 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006988#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6989
Gilles Peskine01d718c2018-09-18 12:01:02 +02006990 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006991 (void) attributes;
6992 (void) key_buffer;
6993 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006994 (void) peer_key;
6995 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006996 (void) shared_secret;
6997 (void) shared_secret_size;
6998 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007000 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007001}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007002
Aditya Deshpande17845b82022-10-13 17:21:01 +01007003/** Internal function for raw key agreement
7004 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007005 * to the driver's implementation if a driver is present.
7006 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007007 * (psa_key_agreement_raw_builtin).
7008 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007009static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7010 psa_key_slot_t *private_key,
7011 const uint8_t *peer_key,
7012 size_t peer_key_length,
7013 uint8_t *shared_secret,
7014 size_t shared_secret_size,
7015 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007016{
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7018 return PSA_ERROR_NOT_SUPPORTED;
7019 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007020
7021 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007023 };
7024
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 return psa_driver_wrapper_key_agreement(&attributes,
7026 private_key->key.data,
7027 private_key->key.bytes, alg,
7028 peer_key, peer_key_length,
7029 shared_secret,
7030 shared_secret_size,
7031 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007032}
7033
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007034/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007035 * to potentially free embedded data structures and wipe confidential data.
7036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007037static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7038 psa_key_derivation_step_t step,
7039 psa_key_slot_t *private_key,
7040 const uint8_t *peer_key,
7041 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007042{
7043 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007044 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007045 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007047
7048 /* Step 1: run the secret agreement algorithm to generate the shared
7049 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 status = psa_key_agreement_raw_internal(ka_alg,
7051 private_key,
7052 peer_key, peer_key_length,
7053 shared_secret,
7054 sizeof(shared_secret),
7055 &shared_secret_length);
7056 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007057 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007059
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007060 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007061 * the shared secret. A shared secret is permitted wherever a key
7062 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 status = psa_key_derivation_input_internal(operation, step,
7064 PSA_KEY_TYPE_DERIVE,
7065 shared_secret,
7066 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007067exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7069 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007070}
7071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7073 psa_key_derivation_step_t step,
7074 mbedtls_svc_key_id_t private_key,
7075 const uint8_t *peer_key,
7076 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007077{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007078 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007079 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007080 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007081
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7083 return PSA_ERROR_INVALID_ARGUMENT;
7084 }
Ronald Cron5c522922020-11-14 16:35:34 +01007085 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7087 if (status != PSA_SUCCESS) {
7088 return status;
7089 }
7090 status = psa_key_agreement_internal(operation, step,
7091 slot,
7092 peer_key, peer_key_length);
7093 if (status != PSA_SUCCESS) {
7094 psa_key_derivation_abort(operation);
7095 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007096 /* If a private key has been added as SECRET, we allow the derived
7097 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007099 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007101 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007102
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007104
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007106}
7107
Gilles Peskine449bd832023-01-11 14:50:10 +01007108psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7109 mbedtls_svc_key_id_t private_key,
7110 const uint8_t *peer_key,
7111 size_t peer_key_length,
7112 uint8_t *output,
7113 size_t output_size,
7114 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007115{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007116 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007117 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007118 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007119
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007121 status = PSA_ERROR_INVALID_ARGUMENT;
7122 goto exit;
7123 }
Ronald Cron5c522922020-11-14 16:35:34 +01007124 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7126 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007129
Gilles Peskine3e561302022-04-14 00:17:15 +02007130 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7131 * for the output size. The PSA specification only guarantees that this
7132 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7133 * but it might be nice to allow smaller buffers if the output fits.
7134 * At the time of writing this comment, with only ECDH implemented,
7135 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7136 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7137 * be exact for it as well. */
7138 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7140 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007141 status = PSA_ERROR_BUFFER_TOO_SMALL;
7142 goto exit;
7143 }
7144
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 status = psa_key_agreement_raw_internal(alg, slot,
7146 peer_key, peer_key_length,
7147 output, output_size,
7148 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007149
7150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007152 /* If an error happens and is not handled properly, the output
7153 * may be used as a key to protect sensitive data. Arrange for such
7154 * a key to be random, which is likely to result in decryption or
7155 * verification errors. This is better than filling the buffer with
7156 * some constant data such as zeros, which would result in the data
7157 * being protected with a reproducible, easily knowable key.
7158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007159 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007160 *output_length = output_size;
7161 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007162
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007164
Gilles Peskine449bd832023-01-11 14:50:10 +01007165 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007166}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007167
7168
Gilles Peskine30524eb2020-11-13 17:02:26 +01007169
Gilles Peskine86a440b2018-11-12 18:39:40 +01007170/****************************************************************/
7171/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007172/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007173
Gilles Peskine30524eb2020-11-13 17:02:26 +01007174/** Initialize the PSA random generator.
7175 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007176static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007177{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007178#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007180#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7181
Gilles Peskine30524eb2020-11-13 17:02:26 +01007182 /* Set default configuration if
7183 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007185 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 }
7187 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007188 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007190
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007192#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7193 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7194 /* The PSA entropy injection feature depends on using NV seed as an entropy
7195 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 mbedtls_entropy_add_source(&rng->entropy,
7197 mbedtls_nv_seed_poll, NULL,
7198 MBEDTLS_ENTROPY_BLOCK_SIZE,
7199 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007200#endif
7201
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007203#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007204}
7205
7206/** Deinitialize the PSA random generator.
7207 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007208static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007209{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007210#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007212#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7214 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007215#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007216}
7217
7218/** Seed the PSA random generator.
7219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007220static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007221{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007222#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7223 /* Do nothing: the external RNG seeds itself. */
7224 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007225 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007226#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007227 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007228 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7229 drbg_seed, sizeof(drbg_seed) - 1);
7230 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007231#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007232}
7233
Gilles Peskine449bd832023-01-11 14:50:10 +01007234psa_status_t psa_generate_random(uint8_t *output,
7235 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007236{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007237 GUARD_MODULE_INITIALIZED;
7238
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007239#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7240
7241 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7243 output, output_size,
7244 &output_length);
7245 if (status != PSA_SUCCESS) {
7246 return status;
7247 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007248 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007249 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 if (output_length != output_size) {
7251 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7252 }
7253 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007254
7255#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7256
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007258 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7260 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7261 output_size);
7262 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7263 output, request_size);
7264 if (ret != 0) {
7265 return mbedtls_to_psa_error(ret);
7266 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007267 output_size -= request_size;
7268 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007269 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007270 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007271#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007272}
7273
Gilles Peskine8814fc42020-12-14 15:33:44 +01007274/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007275 *
7276 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7277 * `psa_generate_random(...)`. The state parameter is ignored since the
7278 * PSA API doesn't support passing an explicit state.
7279 *
7280 * In the non-external case, psa_generate_random() calls an
7281 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7282 * and semantics as mbedtls_psa_get_random(). As an optimization,
7283 * instead of doing this back-and-forth between the PSA API and the
7284 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7285 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007287#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7288int mbedtls_psa_get_random(void *p_rng,
7289 unsigned char *output,
7290 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007291{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007292 /* This function takes a pointer to the RNG state because that's what
7293 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7294 * its own state internally and doesn't let the caller access that state.
7295 * So we just ignore the state parameter, and in practice we'll pass
7296 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007297 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007298 psa_status_t status = psa_generate_random(output, output_size);
7299 if (status == PSA_SUCCESS) {
7300 return 0;
7301 } else {
7302 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7303 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007304}
7305#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7306
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007307#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00007308#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007309
Gilles Peskine449bd832023-01-11 14:50:10 +01007310psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7311 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007312{
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 if (global_data.initialized) {
7314 return PSA_ERROR_NOT_PERMITTED;
7315 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007316
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7318 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7319 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7320 return PSA_ERROR_INVALID_ARGUMENT;
7321 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007322
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007324}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007325#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007326
Ronald Cron3772afe2021-02-08 16:10:05 +01007327/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007328 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007329 * \param type The key type
7330 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007331 *
7332 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007333 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007334 * \retval #PSA_ERROR_INVALID_ARGUMENT
7335 * The size in bits of the key is not valid.
7336 * \retval #PSA_ERROR_NOT_SUPPORTED
7337 * The type and/or the size in bits of the key or the combination of
7338 * the two is not supported.
7339 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007340static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007342{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007343 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007344
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 if (key_type_is_raw_bytes(type)) {
7346 status = psa_validate_unstructured_key_bit_size(type, bits);
7347 if (status != PSA_SUCCESS) {
7348 return status;
7349 }
7350 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007351#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7353 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7354 return PSA_ERROR_NOT_SUPPORTED;
7355 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007356
Ronald Cron01b2aba2020-10-05 09:42:02 +02007357 /* Accept only byte-aligned keys, for the same reasons as
7358 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007359 if (bits % 8 != 0) {
7360 return PSA_ERROR_NOT_SUPPORTED;
7361 }
7362 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007363#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007364
Valerio Setti8bb57632023-05-26 13:48:07 +02007365#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007366 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007367 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007368 return PSA_SUCCESS;
7369 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007370#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007371
Valerio Setti8bb57632023-05-26 13:48:07 +02007372#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007373 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007374 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007375 return PSA_ERROR_NOT_SUPPORTED;
7376 }
7377 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007378#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007379 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007380 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007381 }
7382
Gilles Peskine449bd832023-01-11 14:50:10 +01007383 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007384}
7385
Ronald Cron55ed0592020-10-05 10:30:40 +02007386psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007387 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007389{
7390 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007391 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007392
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 if ((attributes->domain_parameters == NULL) &&
7394 (attributes->domain_parameters_size != 0)) {
7395 return PSA_ERROR_INVALID_ARGUMENT;
7396 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007397
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 if (key_type_is_raw_bytes(type)) {
7399 status = psa_generate_random(key_buffer, key_buffer_size);
7400 if (status != PSA_SUCCESS) {
7401 return status;
7402 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007403
David Brown12ca5032021-02-11 11:02:00 -07007404#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007405 if (type == PSA_KEY_TYPE_DES) {
7406 psa_des_set_key_parity(key_buffer, key_buffer_size);
7407 }
David Brown8107e312021-02-12 08:08:46 -07007408#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007410
Valerio Setti8bb57632023-05-26 13:48:07 +02007411#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) && \
Jaeden Amero424fa932021-05-14 08:34:32 +01007412 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7414 return mbedtls_psa_rsa_generate_key(attributes,
7415 key_buffer,
7416 key_buffer_size,
7417 key_buffer_length);
7418 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007419#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY)
Jaeden Amero424fa932021-05-14 08:34:32 +01007420 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007421
Valerio Setti8bb57632023-05-26 13:48:07 +02007422#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7424 return mbedtls_psa_ecp_generate_key(attributes,
7425 key_buffer,
7426 key_buffer_size,
7427 key_buffer_length);
7428 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007429#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007430
Valerio Setti8bb57632023-05-26 13:48:07 +02007431#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007432 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7433 return mbedtls_psa_ffdh_generate_key(attributes,
7434 key_buffer,
7435 key_buffer_size,
7436 key_buffer_length);
7437 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007438#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Steven Cooreman29149862020-08-05 15:43:42 +02007439 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 (void) key_buffer_length;
7441 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007442 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007443
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007445}
Darryl Green0c6575a2018-11-07 16:05:30 +00007446
Gilles Peskine449bd832023-01-11 14:50:10 +01007447psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7448 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007449{
7450 psa_status_t status;
7451 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007452 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007453 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007454
Ronald Cron81709fc2020-11-14 12:10:32 +01007455 *key = MBEDTLS_SVC_KEY_ID_INIT;
7456
Gilles Peskine0f84d622019-09-12 19:03:13 +02007457 /* Reject any attempt to create a zero-length key so that we don't
7458 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007459 if (psa_get_key_bits(attributes) == 0) {
7460 return PSA_ERROR_INVALID_ARGUMENT;
7461 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007462
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007463 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007464 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7465 return PSA_ERROR_INVALID_ARGUMENT;
7466 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007467
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7469 &slot, &driver);
7470 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007471 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 }
Gilles Peskine11792082019-08-06 18:36:36 +02007473
Ronald Cron977c2472020-10-13 08:32:21 +02007474 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307475 * storage ( thus not in the case of generating a key in a secure element
7476 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7477 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 if (slot->key.data == NULL) {
7479 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7480 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007481 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 attributes->core.type, attributes->core.bits);
7483 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007484 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007485 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007486
7487 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 attributes->core.type,
7489 attributes->core.bits);
7490 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007491 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 attributes, &key_buffer_size);
7493 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007494 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007495 }
Ronald Cron977c2472020-10-13 08:32:21 +02007496 }
Ronald Cron977c2472020-10-13 08:32:21 +02007497
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7499 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007500 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 }
Ronald Cron977c2472020-10-13 08:32:21 +02007502 }
7503
Gilles Peskine449bd832023-01-11 14:50:10 +01007504 status = psa_driver_wrapper_generate_key(attributes,
7505 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007506
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 if (status != PSA_SUCCESS) {
7508 psa_remove_key_data_from_memory(slot);
7509 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007510
Gilles Peskine11792082019-08-06 18:36:36 +02007511exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 if (status == PSA_SUCCESS) {
7513 status = psa_finish_key_creation(slot, driver, key);
7514 }
7515 if (status != PSA_SUCCESS) {
7516 psa_fail_key_creation(slot, driver);
7517 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007518
Gilles Peskine449bd832023-01-11 14:50:10 +01007519 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007520}
7521
Gilles Peskinee59236f2018-01-27 23:32:46 +01007522/****************************************************************/
7523/* Module setup */
7524/****************************************************************/
7525
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007526#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007527psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 void (* entropy_init)(mbedtls_entropy_context *ctx),
7529 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007530{
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7532 return PSA_ERROR_BAD_STATE;
7533 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007534 global_data.rng.entropy_init = entropy_init;
7535 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007537}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007538#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007539
Gilles Peskine449bd832023-01-11 14:50:10 +01007540void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007541{
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 psa_wipe_all_key_slots();
7543 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7544 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007545 }
7546 /* Wipe all remaining data, including configuration.
7547 * In particular, this sets all state indicator to the value
7548 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007550
7551 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007553}
7554
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007555#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7556/** Recover a transaction that was interrupted by a power failure.
7557 *
7558 * This function is called during initialization, before psa_crypto_init()
7559 * returns. If this function returns a failure status, the initialization
7560 * fails.
7561 */
7562static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007563 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007564{
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007566 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7567 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 /* TODO - fall through to the failure case until this
7569 * is implemented.
7570 * https://github.com/ARMmbed/mbed-crypto/issues/218
7571 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007572 default:
7573 /* We found an unsupported transaction in the storage.
7574 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007575 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007576 }
7577}
7578#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7579
Gilles Peskine449bd832023-01-11 14:50:10 +01007580psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007581{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007582 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007583
Gilles Peskinec6b69072018-11-20 21:42:52 +01007584 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007585 if (global_data.initialized != 0) {
7586 return PSA_SUCCESS;
7587 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007588
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007589 /* Init drivers */
7590 status = psa_driver_wrapper_init();
7591 if (status != PSA_SUCCESS) {
7592 goto exit;
7593 }
7594 global_data.drivers_initialized = 1;
7595
Gilles Peskine30524eb2020-11-13 17:02:26 +01007596 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007598 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 status = mbedtls_psa_random_seed(&global_data.rng);
7600 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007601 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007602 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007603 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007604
Gilles Peskine449bd832023-01-11 14:50:10 +01007605 status = psa_initialize_key_slots();
7606 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007607 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007608 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007609
Gilles Peskine4b734222019-07-24 15:56:31 +02007610#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 status = psa_crypto_load_transaction();
7612 if (status == PSA_SUCCESS) {
7613 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7614 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007615 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007616 }
7617 status = psa_crypto_stop_transaction();
7618 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007619 /* There's no transaction to complete. It's all good. */
7620 status = PSA_SUCCESS;
7621 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007622#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007623
Gilles Peskinec6b69072018-11-20 21:42:52 +01007624 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007625 global_data.initialized = 1;
7626
7627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 if (status != PSA_SUCCESS) {
7629 mbedtls_psa_crypto_free();
7630 }
7631 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007632}
7633
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007634psa_status_t psa_crypto_driver_pake_get_password_len(
7635 const psa_crypto_driver_pake_inputs_t *inputs,
7636 size_t *password_len)
7637{
7638 if (inputs->password_len == 0) {
7639 return PSA_ERROR_BAD_STATE;
7640 }
7641
7642 *password_len = inputs->password_len;
7643
7644 return PSA_SUCCESS;
7645}
7646
7647psa_status_t psa_crypto_driver_pake_get_password(
7648 const psa_crypto_driver_pake_inputs_t *inputs,
7649 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7650{
7651 if (inputs->password_len == 0) {
7652 return PSA_ERROR_BAD_STATE;
7653 }
7654
7655 if (buffer_size < inputs->password_len) {
7656 return PSA_ERROR_BUFFER_TOO_SMALL;
7657 }
7658
7659 memcpy(buffer, inputs->password, inputs->password_len);
7660 *buffer_length = inputs->password_len;
7661
7662 return PSA_SUCCESS;
7663}
7664
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007665psa_status_t psa_crypto_driver_pake_get_user_len(
7666 const psa_crypto_driver_pake_inputs_t *inputs,
7667 size_t *user_len)
7668{
7669 if (inputs->user_len == 0) {
7670 return PSA_ERROR_BAD_STATE;
7671 }
7672
7673 *user_len = inputs->user_len;
7674
7675 return PSA_SUCCESS;
7676}
7677
7678psa_status_t psa_crypto_driver_pake_get_user(
7679 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007680 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007681{
7682 if (inputs->user_len == 0) {
7683 return PSA_ERROR_BAD_STATE;
7684 }
7685
Przemek Stekielc0e62502023-03-14 11:49:36 +01007686 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007687 return PSA_ERROR_BUFFER_TOO_SMALL;
7688 }
7689
Przemek Stekielc0e62502023-03-14 11:49:36 +01007690 memcpy(user_id, inputs->user, inputs->user_len);
7691 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007692
7693 return PSA_SUCCESS;
7694}
7695
7696psa_status_t psa_crypto_driver_pake_get_peer_len(
7697 const psa_crypto_driver_pake_inputs_t *inputs,
7698 size_t *peer_len)
7699{
7700 if (inputs->peer_len == 0) {
7701 return PSA_ERROR_BAD_STATE;
7702 }
7703
7704 *peer_len = inputs->peer_len;
7705
7706 return PSA_SUCCESS;
7707}
7708
7709psa_status_t psa_crypto_driver_pake_get_peer(
7710 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007711 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007712{
7713 if (inputs->peer_len == 0) {
7714 return PSA_ERROR_BAD_STATE;
7715 }
7716
Przemek Stekielc0e62502023-03-14 11:49:36 +01007717 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007718 return PSA_ERROR_BUFFER_TOO_SMALL;
7719 }
7720
Przemek Stekielc0e62502023-03-14 11:49:36 +01007721 memcpy(peer_id, inputs->peer, inputs->peer_len);
7722 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007723
7724 return PSA_SUCCESS;
7725}
7726
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007727psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7728 const psa_crypto_driver_pake_inputs_t *inputs,
7729 psa_pake_cipher_suite_t *cipher_suite)
7730{
7731 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7732 return PSA_ERROR_BAD_STATE;
7733 }
7734
7735 *cipher_suite = inputs->cipher_suite;
7736
7737 return PSA_SUCCESS;
7738}
7739
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01007740#if defined(PSA_WANT_ALG_SOME_PAKE)
Neil Armstronga7d08c32022-06-01 18:21:20 +02007741psa_status_t psa_pake_setup(
7742 psa_pake_operation_t *operation,
7743 const psa_pake_cipher_suite_t *cipher_suite)
7744{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007745 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007746
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007747 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007748 status = PSA_ERROR_BAD_STATE;
7749 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007750 }
7751
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007752 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007753 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007754 status = PSA_ERROR_INVALID_ARGUMENT;
7755 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007756 }
7757
Przemek Stekiel51eac532022-12-07 11:04:51 +01007758 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7759
Przemek Stekiele12ed362022-12-21 12:54:46 +01007760 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007761 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7762 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007763 operation->data.inputs.cipher_suite = *cipher_suite;
7764
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007765#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007766 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007767 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007768 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007769
Przemek Stekiele12ed362022-12-21 12:54:46 +01007770 computation_stage->state = PSA_PAKE_STATE_SETUP;
7771 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7772 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7773 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007774 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007775#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007776 {
7777 status = PSA_ERROR_NOT_SUPPORTED;
7778 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007779 }
7780
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007781 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7782
Przemek Stekiel51eac532022-12-07 11:04:51 +01007783 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007784exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007785 psa_pake_abort(operation);
7786 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007787}
7788
7789psa_status_t psa_pake_set_password_key(
7790 psa_pake_operation_t *operation,
7791 mbedtls_svc_key_id_t password)
7792{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007793 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007794 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7795 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007796
Przemek Stekiel51eac532022-12-07 11:04:51 +01007797 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007798 status = PSA_ERROR_BAD_STATE;
7799 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007800 }
7801
Przemek Stekiel6c764412022-11-22 14:05:12 +01007802 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7803 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007804 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007805 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007806 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007807 }
7808
Przemek Stekiel6c764412022-11-22 14:05:12 +01007809 psa_key_attributes_t attributes = {
7810 .core = slot->attr
7811 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007812
Przemek Stekiel51eac532022-12-07 11:04:51 +01007813 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007814
Przemek Stekiel51eac532022-12-07 11:04:51 +01007815 if (type != PSA_KEY_TYPE_PASSWORD &&
7816 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7817 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007818 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007819 }
7820
Przemek Stekiel51eac532022-12-07 11:04:51 +01007821 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7822 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007823 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007824 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007825 }
7826
7827 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7828 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007829 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007830exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007831 if (status != PSA_SUCCESS) {
7832 psa_pake_abort(operation);
7833 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007834 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007835 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007836}
7837
7838psa_status_t psa_pake_set_user(
7839 psa_pake_operation_t *operation,
7840 const uint8_t *user_id,
7841 size_t user_id_len)
7842{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007843 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007844
7845 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007846 status = PSA_ERROR_BAD_STATE;
7847 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007848 }
7849
Przemek Stekiel51eac532022-12-07 11:04:51 +01007850 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007851 status = PSA_ERROR_INVALID_ARGUMENT;
7852 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007853 }
7854
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007855 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007856 status = PSA_ERROR_BAD_STATE;
7857 goto exit;
7858 }
7859
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007860 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7861 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007862 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7863 goto exit;
7864 }
7865
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007866 memcpy(operation->data.inputs.user, user_id, user_id_len);
7867 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007868
7869 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007870exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007871 psa_pake_abort(operation);
7872 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007873}
7874
7875psa_status_t psa_pake_set_peer(
7876 psa_pake_operation_t *operation,
7877 const uint8_t *peer_id,
7878 size_t peer_id_len)
7879{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007881
7882 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007883 status = PSA_ERROR_BAD_STATE;
7884 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007885 }
7886
Przemek Stekiel51eac532022-12-07 11:04:51 +01007887 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007888 status = PSA_ERROR_INVALID_ARGUMENT;
7889 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007890 }
7891
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007892 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007893 status = PSA_ERROR_BAD_STATE;
7894 goto exit;
7895 }
7896
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007897 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7898 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007899 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7900 goto exit;
7901 }
7902
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007903 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7904 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007905
7906 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007907exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007908 psa_pake_abort(operation);
7909 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007910}
7911
7912psa_status_t psa_pake_set_role(
7913 psa_pake_operation_t *operation,
7914 psa_pake_role_t role)
7915{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007916 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007917
Przemek Stekiel51eac532022-12-07 11:04:51 +01007918 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007919 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007920 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007921 }
7922
Przemek Stekiel09104b82023-03-06 14:11:51 +01007923 switch (operation->alg) {
7924#if defined(PSA_WANT_ALG_JPAKE)
7925 case PSA_ALG_JPAKE:
7926 if (role == PSA_PAKE_ROLE_NONE) {
7927 return PSA_SUCCESS;
7928 }
7929 status = PSA_ERROR_INVALID_ARGUMENT;
7930 break;
7931#endif
7932 default:
7933 (void) role;
7934 status = PSA_ERROR_NOT_SUPPORTED;
7935 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007936 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007937exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007938 psa_pake_abort(operation);
7939 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007940}
7941
Przemek Stekielb09c4872023-01-17 12:05:38 +01007942/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007943#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007944static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007945 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007946{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007947 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007948 case PSA_PAKE_OUTPUT_X1_X2:
7949 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007950 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007951 case PSA_PAKE_X1_STEP_KEY_SHARE:
7952 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007953 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7954 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007955 case PSA_PAKE_X1_STEP_ZK_PROOF:
7956 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007957 case PSA_PAKE_X2_STEP_KEY_SHARE:
7958 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007959 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7960 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007961 case PSA_PAKE_X2_STEP_ZK_PROOF:
7962 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007963 default:
7964 return PSA_JPAKE_STEP_INVALID;
7965 }
7966 break;
7967 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007968 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007969 case PSA_PAKE_X1_STEP_KEY_SHARE:
7970 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007971 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7972 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007973 case PSA_PAKE_X1_STEP_ZK_PROOF:
7974 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007975 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007976 return PSA_JPAKE_STEP_INVALID;
7977 }
7978 break;
7979 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007980 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007981 case PSA_PAKE_X1_STEP_KEY_SHARE:
7982 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007983 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7984 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007985 case PSA_PAKE_X1_STEP_ZK_PROOF:
7986 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007987 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007988 return PSA_JPAKE_STEP_INVALID;
7989 }
7990 break;
7991 default:
7992 return PSA_JPAKE_STEP_INVALID;
7993 }
7994 return PSA_JPAKE_STEP_INVALID;
7995}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007996#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007997
Przemek Stekiel2797d372022-12-22 11:19:22 +01007998static psa_status_t psa_pake_complete_inputs(
7999 psa_pake_operation_t *operation)
8000{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008001 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008002 /* Create copy of the inputs on stack as inputs share memory
8003 with the driver context which will be setup by the driver. */
8004 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008005
Przemek Stekielfde11282023-03-13 16:06:09 +01008006 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008007 return PSA_ERROR_BAD_STATE;
8008 }
8009
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008010 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008011 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8012 return PSA_ERROR_BAD_STATE;
8013 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008014 }
8015
Przemek Stekiel18620a32023-01-17 16:34:52 +01008016 /* Clear driver context */
8017 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8018
8019 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008020
8021 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008022 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
8023 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008024
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008025 /* User and peer are translated to role. */
8026 mbedtls_free(inputs.user);
8027 mbedtls_free(inputs.peer);
8028
Przemek Stekiel2797d372022-12-22 11:19:22 +01008029 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008030#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008031 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008032 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008033 psa_jpake_computation_stage_t *computation_stage =
8034 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008035 computation_stage->state = PSA_PAKE_STATE_READY;
8036 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8037 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
8038 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008039 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008040#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008041 {
8042 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008043 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008044 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008045 return status;
8046}
8047
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008048#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008049static psa_status_t psa_jpake_output_prologue(
8050 psa_pake_operation_t *operation,
8051 psa_pake_step_t step)
8052{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008053 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8054 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8055 step != PSA_PAKE_STEP_ZK_PROOF) {
8056 return PSA_ERROR_INVALID_ARGUMENT;
8057 }
8058
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008059 psa_jpake_computation_stage_t *computation_stage =
8060 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008061
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008062 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
8063 return PSA_ERROR_BAD_STATE;
8064 }
8065
8066 if (computation_stage->state != PSA_PAKE_STATE_READY &&
8067 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
8068 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
8069 return PSA_ERROR_BAD_STATE;
8070 }
8071
8072 if (computation_stage->state == PSA_PAKE_STATE_READY) {
8073 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008074 return PSA_ERROR_BAD_STATE;
8075 }
8076
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008077 switch (computation_stage->output_step) {
8078 case PSA_PAKE_STEP_X1_X2:
8079 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008080 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008081 case PSA_PAKE_STEP_X2S:
8082 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008083 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008084 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01008085 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008086 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008087
8088 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
8089 }
8090
8091 /* Check if step matches current sequence */
8092 switch (computation_stage->sequence) {
8093 case PSA_PAKE_X1_STEP_KEY_SHARE:
8094 case PSA_PAKE_X2_STEP_KEY_SHARE:
8095 if (step != PSA_PAKE_STEP_KEY_SHARE) {
8096 return PSA_ERROR_BAD_STATE;
8097 }
8098 break;
8099
8100 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
8101 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
8102 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
8103 return PSA_ERROR_BAD_STATE;
8104 }
8105 break;
8106
8107 case PSA_PAKE_X1_STEP_ZK_PROOF:
8108 case PSA_PAKE_X2_STEP_ZK_PROOF:
8109 if (step != PSA_PAKE_STEP_ZK_PROOF) {
8110 return PSA_ERROR_BAD_STATE;
8111 }
8112 break;
8113
8114 default:
8115 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008116 }
8117
8118 return PSA_SUCCESS;
8119}
8120
8121static psa_status_t psa_jpake_output_epilogue(
8122 psa_pake_operation_t *operation)
8123{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008124 psa_jpake_computation_stage_t *computation_stage =
8125 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008126
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008127 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008128 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008129 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008130 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008131 computation_stage->state = PSA_PAKE_STATE_READY;
8132 computation_stage->output_step++;
8133 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8134 } else {
8135 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008136 }
8137
8138 return PSA_SUCCESS;
8139}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008140#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008141
Neil Armstronga7d08c32022-06-01 18:21:20 +02008142psa_status_t psa_pake_output(
8143 psa_pake_operation_t *operation,
8144 psa_pake_step_t step,
8145 uint8_t *output,
8146 size_t output_size,
8147 size_t *output_length)
8148{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008149 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008150 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008151 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008152
8153 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008154 status = psa_pake_complete_inputs(operation);
8155 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008156 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008157 }
8158 }
8159
8160 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008161 status = PSA_ERROR_BAD_STATE;
8162 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008163 }
8164
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008165 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008166 status = PSA_ERROR_INVALID_ARGUMENT;
8167 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008168 }
8169
Przemek Stekiele12ed362022-12-21 12:54:46 +01008170 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008171#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008172 case PSA_ALG_JPAKE:
8173 status = psa_jpake_output_prologue(operation, step);
8174 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008175 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008176 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008177 driver_step = convert_jpake_computation_stage_to_driver_step(
8178 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008179 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008180#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008181 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008182 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008183 status = PSA_ERROR_NOT_SUPPORTED;
8184 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008185 }
8186
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008187 status = psa_driver_wrapper_pake_output(operation, driver_step,
8188 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008189
8190 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008191 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008192 }
8193
8194 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008195#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008196 case PSA_ALG_JPAKE:
8197 status = psa_jpake_output_epilogue(operation);
8198 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008199 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008200 }
8201 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008202#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008203 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008204 status = PSA_ERROR_NOT_SUPPORTED;
8205 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008206 }
8207
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008208 return PSA_SUCCESS;
8209exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008210 psa_pake_abort(operation);
8211 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008212}
8213
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008214#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008215static psa_status_t psa_jpake_input_prologue(
8216 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008217 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008218{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008219 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8220 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8221 step != PSA_PAKE_STEP_ZK_PROOF) {
8222 return PSA_ERROR_INVALID_ARGUMENT;
8223 }
8224
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008225 psa_jpake_computation_stage_t *computation_stage =
8226 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008227
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008228 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
8229 return PSA_ERROR_BAD_STATE;
8230 }
8231
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008232 if (computation_stage->state != PSA_PAKE_STATE_READY &&
8233 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
8234 computation_stage->state != PSA_PAKE_INPUT_X4S) {
8235 return PSA_ERROR_BAD_STATE;
8236 }
8237
8238 if (computation_stage->state == PSA_PAKE_STATE_READY) {
8239 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008240 return PSA_ERROR_BAD_STATE;
8241 }
8242
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008243 switch (computation_stage->input_step) {
8244 case PSA_PAKE_STEP_X1_X2:
8245 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008246 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008247 case PSA_PAKE_STEP_X2S:
8248 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008249 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008250 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01008251 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008252 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008253
8254 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
8255 }
8256
8257 /* Check if step matches current sequence */
8258 switch (computation_stage->sequence) {
8259 case PSA_PAKE_X1_STEP_KEY_SHARE:
8260 case PSA_PAKE_X2_STEP_KEY_SHARE:
8261 if (step != PSA_PAKE_STEP_KEY_SHARE) {
8262 return PSA_ERROR_BAD_STATE;
8263 }
8264 break;
8265
8266 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
8267 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
8268 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
8269 return PSA_ERROR_BAD_STATE;
8270 }
8271 break;
8272
8273 case PSA_PAKE_X1_STEP_ZK_PROOF:
8274 case PSA_PAKE_X2_STEP_ZK_PROOF:
8275 if (step != PSA_PAKE_STEP_ZK_PROOF) {
8276 return PSA_ERROR_BAD_STATE;
8277 }
8278 break;
8279
8280 default:
8281 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008282 }
8283
8284 return PSA_SUCCESS;
8285}
8286
Przemek Stekiele12ed362022-12-21 12:54:46 +01008287static psa_status_t psa_jpake_input_epilogue(
8288 psa_pake_operation_t *operation)
8289{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008290 psa_jpake_computation_stage_t *computation_stage =
8291 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008292
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008293 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008294 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008295 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008296 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008297 computation_stage->state = PSA_PAKE_STATE_READY;
8298 computation_stage->input_step++;
8299 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8300 } else {
8301 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008302 }
8303
8304 return PSA_SUCCESS;
8305}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008306#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008307
Neil Armstronga7d08c32022-06-01 18:21:20 +02008308psa_status_t psa_pake_input(
8309 psa_pake_operation_t *operation,
8310 psa_pake_step_t step,
8311 const uint8_t *input,
8312 size_t input_length)
8313{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008314 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008315 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008316 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8317 operation->primitive,
8318 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008319
8320 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008321 status = psa_pake_complete_inputs(operation);
8322 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008323 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008324 }
8325 }
8326
8327 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008328 status = PSA_ERROR_BAD_STATE;
8329 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008330 }
8331
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008332 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008333 status = PSA_ERROR_INVALID_ARGUMENT;
8334 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008335 }
8336
Przemek Stekiele12ed362022-12-21 12:54:46 +01008337 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008338#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008339 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008340 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008341 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008342 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008343 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008344 driver_step = convert_jpake_computation_stage_to_driver_step(
8345 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008346 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008347#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008348 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008349 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008350 status = PSA_ERROR_NOT_SUPPORTED;
8351 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008352 }
8353
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008354 status = psa_driver_wrapper_pake_input(operation, driver_step,
8355 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008356
8357 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008358 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008359 }
8360
8361 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008362#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008363 case PSA_ALG_JPAKE:
8364 status = psa_jpake_input_epilogue(operation);
8365 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008366 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008367 }
8368 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008369#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008370 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008371 status = PSA_ERROR_NOT_SUPPORTED;
8372 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008373 }
8374
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008375 return PSA_SUCCESS;
8376exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008377 psa_pake_abort(operation);
8378 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008379}
8380
8381psa_status_t psa_pake_get_implicit_key(
8382 psa_pake_operation_t *operation,
8383 psa_key_derivation_operation_t *output)
8384{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008385 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008386 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008387 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008388 size_t shared_key_len = 0;
8389
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008390 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008391 status = PSA_ERROR_BAD_STATE;
8392 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008393 }
8394
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008395#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008396 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008397 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008398 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008399 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8400 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008401 status = PSA_ERROR_BAD_STATE;
8402 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008403 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008404 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008405#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008406 {
8407 status = PSA_ERROR_NOT_SUPPORTED;
8408 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008409 }
8410
Przemek Stekiel0c781802022-11-29 14:53:13 +01008411 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8412 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008413 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008414 &shared_key_len);
8415
8416 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008417 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008418 }
8419
8420 status = psa_key_derivation_input_bytes(output,
8421 PSA_KEY_DERIVATION_INPUT_SECRET,
8422 shared_key,
8423 shared_key_len);
8424
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008425 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008426exit:
8427 abort_status = psa_pake_abort(operation);
8428 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008429}
8430
8431psa_status_t psa_pake_abort(
8432 psa_pake_operation_t *operation)
8433{
Przemek Stekield69dca92023-01-31 19:59:20 +01008434 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008435
Przemek Stekield69dca92023-01-31 19:59:20 +01008436 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008437 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008438 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008439
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008440 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8441 if (operation->data.inputs.password != NULL) {
8442 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008443 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008444 mbedtls_free(operation->data.inputs.password);
8445 }
8446 if (operation->data.inputs.user != NULL) {
8447 mbedtls_free(operation->data.inputs.user);
8448 }
8449 if (operation->data.inputs.peer != NULL) {
8450 mbedtls_free(operation->data.inputs.peer);
8451 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008452 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008453 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008454
Przemek Stekield69dca92023-01-31 19:59:20 +01008455 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008456}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008457#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008458
Gilles Peskinee59236f2018-01-27 23:32:46 +01008459#endif /* MBEDTLS_PSA_CRYPTO_C */