blob: 4fe20b47e6e38259efc774cd2c59835806e74e78 [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"
Paul Elliott588f8ed2022-12-02 18:10:26 +000085#include "hash_info.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
Przemek Stekielfde11282023-03-13 16:06:09 +010095/* The only two JPAKE user/peer identifiers supported for the time being. */
96static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
97static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
Przemek Stekiel18cd6c92023-03-06 15:40:35 +010098
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099/****************************************************************/
100/* Global data, support functions and library management */
101/****************************************************************/
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104{
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106}
107
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100108/* Values for psa_global_data_t::rng_state */
109#define RNG_NOT_INITIALIZED 0
110#define RNG_INITIALIZED 1
111#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100115 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100116 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100117 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100122#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
123mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
124 &global_data.rng.drbg;
125#endif
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (global_data.initialized == 0) \
129 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300130
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100131int psa_can_do_hash(psa_algorithm_t hash_alg)
132{
133 (void) hash_alg;
134 return global_data.drivers_initialized;
135}
Przemek Stekiel53410502023-04-28 13:18:43 +0200136#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
137 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
138 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200139static int psa_is_dh_key_size_valid(size_t bits) {
140 if (bits != 2048 && bits != 3072 && bits != 4096 &&
141 bits != 6144 && bits != 8192) {
142 return 0;
143 }
144
145 return 1;
146}
Przemek Stekiel53410502023-04-28 13:18:43 +0200147#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR ||
148 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
149 PSA_WANT_KEY_TYPE_DH_KEY_PAIR */
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100152{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100153 /* Mbed TLS error codes can combine a high-level error code and a
154 * low-level error code. The low-level error usually reflects the
155 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 int low_level_ret = -(-ret & 0x007f);
157 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100158 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100160
161 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
162 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200164 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
165 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
166 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
167 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
168 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200170 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200172 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200174
Jaeden Amero93e21112019-02-20 13:57:28 +0000175#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000176 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000177#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100185
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200188
189 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200191 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200193
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100198 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100200 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
210 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100211 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
212 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100220#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100221
222 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
Gilles Peskinee59236f2018-01-27 23:32:46 +0100225 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
226 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
227 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200232 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
Gilles Peskine82e57d12020-11-13 21:31:17 +0100237#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100239 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
240 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100241 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
244 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100246 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100248#endif
249
Gilles Peskinea5905292018-02-07 20:59:33 +0100250 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100252 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100254 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100256 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100258
Gilles Peskinef76aa772018-10-29 19:24:33 +0100259 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100261 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100263 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100265 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100267 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100269 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100271 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100273 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100275
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100276 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
279 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
284 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100286 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
289 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100291 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100293 case MBEDTLS_ERR_PK_INVALID_ALG:
294 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
295 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100297 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200299 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100301
Gilles Peskineff2d2002019-05-06 15:26:23 +0200302 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200304 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200306
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100309 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100311 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
316 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200324
itayzafrir5c753392018-05-08 11:18:38 +0300325 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300326 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300328 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300330 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300332 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
333 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300335 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100337 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100339
Paul Elliott588f8ed2022-12-02 18:10:26 +0000340 case MBEDTLS_ERR_ECP_IN_PROGRESS:
341 return PSA_OPERATION_INCOMPLETE;
342
Janos Follatha13b9052019-11-22 12:48:59 +0000343 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300345
Gilles Peskinee59236f2018-01-27 23:32:46 +0100346 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100348 }
349}
350
Paul Elliottdc42ca82023-02-24 18:11:59 +0000351/**
352 * \brief For output buffers which contain "tags"
353 * (outputs that may be checked for validity like
354 * hashes, MACs and signatures), fill the unused
355 * part of the output buffer (the whole buffer on
356 * error, the trailing part on success) with
357 * something that isn't a valid tag (barring an
358 * attack on the tag and deliberately-crafted
359 * input), in case the caller doesn't check the
360 * return status properly.
361 *
362 * \param output_buffer Pointer to buffer to wipe. May not be NULL
363 * unless \p output_buffer_size is zero.
364 * \param status Status of function called to generate
365 * output_buffer originally
366 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
367 * could be NULL.
368 * \param output_buffer_length Length of data written to output_buffer, must be
369 * less than \p output_buffer_size
370 */
371static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000372 size_t output_buffer_size, size_t output_buffer_length)
373{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000374 size_t offset = 0;
375
376 if (output_buffer_size == 0) {
377 /* If output_buffer_size is 0 then we have nothing to do. We must not
378 call memset because output_buffer may be NULL in this case */
379 return;
380 }
381
382 if (status == PSA_SUCCESS) {
383 offset = output_buffer_length;
384 }
385
386 memset(output_buffer + offset, '!', output_buffer_size - offset);
387}
388
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200389
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200390
391
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392/****************************************************************/
393/* Key management */
394/****************************************************************/
395
Valerio Settid4a5d462023-04-05 18:19:01 +0200396#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100397mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
398 size_t bits,
399 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200400{
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100402 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100404#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100405 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100407#endif
408#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100409 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100411#endif
412#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100413 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100415#endif
416#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100419#endif
420#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100421 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100423 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (bits_is_sloppy) {
425 return MBEDTLS_ECP_DP_SECP521R1;
426 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100428#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100429 }
430 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100431
Paul Elliott8ff510a2020-06-02 17:19:28 +0100432 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100434#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100437#endif
438#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100439 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100441#endif
442#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100445#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100446 }
447 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100448
Paul Elliott8ff510a2020-06-02 17:19:28 +0100449 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100451#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100452 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100454 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (bits_is_sloppy) {
456 return MBEDTLS_ECP_DP_CURVE25519;
457 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100458 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100459#endif
460#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100461 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100463#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100464 }
465 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100466
Paul Elliott8ff510a2020-06-02 17:19:28 +0100467 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100469#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100470 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100472#endif
473#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100474 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100476#endif
477#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100478 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100480#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100481 }
482 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200483 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100484
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100485 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200487}
Valerio Settid4a5d462023-04-05 18:19:01 +0200488#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
491 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492{
493 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200497 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200498 case PSA_KEY_TYPE_PASSWORD:
499 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200500 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100501#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200502 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (bits != 128 && bits != 192 && bits != 256) {
504 return PSA_ERROR_INVALID_ARGUMENT;
505 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506 break;
507#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200508#if defined(PSA_WANT_KEY_TYPE_ARIA)
509 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (bits != 128 && bits != 192 && bits != 256) {
511 return PSA_ERROR_INVALID_ARGUMENT;
512 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200513 break;
514#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100515#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200516 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (bits != 128 && bits != 192 && bits != 256) {
518 return PSA_ERROR_INVALID_ARGUMENT;
519 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200520 break;
521#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100522#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200523 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (bits != 64 && bits != 128 && bits != 192) {
525 return PSA_ERROR_INVALID_ARGUMENT;
526 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527 break;
528#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100529#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200530 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (bits != 256) {
532 return PSA_ERROR_INVALID_ARGUMENT;
533 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200534 break;
535#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200536 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200538 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if (bits % 8 != 0) {
540 return PSA_ERROR_INVALID_ARGUMENT;
541 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544}
545
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100546/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100547 *
Steven Cooremancd640932021-03-08 12:00:27 +0100548 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
549 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100550 *
551 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100552 * \param[in] key_type The key type of the key to be used with the
553 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 *
555 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100556 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100557 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100558 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100559 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100560MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100561 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100563{
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (PSA_ALG_IS_HMAC(algorithm)) {
565 if (key_type == PSA_KEY_TYPE_HMAC) {
566 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100567 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100568 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
571 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
572 * key. */
573 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
574 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
575 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
576 * the block length (larger than 1) for block ciphers. */
577 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
578 return PSA_SUCCESS;
579 }
580 }
581 }
582
583 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100584}
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
587 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200588{
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (slot->key.data != NULL) {
590 return PSA_ERROR_ALREADY_EXISTS;
591 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 slot->key.data = mbedtls_calloc(1, buffer_length);
594 if (slot->key.data == NULL) {
595 return PSA_ERROR_INSUFFICIENT_MEMORY;
596 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200597
Ronald Cronea0f8a62020-11-25 17:52:23 +0100598 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200600}
601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
603 const uint8_t *data,
604 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200605{
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 psa_status_t status = psa_allocate_buffer_to_slot(slot,
607 data_length);
608 if (status != PSA_SUCCESS) {
609 return status;
610 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 memcpy(slot->key.data, data, data_length);
613 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200614}
615
Ronald Crona0fe59f2020-11-29 11:14:53 +0100616psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100617 const psa_key_attributes_t *attributes,
618 const uint8_t *data, size_t data_length,
619 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100621{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100622 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
623 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200625 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (data_length == 0) {
627 return PSA_ERROR_NOT_SUPPORTED;
628 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (key_type_is_raw_bytes(type)) {
631 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
634 *bits);
635 if (status != PSA_SUCCESS) {
636 return status;
637 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200638
Ronald Crondd04d422020-11-28 15:14:42 +0100639 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100641 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 return PSA_SUCCESS;
645 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200646#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
647 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100648 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200649 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100650 return PSA_ERROR_INVALID_ARGUMENT;
651 }
652
653 /* Copy the key material. */
654 memcpy(key_buffer, data, data_length);
655 *key_buffer_length = data_length;
656 *bits = PSA_BYTES_TO_BITS(data_length);
657 (void) key_buffer_size;
658
659 return PSA_SUCCESS;
660 }
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200661#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
662 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
John Durkop9814fa22020-11-04 12:28:15 -0800663#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
665 if (PSA_KEY_TYPE_IS_ECC(type)) {
666 return mbedtls_psa_ecp_import_key(attributes,
667 data, data_length,
668 key_buffer, key_buffer_size,
669 key_buffer_length,
670 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100671 }
John Durkop9814fa22020-11-04 12:28:15 -0800672#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
673 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700674#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
676 if (PSA_KEY_TYPE_IS_RSA(type)) {
677 return mbedtls_psa_rsa_import_key(attributes,
678 data, data_length,
679 key_buffer, key_buffer_size,
680 key_buffer_length,
681 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200682 }
John Durkop9814fa22020-11-04 12:28:15 -0800683#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
684 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100685 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100688}
689
Gilles Peskinef603c712019-01-19 13:40:11 +0100690/** Calculate the intersection of two algorithm usage policies.
691 *
692 * Return 0 (which allows no operation) on incompatibility.
693 */
694static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100695 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100696 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100698{
Gilles Peskine549ea862019-05-22 11:45:59 +0200699 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (alg1 == alg2) {
701 return alg1;
702 }
Steven Cooreman03488022021-02-18 12:07:20 +0100703 /* If the policies are from the same hash-and-sign family, check
704 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
706 PSA_ALG_IS_SIGN_HASH(alg2) &&
707 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
708 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
709 return alg2;
710 }
711 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
712 return alg1;
713 }
Steven Cooreman03488022021-02-18 12:07:20 +0100714 }
715 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100716 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100717 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
719 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
720 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
721 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
722 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100723 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100724
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100725 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
727 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
728 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
729 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100730 }
731 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
733 (alg1_len <= alg2_len)) {
734 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100735 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
737 (alg2_len <= alg1_len)) {
738 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100739 }
740 }
741 /* If the policies are from the same MAC family, check whether one
742 * of them is a minimum-MAC-length policy. Calculate the most
743 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
745 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
746 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100747 /* Validate the combination of key type and algorithm. Since the base
748 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
750 return 0;
751 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100752
Steven Cooreman31a876d2021-03-03 20:47:40 +0100753 /* Get the (exact or at-least) output lengths for both sides of the
754 * requested intersection. None of the currently supported algorithms
755 * have an output length dependent on the actual key size, so setting it
756 * to a bogus value of 0 is currently OK.
757 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100758 * Note that for at-least-this-length wildcard algorithms, the output
759 * length is set to the shortest allowed length, which allows us to
760 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
762 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100763 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100764
Steven Cooremana1d83222021-02-25 10:20:29 +0100765 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
767 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
768 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100769 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100770
771 /* If only one is an at-least-this-length policy, the intersection would
772 * be the other (fixed-length) policy as long as said fixed length is
773 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
775 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100776 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
778 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100779 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100780
Steven Cooremancd640932021-03-08 12:00:27 +0100781 /* If none of them are wildcards, check whether they define the same tag
782 * length. This is still possible here when one is default-length and
783 * the other specific-length. Ensure to always return the
784 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (alg1_len == alg2_len) {
786 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
787 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100788 }
789 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100791}
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793static int psa_key_algorithm_permits(psa_key_type_t key_type,
794 psa_algorithm_t policy_alg,
795 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200796{
Gilles Peskine549ea862019-05-22 11:45:59 +0200797 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (requested_alg == policy_alg) {
799 return 1;
800 }
Steven Cooreman03488022021-02-18 12:07:20 +0100801 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
802 * and requested_alg is the same hash-and-sign family with any hash,
803 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
805 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
806 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
807 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100808 }
809 /* If policy_alg is a wildcard AEAD algorithm of the same base as
810 * the requested algorithm, check the requested tag length to be
811 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (PSA_ALG_IS_AEAD(policy_alg) &&
813 PSA_ALG_IS_AEAD(requested_alg) &&
814 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
815 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
816 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
817 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
818 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100819 }
Steven Cooremancd640932021-03-08 12:00:27 +0100820 /* If policy_alg is a MAC algorithm of the same base as the requested
821 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (PSA_ALG_IS_MAC(policy_alg) &&
823 PSA_ALG_IS_MAC(requested_alg) &&
824 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
825 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100826 /* Validate the combination of key type and algorithm. Since the policy
827 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
829 return 0;
830 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100831
Steven Cooreman31a876d2021-03-03 20:47:40 +0100832 /* Get both the requested output length for the algorithm which is to be
833 * verified, and the default output length for the base algorithm.
834 * Note that none of the currently supported algorithms have an output
835 * length dependent on actual key size, so setting it to a bogus value
836 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100837 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100839 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 key_type, 0,
841 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100842
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100843 /* If the policy is default-length, only allow an algorithm with
844 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
846 return requested_output_length == default_output_length;
847 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100848
849 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100850 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
852 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
853 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100854 }
855
Steven Cooremancd640932021-03-08 12:00:27 +0100856 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
857 * check for the requested MAC length to be equal to or longer than the
858 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
860 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
861 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100862 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200863 }
Steven Cooremance48e852020-10-05 16:02:45 +0200864 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200865 * a key derivation with that key agreement should also be allowed. This
866 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
868 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
869 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
870 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200871 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100872 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200874}
875
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100876/** Test whether a policy permits an algorithm.
877 *
878 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100879 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100880 * \note This function requires providing the key type for which the policy is
881 * being validated, since some algorithm policy definitions (e.g. MAC)
882 * have different properties depending on what kind of cipher it is
883 * combined with.
884 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100885 * \retval PSA_SUCCESS When \p alg is a specific algorithm
886 * allowed by the \p policy.
887 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
888 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
889 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100890 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100891static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
892 psa_key_type_t key_type,
893 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100894{
Steven Cooremand788fab2021-02-25 11:29:17 +0100895 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (alg == 0) {
897 return PSA_ERROR_INVALID_ARGUMENT;
898 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100899
Steven Cooreman7e39f052021-02-23 14:18:32 +0100900 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (PSA_ALG_IS_WILDCARD(alg)) {
902 return PSA_ERROR_INVALID_ARGUMENT;
903 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
906 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
907 return PSA_SUCCESS;
908 } else {
909 return PSA_ERROR_NOT_PERMITTED;
910 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100911}
912
Gilles Peskinef603c712019-01-19 13:40:11 +0100913/** Restrict a key policy based on a constraint.
914 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100915 * \note This function requires providing the key type for which the policy is
916 * being restricted, since some algorithm policy definitions (e.g. MAC)
917 * have different properties depending on what kind of cipher it is
918 * combined with.
919 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100920 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100921 * \param[in,out] policy The policy to restrict.
922 * \param[in] constraint The policy constraint to apply.
923 *
924 * \retval #PSA_SUCCESS
925 * \c *policy contains the intersection of the original value of
926 * \c *policy and \c *constraint.
927 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100928 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100929 * \c *policy is unchanged.
930 */
931static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100932 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100933 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100935{
936 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 psa_key_policy_algorithm_intersection(key_type, policy->alg,
938 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200939 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
941 constraint->alg2);
942 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
943 return PSA_ERROR_INVALID_ARGUMENT;
944 }
945 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
946 return PSA_ERROR_INVALID_ARGUMENT;
947 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100948 policy->usage &= constraint->usage;
949 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200950 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100952}
953
Przemek Stekiel061f6942022-11-30 10:51:35 +0100954/** Get the description of a key given its identifier and policy constraints
955 * and lock it.
956 *
957 * The key must have allow all the usage flags set in \p usage. If \p alg is
958 * nonzero, the key must allow operations with this algorithm. If \p alg is
959 * zero, the algorithm is not checked.
960 *
961 * In case of a persistent key, the function loads the description of the key
962 * into a key slot if not already done.
963 *
964 * On success, the returned key slot is locked. It is the responsibility of
965 * the caller to unlock the key slot when it does not access it anymore.
966 */
967static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100968 mbedtls_svc_key_id_t key,
969 psa_key_slot_t **p_slot,
970 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100972{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100974 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 status = psa_get_and_lock_key_slot(key, p_slot);
977 if (status != PSA_SUCCESS) {
978 return status;
979 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200980 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100981
982 /* Enforce that usage policy for the key slot contains all the flags
983 * required by the usage parameter. There is one exception: public
984 * keys can always be exported, so we treat public key objects as
985 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100987 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100991 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200992 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100993 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100994
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800995 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 if (alg != 0) {
997 status = psa_key_policy_permits(&slot->attr.policy,
998 slot->attr.type,
999 alg);
1000 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001001 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001003 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001006
1007error:
1008 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001012}
Darryl Green940d72c2018-07-13 13:18:51 +01001013
Ronald Cron5c522922020-11-14 16:35:34 +01001014/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001015 *
1016 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001017 * available, as opposed to a key in a secure element and/or to be used
1018 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001019 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001020 * This is a temporary function that may be used instead of
1021 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1022 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001023 *
Ronald Cron5c522922020-11-14 16:35:34 +01001024 * On success, the returned key slot is locked. It is the responsibility of the
1025 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001026 */
Ronald Cron5c522922020-11-14 16:35:34 +01001027static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1028 mbedtls_svc_key_id_t key,
1029 psa_key_slot_t **p_slot,
1030 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001032{
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1034 usage, alg);
1035 if (status != PSA_SUCCESS) {
1036 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001037 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1040 psa_unlock_key_slot(*p_slot);
1041 *p_slot = NULL;
1042 return PSA_ERROR_NOT_SUPPORTED;
1043 }
1044
1045 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001046}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001049{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001050 /* Data pointer will always be either a valid pointer or NULL in an
1051 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (slot->key.data != NULL) {
1053 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1054 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001057 slot->key.data = NULL;
1058 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001061}
1062
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001063/** Completely wipe a slot in memory, including its policy.
1064 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001066{
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 /*
1070 * As the return error code may not be handled in case of multiple errors,
1071 * do our best to report an unexpected lock counter. Assert with
1072 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1073 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1074 * function is called as part of the execution of a test suite, the
1075 * execution of the test suite is stopped in error if the assertion fails.
1076 */
1077 if (slot->lock_count != 1) {
1078 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001079 status = PSA_ERROR_CORRUPTION_DETECTED;
1080 }
1081
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001082 /* Multipart operations may still be using the key. This is safe
1083 * because all multipart operation objects are independent from
1084 * the key slot: if they need to access the key after the setup
1085 * phase, they have a copy of the key. Note that this means that
1086 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001087 /* At this point, key material and other type-specific content has
1088 * been wiped. Clear remaining metadata. We can call memset and not
1089 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 memset(slot, 0, sizeof(*slot));
1091 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001092}
1093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001095{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001096 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001097 psa_status_t status; /* status of the last operation */
1098 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001099#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1100 psa_se_drv_table_entry_t *driver;
1101#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 if (mbedtls_svc_key_id_is_null(key)) {
1104 return PSA_SUCCESS;
1105 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001106
Ronald Cronf2911112020-10-29 17:51:10 +01001107 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001108 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001109 * key, this will load the key description from persistent memory if not
1110 * done yet. We cannot avoid this loading as without it we don't know if
1111 * the key is operated by an SE or not and this information is needed by
1112 * the current implementation.
1113 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 status = psa_get_and_lock_key_slot(key, &slot);
1115 if (status != PSA_SUCCESS) {
1116 return status;
1117 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001118
Ronald Cronf2911112020-10-29 17:51:10 +01001119 /*
1120 * If the key slot containing the key description is under access by the
1121 * library (apart from the present access), the key cannot be destroyed
1122 * yet. For the time being, just return in error. Eventually (to be
1123 * implemented), the key should be destroyed when all accesses have
1124 * stopped.
1125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (slot->lock_count > 1) {
1127 psa_unlock_key_slot(slot);
1128 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001129 }
1130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001132 /* Refuse the destruction of a read-only key (which may or may not work
1133 * if we attempt it, depending on whether the key is merely read-only
1134 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001135 * Just do the best we can, which is to wipe the copy in memory
1136 * (done in this function's cleanup code). */
1137 overall_status = PSA_ERROR_NOT_PERMITTED;
1138 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001139 }
1140
Gilles Peskine354f7672019-07-12 23:46:38 +02001141#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1143 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001144 /* For a key in a secure element, we need to do three things:
1145 * remove the key file in internal storage, destroy the
1146 * key inside the secure element, and update the driver's
1147 * persistent data. Start a transaction that will encompass these
1148 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001150 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001152 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 status = psa_crypto_save_transaction();
1154 if (status != PSA_SUCCESS) {
1155 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001156 /* We should still try to destroy the key in the secure
1157 * element and the key metadata in storage. This is especially
1158 * important if the error is that the storage is full.
1159 * But how to do it exactly without risking an inconsistent
1160 * state after a reset?
1161 * https://github.com/ARMmbed/mbed-crypto/issues/215
1162 */
1163 overall_status = status;
1164 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001165 }
1166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 status = psa_destroy_se_key(driver,
1168 psa_key_slot_get_slot_number(slot));
1169 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001170 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001172 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001173#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1174
Darryl Greend49a4992018-06-18 17:27:26 +01001175#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1177 status = psa_destroy_persistent_key(slot->attr.id);
1178 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001179 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001181
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001182 /* TODO: other slots may have a copy of the same key. We should
1183 * invalidate them.
1184 * https://github.com/ARMmbed/mbed-crypto/issues/214
1185 */
Darryl Greend49a4992018-06-18 17:27:26 +01001186 }
1187#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001188
Gilles Peskinefc762652019-07-22 19:30:34 +02001189#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (driver != NULL) {
1191 status = psa_save_se_persistent_data(driver);
1192 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001193 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 }
1195 status = psa_crypto_stop_transaction();
1196 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001197 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001199 }
1200#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1201
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001204 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001206 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 }
1208 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001209}
1210
John Durkop07cc04a2020-11-16 22:08:34 -08001211#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001212 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001213static psa_status_t psa_get_rsa_public_exponent(
1214 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001216{
1217 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001219 uint8_t *buffer = NULL;
1220 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1224 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001225 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 }
1227 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001228 /* It's the default value, which is reported as an empty string,
1229 * so there's nothing to do. */
1230 goto exit;
1231 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 buflen = mbedtls_mpi_size(&mpi);
1234 buffer = mbedtls_calloc(1, buflen);
1235 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001236 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1237 goto exit;
1238 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1240 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001241 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001243 attributes->domain_parameters = buffer;
1244 attributes->domain_parameters_size = buflen;
1245
1246exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 mbedtls_mpi_free(&mpi);
1248 if (ret != 0) {
1249 mbedtls_free(buffer);
1250 }
1251 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001252}
John Durkop07cc04a2020-11-16 22:08:34 -08001253#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001254 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001255
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001256/** Retrieve all the publicly-accessible attributes of a key.
1257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001258psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1259 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001260{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001261 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001262 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001263 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1268 if (status != PSA_SUCCESS) {
1269 return status;
1270 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001271
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001272 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1274 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001275
1276#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1278 psa_set_key_slot_number(attributes,
1279 psa_key_slot_get_slot_number(slot));
1280 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001281#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001284#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001285 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001286 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001287 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001288 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001289 * is not yet implemented.
1290 * https://github.com/ARMmbed/mbed-crypto/issues/216
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001293 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001294
Ronald Cron90857082020-11-25 14:58:33 +01001295 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 slot->attr.type,
1297 slot->key.data,
1298 slot->key.bytes,
1299 &rsa);
1300 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001301 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 status = psa_get_rsa_public_exponent(rsa,
1305 attributes);
1306 mbedtls_rsa_free(rsa);
1307 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001308 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001309 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001310#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001311 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001312 default:
1313 /* Nothing else to do. */
1314 break;
1315 }
1316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 if (status != PSA_SUCCESS) {
1318 psa_reset_key_attributes(attributes);
1319 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001324}
1325
Gilles Peskinec8000c02019-08-02 20:15:51 +02001326#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1327psa_status_t psa_get_key_slot_number(
1328 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001330{
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001332 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 return PSA_SUCCESS;
1334 } else {
1335 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001336 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001337}
1338#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1341 size_t key_buffer_size,
1342 uint8_t *data,
1343 size_t data_size,
1344 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001345{
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (key_buffer_size > data_size) {
1347 return PSA_ERROR_BUFFER_TOO_SMALL;
1348 }
1349 memcpy(data, key_buffer, key_buffer_size);
1350 memset(data + key_buffer_size, 0,
1351 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001352 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001354}
1355
Ronald Cron7285cda2020-11-26 14:46:37 +01001356psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001357 const psa_key_attributes_t *attributes,
1358 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001360{
Ronald Crond18b5f82020-11-25 16:46:05 +01001361 psa_key_type_t type = attributes->core.type;
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (key_type_is_raw_bytes(type) ||
1364 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001365 PSA_KEY_TYPE_IS_ECC(type) ||
1366 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 return psa_export_key_buffer_internal(
1368 key_buffer, key_buffer_size,
1369 data, data_size, data_length);
1370 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001371 /* This shouldn't happen in the reference implementation, but
1372 it is valid for a special-purpose implementation to omit
1373 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001375 }
1376}
1377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1379 uint8_t *data,
1380 size_t data_size,
1381 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001382{
1383 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1384 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1385 psa_key_slot_t *slot;
1386
Ronald Crone907e552021-01-18 13:22:38 +01001387 /* Reject a zero-length output buffer now, since this can never be a
1388 * valid key representation. This way we know that data must be a valid
1389 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if (data_size == 0) {
1391 return PSA_ERROR_BUFFER_TOO_SMALL;
1392 }
Ronald Crone907e552021-01-18 13:22:38 +01001393
Ronald Cron9486f9d2020-11-26 13:36:23 +01001394 /* Set the key to empty now, so that even when there are errors, we always
1395 * set data_length to a value between 0 and data_size. On error, setting
1396 * the key to empty is a good choice because an empty key representation is
1397 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001398 *data_length = 0;
1399
Ronald Cron9486f9d2020-11-26 13:36:23 +01001400 /* Export requires the EXPORT flag. There is an exception for public keys,
1401 * which don't require any flag, but
1402 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1403 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1405 PSA_KEY_USAGE_EXPORT, 0);
1406 if (status != PSA_SUCCESS) {
1407 return status;
1408 }
mohammad160306e79202018-03-28 13:17:44 +03001409
Ronald Crond18b5f82020-11-25 16:46:05 +01001410 psa_key_attributes_t attributes = {
1411 .core = slot->attr
1412 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 status = psa_driver_wrapper_export_key(&attributes,
1414 slot->key.data, slot->key.bytes,
1415 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001420}
1421
Ronald Cron7285cda2020-11-26 14:46:37 +01001422psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001423 const psa_key_attributes_t *attributes,
1424 const uint8_t *key_buffer,
1425 size_t key_buffer_size,
1426 uint8_t *data,
1427 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001429{
Ronald Crond18b5f82020-11-25 16:46:05 +01001430 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001431
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001432 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
1433 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1434 PSA_KEY_TYPE_IS_DH(type))) {
1435 /* Exporting public -> public */
1436 return psa_export_key_buffer_internal(
1437 key_buffer, key_buffer_size,
1438 data, data_size, data_length);
1439 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001440#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1442 return mbedtls_psa_rsa_export_public_key(attributes,
1443 key_buffer,
1444 key_buffer_size,
1445 data,
1446 data_size,
1447 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001448#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001449 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001451#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1452 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001453 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001454#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1456 return mbedtls_psa_ecp_export_public_key(attributes,
1457 key_buffer,
1458 key_buffer_size,
1459 data,
1460 data_size,
1461 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001462#else
1463 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001465#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1466 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001467 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001468#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
1469 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel359f4622022-12-05 14:11:55 +01001470 return mbedtls_psa_export_ffdh_public_key(attributes,
1471 key_buffer,
1472 key_buffer_size,
1473 data, data_size,
1474 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001475#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001476 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001477#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
1478 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001481 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001482}
Ronald Crond18b5f82020-11-25 16:46:05 +01001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1485 uint8_t *data,
1486 size_t data_size,
1487 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001488{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001489 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001490 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001491 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001492
Ronald Crone907e552021-01-18 13:22:38 +01001493 /* Reject a zero-length output buffer now, since this can never be a
1494 * valid key representation. This way we know that data must be a valid
1495 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (data_size == 0) {
1497 return PSA_ERROR_BUFFER_TOO_SMALL;
1498 }
Ronald Crone907e552021-01-18 13:22:38 +01001499
Darryl Greendd8fb772018-11-07 16:00:44 +00001500 /* Set the key to empty now, so that even when there are errors, we always
1501 * set data_length to a value between 0 and data_size. On error, setting
1502 * the key to empty is a good choice because an empty key representation is
1503 * unlikely to be accepted anywhere. */
1504 *data_length = 0;
1505
1506 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1508 if (status != PSA_SUCCESS) {
1509 return status;
1510 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1513 status = PSA_ERROR_INVALID_ARGUMENT;
1514 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001515 }
1516
Ronald Crond18b5f82020-11-25 16:46:05 +01001517 psa_key_attributes_t attributes = {
1518 .core = slot->attr
1519 };
Ronald Cron67227982020-11-26 15:16:05 +01001520 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001521 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001523
1524exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001528}
1529
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001530MBEDTLS_STATIC_ASSERT(
1531 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1532 "One or more key attribute flag is listed as both external-only and dual-use")
1533MBEDTLS_STATIC_ASSERT(
1534 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1535 "One or more key attribute flag is listed as both internal-only and dual-use")
1536MBEDTLS_STATIC_ASSERT(
1537 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1538 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001539
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001540/** Validate that a key policy is internally well-formed.
1541 *
1542 * This function only rejects invalid policies. It does not validate the
1543 * consistency of the policy with respect to other attributes of the key
1544 * such as the key type.
1545 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001547{
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1549 PSA_KEY_USAGE_COPY |
1550 PSA_KEY_USAGE_ENCRYPT |
1551 PSA_KEY_USAGE_DECRYPT |
1552 PSA_KEY_USAGE_SIGN_MESSAGE |
1553 PSA_KEY_USAGE_VERIFY_MESSAGE |
1554 PSA_KEY_USAGE_SIGN_HASH |
1555 PSA_KEY_USAGE_VERIFY_HASH |
1556 PSA_KEY_USAGE_VERIFY_DERIVATION |
1557 PSA_KEY_USAGE_DERIVE)) != 0) {
1558 return PSA_ERROR_INVALID_ARGUMENT;
1559 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001562}
1563
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001564/** Validate the internal consistency of key attributes.
1565 *
1566 * This function only rejects invalid attribute values. If does not
1567 * validate the consistency of the attributes with any key data that may
1568 * be involved in the creation of the key.
1569 *
1570 * Call this function early in the key creation process.
1571 *
1572 * \param[in] attributes Key attributes for the new key.
1573 * \param[out] p_drv On any return, the driver for the key, if any.
1574 * NULL for a transparent key.
1575 *
1576 */
1577static psa_status_t psa_validate_key_attributes(
1578 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001580{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001581 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1583 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 status = psa_validate_key_location(lifetime, p_drv);
1586 if (status != PSA_SUCCESS) {
1587 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001588 }
1589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 status = psa_validate_key_persistence(lifetime);
1591 if (status != PSA_SUCCESS) {
1592 return status;
1593 }
1594
1595 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1596 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1597 return PSA_ERROR_INVALID_ARGUMENT;
1598 }
1599 } else {
1600 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1601 return PSA_ERROR_INVALID_ARGUMENT;
1602 }
1603 }
1604
1605 status = psa_validate_key_policy(&attributes->core.policy);
1606 if (status != PSA_SUCCESS) {
1607 return status;
1608 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001609
1610 /* Refuse to create overly large keys.
1611 * Note that this doesn't trigger on import if the attributes don't
1612 * explicitly specify a size (so psa_get_key_bits returns 0), so
1613 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1615 return PSA_ERROR_NOT_SUPPORTED;
1616 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001617
Gilles Peskine91e8c332019-08-02 19:19:39 +02001618 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1620 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1621 return PSA_ERROR_INVALID_ARGUMENT;
1622 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001625}
1626
Gilles Peskine4747d192019-04-17 15:05:45 +02001627/** Prepare a key slot to receive key material.
1628 *
1629 * This function allocates a key slot and sets its metadata.
1630 *
1631 * If this function fails, call psa_fail_key_creation().
1632 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001633 * This function is intended to be used as follows:
1634 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001635 * it with the specified attributes, and in case of a volatile key assign it
1636 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001637 * -# Populate the slot with the key material.
1638 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1639 * In case of failure at any step, stop the sequence and call
1640 * psa_fail_key_creation().
1641 *
Ronald Cron5c522922020-11-14 16:35:34 +01001642 * On success, the key slot is locked. It is the responsibility of the caller
1643 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001644 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001645 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001646 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001647 * \param[out] p_slot On success, a pointer to the prepared slot.
1648 * \param[out] p_drv On any return, the driver for the key, if any.
1649 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001650 *
1651 * \retval #PSA_SUCCESS
1652 * The key slot is ready to receive key material.
1653 * \return If this function fails, the key slot is an invalid state.
1654 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 */
1656static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001657 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001658 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001659 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001661{
1662 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001663 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001664 psa_key_slot_t *slot;
1665
Gilles Peskinedf179142019-07-15 22:02:14 +02001666 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001667 *p_drv = NULL;
1668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 status = psa_validate_key_attributes(attributes, p_drv);
1670 if (status != PSA_SUCCESS) {
1671 return status;
1672 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1675 if (status != PSA_SUCCESS) {
1676 return status;
1677 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001678 slot = *p_slot;
1679
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001680 /* We're storing the declared bit-size of the key. It's up to each
1681 * creation mechanism to verify that this information is correct.
1682 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001683 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001684 * is optional (import, copy). In case of a volatile key, assign it the
1685 * volatile key identifier associated to the slot returned to contain its
1686 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001687
1688 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001690#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1691 slot->attr.id = volatile_key_id;
1692#else
1693 slot->attr.id.key_id = volatile_key_id;
1694#endif
1695 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001696
Gilles Peskine91e8c332019-08-02 19:19:39 +02001697 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001698 * external-only flags, query `attributes`. Thanks to the check
1699 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001700 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001701 * may have set. */
1702 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001703
Gilles Peskinecbaff462019-07-12 23:46:04 +02001704#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001705 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001706 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001707 * create the key file in internal storage, create the
1708 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001709 * persistent data. This is done by starting a transaction that will
1710 * encompass these three actions.
1711 * For registering a volatile key, we just need to find an appropriate
1712 * slot number inside the SE. Since the key is designated volatile, creating
1713 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001714 /* The first thing to do is to find a slot number for the new key.
1715 * We save the slot number in persistent storage as part of the
1716 * transaction data. It will be needed to recover if the power
1717 * fails during the key creation process, to clean up on the secure
1718 * element side after restarting. Obtaining a slot number from the
1719 * secure element driver updates its persistent state, but we do not yet
1720 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001721 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001723 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1725 &slot_number);
1726 if (status != PSA_SUCCESS) {
1727 return status;
1728 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1731 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001732 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001733 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001734 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 status = psa_crypto_save_transaction();
1736 if (status != PSA_SUCCESS) {
1737 (void) psa_crypto_stop_transaction();
1738 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001739 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001740 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001741
1742 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001744 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001747 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001749 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001750#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001753}
Gilles Peskine4747d192019-04-17 15:05:45 +02001754
1755/** Finalize the creation of a key once its key material has been set.
1756 *
1757 * This entails writing the key to persistent storage.
1758 *
1759 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001760 * See the documentation of psa_start_key_creation() for the intended use
1761 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001762 *
Ronald Cron5c522922020-11-14 16:35:34 +01001763 * If the finalization succeeds, the function unlocks the key slot (it was
1764 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1765 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001766 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001767 * \param[in,out] slot Pointer to the slot with key material.
1768 * \param[in] driver The secure element driver for the key,
1769 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001770 * \param[out] key On success, identifier of the key. Note that the
1771 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001772 *
1773 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001774 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001775 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1776 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1777 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1778 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1779 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1780 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001781 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001782 * \return If this function fails, the key slot is an invalid state.
1783 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001784 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001785static psa_status_t psa_finish_key_creation(
1786 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001787 psa_se_drv_table_entry_t *driver,
1788 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001789{
1790 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001791 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001792 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001793
1794#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001796#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001798 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001799 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001801
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001802 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1803 sizeof(data.slot_number),
1804 "Slot number size does not match psa_se_key_data_storage_t");
1805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1807 status = psa_save_persistent_key(&slot->attr,
1808 (uint8_t *) &data,
1809 sizeof(data));
1810 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001811#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1812 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001813 /* Key material is saved in export representation in the slot, so
1814 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 status = psa_save_persistent_key(&slot->attr,
1816 slot->key.data,
1817 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001818 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001819 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001820#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1821
Gilles Peskinecbaff462019-07-12 23:46:04 +02001822#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001823 /* Finish the transaction for a key creation. This does not
1824 * happen when registering an existing key. Detect this case
1825 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001826 * creation of a persistent key in a secure element requires a transaction,
1827 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 if (driver != NULL &&
1829 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1830 status = psa_save_se_persistent_data(driver);
1831 if (status != PSA_SUCCESS) {
1832 psa_destroy_persistent_key(slot->attr.id);
1833 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001834 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001836 }
1837#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001840 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 status = psa_unlock_key_slot(slot);
1842 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001843 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001845 }
Ronald Cron50972942020-11-14 11:28:25 +01001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001848}
1849
1850/** Abort the creation of a key.
1851 *
1852 * You may call this function after calling psa_start_key_creation(),
1853 * or after psa_finish_key_creation() fails. In other circumstances, this
1854 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001855 * See the documentation of psa_start_key_creation() for the intended use
1856 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001857 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001858 * \param[in,out] slot Pointer to the slot with key material.
1859 * \param[in] driver The secure element driver for the key,
1860 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001861 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862static void psa_fail_key_creation(psa_key_slot_t *slot,
1863 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001864{
Gilles Peskine011e4282019-06-26 18:34:38 +02001865 (void) driver;
1866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001868 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001870
1871#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001872 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001873 * element, and the failure happened later (when saving metadata
1874 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001875 * element.
1876 * https://github.com/ARMmbed/mbed-crypto/issues/217
1877 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001878
Gilles Peskined7729582019-08-05 15:55:54 +02001879 /* Abort the ongoing transaction if any (there may not be one if
1880 * the creation process failed before starting one, or if the
1881 * key creation is a registration of a key in a secure element).
1882 * Earlier functions must already have done what it takes to undo any
1883 * partial creation. All that's left is to update the transaction data
1884 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001886#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001889}
1890
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001891/** Validate optional attributes during key creation.
1892 *
1893 * Some key attributes are optional during key creation. If they are
1894 * specified in the attributes structure, check that they are consistent
1895 * with the data in the slot.
1896 *
1897 * This function should be called near the end of key creation, after
1898 * the slot in memory is fully populated but before saving persistent data.
1899 */
1900static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001901 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001903{
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 if (attributes->core.type != 0) {
1905 if (attributes->core.type != slot->attr.type) {
1906 return PSA_ERROR_INVALID_ARGUMENT;
1907 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001908 }
1909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001911#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1913 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001914 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001915 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001917
Ronald Cron90857082020-11-25 14:58:33 +01001918 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 slot->attr.type,
1920 slot->key.data,
1921 slot->key.bytes,
1922 &rsa);
1923 if (status != PSA_SUCCESS) {
1924 return status;
1925 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 mbedtls_mpi_init(&actual);
1928 mbedtls_mpi_init(&required);
1929 ret = mbedtls_rsa_export(rsa,
1930 NULL, NULL, NULL, NULL, &actual);
1931 mbedtls_rsa_free(rsa);
1932 mbedtls_free(rsa);
1933 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001934 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 }
1936 ret = mbedtls_mpi_read_binary(&required,
1937 attributes->domain_parameters,
1938 attributes->domain_parameters_size);
1939 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 }
1942 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001943 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 }
1945rsa_exit:
1946 mbedtls_mpi_free(&actual);
1947 mbedtls_mpi_free(&required);
1948 if (ret != 0) {
1949 return mbedtls_to_psa_error(ret);
1950 }
1951 } else
John Durkop9814fa22020-11-04 12:28:15 -08001952#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1953 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001954 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001956 }
1957 }
1958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if (attributes->core.bits != 0) {
1960 if (attributes->core.bits != slot->attr.bits) {
1961 return PSA_ERROR_INVALID_ARGUMENT;
1962 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963 }
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001966}
1967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1969 const uint8_t *data,
1970 size_t data_length,
1971 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001972{
1973 psa_status_t status;
1974 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001975 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001976 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301977 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001978
Ronald Cron81709fc2020-11-14 12:10:32 +01001979 *key = MBEDTLS_SVC_KEY_ID_INIT;
1980
Gilles Peskine0f84d622019-09-12 19:03:13 +02001981 /* Reject zero-length symmetric keys (including raw data key objects).
1982 * This also rejects any key which might be encoded as an empty string,
1983 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (data_length == 0) {
1985 return PSA_ERROR_INVALID_ARGUMENT;
1986 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001987
Archana449608b2021-09-08 15:36:05 +05301988 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (data_length > SIZE_MAX / 8) {
1990 return PSA_ERROR_NOT_SUPPORTED;
1991 }
Archana6ed4bda2021-08-04 10:47:15 +05301992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1994 &slot, &driver);
1995 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001996 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001998
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001999 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302000 * storage ( thus not in the case of importing a key in a secure element
2001 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2002 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 if (slot->key.data == NULL) {
2004 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302005 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 attributes, data, data_length, &storage_size);
2007 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302008 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 }
Archanad8a83dc2021-06-14 10:04:16 +05302010 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 status = psa_allocate_buffer_to_slot(slot, storage_size);
2012 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002013 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002015 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002016
2017 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 status = psa_driver_wrapper_import_key(attributes,
2019 data, data_length,
2020 slot->key.data,
2021 slot->key.bytes,
2022 &slot->key.bytes, &bits);
2023 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002024 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002028 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002030 status = PSA_ERROR_INVALID_ARGUMENT;
2031 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002032 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002033
Archana6ed4bda2021-08-04 10:47:15 +05302034 /* Enforce a size limit, and in particular ensure that the bit
2035 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302037 status = PSA_ERROR_NOT_SUPPORTED;
2038 goto exit;
2039 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 status = psa_validate_optional_attributes(slot, attributes);
2041 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002042 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002046exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 if (status != PSA_SUCCESS) {
2048 psa_fail_key_creation(slot, driver);
2049 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002052}
2053
Gilles Peskined7729582019-08-05 15:55:54 +02002054#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2055psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002057{
2058 psa_status_t status;
2059 psa_key_slot_t *slot = NULL;
2060 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002061 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002062
2063 /* Leaving attributes unspecified is not currently supported.
2064 * It could make sense to query the key type and size from the
2065 * secure element, but not all secure elements support this
2066 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2068 return PSA_ERROR_NOT_SUPPORTED;
2069 }
2070 if (psa_get_key_bits(attributes) == 0) {
2071 return PSA_ERROR_NOT_SUPPORTED;
2072 }
Gilles Peskined7729582019-08-05 15:55:54 +02002073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2075 &slot, &driver);
2076 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 }
Gilles Peskined7729582019-08-05 15:55:54 +02002079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002081
2082exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 if (status != PSA_SUCCESS) {
2084 psa_fail_key_creation(slot, driver);
2085 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002086
Gilles Peskined7729582019-08-05 15:55:54 +02002087 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 psa_close_key(key);
2089 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002090}
2091#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2094 const psa_key_attributes_t *specified_attributes,
2095 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002096{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002098 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002099 psa_key_slot_t *source_slot = NULL;
2100 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002101 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002102 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302103 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002104
Ronald Cron81709fc2020-11-14 12:10:32 +01002105 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2106
Archana8a180362021-07-05 02:18:48 +05302107 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2109 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 status = psa_validate_optional_attributes(source_slot,
2114 specified_attributes);
2115 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002118
Archana9d17bf42021-09-10 06:22:44 +05302119 /* The target key type and number of bits have been validated by
2120 * psa_validate_optional_attributes() to be either equal to zero or
2121 * equal to the ones of the source key. So it is safe to inherit
2122 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302123 * */
Archana9d17bf42021-09-10 06:22:44 +05302124 actual_attributes.core.bits = source_slot->attr.bits;
2125 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302126
2127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 status = psa_restrict_key_policy(source_slot->attr.type,
2129 &actual_attributes.core.policy,
2130 &source_slot->attr.policy);
2131 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2136 &target_slot, &driver);
2137 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 }
2140 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2141 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302142 /*
Archana9d17bf42021-09-10 06:22:44 +05302143 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302144 * the source key would need to be exported as plaintext and re-imported
2145 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302146 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302147 * appropriate API invocations from the application, if needed.
2148 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002149 status = PSA_ERROR_NOT_SUPPORTED;
2150 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002151 }
Archana8a180362021-07-05 02:18:48 +05302152 /*
2153 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302154 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302155 * - For opaque keys this translates to an invocation of the drivers'
2156 * copy_key entry point through the dispatch layer.
2157 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2159 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2160 &storage_size);
2161 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 }
Archana374fe5b2021-09-08 15:50:28 +05302164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2166 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302167 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 }
Archana374fe5b2021-09-08 15:50:28 +05302169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 status = psa_driver_wrapper_copy_key(&actual_attributes,
2171 source_slot->key.data,
2172 source_slot->key.bytes,
2173 target_slot->key.data,
2174 target_slot->key.bytes,
2175 &target_slot->key.bytes);
2176 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302177 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 }
2179 } else {
2180 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302181 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 source_slot->key.bytes);
2183 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302184 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 }
Archana8a180362021-07-05 02:18:48 +05302186 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002188exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (status != PSA_SUCCESS) {
2190 psa_fail_key_creation(target_slot, driver);
2191 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002196}
2197
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002198
2199
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002200/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002201/* Message digests */
2202/****************************************************************/
2203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002205{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002206 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (operation->id == 0) {
2208 return PSA_SUCCESS;
2209 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002212 operation->id = 0;
2213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002215}
2216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2218 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002219{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002220 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002221
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002222 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002224 status = PSA_ERROR_BAD_STATE;
2225 goto exit;
2226 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002229 status = PSA_ERROR_INVALID_ARGUMENT;
2230 goto exit;
2231 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002232
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002233 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2234 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002238
2239exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 if (status != PSA_SUCCESS) {
2241 psa_hash_abort(operation);
2242 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002243
2244 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245}
2246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2248 const uint8_t *input,
2249 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002250{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002251 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002254 status = PSA_ERROR_BAD_STATE;
2255 goto exit;
2256 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002257
Steven Cooremanc8288352021-03-04 14:02:19 +01002258 /* Don't require hash implementations to behave correctly on a
2259 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 if (input_length == 0) {
2261 return PSA_SUCCESS;
2262 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002265
2266exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 if (status != PSA_SUCCESS) {
2268 psa_hash_abort(operation);
2269 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272}
2273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2275 uint8_t *hash,
2276 size_t hash_size,
2277 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002279 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 if (operation->id == 0) {
2281 return PSA_ERROR_BAD_STATE;
2282 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002283
Steven Cooreman1e582352021-02-18 17:24:37 +01002284 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 operation, hash, hash_size, hash_length);
2286 psa_hash_abort(operation);
2287 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288}
2289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2291 const uint8_t *hash,
2292 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002293{
Ronald Cron69a63422021-10-18 09:47:58 +02002294 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002296 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 operation,
2298 actual_hash, sizeof(actual_hash),
2299 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002302 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002306 status = PSA_ERROR_INVALID_SIGNATURE;
2307 goto exit;
2308 }
2309
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002311 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002313
2314exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2316 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002317 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321}
2322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323psa_status_t psa_hash_compute(psa_algorithm_t alg,
2324 const uint8_t *input, size_t input_length,
2325 uint8_t *hash, size_t hash_size,
2326 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002327{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002328 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if (!PSA_ALG_IS_HASH(alg)) {
2330 return PSA_ERROR_INVALID_ARGUMENT;
2331 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2334 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002335}
2336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337psa_status_t psa_hash_compare(psa_algorithm_t alg,
2338 const uint8_t *input, size_t input_length,
2339 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002340{
Ronald Cron69a63422021-10-18 09:47:58 +02002341 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002342 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (!PSA_ALG_IS_HASH(alg)) {
2345 return PSA_ERROR_INVALID_ARGUMENT;
2346 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002347
Steven Cooreman1e582352021-02-18 17:24:37 +01002348 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 alg, input, input_length,
2350 actual_hash, sizeof(actual_hash),
2351 &actual_hash_length);
2352 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002353 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 }
2355 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002356 status = PSA_ERROR_INVALID_SIGNATURE;
2357 goto exit;
2358 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002360 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002362
2363exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2365 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002366}
2367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2369 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002370{
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (source_operation->id == 0 ||
2372 target_operation->id != 0) {
2373 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002374 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2377 target_operation);
2378 if (status != PSA_SUCCESS) {
2379 psa_hash_abort(target_operation);
2380 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002383}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002384
2385
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002387/* MAC */
2388/****************************************************************/
2389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002391{
Steven Cooremane6804192021-03-19 18:28:56 +01002392 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (operation->id == 0) {
2394 return PSA_SUCCESS;
2395 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002398 operation->mac_size = 0;
2399 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002400 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002403}
2404
Ronald Cron2dff3b22021-06-17 16:33:22 +02002405static psa_status_t psa_mac_finalize_alg_and_key_validation(
2406 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002407 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002410 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 psa_key_type_t key_type = psa_get_key_type(attributes);
2412 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if (!PSA_ALG_IS_MAC(alg)) {
2415 return PSA_ERROR_INVALID_ARGUMENT;
2416 }
Steven Cooremane6804192021-03-19 18:28:56 +01002417
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002418 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 status = psa_mac_key_can_do(alg, key_type);
2420 if (status != PSA_SUCCESS) {
2421 return status;
2422 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002423
Steven Cooremand1a68f12021-05-10 11:13:41 +02002424 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002428 /* A very short MAC is too short for security since it can be
2429 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2430 * so we make this our minimum, even though 32 bits is still
2431 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002433 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2436 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002437 /* It's impossible to "truncate" to a larger length than the full length
2438 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002440 }
2441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002443 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2444 * that is disabled in the compile-time configuration. The result can
2445 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2446 * configuration into account. In this case, force a return of
2447 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2448 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2449 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2450 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2451 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002453 }
2454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002456}
2457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2459 mbedtls_svc_key_id_t key,
2460 psa_algorithm_t alg,
2461 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002462{
2463 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2464 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002465 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002466
2467 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002469 status = PSA_ERROR_BAD_STATE;
2470 goto exit;
2471 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472
2473 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 key,
2475 &slot,
2476 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2477 alg);
2478 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002479 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002481
2482 psa_key_attributes_t attributes = {
2483 .core = slot->attr
2484 };
2485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2487 &operation->mac_size);
2488 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491
2492 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002493 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (is_sign) {
2495 status = psa_driver_wrapper_mac_sign_setup(operation,
2496 &attributes,
2497 slot->key.data,
2498 slot->key.bytes,
2499 alg);
2500 } else {
2501 status = psa_driver_wrapper_mac_verify_setup(operation,
2502 &attributes,
2503 slot->key.data,
2504 slot->key.bytes,
2505 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002507
Gilles Peskinefbfac682018-07-08 20:51:54 +02002508exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 if (status != PSA_SUCCESS) {
2510 psa_mac_abort(operation);
2511 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002516}
2517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2519 mbedtls_svc_key_id_t key,
2520 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002521{
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002523}
2524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2526 mbedtls_svc_key_id_t key,
2527 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002528{
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002530}
2531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2533 const uint8_t *input,
2534 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535{
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (operation->id == 0) {
2537 return PSA_ERROR_BAD_STATE;
2538 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002540 /* Don't require hash implementations to behave correctly on a
2541 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if (input_length == 0) {
2543 return PSA_SUCCESS;
2544 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2547 input, input_length);
2548 if (status != PSA_SUCCESS) {
2549 psa_mac_abort(operation);
2550 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002553}
2554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2556 uint8_t *mac,
2557 size_t mac_size,
2558 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002559{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2561 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002564 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002565 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002566 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002569 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002570 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002571 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002572
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002573 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2574 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002576 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002577 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002578 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002581 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002582 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002583 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 status = psa_driver_wrapper_mac_sign_finish(operation,
2586 mac, operation->mac_size,
2587 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002588
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002589exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002590 /* In case of success, set the potential excess room in the output buffer
2591 * to an invalid value, to avoid potentially leaking a longer MAC.
2592 * In case of error, set the output length and content to a safe default,
2593 * such that in case the caller misses an error check, the output would be
2594 * an unachievable MAC.
2595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002597 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002598 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002599 }
mohammad16036df908f2018-04-02 08:34:15 -07002600
Paul Elliottdc42ca82023-02-24 18:11:59 +00002601 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002606}
2607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2609 const uint8_t *mac,
2610 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002612 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2613 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002616 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002617 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002618 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002621 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002622 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002623 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002626 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002627 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002628 }
2629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 status = psa_driver_wrapper_mac_verify_finish(operation,
2631 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002632
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002633exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002637}
2638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2640 psa_algorithm_t alg,
2641 const uint8_t *input,
2642 size_t input_length,
2643 uint8_t *mac,
2644 size_t mac_size,
2645 size_t *mac_length,
2646 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002647{
2648 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002649 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2650 psa_key_slot_t *slot;
2651 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002652
Ronald Cron51131b52021-06-17 17:17:20 +02002653 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 key,
2655 &slot,
2656 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2657 alg);
2658 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002659 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002661
Ronald Cron51131b52021-06-17 17:17:20 +02002662 psa_key_attributes_t attributes = {
2663 .core = slot->attr
2664 };
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2667 &operation_mac_size);
2668 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002669 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002673 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002674 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002675 }
2676
2677 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 &attributes,
2679 slot->key.data, slot->key.bytes,
2680 alg,
2681 input, input_length,
2682 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002683
2684exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002685 /* In case of success, set the potential excess room in the output buffer
2686 * to an invalid value, to avoid potentially leaking a longer MAC.
2687 * In case of error, set the output length and content to a safe default,
2688 * such that in case the caller misses an error check, the output would be
2689 * an unachievable MAC.
2690 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002692 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002693 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002694 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002695
2696 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002701}
2702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002704 psa_algorithm_t alg,
2705 const uint8_t *input,
2706 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 uint8_t *mac,
2708 size_t mac_size,
2709 size_t *mac_length)
2710{
2711 return psa_mac_compute_internal(key, alg,
2712 input, input_length,
2713 mac, mac_size, mac_length, 1);
2714}
2715
2716psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2717 psa_algorithm_t alg,
2718 const uint8_t *input,
2719 size_t input_length,
2720 const uint8_t *mac,
2721 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002722{
2723 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002724 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2725 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 status = psa_mac_compute_internal(key, alg,
2728 input, input_length,
2729 actual_mac, sizeof(actual_mac),
2730 &actual_mac_length, 0);
2731 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002732 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002736 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002737 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002738 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002740 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002741 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002742 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002743
2744exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002748}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002749
Gilles Peskinee59236f2018-01-27 23:32:46 +01002750/****************************************************************/
2751/* Asymmetric cryptography */
2752/****************************************************************/
2753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2755 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002756{
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if (input_is_message) {
2758 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2759 return PSA_ERROR_INVALID_ARGUMENT;
2760 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2763 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2764 return PSA_ERROR_INVALID_ARGUMENT;
2765 }
2766 }
2767 } else {
2768 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2769 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002770 }
2771 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002774}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2777 int input_is_message,
2778 psa_algorithm_t alg,
2779 const uint8_t *input,
2780 size_t input_length,
2781 uint8_t *signature,
2782 size_t signature_size,
2783 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002784{
2785 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2786 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2787 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002788
2789 *signature_length = 0;
2790
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 status = psa_sign_verify_check_alg(input_is_message, alg);
2792 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002793 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002795
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002796 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002797 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002798 * buffer can in principle be empty since it doesn't actually have
2799 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 if (signature_size == 0) {
2801 return PSA_ERROR_BUFFER_TOO_SMALL;
2802 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002803
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002804 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 key, &slot,
2806 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2807 PSA_KEY_USAGE_SIGN_HASH,
2808 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002811 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002815 status = PSA_ERROR_INVALID_ARGUMENT;
2816 goto exit;
2817 }
2818
2819 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002821 };
2822
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002824 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002825 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002826 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 signature, signature_size, signature_length);
2828 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002829
2830 status = psa_driver_wrapper_sign_hash(
2831 &attributes, slot->key.data, slot->key.bytes,
2832 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002834 }
2835
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002836
2837exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002838 psa_wipe_tag_output_buffer(signature, status, signature_size,
2839 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002840
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002844}
2845
Gilles Peskine449bd832023-01-11 14:50:10 +01002846static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2847 int input_is_message,
2848 psa_algorithm_t alg,
2849 const uint8_t *input,
2850 size_t input_length,
2851 const uint8_t *signature,
2852 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002853{
2854 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2855 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2856 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 status = psa_sign_verify_check_alg(input_is_message, alg);
2859 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002860 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002862
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002863 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 key, &slot,
2865 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2866 PSA_KEY_USAGE_VERIFY_HASH,
2867 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 if (status != PSA_SUCCESS) {
2870 return status;
2871 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002872
2873 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002875 };
2876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002878 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002879 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002880 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 signature, signature_length);
2882 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002883 status = psa_driver_wrapper_verify_hash(
2884 &attributes, slot->key.data, slot->key.bytes,
2885 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002887 }
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-arm5b446522021-04-20 12:11:35 +02002892
2893}
2894
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002895psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002896 const psa_key_attributes_t *attributes,
2897 const uint8_t *key_buffer,
2898 size_t key_buffer_size,
2899 psa_algorithm_t alg,
2900 const uint8_t *input,
2901 size_t input_length,
2902 uint8_t *signature,
2903 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002905{
2906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002907
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002909 size_t hash_length;
2910 uint8_t hash[PSA_HASH_MAX_SIZE];
2911
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002912 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 PSA_ALG_SIGN_GET_HASH(alg),
2914 input, input_length,
2915 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002918 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002920
2921 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 attributes, key_buffer, key_buffer_size,
2923 alg, hash, hash_length,
2924 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002925 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002928}
2929
Gilles Peskine449bd832023-01-11 14:50:10 +01002930psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2931 psa_algorithm_t alg,
2932 const uint8_t *input,
2933 size_t input_length,
2934 uint8_t *signature,
2935 size_t signature_size,
2936 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002937{
2938 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002939 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002941}
2942
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002943psa_status_t psa_verify_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 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002952{
2953 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002954
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002956 size_t hash_length;
2957 uint8_t hash[PSA_HASH_MAX_SIZE];
2958
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002959 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 PSA_ALG_SIGN_GET_HASH(alg),
2961 input, input_length,
2962 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002965 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002967
2968 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 attributes, key_buffer, key_buffer_size,
2970 alg, hash, hash_length,
2971 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002972 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002975}
2976
Gilles Peskine449bd832023-01-11 14:50:10 +01002977psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2978 psa_algorithm_t alg,
2979 const uint8_t *input,
2980 size_t input_length,
2981 const uint8_t *signature,
2982 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002983{
2984 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002985 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002987}
2988
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002989psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002990 const psa_key_attributes_t *attributes,
2991 const uint8_t *key_buffer, size_t key_buffer_size,
2992 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002994{
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2996 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2997 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002998#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3000 return mbedtls_psa_rsa_sign_hash(
3001 attributes,
3002 key_buffer, key_buffer_size,
3003 alg, hash, hash_length,
3004 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003005#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3006 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 } else {
3008 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003009 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3011 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003012#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3014 return mbedtls_psa_ecdsa_sign_hash(
3015 attributes,
3016 key_buffer, key_buffer_size,
3017 alg, hash, hash_length,
3018 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003019#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3020 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 } else {
3022 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003023 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003024 }
Ronald Cron4501c982021-03-19 20:09:32 +01003025
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 (void) key_buffer;
3027 (void) key_buffer_size;
3028 (void) hash;
3029 (void) hash_length;
3030 (void) signature;
3031 (void) signature_size;
3032 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003035}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3038 psa_algorithm_t alg,
3039 const uint8_t *hash,
3040 size_t hash_length,
3041 uint8_t *signature,
3042 size_t signature_size,
3043 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003044{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003045 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003046 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003048}
3049
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003050psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003051 const psa_key_attributes_t *attributes,
3052 const uint8_t *key_buffer, size_t key_buffer_size,
3053 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003055{
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3057 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3058 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003059#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3061 return mbedtls_psa_rsa_verify_hash(
3062 attributes,
3063 key_buffer, key_buffer_size,
3064 alg, hash, hash_length,
3065 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003066#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3067 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 } else {
3069 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003070 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3072 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003073#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3075 return mbedtls_psa_ecdsa_verify_hash(
3076 attributes,
3077 key_buffer, key_buffer_size,
3078 alg, hash, hash_length,
3079 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003080#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3081 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 } else {
3083 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003084 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003085 }
Ronald Cron4501c982021-03-19 20:09:32 +01003086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 (void) key_buffer;
3088 (void) key_buffer_size;
3089 (void) hash;
3090 (void) hash_length;
3091 (void) signature;
3092 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003095}
3096
Gilles Peskine449bd832023-01-11 14:50:10 +01003097psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3098 psa_algorithm_t alg,
3099 const uint8_t *hash,
3100 size_t hash_length,
3101 const uint8_t *signature,
3102 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003103{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003104 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003105 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003107}
3108
Gilles Peskine449bd832023-01-11 14:50:10 +01003109psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3110 psa_algorithm_t alg,
3111 const uint8_t *input,
3112 size_t input_length,
3113 const uint8_t *salt,
3114 size_t salt_length,
3115 uint8_t *output,
3116 size_t output_size,
3117 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003118{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003119 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003120 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003121 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003122
Darryl Green5cc689a2018-07-24 15:34:10 +01003123 (void) input;
3124 (void) input_length;
3125 (void) salt;
3126 (void) output;
3127 (void) output_size;
3128
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003129 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3132 return PSA_ERROR_INVALID_ARGUMENT;
3133 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003134
Ronald Cron5c522922020-11-14 16:35:34 +01003135 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3137 if (status != PSA_SUCCESS) {
3138 return status;
3139 }
3140 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3141 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003142 status = PSA_ERROR_INVALID_ARGUMENT;
3143 goto exit;
3144 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003145
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003146 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003148 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003149
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003150 status = psa_driver_wrapper_asymmetric_encrypt(
3151 &attributes, slot->key.data, slot->key.bytes,
3152 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003154exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003158}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003159
Gilles Peskine449bd832023-01-11 14:50:10 +01003160psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3161 psa_algorithm_t alg,
3162 const uint8_t *input,
3163 size_t input_length,
3164 const uint8_t *salt,
3165 size_t salt_length,
3166 uint8_t *output,
3167 size_t output_size,
3168 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003169{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003171 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003172 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003173
Darryl Green5cc689a2018-07-24 15:34:10 +01003174 (void) input;
3175 (void) input_length;
3176 (void) salt;
3177 (void) output;
3178 (void) output_size;
3179
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003180 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3183 return PSA_ERROR_INVALID_ARGUMENT;
3184 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003185
Ronald Cron5c522922020-11-14 16:35:34 +01003186 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3188 if (status != PSA_SUCCESS) {
3189 return status;
3190 }
3191 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003192 status = PSA_ERROR_INVALID_ARGUMENT;
3193 goto exit;
3194 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003196 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003198 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003199
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003200 status = psa_driver_wrapper_asymmetric_decrypt(
3201 &attributes, slot->key.data, slot->key.bytes,
3202 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003204
3205exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003209}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003210
Paul Elliott9fe12f62022-11-30 19:16:02 +00003211/****************************************************************/
3212/* Asymmetric interruptible cryptography */
3213/****************************************************************/
3214
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003215static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3216
Paul Elliott9fe12f62022-11-30 19:16:02 +00003217void psa_interruptible_set_max_ops(uint32_t max_ops)
3218{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003219 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003220}
3221
3222uint32_t psa_interruptible_get_max_ops(void)
3223{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003224 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003225}
3226
Paul Elliott9fe12f62022-11-30 19:16:02 +00003227uint32_t psa_sign_hash_get_num_ops(
3228 const psa_sign_hash_interruptible_operation_t *operation)
3229{
Paul Elliott296ede92022-12-15 17:00:30 +00003230 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003231}
3232
3233uint32_t psa_verify_hash_get_num_ops(
3234 const psa_verify_hash_interruptible_operation_t *operation)
3235{
Paul Elliott296ede92022-12-15 17:00:30 +00003236 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003237}
3238
Paul Elliott59ad9452022-12-18 15:09:02 +00003239static psa_status_t psa_sign_hash_abort_internal(
3240 psa_sign_hash_interruptible_operation_t *operation)
3241{
3242 if (operation->id == 0) {
3243 /* The object has (apparently) been initialized but it is not (yet)
3244 * in use. It's ok to call abort on such an object, and there's
3245 * nothing to do. */
3246 return PSA_SUCCESS;
3247 }
3248
3249 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3250
3251 status = psa_driver_wrapper_sign_hash_abort(operation);
3252
3253 operation->id = 0;
3254
Paul Elliotte6145dc2023-02-07 12:51:21 +00003255 /* Do not clear either the error_occurred or num_ops elements here as they
3256 * only want to be cleared by the application calling abort, not by abort
3257 * being called at completion of an operation. */
3258
Paul Elliott59ad9452022-12-18 15:09:02 +00003259 return status;
3260}
3261
Paul Elliott9fe12f62022-11-30 19:16:02 +00003262psa_status_t psa_sign_hash_start(
3263 psa_sign_hash_interruptible_operation_t *operation,
3264 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3265 const uint8_t *hash, size_t hash_length)
3266{
3267 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3268 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3269 psa_key_slot_t *slot;
3270
Paul Elliottc9774412023-02-06 15:14:07 +00003271 /* Check that start has not been previously called, or operation has not
3272 * previously errored. */
3273 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003274 return PSA_ERROR_BAD_STATE;
3275 }
3276
Paul Elliott9fe12f62022-11-30 19:16:02 +00003277 status = psa_sign_verify_check_alg(0, alg);
3278 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003279 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003280 return status;
3281 }
3282
3283 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3284 PSA_KEY_USAGE_SIGN_HASH,
3285 alg);
3286
3287 if (status != PSA_SUCCESS) {
3288 goto exit;
3289 }
3290
3291 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3292 status = PSA_ERROR_INVALID_ARGUMENT;
3293 goto exit;
3294 }
3295
3296 psa_key_attributes_t attributes = {
3297 .core = slot->attr
3298 };
3299
Paul Elliott296ede92022-12-15 17:00:30 +00003300 /* Ensure ops count gets reset, in case of operation re-use. */
3301 operation->num_ops = 0;
3302
Paul Elliott9fe12f62022-11-30 19:16:02 +00003303 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3304 slot->key.data,
3305 slot->key.bytes, alg,
3306 hash, hash_length);
3307exit:
3308
3309 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003310 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003311 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003312 }
3313
3314 unlock_status = psa_unlock_key_slot(slot);
3315
Paul Elliottc9774412023-02-06 15:14:07 +00003316 if (unlock_status != PSA_SUCCESS) {
3317 operation->error_occurred = 1;
3318 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003319
Paul Elliottc9774412023-02-06 15:14:07 +00003320 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003321}
3322
3323
3324psa_status_t psa_sign_hash_complete(
3325 psa_sign_hash_interruptible_operation_t *operation,
3326 uint8_t *signature, size_t signature_size,
3327 size_t *signature_length)
3328{
3329 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3330
3331 *signature_length = 0;
3332
Paul Elliottc9774412023-02-06 15:14:07 +00003333 /* Check that start has been called first, and that operation has not
3334 * previously errored. */
3335 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003336 status = PSA_ERROR_BAD_STATE;
3337 goto exit;
3338 }
3339
Paul Elliott096abc42023-02-03 18:33:23 +00003340 /* Immediately reject a zero-length signature buffer. This guarantees that
3341 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003342 if (signature_size == 0) {
3343 status = PSA_ERROR_BUFFER_TOO_SMALL;
3344 goto exit;
3345 }
3346
3347 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3348 signature_size,
3349 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003350
Paul Elliott296ede92022-12-15 17:00:30 +00003351 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003352 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003353
Paul Elliottebe225c2023-02-10 14:32:53 +00003354exit:
3355
Paul Elliott59200a22023-02-21 15:07:40 +00003356 psa_wipe_tag_output_buffer(signature, status, signature_size,
3357 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003358
Paul Elliott53bb3122023-02-10 14:22:22 +00003359 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003360 if (status != PSA_SUCCESS) {
3361 operation->error_occurred = 1;
3362 }
3363
Paul Elliott59ad9452022-12-18 15:09:02 +00003364 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003365 }
3366
3367 return status;
3368}
3369
3370psa_status_t psa_sign_hash_abort(
3371 psa_sign_hash_interruptible_operation_t *operation)
3372{
Paul Elliott59ad9452022-12-18 15:09:02 +00003373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3374
3375 status = psa_sign_hash_abort_internal(operation);
3376
3377 /* We clear the number of ops done here, so that it is not cleared when
3378 * the operation fails or succeeds, only on manual abort. */
3379 operation->num_ops = 0;
3380
Paul Elliottc9774412023-02-06 15:14:07 +00003381 /* Likewise, failure state. */
3382 operation->error_occurred = 0;
3383
Paul Elliott59ad9452022-12-18 15:09:02 +00003384 return status;
3385}
3386
3387static psa_status_t psa_verify_hash_abort_internal(
3388 psa_verify_hash_interruptible_operation_t *operation)
3389{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003390 if (operation->id == 0) {
3391 /* The object has (apparently) been initialized but it is not (yet)
3392 * in use. It's ok to call abort on such an object, and there's
3393 * nothing to do. */
3394 return PSA_SUCCESS;
3395 }
3396
Paul Elliott59ad9452022-12-18 15:09:02 +00003397 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3398
3399 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003400
3401 operation->id = 0;
3402
Paul Elliotte6145dc2023-02-07 12:51:21 +00003403 /* Do not clear either the error_occurred or num_ops elements here as they
3404 * only want to be cleared by the application calling abort, not by abort
3405 * being called at completion of an operation. */
3406
Paul Elliott59ad9452022-12-18 15:09:02 +00003407 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003408}
3409
3410psa_status_t psa_verify_hash_start(
3411 psa_verify_hash_interruptible_operation_t *operation,
3412 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3413 const uint8_t *hash, size_t hash_length,
3414 const uint8_t *signature, size_t signature_length)
3415{
3416 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3417 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3418 psa_key_slot_t *slot;
3419
Paul Elliottc9774412023-02-06 15:14:07 +00003420 /* Check that start has not been previously called, or operation has not
3421 * previously errored. */
3422 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003423 return PSA_ERROR_BAD_STATE;
3424 }
3425
3426 status = psa_sign_verify_check_alg(0, alg);
3427 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003428 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003429 return status;
3430 }
3431
3432 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3433 PSA_KEY_USAGE_VERIFY_HASH,
3434 alg);
3435
3436 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003437 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003438 return status;
3439 }
3440
3441 psa_key_attributes_t attributes = {
3442 .core = slot->attr
3443 };
3444
Paul Elliott296ede92022-12-15 17:00:30 +00003445 /* Ensure ops count gets reset, in case of operation re-use. */
3446 operation->num_ops = 0;
3447
Paul Elliott9fe12f62022-11-30 19:16:02 +00003448 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3449 slot->key.data,
3450 slot->key.bytes,
3451 alg, hash, hash_length,
3452 signature, signature_length);
3453
3454 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003455 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003456 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003457 }
3458
3459 unlock_status = psa_unlock_key_slot(slot);
3460
Paul Elliottc9774412023-02-06 15:14:07 +00003461 if (unlock_status != PSA_SUCCESS) {
3462 operation->error_occurred = 1;
3463 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003464
Paul Elliottc9774412023-02-06 15:14:07 +00003465 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003466}
3467
3468psa_status_t psa_verify_hash_complete(
3469 psa_verify_hash_interruptible_operation_t *operation)
3470{
3471 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3472
Paul Elliottc9774412023-02-06 15:14:07 +00003473 /* Check that start has been called first, and that operation has not
3474 * previously errored. */
3475 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003476 status = PSA_ERROR_BAD_STATE;
3477 goto exit;
3478 }
3479
3480 status = psa_driver_wrapper_verify_hash_complete(operation);
3481
Paul Elliott296ede92022-12-15 17:00:30 +00003482 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003483 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003484 operation);
3485
Paul Elliottebe225c2023-02-10 14:32:53 +00003486exit:
3487
Paul Elliott9fe12f62022-11-30 19:16:02 +00003488 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003489 if (status != PSA_SUCCESS) {
3490 operation->error_occurred = 1;
3491 }
3492
Paul Elliott59ad9452022-12-18 15:09:02 +00003493 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003494 }
3495
3496 return status;
3497}
3498
3499psa_status_t psa_verify_hash_abort(
3500 psa_verify_hash_interruptible_operation_t *operation)
3501{
Paul Elliott59ad9452022-12-18 15:09:02 +00003502 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003503
Paul Elliott59ad9452022-12-18 15:09:02 +00003504 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003505
Paul Elliott59ad9452022-12-18 15:09:02 +00003506 /* We clear the number of ops done here, so that it is not cleared when
3507 * the operation fails or succeeds, only on manual abort. */
3508 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003509
Paul Elliottc9774412023-02-06 15:14:07 +00003510 /* Likewise, failure state. */
3511 operation->error_occurred = 0;
3512
Paul Elliott59ad9452022-12-18 15:09:02 +00003513 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003514}
3515
Paul Elliott588f8ed2022-12-02 18:10:26 +00003516/****************************************************************/
3517/* Asymmetric interruptible cryptography internal */
3518/* implementations */
3519/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003520
Paul Elliott588f8ed2022-12-02 18:10:26 +00003521void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3522{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003523
Paul Elliott588f8ed2022-12-02 18:10:26 +00003524#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3525 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3526 defined(MBEDTLS_ECP_RESTARTABLE)
3527
3528 /* Internal implementation uses zero to indicate infinite number max ops,
3529 * therefore avoid this value, and set to minimum possible. */
3530 if (max_ops == 0) {
3531 max_ops = 1;
3532 }
3533
Paul Elliott588f8ed2022-12-02 18:10:26 +00003534 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003535#else
3536 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003537#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3538 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3539 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3540}
3541
Paul Elliott588f8ed2022-12-02 18:10:26 +00003542uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003543 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003544{
3545#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3546 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3547 defined(MBEDTLS_ECP_RESTARTABLE)
3548
Paul Elliott93d9ca82023-02-15 18:14:21 +00003549 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003550#else
3551 (void) operation;
3552 return 0;
3553#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3554 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3555 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3556}
3557
3558uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003559 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003560{
3561 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3562 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3563 defined(MBEDTLS_ECP_RESTARTABLE)
3564
Paul Elliott93d9ca82023-02-15 18:14:21 +00003565 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003566#else
3567 (void) operation;
3568 return 0;
3569#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3570 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3571 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3572}
3573
3574psa_status_t mbedtls_psa_sign_hash_start(
3575 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3576 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3577 size_t key_buffer_size, psa_algorithm_t alg,
3578 const uint8_t *hash, size_t hash_length)
3579{
3580 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003581 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003582
Paul Elliott068fe072023-01-16 13:59:15 +00003583 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3584 return PSA_ERROR_NOT_SUPPORTED;
3585 }
3586
3587 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003588 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003589 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003590
3591#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003592 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3593 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003594
Paul Elliotta1c94092023-02-15 16:38:04 +00003595 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3596
Paul Elliott93d9ca82023-02-15 18:14:21 +00003597 /* Ensure num_ops is zero'ed in case of context re-use. */
3598 operation->num_ops = 0;
3599
Paul Elliott068fe072023-01-16 13:59:15 +00003600 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3601 attributes->core.bits,
3602 key_buffer,
3603 key_buffer_size,
3604 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003605
Paul Elliott068fe072023-01-16 13:59:15 +00003606 if (status != PSA_SUCCESS) {
3607 return status;
3608 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609
Paul Elliott1bc59df2023-02-05 13:41:57 +00003610 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003611 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003612
Paul Elliott068fe072023-01-16 13:59:15 +00003613 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3614 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3615 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003616
Paul Elliott0290a762023-02-09 14:30:24 +00003617 /* We only need to store the same length of hash as the private key size
3618 * here, it would be truncated by the internal implementation anyway. */
3619 required_hash_length = (hash_length < operation->coordinate_bytes ?
3620 hash_length : operation->coordinate_bytes);
3621
Paul Elliottba70ad42023-02-15 18:23:53 +00003622 if (required_hash_length > sizeof(operation->hash)) {
3623 /* Shouldn't happen, but better safe than sorry. */
3624 return PSA_ERROR_CORRUPTION_DETECTED;
3625 }
3626
Paul Elliott0290a762023-02-09 14:30:24 +00003627 memcpy(operation->hash, hash, required_hash_length);
3628 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003629
3630 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003631
3632#else
Paul Elliott068fe072023-01-16 13:59:15 +00003633 (void) operation;
3634 (void) key_buffer;
3635 (void) key_buffer_size;
3636 (void) alg;
3637 (void) hash;
3638 (void) hash_length;
3639 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003640 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641
Paul Elliott068fe072023-01-16 13:59:15 +00003642 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3644 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3645 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646}
3647
3648psa_status_t mbedtls_psa_sign_hash_complete(
3649 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3650 uint8_t *signature, size_t signature_size,
3651 size_t *signature_length)
3652{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003653#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3654 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3655 defined(MBEDTLS_ECP_RESTARTABLE)
3656
Paul Elliotta1c94092023-02-15 16:38:04 +00003657 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003658 mbedtls_mpi r;
3659 mbedtls_mpi s;
3660
Paul Elliott46845252023-02-03 14:59:11 +00003661 mbedtls_mpi_init(&r);
3662 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003663
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003664 /* Ensure max_ops is set to the current value (or default). */
3665 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3666
Paul Elliotta1c94092023-02-15 16:38:04 +00003667 if (signature_size < 2 * operation->coordinate_bytes) {
3668 status = PSA_ERROR_BUFFER_TOO_SMALL;
3669 goto exit;
3670 }
3671
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003673
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3675 status = mbedtls_to_psa_error(
3676 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003677 &r,
3678 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003679 &operation->ctx->d,
3680 operation->hash,
3681 operation->hash_length,
3682 operation->md_alg,
3683 mbedtls_psa_get_random,
3684 MBEDTLS_PSA_RANDOM_STATE,
3685 &operation->restart_ctx));
3686#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003687 status = PSA_ERROR_NOT_SUPPORTED;
3688 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3690 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003691 status = mbedtls_to_psa_error(
3692 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003693 &r,
3694 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003695 &operation->ctx->d,
3696 operation->hash,
3697 operation->hash_length,
3698 mbedtls_psa_get_random,
3699 MBEDTLS_PSA_RANDOM_STATE,
3700 mbedtls_psa_get_random,
3701 MBEDTLS_PSA_RANDOM_STATE,
3702 &operation->restart_ctx));
3703 }
3704
Paul Elliottf8e5b562023-02-19 18:43:45 +00003705 /* Hide the fact that the restart context only holds a delta of number of
3706 * ops done during the last operation, not an absolute value. */
3707 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3708
Paul Elliott724bd252023-02-08 12:35:08 +00003709 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003710 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003711 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003713 operation->coordinate_bytes)
3714 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715
3716 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003717 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718 }
3719
3720 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003721 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003723 operation->coordinate_bytes,
3724 operation->coordinate_bytes)
3725 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003726
3727 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003728 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729 }
3730
Paul Elliott1bc59df2023-02-05 13:41:57 +00003731 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003732
Paul Elliott724bd252023-02-08 12:35:08 +00003733 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734 }
Paul Elliott724bd252023-02-08 12:35:08 +00003735
3736exit:
3737
3738 mbedtls_mpi_free(&r);
3739 mbedtls_mpi_free(&s);
3740 return status;
3741
Paul Elliott588f8ed2022-12-02 18:10:26 +00003742 #else
3743
3744 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003745 (void) signature;
3746 (void) signature_size;
3747 (void) signature_length;
3748
3749 return PSA_ERROR_NOT_SUPPORTED;
3750
3751#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3752 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3753 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3754}
3755
3756psa_status_t mbedtls_psa_sign_hash_abort(
3757 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3758{
3759
3760#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3761 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3762 defined(MBEDTLS_ECP_RESTARTABLE)
3763
3764 if (operation->ctx) {
3765 mbedtls_ecdsa_free(operation->ctx);
3766 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003767 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003768 }
3769
3770 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3771
Paul Elliott93d9ca82023-02-15 18:14:21 +00003772 operation->num_ops = 0;
3773
Paul Elliott588f8ed2022-12-02 18:10:26 +00003774 return PSA_SUCCESS;
3775
3776#else
3777
3778 (void) operation;
3779
3780 return PSA_ERROR_NOT_SUPPORTED;
3781
3782#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3783 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3784 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3785}
3786
3787psa_status_t mbedtls_psa_verify_hash_start(
3788 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3789 const psa_key_attributes_t *attributes,
3790 const uint8_t *key_buffer, size_t key_buffer_size,
3791 psa_algorithm_t alg,
3792 const uint8_t *hash, size_t hash_length,
3793 const uint8_t *signature, size_t signature_length)
3794{
3795 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003796 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003797 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798
Paul Elliott068fe072023-01-16 13:59:15 +00003799 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3800 return PSA_ERROR_NOT_SUPPORTED;
3801 }
3802
3803 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003804 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003805 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003806
3807#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003808 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3809 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003810
Paul Elliotta1c94092023-02-15 16:38:04 +00003811 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3812 mbedtls_mpi_init(&operation->r);
3813 mbedtls_mpi_init(&operation->s);
3814
Paul Elliott93d9ca82023-02-15 18:14:21 +00003815 /* Ensure num_ops is zero'ed in case of context re-use. */
3816 operation->num_ops = 0;
3817
Paul Elliott068fe072023-01-16 13:59:15 +00003818 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3819 attributes->core.bits,
3820 key_buffer,
3821 key_buffer_size,
3822 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003823
Paul Elliott068fe072023-01-16 13:59:15 +00003824 if (status != PSA_SUCCESS) {
3825 return status;
3826 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003827
Paul Elliottc569fc22023-02-10 13:02:54 +00003828 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003829
Paul Elliott1bc59df2023-02-05 13:41:57 +00003830 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003831 return PSA_ERROR_INVALID_SIGNATURE;
3832 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003833
Paul Elliott068fe072023-01-16 13:59:15 +00003834 status = mbedtls_to_psa_error(
3835 mbedtls_mpi_read_binary(&operation->r,
3836 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003837 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003838
Paul Elliott068fe072023-01-16 13:59:15 +00003839 if (status != PSA_SUCCESS) {
3840 return status;
3841 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003842
Paul Elliott068fe072023-01-16 13:59:15 +00003843 status = mbedtls_to_psa_error(
3844 mbedtls_mpi_read_binary(&operation->s,
3845 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003846 coordinate_bytes,
3847 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003848
Paul Elliott068fe072023-01-16 13:59:15 +00003849 if (status != PSA_SUCCESS) {
3850 return status;
3851 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003852
Paul Elliott2c9843f2023-02-15 17:32:42 +00003853 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854
Paul Elliott2c9843f2023-02-15 17:32:42 +00003855 if (status != PSA_SUCCESS) {
3856 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003857 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003858
Paul Elliott0290a762023-02-09 14:30:24 +00003859 /* We only need to store the same length of hash as the private key size
3860 * here, it would be truncated by the internal implementation anyway. */
3861 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3862 coordinate_bytes);
3863
Paul Elliottba70ad42023-02-15 18:23:53 +00003864 if (required_hash_length > sizeof(operation->hash)) {
3865 /* Shouldn't happen, but better safe than sorry. */
3866 return PSA_ERROR_CORRUPTION_DETECTED;
3867 }
3868
Paul Elliott0290a762023-02-09 14:30:24 +00003869 memcpy(operation->hash, hash, required_hash_length);
3870 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003871
3872 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003873#else
Paul Elliott068fe072023-01-16 13:59:15 +00003874 (void) operation;
3875 (void) key_buffer;
3876 (void) key_buffer_size;
3877 (void) alg;
3878 (void) hash;
3879 (void) hash_length;
3880 (void) signature;
3881 (void) signature_length;
3882 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003883 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003884 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885
Paul Elliott068fe072023-01-16 13:59:15 +00003886 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003887#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3888 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3889 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003890}
3891
3892psa_status_t mbedtls_psa_verify_hash_complete(
3893 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3894{
3895
3896#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3897 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3898 defined(MBEDTLS_ECP_RESTARTABLE)
3899
Paul Elliottf8e5b562023-02-19 18:43:45 +00003900 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3901
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003902 /* Ensure max_ops is set to the current value (or default). */
3903 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3904
Paul Elliottf8e5b562023-02-19 18:43:45 +00003905 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003906 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3907 operation->hash,
3908 operation->hash_length,
3909 &operation->ctx->Q,
3910 &operation->r,
3911 &operation->s,
3912 &operation->restart_ctx));
3913
Paul Elliottf8e5b562023-02-19 18:43:45 +00003914 /* Hide the fact that the restart context only holds a delta of number of
3915 * ops done during the last operation, not an absolute value. */
3916 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3917
3918 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003919#else
3920 (void) operation;
3921
3922 return PSA_ERROR_NOT_SUPPORTED;
3923
3924#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3925 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3926 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3927}
3928
3929psa_status_t mbedtls_psa_verify_hash_abort(
3930 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3931{
3932
3933#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3934 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3935 defined(MBEDTLS_ECP_RESTARTABLE)
3936
3937 if (operation->ctx) {
3938 mbedtls_ecdsa_free(operation->ctx);
3939 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003940 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003941 }
3942
3943 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3944
Paul Elliott93d9ca82023-02-15 18:14:21 +00003945 operation->num_ops = 0;
3946
Paul Elliott588f8ed2022-12-02 18:10:26 +00003947 mbedtls_mpi_free(&operation->r);
3948 mbedtls_mpi_free(&operation->s);
3949
3950 return PSA_SUCCESS;
3951
3952#else
3953 (void) operation;
3954
3955 return PSA_ERROR_NOT_SUPPORTED;
3956
3957#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3958 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3959 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3960}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003961
mohammad1603503973b2018-03-12 15:59:30 +02003962/****************************************************************/
3963/* Symmetric cryptography */
3964/****************************************************************/
3965
Gilles Peskine449bd832023-01-11 14:50:10 +01003966static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3967 mbedtls_svc_key_id_t key,
3968 psa_algorithm_t alg,
3969 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003970{
3971 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3972 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003973 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3975 PSA_KEY_USAGE_ENCRYPT :
3976 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003977
3978 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003980 status = PSA_ERROR_BAD_STATE;
3981 goto exit;
3982 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003983
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003985 status = PSA_ERROR_INVALID_ARGUMENT;
3986 goto exit;
3987 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003988
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3990 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003991 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003993
Ronald Cron49fafa92021-03-10 08:34:23 +01003994 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003995 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003996 * so we only set it (in the driver wrapper) after resources have been
3997 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003998 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004000 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004002 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 }
4004 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004005
Ronald Crona4af55f2020-12-14 14:36:06 +01004006 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004008 };
4009
Ronald Cronab99ac22020-10-01 16:38:28 +02004010 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 if (cipher_operation == MBEDTLS_ENCRYPT) {
4012 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4013 &attributes,
4014 slot->key.data,
4015 slot->key.bytes,
4016 alg);
4017 } else {
4018 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4019 &attributes,
4020 slot->key.data,
4021 slot->key.bytes,
4022 alg);
4023 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004024
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004025exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 if (status != PSA_SUCCESS) {
4027 psa_cipher_abort(operation);
4028 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004033}
4034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4036 mbedtls_svc_key_id_t key,
4037 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004038{
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004040}
4041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4043 mbedtls_svc_key_id_t key,
4044 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004045{
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004047}
4048
Gilles Peskine449bd832023-01-11 14:50:10 +01004049psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4050 uint8_t *iv,
4051 size_t iv_size,
4052 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004053{
Ronald Cron6d051732020-10-01 14:10:20 +02004054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004055 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4056 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004057
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004059 status = PSA_ERROR_BAD_STATE;
4060 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004061 }
4062
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004064 status = PSA_ERROR_BAD_STATE;
4065 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004066 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004067
Ronald Cron2fb90522021-07-05 12:31:44 +02004068 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004070 status = PSA_ERROR_BUFFER_TOO_SMALL;
4071 goto exit;
4072 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004073
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004075 status = PSA_ERROR_GENERIC_ERROR;
4076 goto exit;
4077 }
4078
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 status = psa_generate_random(local_iv, default_iv_length);
4080 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004081 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 }
Ronald Cron5618a392021-03-26 09:52:26 +01004083
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 status = psa_driver_wrapper_cipher_set_iv(operation,
4085 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004086
4087exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 if (status == PSA_SUCCESS) {
4089 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004090 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004091 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004093 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004095 }
Ronald Cron6d051732020-10-01 14:10:20 +02004096
Gilles Peskine449bd832023-01-11 14:50:10 +01004097 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004098}
4099
Gilles Peskine449bd832023-01-11 14:50:10 +01004100psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4101 const uint8_t *iv,
4102 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004103{
Ronald Cron6d051732020-10-01 14:10:20 +02004104 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004105
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;
4109 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004110
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;
4114 }
Ronald Crona0d68172021-03-26 10:15:08 +01004115
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004117 status = PSA_ERROR_INVALID_ARGUMENT;
4118 goto exit;
4119 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004120
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 status = psa_driver_wrapper_cipher_set_iv(operation,
4122 iv,
4123 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004124
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004125exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004127 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 } else {
4129 psa_cipher_abort(operation);
4130 }
4131 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004132}
4133
Gilles Peskine449bd832023-01-11 14:50:10 +01004134psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4135 const uint8_t *input,
4136 size_t input_length,
4137 uint8_t *output,
4138 size_t output_size,
4139 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004140{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004144 status = PSA_ERROR_BAD_STATE;
4145 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004146 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004147
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004149 status = PSA_ERROR_BAD_STATE;
4150 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004151 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 status = psa_driver_wrapper_cipher_update(operation,
4154 input,
4155 input_length,
4156 output,
4157 output_size,
4158 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004159
4160exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 if (status != PSA_SUCCESS) {
4162 psa_cipher_abort(operation);
4163 }
Ronald Cron6d051732020-10-01 14:10:20 +02004164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004166}
4167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4169 uint8_t *output,
4170 size_t output_size,
4171 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004172{
David Saadab4ecc272019-02-14 13:48:10 +02004173 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004176 status = PSA_ERROR_BAD_STATE;
4177 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004178 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004181 status = PSA_ERROR_BAD_STATE;
4182 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004183 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 status = psa_driver_wrapper_cipher_finish(operation,
4186 output,
4187 output_size,
4188 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004189
4190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 if (status == PSA_SUCCESS) {
4192 return psa_cipher_abort(operation);
4193 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004194 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004198 }
mohammad1603503973b2018-03-12 15:59:30 +02004199}
4200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004202{
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004204 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004205 * in use. It's ok to call abort on such an object, and there's
4206 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004208 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004209
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004211
Ronald Cron49fafa92021-03-10 08:34:23 +01004212 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004213 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004214 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004217}
4218
Gilles Peskine449bd832023-01-11 14:50:10 +01004219psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4220 psa_algorithm_t alg,
4221 const uint8_t *input,
4222 size_t input_length,
4223 uint8_t *output,
4224 size_t output_size,
4225 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004226{
4227 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004228 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004229 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004230 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4231 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004234 status = PSA_ERROR_INVALID_ARGUMENT;
4235 goto exit;
4236 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004237
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4239 PSA_KEY_USAGE_ENCRYPT,
4240 alg);
4241 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004242 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004244
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004245 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004247 };
4248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4250 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004251 status = PSA_ERROR_GENERIC_ERROR;
4252 goto exit;
4253 }
4254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 if (default_iv_length > 0) {
4256 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004257 status = PSA_ERROR_BUFFER_TOO_SMALL;
4258 goto exit;
4259 }
4260
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 status = psa_generate_random(local_iv, default_iv_length);
4262 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004263 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004265 }
4266
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004267 status = psa_driver_wrapper_cipher_encrypt(
4268 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004269 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004270 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004272
4273exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 unlock_status = psa_unlock_key_slot(slot);
4275 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004276 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004277 }
Ronald Cron23919522021-07-08 19:00:07 +02004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 if (status == PSA_SUCCESS) {
4280 if (default_iv_length > 0) {
4281 memcpy(output, local_iv, default_iv_length);
4282 }
4283 *output_length += default_iv_length;
4284 } else {
4285 *output_length = 0;
4286 }
4287
4288 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004289}
4290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4292 psa_algorithm_t alg,
4293 const uint8_t *input,
4294 size_t input_length,
4295 uint8_t *output,
4296 size_t output_size,
4297 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004298{
4299 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004300 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004301 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004304 status = PSA_ERROR_INVALID_ARGUMENT;
4305 goto exit;
4306 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004307
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4309 PSA_KEY_USAGE_DECRYPT,
4310 alg);
4311 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004312 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004314
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004315 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004317 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004318
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4320 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004321 status = PSA_ERROR_INVALID_ARGUMENT;
4322 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004324 status = PSA_ERROR_INVALID_ARGUMENT;
4325 goto exit;
4326 }
4327
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004328 status = psa_driver_wrapper_cipher_decrypt(
4329 &attributes, slot->key.data, slot->key.bytes,
4330 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004332
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004333exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 unlock_status = psa_unlock_key_slot(slot);
4335 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004336 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004340 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 }
Ronald Cron23919522021-07-08 19:00:07 +02004342
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004344}
4345
4346
mohammad16035955c982018-04-26 00:53:03 +03004347/****************************************************************/
4348/* AEAD */
4349/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004350
Paul Elliottbaff51c2021-09-28 17:44:45 +01004351/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004352static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004353{
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004355}
4356
4357/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004358static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4359 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004360{
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004364#if defined(PSA_WANT_ALG_GCM)
4365 case PSA_ALG_GCM:
4366 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 * arbitrarily large nonces. Please note that we do not generally
4368 * recommend the usage of nonces of greater length than
4369 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4370 * size, which can then lead to collisions if you encrypt a very
4371 * large number of messages.*/
4372 if (nonce_length != 0) {
4373 return PSA_SUCCESS;
4374 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004375 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004376#endif /* PSA_WANT_ALG_GCM */
4377#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004378 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 if (nonce_length >= 7 && nonce_length <= 13) {
4380 return PSA_SUCCESS;
4381 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004382 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004383#endif /* PSA_WANT_ALG_CCM */
4384#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004385 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 if (nonce_length == 12) {
4387 return PSA_SUCCESS;
4388 } else if (nonce_length == 8) {
4389 return PSA_ERROR_NOT_SUPPORTED;
4390 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004391 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004392#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004393 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004394 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004396 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004399}
4400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004402{
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4404 return PSA_ERROR_INVALID_ARGUMENT;
4405 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004408}
4409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4411 psa_algorithm_t alg,
4412 const uint8_t *nonce,
4413 size_t nonce_length,
4414 const uint8_t *additional_data,
4415 size_t additional_data_length,
4416 const uint8_t *plaintext,
4417 size_t plaintext_length,
4418 uint8_t *ciphertext,
4419 size_t ciphertext_size,
4420 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004421{
Ronald Cron215633c2021-03-16 17:15:37 +01004422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4423 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004424
mohammad1603f08a5502018-06-03 15:05:47 +03004425 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 status = psa_aead_check_algorithm(alg);
4428 if (status != PSA_SUCCESS) {
4429 return status;
4430 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004431
Ronald Cron9a986162021-03-26 12:40:07 +01004432 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4434 if (status != PSA_SUCCESS) {
4435 return status;
4436 }
mohammad16035955c982018-04-26 00:53:03 +03004437
Ronald Cron215633c2021-03-16 17:15:37 +01004438 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004440 };
mohammad16035955c982018-04-26 00:53:03 +03004441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 status = psa_aead_check_nonce_length(alg, nonce_length);
4443 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004444 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004446
Ronald Cronde822812021-03-17 16:08:20 +01004447 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004448 &attributes, slot->key.data, slot->key.bytes,
4449 alg,
4450 nonce, nonce_length,
4451 additional_data, additional_data_length,
4452 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4456 memset(ciphertext, 0, ciphertext_size);
4457 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004458
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004459exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004463}
4464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4466 psa_algorithm_t alg,
4467 const uint8_t *nonce,
4468 size_t nonce_length,
4469 const uint8_t *additional_data,
4470 size_t additional_data_length,
4471 const uint8_t *ciphertext,
4472 size_t ciphertext_length,
4473 uint8_t *plaintext,
4474 size_t plaintext_size,
4475 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004476{
Ronald Cron215633c2021-03-16 17:15:37 +01004477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4478 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004479
Gilles Peskineee652a32018-06-01 19:23:52 +02004480 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 status = psa_aead_check_algorithm(alg);
4483 if (status != PSA_SUCCESS) {
4484 return status;
4485 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004486
Ronald Cron9a986162021-03-26 12:40:07 +01004487 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4489 if (status != PSA_SUCCESS) {
4490 return status;
4491 }
mohammad16035955c982018-04-26 00:53:03 +03004492
Ronald Cron215633c2021-03-16 17:15:37 +01004493 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004495 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004496
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 status = psa_aead_check_nonce_length(alg, nonce_length);
4498 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004499 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004501
Ronald Cronde822812021-03-17 16:08:20 +01004502 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004503 &attributes, slot->key.data, slot->key.bytes,
4504 alg,
4505 nonce, nonce_length,
4506 additional_data, additional_data_length,
4507 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 if (status != PSA_SUCCESS && plaintext_size != 0) {
4511 memset(plaintext, 0, plaintext_size);
4512 }
mohammad160360a64d02018-06-03 17:20:42 +03004513
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 return status;
mohammad16035955c982018-04-26 00:53:03 +03004518}
4519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4521{
4522 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004525#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004527 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4529 return PSA_ERROR_INVALID_ARGUMENT;
4530 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004531 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004532#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004533
Przemek Stekiel86679c72022-10-06 17:06:56 +02004534#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004536 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4538 return PSA_ERROR_INVALID_ARGUMENT;
4539 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004540 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004541#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004542
Przemek Stekiel86679c72022-10-06 17:06:56 +02004543#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004545 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 if (tag_len != 16) {
4547 return PSA_ERROR_INVALID_ARGUMENT;
4548 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004549 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004550#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004551
4552 default:
4553 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004555 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004557}
4558
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004559/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004560static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4561 int is_encrypt,
4562 mbedtls_svc_key_id_t key,
4563 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004564{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4566 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4567 psa_key_slot_t *slot = NULL;
4568 psa_key_usage_t key_usage = 0;
4569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 status = psa_aead_check_algorithm(alg);
4571 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004572 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004576 status = PSA_ERROR_BAD_STATE;
4577 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004578 }
4579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 if (operation->nonce_set || operation->lengths_set ||
4581 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004582 status = PSA_ERROR_BAD_STATE;
4583 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004584 }
4585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004587 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004589 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4593 alg);
4594 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004595 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004597
4598 psa_key_attributes_t attributes = {
4599 .core = slot->attr
4600 };
4601
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004605
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 if (is_encrypt) {
4607 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4608 &attributes,
4609 slot->key.data,
4610 slot->key.bytes,
4611 alg);
4612 } else {
4613 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4614 &attributes,
4615 slot->key.data,
4616 slot->key.bytes,
4617 alg);
4618 }
4619 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004620 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004624
4625exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004627
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004629 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004631 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 } else {
4633 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004634 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004635
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004637}
4638
Paul Elliott302ff6b2021-04-20 18:10:30 +01004639/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004640psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4641 mbedtls_svc_key_id_t key,
4642 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004643{
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004645}
4646
4647/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004648psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4649 mbedtls_svc_key_id_t key,
4650 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004651{
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004653}
4654
4655/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004656psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4657 uint8_t *nonce,
4658 size_t nonce_size,
4659 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004660{
4661 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004662 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004663 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004664
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004665 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004668 status = PSA_ERROR_BAD_STATE;
4669 goto exit;
4670 }
4671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004673 status = PSA_ERROR_BAD_STATE;
4674 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004675 }
4676
Paul Elliotte193ea82021-10-01 13:00:16 +01004677 /* For CCM, this size may not be correct according to the PSA
4678 * specification. The PSA Crypto 1.0.1 specification states:
4679 *
4680 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4681 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4682 *
4683 * However this restriction that L has to be the smallest integer is not
4684 * applied in practice, and it is not implementable here since the
4685 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4687 operation->alg);
4688 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004689 status = PSA_ERROR_BUFFER_TOO_SMALL;
4690 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004691 }
4692
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 status = psa_generate_random(local_nonce, required_nonce_size);
4694 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004695 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004699
Paul Elliott39dc6b82021-05-11 19:16:09 +01004700exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 if (status == PSA_SUCCESS) {
4702 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004703 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 } else {
4705 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004706 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004709}
4710
4711/* Set the nonce for a multipart authenticated encryption or decryption
4712 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004713psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4714 const uint8_t *nonce,
4715 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004716{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004717 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4718
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004720 status = PSA_ERROR_BAD_STATE;
4721 goto exit;
4722 }
4723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004725 status = PSA_ERROR_BAD_STATE;
4726 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004727 }
4728
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4730 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004731 status = PSA_ERROR_INVALID_ARGUMENT;
4732 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004733 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4736 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004737
Paul Elliott39dc6b82021-05-11 19:16:09 +01004738exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004740 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 } else {
4742 psa_aead_abort(operation);
4743 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004746}
4747
4748/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004749psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4750 size_t ad_length,
4751 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004752{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004753 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004756 status = PSA_ERROR_BAD_STATE;
4757 goto exit;
4758 }
4759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 if (operation->lengths_set || operation->ad_started ||
4761 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004762 status = PSA_ERROR_BAD_STATE;
4763 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004764 }
4765
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004767#if defined(PSA_WANT_ALG_GCM)
4768 case PSA_ALG_GCM:
4769 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 * bits. Without the guard this code will generate warnings on 32bit
4771 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004772#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 if (((uint64_t) ad_length) >> 61 != 0 ||
4774 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004775 status = PSA_ERROR_INVALID_ARGUMENT;
4776 goto exit;
4777 }
Paul Elliott325d3742021-09-27 17:56:28 +01004778#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004779 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004780#endif /* PSA_WANT_ALG_GCM */
4781#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004782 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004784 status = PSA_ERROR_INVALID_ARGUMENT;
4785 goto exit;
4786 }
4787 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004788#endif /* PSA_WANT_ALG_CCM */
4789#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004790 case PSA_ALG_CHACHA20_POLY1305:
4791 /* No length restrictions for ChaChaPoly. */
4792 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004793#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004794 default:
4795 break;
4796 }
Paul Elliott325d3742021-09-27 17:56:28 +01004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4799 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004800
Paul Elliott39dc6b82021-05-11 19:16:09 +01004801exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004803 operation->ad_remaining = ad_length;
4804 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004805 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 } else {
4807 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004808 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004809
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004811}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004812
4813/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004814psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4815 const uint8_t *input,
4816 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004817{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004818 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4819
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004821 status = PSA_ERROR_BAD_STATE;
4822 goto exit;
4823 }
4824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004826 status = PSA_ERROR_BAD_STATE;
4827 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004828 }
4829
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 if (operation->lengths_set) {
4831 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004832 status = PSA_ERROR_INVALID_ARGUMENT;
4833 goto exit;
4834 }
4835
4836 operation->ad_remaining -= input_length;
4837 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004838#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004840 status = PSA_ERROR_BAD_STATE;
4841 goto exit;
4842 }
4843#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004844
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 status = psa_driver_wrapper_aead_update_ad(operation, input,
4846 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004847
Paul Elliott39dc6b82021-05-11 19:16:09 +01004848exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004850 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 } else {
4852 psa_aead_abort(operation);
4853 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004854
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004856}
4857
4858/* Encrypt or decrypt a message fragment in an active multipart AEAD
4859 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004860psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4861 const uint8_t *input,
4862 size_t input_length,
4863 uint8_t *output,
4864 size_t output_size,
4865 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004866{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004868
4869 *output_length = 0;
4870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004872 status = PSA_ERROR_BAD_STATE;
4873 goto exit;
4874 }
4875
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004877 status = PSA_ERROR_BAD_STATE;
4878 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004879 }
4880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004882 /* Additional data length was supplied, but not all the additional
4883 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004885 status = PSA_ERROR_INVALID_ARGUMENT;
4886 goto exit;
4887 }
4888
4889 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004891 status = PSA_ERROR_INVALID_ARGUMENT;
4892 goto exit;
4893 }
4894
4895 operation->body_remaining -= input_length;
4896 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004897#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004899 status = PSA_ERROR_BAD_STATE;
4900 goto exit;
4901 }
4902#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004903
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4905 output, output_size,
4906 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004907
Paul Elliott39dc6b82021-05-11 19:16:09 +01004908exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004910 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 } else {
4912 psa_aead_abort(operation);
4913 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004916}
4917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004919{
Gilles Peskine449bd832023-01-11 14:50:10 +01004920 if (operation->id == 0 || !operation->nonce_set) {
4921 return PSA_ERROR_BAD_STATE;
4922 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4925 operation->body_remaining != 0)) {
4926 return PSA_ERROR_INVALID_ARGUMENT;
4927 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004930}
4931
Paul Elliott302ff6b2021-04-20 18:10:30 +01004932/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004933psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4934 uint8_t *ciphertext,
4935 size_t ciphertext_size,
4936 size_t *ciphertext_length,
4937 uint8_t *tag,
4938 size_t tag_size,
4939 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004940{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4942
Paul Elliott302ff6b2021-04-20 18:10:30 +01004943 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004944 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004945
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 status = psa_aead_final_checks(operation);
4947 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004948 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004952 status = PSA_ERROR_BAD_STATE;
4953 goto exit;
4954 }
4955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4957 ciphertext_size,
4958 ciphertext_length,
4959 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004960
Paul Elliott39dc6b82021-05-11 19:16:09 +01004961exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004962
4963
Paul Elliottf47b0952021-05-21 18:02:33 +01004964 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004965 * the zero tag size, make sure the tag is set to something implausible.
4966 * Even if the operation succeeds, make sure we clear the rest of the
4967 * buffer to prevent potential leakage of anything previously placed in
4968 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004969 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004970
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004972
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004974}
4975
4976/* Finish authenticating and decrypting a message in a multipart AEAD
4977 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004978psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4979 uint8_t *plaintext,
4980 size_t plaintext_size,
4981 size_t *plaintext_length,
4982 const uint8_t *tag,
4983 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004985 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4986
Paul Elliott302ff6b2021-04-20 18:10:30 +01004987 *plaintext_length = 0;
4988
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 status = psa_aead_final_checks(operation);
4990 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004991 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004993
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004995 status = PSA_ERROR_BAD_STATE;
4996 goto exit;
4997 }
4998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5000 plaintext_size,
5001 plaintext_length,
5002 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005003
5004exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005006
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005008}
5009
5010/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005011psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005012{
5013 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5014
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005016 /* The object has (apparently) been initialized but it is not (yet)
5017 * in use. It's ok to call abort on such an object, and there's
5018 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005020 }
5021
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005025
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005027}
5028
Gilles Peskinee59236f2018-01-27 23:32:46 +01005029/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005030/* Generators */
5031/****************************************************************/
5032
Przemek Stekiel69c46792022-06-10 12:59:51 +02005033#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005034 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005035 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5036 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005037#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005038#endif /* At least one builtin KDF */
5039
Przemek Stekiel69c46792022-06-10 12:59:51 +02005040#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005041 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5042 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5043static psa_status_t psa_key_derivation_start_hmac(
5044 psa_mac_operation_t *operation,
5045 psa_algorithm_t hash_alg,
5046 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005048{
5049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5050 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5052 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5053 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005054
Steven Cooreman72f736a2021-05-07 14:14:37 +02005055 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 status = psa_driver_wrapper_mac_sign_setup(operation,
5059 &attributes,
5060 hmac_key, hmac_key_length,
5061 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005062
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 psa_reset_key_attributes(&attributes);
5064 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005065}
5066#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005067
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005068#define HKDF_STATE_INIT 0 /* no input yet */
5069#define HKDF_STATE_STARTED 1 /* got salt */
5070#define HKDF_STATE_KEYED 2 /* got key */
5071#define HKDF_STATE_OUTPUT 3 /* output started */
5072
Gilles Peskinecbe66502019-05-16 16:59:18 +02005073static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005075{
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5077 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5078 } else {
5079 return operation->alg;
5080 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005081}
5082
Gilles Peskine449bd832023-01-11 14:50:10 +01005083psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005084{
5085 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5087 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005088 /* The object has (apparently) been initialized but it is not
5089 * in use. It's ok to call abort on such an object, and there's
5090 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005092#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5094 mbedtls_free(operation->ctx.hkdf.info);
5095 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5096 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005097#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005098#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5099 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5101 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5102 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5103 if (operation->ctx.tls12_prf.secret != NULL) {
5104 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5105 operation->ctx.tls12_prf.secret_length);
5106 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005107 }
5108
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 if (operation->ctx.tls12_prf.seed != NULL) {
5110 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5111 operation->ctx.tls12_prf.seed_length);
5112 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005113 }
5114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 if (operation->ctx.tls12_prf.label != NULL) {
5116 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5117 operation->ctx.tls12_prf.label_length);
5118 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005119 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005120#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 if (operation->ctx.tls12_prf.other_secret != NULL) {
5122 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5123 operation->ctx.tls12_prf.other_secret_length);
5124 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005125 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005126#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005127 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005128
5129 /* We leave the fields Ai and output_block to be erased safely by the
5130 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005132#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5133 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005134#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5136 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5137 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5138 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005139#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005140 {
5141 status = PSA_ERROR_BAD_STATE;
5142 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 mbedtls_platform_zeroize(operation, sizeof(*operation));
5144 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005145}
5146
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005147psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005149{
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005153 }
5154
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005157}
5158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5160 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005161{
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 if (operation->alg == 0) {
5163 return PSA_ERROR_BAD_STATE;
5164 }
5165 if (capacity > operation->capacity) {
5166 return PSA_ERROR_INVALID_ARGUMENT;
5167 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005170}
5171
Przemek Stekiel69c46792022-06-10 12:59:51 +02005172#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005173/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005174static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5175 psa_algorithm_t kdf_alg,
5176 uint8_t *output,
5177 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005178{
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5180 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005181 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005182 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005183#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005185#else
5186 const uint8_t last_block = 0xff;
5187#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005188
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 if (hkdf->state < HKDF_STATE_KEYED ||
5190 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005191#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005193#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 )) {
5195 return PSA_ERROR_BAD_STATE;
5196 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005197 hkdf->state = HKDF_STATE_OUTPUT;
5198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005200 /* Copy what remains of the current block */
5201 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005203 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 }
5205 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005206 output += n;
5207 output_length -= n;
5208 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005210 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005212 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005213 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005215 * object was corrupted or if this function is called directly
5216 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 if (hkdf->block_number == last_block) {
5218 return PSA_ERROR_BAD_STATE;
5219 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005220
5221 /* We need a new block */
5222 ++hkdf->block_number;
5223 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005224
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5226 hash_alg,
5227 hkdf->prk,
5228 hash_length);
5229 if (status != PSA_SUCCESS) {
5230 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005231 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005232
5233 if (hkdf->block_number != 1) {
5234 status = psa_mac_update(&hkdf->hmac,
5235 hkdf->output_block,
5236 hash_length);
5237 if (status != PSA_SUCCESS) {
5238 return status;
5239 }
5240 }
5241 status = psa_mac_update(&hkdf->hmac,
5242 hkdf->info,
5243 hkdf->info_length);
5244 if (status != PSA_SUCCESS) {
5245 return status;
5246 }
5247 status = psa_mac_update(&hkdf->hmac,
5248 &hkdf->block_number, 1);
5249 if (status != PSA_SUCCESS) {
5250 return status;
5251 }
5252 status = psa_mac_sign_finish(&hkdf->hmac,
5253 hkdf->output_block,
5254 sizeof(hkdf->output_block),
5255 &hmac_output_length);
5256 if (status != PSA_SUCCESS) {
5257 return status;
5258 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005259 }
5260
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005262}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005263#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005264
John Durkop07cc04a2020-11-16 22:08:34 -08005265#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5266 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005267static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5268 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005270{
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5272 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005273 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5274 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005275 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005276
Janos Follath7742fee2019-06-17 12:58:10 +01005277 /* We can't be wanting more output after block 0xff, otherwise
5278 * the capacity check in psa_key_derivation_output_bytes() would have
5279 * prevented this call. It could happen only if the operation
5280 * object was corrupted or if this function is called directly
5281 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 if (tls12_prf->block_number == 0xff) {
5283 return PSA_ERROR_CORRUPTION_DETECTED;
5284 }
Janos Follath7742fee2019-06-17 12:58:10 +01005285
5286 /* We need a new block */
5287 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005288 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005289
5290 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5291 *
5292 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5293 *
5294 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5295 * HMAC_hash(secret, A(2) + seed) +
5296 * HMAC_hash(secret, A(3) + seed) + ...
5297 *
5298 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005299 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005300 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005301 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005302 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005303 * is currently extracted as `output_block` and where i is
5304 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005305 */
5306
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 status = psa_key_derivation_start_hmac(&hmac,
5308 hash_alg,
5309 tls12_prf->secret,
5310 tls12_prf->secret_length);
5311 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005312 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005314
5315 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005317 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5318 * the variable seed and in this instance means it in the context of the
5319 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 status = psa_mac_update(&hmac,
5321 tls12_prf->label,
5322 tls12_prf->label_length);
5323 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005324 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 }
5326 status = psa_mac_update(&hmac,
5327 tls12_prf->seed,
5328 tls12_prf->seed_length);
5329 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005330 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 }
5332 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005333 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5335 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005336 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005338 }
5339
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 status = psa_mac_sign_finish(&hmac,
5341 tls12_prf->Ai, hash_length,
5342 &hmac_output_length);
5343 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005344 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 }
5346 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005347 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005349
5350 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 status = psa_key_derivation_start_hmac(&hmac,
5352 hash_alg,
5353 tls12_prf->secret,
5354 tls12_prf->secret_length);
5355 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005356 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 }
5358 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5359 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005360 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 }
5362 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5363 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005364 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 }
5366 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5367 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005368 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 }
5370 status = psa_mac_sign_finish(&hmac,
5371 tls12_prf->output_block, hash_length,
5372 &hmac_output_length);
5373 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005374 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005376
Janos Follath7742fee2019-06-17 12:58:10 +01005377
5378cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 cleanup_status = psa_mac_abort(&hmac);
5380 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005381 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005383
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005385}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005386
Janos Follath844eb0e2019-06-19 12:10:49 +01005387static psa_status_t psa_key_derivation_tls12_prf_read(
5388 psa_tls12_prf_key_derivation_t *tls12_prf,
5389 psa_algorithm_t alg,
5390 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005392{
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5394 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005395 psa_status_t status;
5396 uint8_t offset, length;
5397
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005399 case PSA_TLS12_PRF_STATE_LABEL_SET:
5400 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5401 break;
5402 case PSA_TLS12_PRF_STATE_OUTPUT:
5403 break;
5404 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005406 }
5407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005409 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 if (tls12_prf->left_in_block == 0) {
5411 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5412 alg);
5413 if (status != PSA_SUCCESS) {
5414 return status;
5415 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005416
5417 continue;
5418 }
5419
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005421 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005423 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005425
5426 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005428 output += length;
5429 output_length -= length;
5430 tls12_prf->left_in_block -= length;
5431 }
5432
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005434}
John Durkop07cc04a2020-11-16 22:08:34 -08005435#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5436 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005437
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005438#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5439static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5440 psa_tls12_ecjpake_to_pms_t *ecjpake,
5441 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005443{
5444 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005445 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 if (output_length != 32) {
5448 return PSA_ERROR_INVALID_ARGUMENT;
5449 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5452 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5453 &output_size);
5454 if (status != PSA_SUCCESS) {
5455 return status;
5456 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 if (output_size != output_length) {
5459 return PSA_ERROR_GENERIC_ERROR;
5460 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005461
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005463}
5464#endif
5465
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005466psa_status_t psa_key_derivation_output_bytes(
5467 psa_key_derivation_operation_t *operation,
5468 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005470{
5471 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005475 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005477 }
5478
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005480 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005481 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005482 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005483 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005484 goto exit;
5485 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005487 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005488 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005489 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5490 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005491 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005492 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005494 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005495 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005496
Przemek Stekiel69c46792022-06-10 12:59:51 +02005497#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5499 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5500 output, output_length);
5501 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005502#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005503#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5504 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5506 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5507 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5508 kdf_alg, output,
5509 output_length);
5510 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005511#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5512 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005513#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005515 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5517 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005518#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5519
Gilles Peskineeab56e42018-07-12 17:12:33 +02005520 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005521 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005523 }
5524
5525exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005527 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005528 * This allows us to differentiate between exhausted operations and
5529 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5530 * operations. */
5531 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005533 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005535 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005537}
5538
David Brown7807bf72021-02-09 16:28:23 -07005539#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005540static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005541{
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 if (data_size >= 8) {
5543 mbedtls_des_key_set_parity(data);
5544 }
5545 if (data_size >= 16) {
5546 mbedtls_des_key_set_parity(data + 8);
5547 }
5548 if (data_size >= 24) {
5549 mbedtls_des_key_set_parity(data + 16);
5550 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005551}
David Brown7807bf72021-02-09 16:28:23 -07005552#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005553
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005554/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 * ECC keys on a Weierstrass elliptic curve require the generation
5556 * of a private key which is an integer
5557 * in the range [1, N - 1], where N is the boundary of the private key domain:
5558 * N is the prime p for Diffie-Hellman, or the order of the
5559 * curve’s base point for ECC.
5560 *
5561 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5562 * This function generates the private key using the following process:
5563 *
5564 * 1. Draw a byte string of length ceiling(m/8) bytes.
5565 * 2. If m is not a multiple of 8, set the most significant
5566 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5567 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5568 * 4. If k > N - 2, discard the result and return to step 1.
5569 * 5. Output k + 1 as the private key.
5570 *
5571 * This method allows compliance to NIST standards, specifically the methods titled
5572 * Key-Pair Generation by Testing Candidates in the following publications:
5573 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5574 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5575 * Diffie-Hellman keys.
5576 *
5577 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5578 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5579 *
5580 * Note: Function allocates memory for *data buffer, so given *data should be
5581 * always NULL.
5582 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005583#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5584 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005585 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5586 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5587 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005588static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005589 psa_key_slot_t *slot,
5590 size_t bits,
5591 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005592 uint8_t **data
5593 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005594{
Valerio Setti52789862023-04-07 12:13:11 +02005595#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005596 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005597 mbedtls_mpi k;
5598 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 mbedtls_mpi_init(&k);
5603 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005604
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005605 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005607 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005609
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005611 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005612 goto cleanup;
5613 }
5614
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005615 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005617
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005619
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005620 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005621 /* Let m be the bit size of N. */
5622 size_t m = ecp_group.nbits;
5623
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005625
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005626 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005628
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005629 /* Note: This function is always called with *data == NULL and it
5630 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 *data = mbedtls_calloc(1, m_bytes);
5632 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005633 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005634 goto cleanup;
5635 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005636
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005638 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005640 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005642
5643 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005645 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005646 * (8 * ceiling(m/8) - m) bits of the first byte in
5647 * the string to zero.
5648 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005650 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005651 }
5652
5653 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 * big-endian byte string.
5655 */
5656 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005657
5658 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 * Result of comparison is returned. When it indicates error
5660 * then this function is called again.
5661 */
5662 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005663 }
5664
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005665 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5667 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005668cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 if (ret != 0) {
5670 status = mbedtls_to_psa_error(ret);
5671 }
5672 if (status != PSA_SUCCESS) {
5673 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005674 *data = NULL;
5675 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 mbedtls_mpi_free(&k);
5677 mbedtls_mpi_free(&diff_N_2);
5678 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005679#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005680 (void) slot;
5681 (void) bits;
5682 (void) operation;
5683 (void) data;
5684 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005685#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005686}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005687
5688/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5689 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5690 *
5691 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5692 * draw a 32-byte string and process it as specified in
5693 * Elliptic Curves for Security [RFC7748] §5.
5694 *
5695 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5696 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5697 *
5698 * Note: Function allocates memory for *data buffer, so given *data should be
5699 * always NULL.
5700 */
5701
5702static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5703 size_t bits,
5704 psa_key_derivation_operation_t *operation,
5705 uint8_t **data
5706 )
5707{
5708 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005709 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005712 case 255:
5713 output_length = 32;
5714 break;
5715 case 448:
5716 output_length = 56;
5717 break;
5718 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005720 break;
5721 }
5722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 if (*data == NULL) {
5726 return PSA_ERROR_INSUFFICIENT_MEMORY;
5727 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005732 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005736 case 255:
5737 (*data)[0] &= 248;
5738 (*data)[31] &= 127;
5739 (*data)[31] |= 64;
5740 break;
5741 case 448:
5742 (*data)[0] &= 252;
5743 (*data)[55] |= 128;
5744 break;
5745 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005747 break;
5748 }
5749
5750 return status;
5751}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005752#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5753 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005754 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5755 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5756 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005757
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005758static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005759 psa_key_slot_t *slot,
5760 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005762{
5763 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305765 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005766 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5769 return PSA_ERROR_INVALID_ARGUMENT;
5770 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005771
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005772#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5773 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005774 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5775 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5776 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5778 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5779 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005780 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5782 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005783 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 }
5785 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005786 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5788 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005789 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005791 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005792 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005793#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5794 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005795 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5796 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5797 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 if (key_type_is_raw_bytes(slot->attr.type)) {
5799 if (bits % 8 != 0) {
5800 return PSA_ERROR_INVALID_ARGUMENT;
5801 }
5802 data = mbedtls_calloc(1, bytes);
5803 if (data == NULL) {
5804 return PSA_ERROR_INSUFFICIENT_MEMORY;
5805 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 status = psa_key_derivation_output_bytes(operation, data, bytes);
5808 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005809 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 }
David Brown12ca5032021-02-11 11:02:00 -07005811#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5813 psa_des_set_key_parity(data, bytes);
5814 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005815#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 } else {
5817 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005818 }
Ronald Crondd04d422020-11-28 15:14:42 +01005819
Ronald Cronbf33c932020-11-28 18:06:53 +01005820 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005821 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005823 };
5824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5826 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5827 &storage_size);
5828 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305829 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 }
Archanad8a83dc2021-06-14 10:04:16 +05305831 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 status = psa_allocate_buffer_to_slot(slot, storage_size);
5833 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305834 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 }
Archanad8a83dc2021-06-14 10:04:16 +05305836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 status = psa_driver_wrapper_import_key(&attributes,
5838 data, bytes,
5839 slot->key.data,
5840 slot->key.bytes,
5841 &slot->key.bytes, &bits);
5842 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005843 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005845
5846exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 mbedtls_free(data);
5848 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005849}
5850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5852 psa_key_derivation_operation_t *operation,
5853 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005854{
5855 psa_status_t status;
5856 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005857 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005858
Ronald Cron81709fc2020-11-14 12:10:32 +01005859 *key = MBEDTLS_SVC_KEY_ID_INIT;
5860
Gilles Peskine0f84d622019-09-12 19:03:13 +02005861 /* Reject any attempt to create a zero-length key so that we don't
5862 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 if (psa_get_key_bits(attributes) == 0) {
5864 return PSA_ERROR_INVALID_ARGUMENT;
5865 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 if (operation->alg == PSA_ALG_NONE) {
5868 return PSA_ERROR_BAD_STATE;
5869 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 if (!operation->can_output_key) {
5872 return PSA_ERROR_NOT_PERMITTED;
5873 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5876 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005877#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005879 /* Deriving a key in a secure element is not implemented yet. */
5880 status = PSA_ERROR_NOT_SUPPORTED;
5881 }
5882#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 if (status == PSA_SUCCESS) {
5884 status = psa_generate_derived_key_internal(slot,
5885 attributes->core.bits,
5886 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005887 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 if (status == PSA_SUCCESS) {
5889 status = psa_finish_key_creation(slot, driver, key);
5890 }
5891 if (status != PSA_SUCCESS) {
5892 psa_fail_key_creation(slot, driver);
5893 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005896}
5897
Gilles Peskineeab56e42018-07-12 17:12:33 +02005898
5899
5900/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005901/* Key derivation */
5902/****************************************************************/
5903
Steven Cooreman932ffb72021-02-15 12:14:32 +01005904#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005905static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005906{
5907#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5909 return 1;
5910 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005911#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005912#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5914 return 1;
5915 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005916#endif
5917#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5919 return 1;
5920 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005921#endif
5922#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5924 return 1;
5925 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005926#endif
5927#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5929 return 1;
5930 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005931#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005932#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5934 return 1;
5935 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005936#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005938}
5939
Gilles Peskine449bd832023-01-11 14:50:10 +01005940static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005941{
5942 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 psa_status_t status = psa_hash_setup(&operation, alg);
5944 psa_hash_abort(&operation);
5945 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005946}
5947
Gilles Peskine969c5d62019-01-16 15:53:06 +01005948static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005949 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005951{
Janos Follath5fe19732019-06-20 15:09:30 +01005952 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5953 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005955
Gilles Peskine969c5d62019-01-16 15:53:06 +01005956 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 if (!is_kdf_alg_supported(kdf_alg)) {
5958 return PSA_ERROR_NOT_SUPPORTED;
5959 }
John Durkop07cc04a2020-11-16 22:08:34 -08005960
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005961 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005962 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5964 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5965 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5966 if (hash_size == 0) {
5967 return PSA_ERROR_NOT_SUPPORTED;
5968 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005969
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005970 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5971 * we might fail later, which is somewhat unfriendly and potentially
5972 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 psa_status_t status = psa_hash_try_support(hash_alg);
5974 if (status != PSA_SUCCESS) {
5975 return status;
5976 }
5977 } else {
5978 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005979 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5982 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5983 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5984 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005985 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005986#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005987 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5989 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005990 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005992#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5993 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 operation->capacity = 255 * hash_size;
5995 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005996}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005997
Gilles Peskine449bd832023-01-11 14:50:10 +01005998static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005999{
6000#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 if (alg == PSA_ALG_ECDH) {
6002 return PSA_SUCCESS;
6003 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006004#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006005#if defined(PSA_WANT_ALG_FFDH)
6006 if (alg == PSA_ALG_FFDH) {
6007 return PSA_SUCCESS;
6008 }
6009#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006010 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006012}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006013
6014static int psa_key_derivation_allows_free_form_secret_input(
6015 psa_algorithm_t kdf_alg)
6016{
6017#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6018 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6019 return 0;
6020 }
6021#endif
6022 (void) kdf_alg;
6023 return 1;
6024}
John Durkop07cc04a2020-11-16 22:08:34 -08006025#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6028 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006029{
6030 psa_status_t status;
6031
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 if (operation->alg != 0) {
6033 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006034 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6037 return PSA_ERROR_INVALID_ARGUMENT;
6038 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6039#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6040 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6041 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6042 status = psa_key_agreement_try_support(ka_alg);
6043 if (status != PSA_SUCCESS) {
6044 return status;
6045 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006046 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6047 return PSA_ERROR_INVALID_ARGUMENT;
6048 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6050#else
6051 return PSA_ERROR_NOT_SUPPORTED;
6052#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6053 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6054#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6055 status = psa_key_derivation_setup_kdf(operation, alg);
6056#else
6057 return PSA_ERROR_NOT_SUPPORTED;
6058#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6059 } else {
6060 return PSA_ERROR_INVALID_ARGUMENT;
6061 }
6062
6063 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006064 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 }
6066 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006067}
6068
Przemek Stekiel69c46792022-06-10 12:59:51 +02006069#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006070static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6071 psa_algorithm_t kdf_alg,
6072 psa_key_derivation_step_t step,
6073 const uint8_t *data,
6074 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006075{
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006077 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006079 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006080#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6082 return PSA_ERROR_INVALID_ARGUMENT;
6083 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006084#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 if (hkdf->state != HKDF_STATE_INIT) {
6086 return PSA_ERROR_BAD_STATE;
6087 } else {
6088 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6089 hash_alg,
6090 data, data_length);
6091 if (status != PSA_SUCCESS) {
6092 return status;
6093 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006094 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006096 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006097 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006098#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006100 /* We shouldn't be in different state as HKDF_EXPAND only allows
6101 * two inputs: SECRET (this case) and INFO which does not modify
6102 * the state. It could happen only if the hkdf
6103 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 if (hkdf->state != HKDF_STATE_INIT) {
6105 return PSA_ERROR_BAD_STATE;
6106 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006107
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006108 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6110 return PSA_ERROR_INVALID_ARGUMENT;
6111 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 memcpy(hkdf->prk, data, data_length);
6114 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006115#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006116 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006117 /* HKDF: If no salt was provided, use an empty salt.
6118 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006120#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6122 return PSA_ERROR_BAD_STATE;
6123 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006124#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6126 hash_alg,
6127 NULL, 0);
6128 if (status != PSA_SUCCESS) {
6129 return status;
6130 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006131 hkdf->state = HKDF_STATE_STARTED;
6132 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 if (hkdf->state != HKDF_STATE_STARTED) {
6134 return PSA_ERROR_BAD_STATE;
6135 }
6136 status = psa_mac_update(&hkdf->hmac,
6137 data, data_length);
6138 if (status != PSA_SUCCESS) {
6139 return status;
6140 }
6141 status = psa_mac_sign_finish(&hkdf->hmac,
6142 hkdf->prk,
6143 sizeof(hkdf->prk),
6144 &data_length);
6145 if (status != PSA_SUCCESS) {
6146 return status;
6147 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006148 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006149
Gilles Peskine2b522db2019-04-12 15:11:49 +02006150 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006151 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006152#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006154 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006156 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006158#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006159 {
6160 /* Block 0 is empty, and the next block will be
6161 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006163 }
6164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006166 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006167#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6169 return PSA_ERROR_INVALID_ARGUMENT;
6170 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006171#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006172#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6174 hkdf->state == HKDF_STATE_INIT) {
6175 return PSA_ERROR_BAD_STATE;
6176 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006177#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 if (hkdf->state == HKDF_STATE_OUTPUT) {
6179 return PSA_ERROR_BAD_STATE;
6180 }
6181 if (hkdf->info_set) {
6182 return PSA_ERROR_BAD_STATE;
6183 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006184 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 if (data_length != 0) {
6186 hkdf->info = mbedtls_calloc(1, data_length);
6187 if (hkdf->info == NULL) {
6188 return PSA_ERROR_INSUFFICIENT_MEMORY;
6189 }
6190 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006191 }
6192 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006194 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006196 }
6197}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006198#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006199
John Durkop07cc04a2020-11-16 22:08:34 -08006200#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6201 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006202static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6203 const uint8_t *data,
6204 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006205{
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6207 return PSA_ERROR_BAD_STATE;
6208 }
Janos Follathf08e2652019-06-13 09:05:41 +01006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 if (data_length != 0) {
6211 prf->seed = mbedtls_calloc(1, data_length);
6212 if (prf->seed == NULL) {
6213 return PSA_ERROR_INSUFFICIENT_MEMORY;
6214 }
Janos Follathf08e2652019-06-13 09:05:41 +01006215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006217 prf->seed_length = data_length;
6218 }
Janos Follathf08e2652019-06-13 09:05:41 +01006219
Gilles Peskine43f958b2020-12-13 14:55:14 +01006220 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006223}
6224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6226 const uint8_t *data,
6227 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006228{
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6230 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6231 return PSA_ERROR_BAD_STATE;
6232 }
Janos Follath81550542019-06-13 14:26:34 +01006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 if (data_length != 0) {
6235 prf->secret = mbedtls_calloc(1, data_length);
6236 if (prf->secret == NULL) {
6237 return PSA_ERROR_INSUFFICIENT_MEMORY;
6238 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006239
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006241 prf->secret_length = data_length;
6242 }
Janos Follath81550542019-06-13 14:26:34 +01006243
Gilles Peskine43f958b2020-12-13 14:55:14 +01006244 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006247}
6248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6250 const uint8_t *data,
6251 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006252{
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6254 return PSA_ERROR_BAD_STATE;
6255 }
Janos Follath63028dd2019-06-13 09:15:47 +01006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 if (data_length != 0) {
6258 prf->label = mbedtls_calloc(1, data_length);
6259 if (prf->label == NULL) {
6260 return PSA_ERROR_INSUFFICIENT_MEMORY;
6261 }
Janos Follath63028dd2019-06-13 09:15:47 +01006262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006264 prf->label_length = data_length;
6265 }
Janos Follath63028dd2019-06-13 09:15:47 +01006266
Gilles Peskine43f958b2020-12-13 14:55:14 +01006267 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006268
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006270}
6271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6273 psa_key_derivation_step_t step,
6274 const uint8_t *data,
6275 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006276{
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006278 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006280 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006282 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006284 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006286 }
6287}
John Durkop07cc04a2020-11-16 22:08:34 -08006288#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6289 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6290
6291#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6292static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6293 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006294 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006296{
6297 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6299 4 + data_length + prf->other_secret_length :
6300 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6303 return PSA_ERROR_INVALID_ARGUMENT;
6304 }
John Durkop07cc04a2020-11-16 22:08:34 -08006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 uint8_t *pms = mbedtls_calloc(1, pms_len);
6307 if (pms == NULL) {
6308 return PSA_ERROR_INSUFFICIENT_MEMORY;
6309 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006310 uint8_t *cur = pms;
6311
6312 /* pure-PSK:
6313 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006314 *
6315 * The premaster secret is formed as follows: if the PSK is N octets
6316 * long, concatenate a uint16 with the value N, N zero octets, a second
6317 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006318 *
6319 * mixed-PSK:
6320 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6321 * follows: concatenate a uint16 with the length of the other secret,
6322 * the other secret itself, uint16 with the length of PSK, and the
6323 * PSK itself.
6324 * For details please check:
6325 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6326 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6327 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006328 */
6329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6331 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6332 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6333 if (prf->other_secret_length != 0) {
6334 memcpy(cur, prf->other_secret, prf->other_secret_length);
6335 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006336 cur += prf->other_secret_length;
6337 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 } else {
6339 *cur++ = MBEDTLS_BYTE_1(data_length);
6340 *cur++ = MBEDTLS_BYTE_0(data_length);
6341 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006342 cur += data_length;
6343 }
6344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 *cur++ = MBEDTLS_BYTE_1(data_length);
6346 *cur++ = MBEDTLS_BYTE_0(data_length);
6347 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006348 cur += data_length;
6349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 mbedtls_platform_zeroize(pms, pms_len);
6353 mbedtls_free(pms);
6354 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006355}
Janos Follath6660f0e2019-06-17 08:44:03 +01006356
Przemek Stekiele47201b2022-04-19 13:53:28 +02006357static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6358 psa_tls12_prf_key_derivation_t *prf,
6359 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006361{
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6363 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006364 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006365
6366 if (data_length != 0) {
6367 prf->other_secret = mbedtls_calloc(1, data_length);
6368 if (prf->other_secret == NULL) {
6369 return PSA_ERROR_INSUFFICIENT_MEMORY;
6370 }
6371
6372 memcpy(prf->other_secret, data, data_length);
6373 prf->other_secret_length = data_length;
6374 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006375 prf->other_secret_length = 0;
6376 }
6377
6378 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006381}
6382
Janos Follath6660f0e2019-06-17 08:44:03 +01006383static psa_status_t psa_tls12_prf_psk_to_ms_input(
6384 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006385 psa_key_derivation_step_t step,
6386 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006388{
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006390 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 return psa_tls12_prf_psk_to_ms_set_key(prf,
6392 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006393 break;
6394 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6396 data,
6397 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006398 break;
6399 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006401 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006402
Przemek Stekiele47201b2022-04-19 13:53:28 +02006403 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006404}
John Durkop07cc04a2020-11-16 22:08:34 -08006405#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006406
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006407#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6408static psa_status_t psa_tls12_ecjpake_to_pms_input(
6409 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006410 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006411 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006413{
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6415 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6416 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006417 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006418
6419 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 if (data[0] != 0x04) {
6421 return PSA_ERROR_INVALID_ARGUMENT;
6422 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006423
6424 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006428}
6429#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006430/** Check whether the given key type is acceptable for the given
6431 * input step of a key derivation.
6432 *
6433 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6434 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6435 * Both secret and non-secret inputs can alternatively have the type
6436 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6437 * that the input was passed as a buffer rather than via a key object.
6438 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006439static int psa_key_derivation_check_input_type(
6440 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006442{
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006444 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 if (key_type == PSA_KEY_TYPE_DERIVE) {
6446 return PSA_SUCCESS;
6447 }
6448 if (key_type == PSA_KEY_TYPE_NONE) {
6449 return PSA_SUCCESS;
6450 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006451 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006452 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 if (key_type == PSA_KEY_TYPE_DERIVE) {
6454 return PSA_SUCCESS;
6455 }
6456 if (key_type == PSA_KEY_TYPE_NONE) {
6457 return PSA_SUCCESS;
6458 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006459 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006460 case PSA_KEY_DERIVATION_INPUT_LABEL:
6461 case PSA_KEY_DERIVATION_INPUT_SALT:
6462 case PSA_KEY_DERIVATION_INPUT_INFO:
6463 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6465 return PSA_SUCCESS;
6466 }
6467 if (key_type == PSA_KEY_TYPE_NONE) {
6468 return PSA_SUCCESS;
6469 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006470 break;
6471 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006473}
6474
Janos Follathb80a94e2019-06-12 15:54:46 +01006475static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006476 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006477 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006478 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006479 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006481{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006482 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006484
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 status = psa_key_derivation_check_input_type(step, key_type);
6486 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006487 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006489
Przemek Stekiel69c46792022-06-10 12:59:51 +02006490#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6492 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6493 step, data, data_length);
6494 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006495#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006496#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6498 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6499 step, data, data_length);
6500 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006501#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6502#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6504 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6505 step, data, data_length);
6506 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006507#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006508#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006510 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6512 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006513#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006514 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006515 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006516 (void) data;
6517 (void) data_length;
6518 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006520 }
6521
Gilles Peskine224b0d62019-09-23 18:13:17 +02006522exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 if (status != PSA_SUCCESS) {
6524 psa_key_derivation_abort(operation);
6525 }
6526 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006527}
6528
Janos Follath51f4a0f2019-06-14 11:35:55 +01006529psa_status_t psa_key_derivation_input_bytes(
6530 psa_key_derivation_operation_t *operation,
6531 psa_key_derivation_step_t step,
6532 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006534{
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 return psa_key_derivation_input_internal(operation, step,
6536 PSA_KEY_TYPE_NONE,
6537 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006538}
6539
Janos Follath51f4a0f2019-06-14 11:35:55 +01006540psa_status_t psa_key_derivation_input_key(
6541 psa_key_derivation_operation_t *operation,
6542 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006544{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006545 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006546 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006547 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006548
Ronald Cron5c522922020-11-14 16:35:34 +01006549 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6551 if (status != PSA_SUCCESS) {
6552 psa_key_derivation_abort(operation);
6553 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006554 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006555
6556 /* Passing a key object as a SECRET input unlocks the permission
6557 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006559 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006561
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 status = psa_key_derivation_input_internal(operation,
6563 step, slot->attr.type,
6564 slot->key.data,
6565 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006566
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006568
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006570}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006571
6572
6573
6574/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006575/* Key agreement */
6576/****************************************************************/
6577
Gilles Peskine449bd832023-01-11 14:50:10 +01006578psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6579 const uint8_t *key_buffer,
6580 size_t key_buffer_size,
6581 psa_algorithm_t alg,
6582 const uint8_t *peer_key,
6583 size_t peer_key_length,
6584 uint8_t *shared_secret,
6585 size_t shared_secret_size,
6586 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006587{
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006589#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006590 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6592 key_buffer_size, alg,
6593 peer_key, peer_key_length,
6594 shared_secret,
6595 shared_secret_size,
6596 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006597#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006598
6599#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6600 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006601 return mbedtls_psa_key_agreement_ffdh(attributes,
6602 peer_key,
6603 peer_key_length,
6604 key_buffer,
6605 key_buffer_size,
6606 shared_secret,
6607 shared_secret_size,
6608 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006609#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6610
Gilles Peskine01d718c2018-09-18 12:01:02 +02006611 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006612 (void) attributes;
6613 (void) key_buffer;
6614 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006615 (void) peer_key;
6616 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006617 (void) shared_secret;
6618 (void) shared_secret_size;
6619 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006621 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006622}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006623
Aditya Deshpande17845b82022-10-13 17:21:01 +01006624/** Internal function for raw key agreement
6625 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006626 * to the driver's implementation if a driver is present.
6627 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006628 * (psa_key_agreement_raw_builtin).
6629 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006630static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6631 psa_key_slot_t *private_key,
6632 const uint8_t *peer_key,
6633 size_t peer_key_length,
6634 uint8_t *shared_secret,
6635 size_t shared_secret_size,
6636 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006637{
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6639 return PSA_ERROR_NOT_SUPPORTED;
6640 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006641
6642 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006644 };
6645
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 return psa_driver_wrapper_key_agreement(&attributes,
6647 private_key->key.data,
6648 private_key->key.bytes, alg,
6649 peer_key, peer_key_length,
6650 shared_secret,
6651 shared_secret_size,
6652 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006653}
6654
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006655/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006656 * to potentially free embedded data structures and wipe confidential data.
6657 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006658static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6659 psa_key_derivation_step_t step,
6660 psa_key_slot_t *private_key,
6661 const uint8_t *peer_key,
6662 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006663{
6664 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006665 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006666 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006668
6669 /* Step 1: run the secret agreement algorithm to generate the shared
6670 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 status = psa_key_agreement_raw_internal(ka_alg,
6672 private_key,
6673 peer_key, peer_key_length,
6674 shared_secret,
6675 sizeof(shared_secret),
6676 &shared_secret_length);
6677 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006678 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006679 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006680
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006681 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006682 * the shared secret. A shared secret is permitted wherever a key
6683 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 status = psa_key_derivation_input_internal(operation, step,
6685 PSA_KEY_TYPE_DERIVE,
6686 shared_secret,
6687 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006688exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6690 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006691}
6692
Gilles Peskine449bd832023-01-11 14:50:10 +01006693psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6694 psa_key_derivation_step_t step,
6695 mbedtls_svc_key_id_t private_key,
6696 const uint8_t *peer_key,
6697 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006698{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006699 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006700 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006701 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006702
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6704 return PSA_ERROR_INVALID_ARGUMENT;
6705 }
Ronald Cron5c522922020-11-14 16:35:34 +01006706 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6708 if (status != PSA_SUCCESS) {
6709 return status;
6710 }
6711 status = psa_key_agreement_internal(operation, step,
6712 slot,
6713 peer_key, peer_key_length);
6714 if (status != PSA_SUCCESS) {
6715 psa_key_derivation_abort(operation);
6716 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006717 /* If a private key has been added as SECRET, we allow the derived
6718 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006720 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006722 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006723
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006725
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006727}
6728
Gilles Peskine449bd832023-01-11 14:50:10 +01006729psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6730 mbedtls_svc_key_id_t private_key,
6731 const uint8_t *peer_key,
6732 size_t peer_key_length,
6733 uint8_t *output,
6734 size_t output_size,
6735 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006736{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006737 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006738 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006739 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006740
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006742 status = PSA_ERROR_INVALID_ARGUMENT;
6743 goto exit;
6744 }
Ronald Cron5c522922020-11-14 16:35:34 +01006745 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6747 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006748 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006750
Gilles Peskine3e561302022-04-14 00:17:15 +02006751 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6752 * for the output size. The PSA specification only guarantees that this
6753 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6754 * but it might be nice to allow smaller buffers if the output fits.
6755 * At the time of writing this comment, with only ECDH implemented,
6756 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6757 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6758 * be exact for it as well. */
6759 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6761 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006762 status = PSA_ERROR_BUFFER_TOO_SMALL;
6763 goto exit;
6764 }
6765
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 status = psa_key_agreement_raw_internal(alg, slot,
6767 peer_key, peer_key_length,
6768 output, output_size,
6769 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006770
6771exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006772 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006773 /* If an error happens and is not handled properly, the output
6774 * may be used as a key to protect sensitive data. Arrange for such
6775 * a key to be random, which is likely to result in decryption or
6776 * verification errors. This is better than filling the buffer with
6777 * some constant data such as zeros, which would result in the data
6778 * being protected with a reproducible, easily knowable key.
6779 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006780 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006781 *output_length = output_size;
6782 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006783
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006785
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006787}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006788
6789
Gilles Peskine30524eb2020-11-13 17:02:26 +01006790
Gilles Peskine86a440b2018-11-12 18:39:40 +01006791/****************************************************************/
6792/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006793/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006794
Gilles Peskine30524eb2020-11-13 17:02:26 +01006795/** Initialize the PSA random generator.
6796 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006797static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006798{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006799#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006801#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6802
Gilles Peskine30524eb2020-11-13 17:02:26 +01006803 /* Set default configuration if
6804 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006806 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 }
6808 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006809 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006810 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006811
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006813#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6814 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6815 /* The PSA entropy injection feature depends on using NV seed as an entropy
6816 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 mbedtls_entropy_add_source(&rng->entropy,
6818 mbedtls_nv_seed_poll, NULL,
6819 MBEDTLS_ENTROPY_BLOCK_SIZE,
6820 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006821#endif
6822
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006824#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006825}
6826
6827/** Deinitialize the PSA random generator.
6828 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006829static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006830{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006831#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006833#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6835 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006836#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006837}
6838
6839/** Seed the PSA random generator.
6840 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006841static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006842{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006843#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6844 /* Do nothing: the external RNG seeds itself. */
6845 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006847#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006848 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6850 drbg_seed, sizeof(drbg_seed) - 1);
6851 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006852#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006853}
6854
Gilles Peskine449bd832023-01-11 14:50:10 +01006855psa_status_t psa_generate_random(uint8_t *output,
6856 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006857{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006858 GUARD_MODULE_INITIALIZED;
6859
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006860#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6861
6862 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6864 output, output_size,
6865 &output_length);
6866 if (status != PSA_SUCCESS) {
6867 return status;
6868 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006869 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006870 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 if (output_length != output_size) {
6872 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6873 }
6874 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006875
6876#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6877
Gilles Peskine449bd832023-01-11 14:50:10 +01006878 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006879 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6881 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6882 output_size);
6883 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6884 output, request_size);
6885 if (ret != 0) {
6886 return mbedtls_to_psa_error(ret);
6887 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006888 output_size -= request_size;
6889 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006890 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006892#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006893}
6894
Gilles Peskine8814fc42020-12-14 15:33:44 +01006895/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006896 *
6897 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6898 * `psa_generate_random(...)`. The state parameter is ignored since the
6899 * PSA API doesn't support passing an explicit state.
6900 *
6901 * In the non-external case, psa_generate_random() calls an
6902 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6903 * and semantics as mbedtls_psa_get_random(). As an optimization,
6904 * instead of doing this back-and-forth between the PSA API and the
6905 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6906 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006908#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6909int mbedtls_psa_get_random(void *p_rng,
6910 unsigned char *output,
6911 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006912{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006913 /* This function takes a pointer to the RNG state because that's what
6914 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6915 * its own state internally and doesn't let the caller access that state.
6916 * So we just ignore the state parameter, and in practice we'll pass
6917 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006918 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006919 psa_status_t status = psa_generate_random(output, output_size);
6920 if (status == PSA_SUCCESS) {
6921 return 0;
6922 } else {
6923 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6924 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006925}
6926#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6927
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006928#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006929#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006930
Gilles Peskine449bd832023-01-11 14:50:10 +01006931psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6932 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006933{
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 if (global_data.initialized) {
6935 return PSA_ERROR_NOT_PERMITTED;
6936 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006937
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6939 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6940 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6941 return PSA_ERROR_INVALID_ARGUMENT;
6942 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006943
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006945}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006946#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006947
Ronald Cron3772afe2021-02-08 16:10:05 +01006948/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006949 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006950 * \param type The key type
6951 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006952 *
6953 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006954 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006955 * \retval #PSA_ERROR_INVALID_ARGUMENT
6956 * The size in bits of the key is not valid.
6957 * \retval #PSA_ERROR_NOT_SUPPORTED
6958 * The type and/or the size in bits of the key or the combination of
6959 * the two is not supported.
6960 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006961static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006963{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 if (key_type_is_raw_bytes(type)) {
6967 status = psa_validate_unstructured_key_bit_size(type, bits);
6968 if (status != PSA_SUCCESS) {
6969 return status;
6970 }
6971 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006972#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6974 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6975 return PSA_ERROR_NOT_SUPPORTED;
6976 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006977
Ronald Cron01b2aba2020-10-05 09:42:02 +02006978 /* Accept only byte-aligned keys, for the same reasons as
6979 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 if (bits % 8 != 0) {
6981 return PSA_ERROR_NOT_SUPPORTED;
6982 }
6983 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006984#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006985
Ronald Cron5c4d3862020-12-07 11:07:24 +01006986#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006988 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 return PSA_SUCCESS;
6990 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006991#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01006992
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02006993#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01006994 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02006995 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01006996 return PSA_ERROR_NOT_SUPPORTED;
6997 }
6998 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02006999#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007000 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007002 }
7003
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007005}
7006
Ronald Cron55ed0592020-10-05 10:30:40 +02007007psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007008 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007010{
7011 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007012 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007013
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 if ((attributes->domain_parameters == NULL) &&
7015 (attributes->domain_parameters_size != 0)) {
7016 return PSA_ERROR_INVALID_ARGUMENT;
7017 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007018
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 if (key_type_is_raw_bytes(type)) {
7020 status = psa_generate_random(key_buffer, key_buffer_size);
7021 if (status != PSA_SUCCESS) {
7022 return status;
7023 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007024
David Brown12ca5032021-02-11 11:02:00 -07007025#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 if (type == PSA_KEY_TYPE_DES) {
7027 psa_des_set_key_parity(key_buffer, key_buffer_size);
7028 }
David Brown8107e312021-02-12 08:08:46 -07007029#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007030 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007031
Jaeden Amero424fa932021-05-14 08:34:32 +01007032#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7033 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7035 return mbedtls_psa_rsa_generate_key(attributes,
7036 key_buffer,
7037 key_buffer_size,
7038 key_buffer_length);
7039 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007040#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7041 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007042
John Durkop9814fa22020-11-04 12:28:15 -08007043#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7045 return mbedtls_psa_ecp_generate_key(attributes,
7046 key_buffer,
7047 key_buffer_size,
7048 key_buffer_length);
7049 } else
John Durkop9814fa22020-11-04 12:28:15 -08007050#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007051
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007052#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007053 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7054 return mbedtls_psa_ffdh_generate_key(attributes,
7055 key_buffer,
7056 key_buffer_size,
7057 key_buffer_length);
7058 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007059#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007060 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 (void) key_buffer_length;
7062 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007063 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007064
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007066}
Darryl Green0c6575a2018-11-07 16:05:30 +00007067
Gilles Peskine449bd832023-01-11 14:50:10 +01007068psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7069 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007070{
7071 psa_status_t status;
7072 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007073 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007074 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007075
Ronald Cron81709fc2020-11-14 12:10:32 +01007076 *key = MBEDTLS_SVC_KEY_ID_INIT;
7077
Gilles Peskine0f84d622019-09-12 19:03:13 +02007078 /* Reject any attempt to create a zero-length key so that we don't
7079 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 if (psa_get_key_bits(attributes) == 0) {
7081 return PSA_ERROR_INVALID_ARGUMENT;
7082 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007083
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007084 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7086 return PSA_ERROR_INVALID_ARGUMENT;
7087 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7090 &slot, &driver);
7091 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 }
Gilles Peskine11792082019-08-06 18:36:36 +02007094
Ronald Cron977c2472020-10-13 08:32:21 +02007095 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307096 * storage ( thus not in the case of generating a key in a secure element
7097 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7098 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 if (slot->key.data == NULL) {
7100 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7101 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007102 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 attributes->core.type, attributes->core.bits);
7104 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007105 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007107
7108 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 attributes->core.type,
7110 attributes->core.bits);
7111 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007112 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 attributes, &key_buffer_size);
7114 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 }
Ronald Cron977c2472020-10-13 08:32:21 +02007117 }
Ronald Cron977c2472020-10-13 08:32:21 +02007118
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7120 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007122 }
Ronald Cron977c2472020-10-13 08:32:21 +02007123 }
7124
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 status = psa_driver_wrapper_generate_key(attributes,
7126 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007127
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 if (status != PSA_SUCCESS) {
7129 psa_remove_key_data_from_memory(slot);
7130 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007131
Gilles Peskine11792082019-08-06 18:36:36 +02007132exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007133 if (status == PSA_SUCCESS) {
7134 status = psa_finish_key_creation(slot, driver, key);
7135 }
7136 if (status != PSA_SUCCESS) {
7137 psa_fail_key_creation(slot, driver);
7138 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007139
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007141}
7142
Gilles Peskinee59236f2018-01-27 23:32:46 +01007143/****************************************************************/
7144/* Module setup */
7145/****************************************************************/
7146
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007147#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007148psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 void (* entropy_init)(mbedtls_entropy_context *ctx),
7150 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007151{
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7153 return PSA_ERROR_BAD_STATE;
7154 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007155 global_data.rng.entropy_init = entropy_init;
7156 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007158}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007159#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007160
Gilles Peskine449bd832023-01-11 14:50:10 +01007161void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007162{
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 psa_wipe_all_key_slots();
7164 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7165 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007166 }
7167 /* Wipe all remaining data, including configuration.
7168 * In particular, this sets all state indicator to the value
7169 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007171
7172 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007174}
7175
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007176#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7177/** Recover a transaction that was interrupted by a power failure.
7178 *
7179 * This function is called during initialization, before psa_crypto_init()
7180 * returns. If this function returns a failure status, the initialization
7181 * fails.
7182 */
7183static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007185{
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007187 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7188 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 /* TODO - fall through to the failure case until this
7190 * is implemented.
7191 * https://github.com/ARMmbed/mbed-crypto/issues/218
7192 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007193 default:
7194 /* We found an unsupported transaction in the storage.
7195 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007197 }
7198}
7199#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7200
Gilles Peskine449bd832023-01-11 14:50:10 +01007201psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007202{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007203 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007204
Gilles Peskinec6b69072018-11-20 21:42:52 +01007205 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007206 if (global_data.initialized != 0) {
7207 return PSA_SUCCESS;
7208 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007209
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007210 /* Init drivers */
7211 status = psa_driver_wrapper_init();
7212 if (status != PSA_SUCCESS) {
7213 goto exit;
7214 }
7215 global_data.drivers_initialized = 1;
7216
Gilles Peskine30524eb2020-11-13 17:02:26 +01007217 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007219 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 status = mbedtls_psa_random_seed(&global_data.rng);
7221 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007222 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007224 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007225
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 status = psa_initialize_key_slots();
7227 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007228 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007230
Gilles Peskine4b734222019-07-24 15:56:31 +02007231#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 status = psa_crypto_load_transaction();
7233 if (status == PSA_SUCCESS) {
7234 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7235 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 }
7238 status = psa_crypto_stop_transaction();
7239 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007240 /* There's no transaction to complete. It's all good. */
7241 status = PSA_SUCCESS;
7242 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007243#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007244
Gilles Peskinec6b69072018-11-20 21:42:52 +01007245 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007246 global_data.initialized = 1;
7247
7248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 if (status != PSA_SUCCESS) {
7250 mbedtls_psa_crypto_free();
7251 }
7252 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007253}
7254
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007255psa_status_t psa_crypto_driver_pake_get_password_len(
7256 const psa_crypto_driver_pake_inputs_t *inputs,
7257 size_t *password_len)
7258{
7259 if (inputs->password_len == 0) {
7260 return PSA_ERROR_BAD_STATE;
7261 }
7262
7263 *password_len = inputs->password_len;
7264
7265 return PSA_SUCCESS;
7266}
7267
7268psa_status_t psa_crypto_driver_pake_get_password(
7269 const psa_crypto_driver_pake_inputs_t *inputs,
7270 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7271{
7272 if (inputs->password_len == 0) {
7273 return PSA_ERROR_BAD_STATE;
7274 }
7275
7276 if (buffer_size < inputs->password_len) {
7277 return PSA_ERROR_BUFFER_TOO_SMALL;
7278 }
7279
7280 memcpy(buffer, inputs->password, inputs->password_len);
7281 *buffer_length = inputs->password_len;
7282
7283 return PSA_SUCCESS;
7284}
7285
7286psa_status_t psa_crypto_driver_pake_get_role(
7287 const psa_crypto_driver_pake_inputs_t *inputs,
7288 psa_pake_role_t *role)
7289{
7290 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7291 return PSA_ERROR_BAD_STATE;
7292 }
7293
7294 *role = inputs->role;
7295
7296 return PSA_SUCCESS;
7297}
7298
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007299psa_status_t psa_crypto_driver_pake_get_user_len(
7300 const psa_crypto_driver_pake_inputs_t *inputs,
7301 size_t *user_len)
7302{
7303 if (inputs->user_len == 0) {
7304 return PSA_ERROR_BAD_STATE;
7305 }
7306
7307 *user_len = inputs->user_len;
7308
7309 return PSA_SUCCESS;
7310}
7311
7312psa_status_t psa_crypto_driver_pake_get_user(
7313 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007314 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007315{
7316 if (inputs->user_len == 0) {
7317 return PSA_ERROR_BAD_STATE;
7318 }
7319
Przemek Stekielc0e62502023-03-14 11:49:36 +01007320 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007321 return PSA_ERROR_BUFFER_TOO_SMALL;
7322 }
7323
Przemek Stekielc0e62502023-03-14 11:49:36 +01007324 memcpy(user_id, inputs->user, inputs->user_len);
7325 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007326
7327 return PSA_SUCCESS;
7328}
7329
7330psa_status_t psa_crypto_driver_pake_get_peer_len(
7331 const psa_crypto_driver_pake_inputs_t *inputs,
7332 size_t *peer_len)
7333{
7334 if (inputs->peer_len == 0) {
7335 return PSA_ERROR_BAD_STATE;
7336 }
7337
7338 *peer_len = inputs->peer_len;
7339
7340 return PSA_SUCCESS;
7341}
7342
7343psa_status_t psa_crypto_driver_pake_get_peer(
7344 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007345 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007346{
7347 if (inputs->peer_len == 0) {
7348 return PSA_ERROR_BAD_STATE;
7349 }
7350
Przemek Stekielc0e62502023-03-14 11:49:36 +01007351 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007352 return PSA_ERROR_BUFFER_TOO_SMALL;
7353 }
7354
Przemek Stekielc0e62502023-03-14 11:49:36 +01007355 memcpy(peer_id, inputs->peer, inputs->peer_len);
7356 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007357
7358 return PSA_SUCCESS;
7359}
7360
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007361psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7362 const psa_crypto_driver_pake_inputs_t *inputs,
7363 psa_pake_cipher_suite_t *cipher_suite)
7364{
7365 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7366 return PSA_ERROR_BAD_STATE;
7367 }
7368
7369 *cipher_suite = inputs->cipher_suite;
7370
7371 return PSA_SUCCESS;
7372}
7373
Neil Armstronga7d08c32022-06-01 18:21:20 +02007374psa_status_t psa_pake_setup(
7375 psa_pake_operation_t *operation,
7376 const psa_pake_cipher_suite_t *cipher_suite)
7377{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007378 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007379
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007380 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007381 status = PSA_ERROR_BAD_STATE;
7382 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007383 }
7384
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007385 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007386 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007387 status = PSA_ERROR_INVALID_ARGUMENT;
7388 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007389 }
7390
Przemek Stekiel51eac532022-12-07 11:04:51 +01007391 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7392
Przemek Stekiele12ed362022-12-21 12:54:46 +01007393 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007394 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7395 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007396 operation->data.inputs.cipher_suite = *cipher_suite;
7397
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007398#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007399 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007400 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007401 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007402
Przemek Stekiele12ed362022-12-21 12:54:46 +01007403 computation_stage->state = PSA_PAKE_STATE_SETUP;
7404 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7405 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7406 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007407 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007408#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007409 {
7410 status = PSA_ERROR_NOT_SUPPORTED;
7411 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007412 }
7413
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007414 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7415
Przemek Stekiel51eac532022-12-07 11:04:51 +01007416 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007417exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007418 psa_pake_abort(operation);
7419 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007420}
7421
7422psa_status_t psa_pake_set_password_key(
7423 psa_pake_operation_t *operation,
7424 mbedtls_svc_key_id_t password)
7425{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007427 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7428 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007429
Przemek Stekiel51eac532022-12-07 11:04:51 +01007430 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007431 status = PSA_ERROR_BAD_STATE;
7432 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007433 }
7434
Przemek Stekiel6c764412022-11-22 14:05:12 +01007435 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7436 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007437 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007438 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007439 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007440 }
7441
Przemek Stekiel6c764412022-11-22 14:05:12 +01007442 psa_key_attributes_t attributes = {
7443 .core = slot->attr
7444 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007445
Przemek Stekiel51eac532022-12-07 11:04:51 +01007446 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007447
Przemek Stekiel51eac532022-12-07 11:04:51 +01007448 if (type != PSA_KEY_TYPE_PASSWORD &&
7449 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7450 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007451 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007452 }
7453
Przemek Stekiel51eac532022-12-07 11:04:51 +01007454 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7455 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007456 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007457 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007458 }
7459
7460 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7461 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007462 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007463exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007464 if (status != PSA_SUCCESS) {
7465 psa_pake_abort(operation);
7466 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007467 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007468 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007469}
7470
7471psa_status_t psa_pake_set_user(
7472 psa_pake_operation_t *operation,
7473 const uint8_t *user_id,
7474 size_t user_id_len)
7475{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007476 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007477
7478 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007479 status = PSA_ERROR_BAD_STATE;
7480 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007481 }
7482
Przemek Stekiel51eac532022-12-07 11:04:51 +01007483 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007484 status = PSA_ERROR_INVALID_ARGUMENT;
7485 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007486 }
7487
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007488 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007489 status = PSA_ERROR_BAD_STATE;
7490 goto exit;
7491 }
7492
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007493 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007494 if ((user_id_len != sizeof(jpake_server_id) ||
7495 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7496 (user_id_len != sizeof(jpake_client_id) ||
7497 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007498 status = PSA_ERROR_NOT_SUPPORTED;
7499 goto exit;
7500 }
7501
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007502 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7503 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007504 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7505 goto exit;
7506 }
7507
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007508 memcpy(operation->data.inputs.user, user_id, user_id_len);
7509 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007510
7511 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007512exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007513 psa_pake_abort(operation);
7514 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007515}
7516
7517psa_status_t psa_pake_set_peer(
7518 psa_pake_operation_t *operation,
7519 const uint8_t *peer_id,
7520 size_t peer_id_len)
7521{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007523
7524 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007525 status = PSA_ERROR_BAD_STATE;
7526 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007527 }
7528
Przemek Stekiel51eac532022-12-07 11:04:51 +01007529 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007530 status = PSA_ERROR_INVALID_ARGUMENT;
7531 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007532 }
7533
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007534 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007535 status = PSA_ERROR_BAD_STATE;
7536 goto exit;
7537 }
7538
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007539 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007540 if ((peer_id_len != sizeof(jpake_server_id) ||
7541 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7542 (peer_id_len != sizeof(jpake_client_id) ||
7543 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007544 status = PSA_ERROR_NOT_SUPPORTED;
7545 goto exit;
7546 }
7547
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007548 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7549 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007550 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7551 goto exit;
7552 }
7553
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007554 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7555 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007556
7557 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007558exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007559 psa_pake_abort(operation);
7560 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007561}
7562
7563psa_status_t psa_pake_set_role(
7564 psa_pake_operation_t *operation,
7565 psa_pake_role_t role)
7566{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007568
Przemek Stekiel51eac532022-12-07 11:04:51 +01007569 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007570 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007571 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007572 }
7573
Przemek Stekiel09104b82023-03-06 14:11:51 +01007574 switch (operation->alg) {
7575#if defined(PSA_WANT_ALG_JPAKE)
7576 case PSA_ALG_JPAKE:
7577 if (role == PSA_PAKE_ROLE_NONE) {
7578 return PSA_SUCCESS;
7579 }
7580 status = PSA_ERROR_INVALID_ARGUMENT;
7581 break;
7582#endif
7583 default:
7584 (void) role;
7585 status = PSA_ERROR_NOT_SUPPORTED;
7586 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007587 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007588exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007589 psa_pake_abort(operation);
7590 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007591}
7592
Przemek Stekielb09c4872023-01-17 12:05:38 +01007593/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007594#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007595static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007596 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007597{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007598 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007599 case PSA_PAKE_OUTPUT_X1_X2:
7600 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007601 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007602 case PSA_PAKE_X1_STEP_KEY_SHARE:
7603 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007604 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7605 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007606 case PSA_PAKE_X1_STEP_ZK_PROOF:
7607 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007608 case PSA_PAKE_X2_STEP_KEY_SHARE:
7609 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007610 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7611 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007612 case PSA_PAKE_X2_STEP_ZK_PROOF:
7613 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007614 default:
7615 return PSA_JPAKE_STEP_INVALID;
7616 }
7617 break;
7618 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007619 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007620 case PSA_PAKE_X1_STEP_KEY_SHARE:
7621 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007622 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7623 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007624 case PSA_PAKE_X1_STEP_ZK_PROOF:
7625 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007626 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007627 return PSA_JPAKE_STEP_INVALID;
7628 }
7629 break;
7630 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007631 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007632 case PSA_PAKE_X1_STEP_KEY_SHARE:
7633 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007634 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7635 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007636 case PSA_PAKE_X1_STEP_ZK_PROOF:
7637 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007638 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007639 return PSA_JPAKE_STEP_INVALID;
7640 }
7641 break;
7642 default:
7643 return PSA_JPAKE_STEP_INVALID;
7644 }
7645 return PSA_JPAKE_STEP_INVALID;
7646}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007647#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007648
Przemek Stekiel2797d372022-12-22 11:19:22 +01007649static psa_status_t psa_pake_complete_inputs(
7650 psa_pake_operation_t *operation)
7651{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007652 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007653 /* Create copy of the inputs on stack as inputs share memory
7654 with the driver context which will be setup by the driver. */
7655 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007656
Przemek Stekielfde11282023-03-13 16:06:09 +01007657 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007658 return PSA_ERROR_BAD_STATE;
7659 }
7660
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007661 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007662 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7663 return PSA_ERROR_BAD_STATE;
7664 }
7665 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7666 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007667 inputs.role = PSA_PAKE_ROLE_CLIENT;
7668 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007669 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7670 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007671 inputs.role = PSA_PAKE_ROLE_SERVER;
7672 }
7673
7674 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7675 inputs.role != PSA_PAKE_ROLE_SERVER) {
7676 return PSA_ERROR_NOT_SUPPORTED;
7677 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007678 }
7679
Przemek Stekiel18620a32023-01-17 16:34:52 +01007680 /* Clear driver context */
7681 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7682
7683 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007684
7685 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007686 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7687 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007688
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007689 /* User and peer are translated to role. */
7690 mbedtls_free(inputs.user);
7691 mbedtls_free(inputs.peer);
7692
Przemek Stekiel2797d372022-12-22 11:19:22 +01007693 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007694#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007695 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007696 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007697 psa_jpake_computation_stage_t *computation_stage =
7698 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007699 computation_stage->state = PSA_PAKE_STATE_READY;
7700 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7701 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7702 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007703 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007704#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007705 {
7706 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007707 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007708 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007709 return status;
7710}
7711
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007712#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007713static psa_status_t psa_jpake_output_prologue(
7714 psa_pake_operation_t *operation,
7715 psa_pake_step_t step)
7716{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007717 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7718 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7719 step != PSA_PAKE_STEP_ZK_PROOF) {
7720 return PSA_ERROR_INVALID_ARGUMENT;
7721 }
7722
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007723 psa_jpake_computation_stage_t *computation_stage =
7724 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007725
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007726 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7727 return PSA_ERROR_BAD_STATE;
7728 }
7729
7730 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7731 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7732 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7733 return PSA_ERROR_BAD_STATE;
7734 }
7735
7736 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7737 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007738 return PSA_ERROR_BAD_STATE;
7739 }
7740
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007741 switch (computation_stage->output_step) {
7742 case PSA_PAKE_STEP_X1_X2:
7743 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007744 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007745 case PSA_PAKE_STEP_X2S:
7746 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007747 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007748 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007749 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007750 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007751
7752 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7753 }
7754
7755 /* Check if step matches current sequence */
7756 switch (computation_stage->sequence) {
7757 case PSA_PAKE_X1_STEP_KEY_SHARE:
7758 case PSA_PAKE_X2_STEP_KEY_SHARE:
7759 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7760 return PSA_ERROR_BAD_STATE;
7761 }
7762 break;
7763
7764 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7765 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7766 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7767 return PSA_ERROR_BAD_STATE;
7768 }
7769 break;
7770
7771 case PSA_PAKE_X1_STEP_ZK_PROOF:
7772 case PSA_PAKE_X2_STEP_ZK_PROOF:
7773 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7774 return PSA_ERROR_BAD_STATE;
7775 }
7776 break;
7777
7778 default:
7779 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007780 }
7781
7782 return PSA_SUCCESS;
7783}
7784
7785static psa_status_t psa_jpake_output_epilogue(
7786 psa_pake_operation_t *operation)
7787{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007788 psa_jpake_computation_stage_t *computation_stage =
7789 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007790
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007791 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007792 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007793 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007794 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007795 computation_stage->state = PSA_PAKE_STATE_READY;
7796 computation_stage->output_step++;
7797 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7798 } else {
7799 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007800 }
7801
7802 return PSA_SUCCESS;
7803}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007804#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007805
Neil Armstronga7d08c32022-06-01 18:21:20 +02007806psa_status_t psa_pake_output(
7807 psa_pake_operation_t *operation,
7808 psa_pake_step_t step,
7809 uint8_t *output,
7810 size_t output_size,
7811 size_t *output_length)
7812{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007814 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007815 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007816
7817 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007818 status = psa_pake_complete_inputs(operation);
7819 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007820 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007821 }
7822 }
7823
7824 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007825 status = PSA_ERROR_BAD_STATE;
7826 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007827 }
7828
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007829 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007830 status = PSA_ERROR_INVALID_ARGUMENT;
7831 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007832 }
7833
Przemek Stekiele12ed362022-12-21 12:54:46 +01007834 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007835#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007836 case PSA_ALG_JPAKE:
7837 status = psa_jpake_output_prologue(operation, step);
7838 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007839 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007840 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007841 driver_step = convert_jpake_computation_stage_to_driver_step(
7842 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007843 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007844#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007845 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007846 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007847 status = PSA_ERROR_NOT_SUPPORTED;
7848 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007849 }
7850
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007851 status = psa_driver_wrapper_pake_output(operation, driver_step,
7852 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007853
7854 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007855 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007856 }
7857
7858 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007859#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007860 case PSA_ALG_JPAKE:
7861 status = psa_jpake_output_epilogue(operation);
7862 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007863 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007864 }
7865 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007866#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007867 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007868 status = PSA_ERROR_NOT_SUPPORTED;
7869 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007870 }
7871
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007872 return PSA_SUCCESS;
7873exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007874 psa_pake_abort(operation);
7875 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007876}
7877
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007878#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007879static psa_status_t psa_jpake_input_prologue(
7880 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007881 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007882{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007883 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7884 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7885 step != PSA_PAKE_STEP_ZK_PROOF) {
7886 return PSA_ERROR_INVALID_ARGUMENT;
7887 }
7888
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007889 psa_jpake_computation_stage_t *computation_stage =
7890 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007891
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007892 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7893 return PSA_ERROR_BAD_STATE;
7894 }
7895
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007896 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7897 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7898 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7899 return PSA_ERROR_BAD_STATE;
7900 }
7901
7902 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7903 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007904 return PSA_ERROR_BAD_STATE;
7905 }
7906
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007907 switch (computation_stage->input_step) {
7908 case PSA_PAKE_STEP_X1_X2:
7909 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007910 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007911 case PSA_PAKE_STEP_X2S:
7912 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007913 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007914 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007915 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007916 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007917
7918 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7919 }
7920
7921 /* Check if step matches current sequence */
7922 switch (computation_stage->sequence) {
7923 case PSA_PAKE_X1_STEP_KEY_SHARE:
7924 case PSA_PAKE_X2_STEP_KEY_SHARE:
7925 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7926 return PSA_ERROR_BAD_STATE;
7927 }
7928 break;
7929
7930 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7931 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7932 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7933 return PSA_ERROR_BAD_STATE;
7934 }
7935 break;
7936
7937 case PSA_PAKE_X1_STEP_ZK_PROOF:
7938 case PSA_PAKE_X2_STEP_ZK_PROOF:
7939 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7940 return PSA_ERROR_BAD_STATE;
7941 }
7942 break;
7943
7944 default:
7945 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007946 }
7947
7948 return PSA_SUCCESS;
7949}
7950
Przemek Stekiele12ed362022-12-21 12:54:46 +01007951static psa_status_t psa_jpake_input_epilogue(
7952 psa_pake_operation_t *operation)
7953{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007954 psa_jpake_computation_stage_t *computation_stage =
7955 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007956
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007957 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007958 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007959 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007960 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007961 computation_stage->state = PSA_PAKE_STATE_READY;
7962 computation_stage->input_step++;
7963 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7964 } else {
7965 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007966 }
7967
7968 return PSA_SUCCESS;
7969}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007970#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007971
Neil Armstronga7d08c32022-06-01 18:21:20 +02007972psa_status_t psa_pake_input(
7973 psa_pake_operation_t *operation,
7974 psa_pake_step_t step,
7975 const uint8_t *input,
7976 size_t input_length)
7977{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007978 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007979 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007980 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
7981 operation->primitive,
7982 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007983
7984 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007985 status = psa_pake_complete_inputs(operation);
7986 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007987 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007988 }
7989 }
7990
7991 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007992 status = PSA_ERROR_BAD_STATE;
7993 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007994 }
7995
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007996 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007997 status = PSA_ERROR_INVALID_ARGUMENT;
7998 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007999 }
8000
Przemek Stekiele12ed362022-12-21 12:54:46 +01008001 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008002#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008003 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008004 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008005 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008006 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008007 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008008 driver_step = convert_jpake_computation_stage_to_driver_step(
8009 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008010 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008011#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008012 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008013 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008014 status = PSA_ERROR_NOT_SUPPORTED;
8015 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008016 }
8017
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008018 status = psa_driver_wrapper_pake_input(operation, driver_step,
8019 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008020
8021 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008022 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008023 }
8024
8025 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008026#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008027 case PSA_ALG_JPAKE:
8028 status = psa_jpake_input_epilogue(operation);
8029 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008030 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008031 }
8032 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008033#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008034 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008035 status = PSA_ERROR_NOT_SUPPORTED;
8036 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008037 }
8038
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008039 return PSA_SUCCESS;
8040exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008041 psa_pake_abort(operation);
8042 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008043}
8044
8045psa_status_t psa_pake_get_implicit_key(
8046 psa_pake_operation_t *operation,
8047 psa_key_derivation_operation_t *output)
8048{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008050 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008051 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008052 size_t shared_key_len = 0;
8053
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008054 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008055 status = PSA_ERROR_BAD_STATE;
8056 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008057 }
8058
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008059#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008060 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008061 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008062 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008063 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8064 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008065 status = PSA_ERROR_BAD_STATE;
8066 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008067 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008068 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008069#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008070 {
8071 status = PSA_ERROR_NOT_SUPPORTED;
8072 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008073 }
8074
Przemek Stekiel0c781802022-11-29 14:53:13 +01008075 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8076 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008077 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008078 &shared_key_len);
8079
8080 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008081 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008082 }
8083
8084 status = psa_key_derivation_input_bytes(output,
8085 PSA_KEY_DERIVATION_INPUT_SECRET,
8086 shared_key,
8087 shared_key_len);
8088
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008089 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008090exit:
8091 abort_status = psa_pake_abort(operation);
8092 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008093}
8094
8095psa_status_t psa_pake_abort(
8096 psa_pake_operation_t *operation)
8097{
Przemek Stekield69dca92023-01-31 19:59:20 +01008098 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008099
Przemek Stekield69dca92023-01-31 19:59:20 +01008100 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008101 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008102 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008103
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008104 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8105 if (operation->data.inputs.password != NULL) {
8106 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008107 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008108 mbedtls_free(operation->data.inputs.password);
8109 }
8110 if (operation->data.inputs.user != NULL) {
8111 mbedtls_free(operation->data.inputs.user);
8112 }
8113 if (operation->data.inputs.peer != NULL) {
8114 mbedtls_free(operation->data.inputs.peer);
8115 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008116 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008117 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008118
Przemek Stekield69dca92023-01-31 19:59:20 +01008119 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008120}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008121
Gilles Peskinee59236f2018-01-27 23:32:46 +01008122#endif /* MBEDTLS_PSA_CRYPTO_C */