blob: 048ab58b33f408929ec6de78828b5169c385888a [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010038#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010039#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010040#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010042#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020044#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020045#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010046#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010047/* Include internal declarations that are useful for implementing persistently
48 * stored keys. */
49#include "psa_crypto_storage.h"
50
Gilles Peskineb2b64d32020-12-14 16:43:58 +010051#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010052
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020062#include "mbedtls/chacha20.h"
63#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020068#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/error.h"
72#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010074#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000075#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010076#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000077#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010078#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000079#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/sha1.h"
83#include "mbedtls/sha256.h"
84#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020085#include "md_psa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020090#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020091#endif
92
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010093/****************************************************************/
94/* Global data, support functions and library management */
95/****************************************************************/
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100}
101
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100102/* Values for psa_global_data_t::rng_state */
103#define RNG_NOT_INITIALIZED 0
104#define RNG_INITIALIZED 1
105#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100110 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100111 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112} psa_global_data_t;
113
114static psa_global_data_t global_data;
115
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100116#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
117mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
118 &global_data.rng.drbg;
119#endif
120
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (global_data.initialized == 0) \
123 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100125int psa_can_do_hash(psa_algorithm_t hash_alg)
126{
127 (void) hash_alg;
128 return global_data.drivers_initialized;
129}
Valerio Setti8bb57632023-05-26 13:48:07 +0200130#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200131 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Setti8bb57632023-05-26 13:48:07 +0200132 defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200133static int psa_is_dh_key_size_valid(size_t bits)
134{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200135 if (bits != 2048 && bits != 3072 && bits != 4096 &&
136 bits != 6144 && bits != 8192) {
137 return 0;
138 }
139
140 return 1;
141}
Valerio Setti8bb57632023-05-26 13:48:07 +0200142#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200143 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Setti8bb57632023-05-26 13:48:07 +0200144 MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100148 /* Mbed TLS error codes can combine a high-level error code and a
149 * low-level error code. The low-level error usually reflects the
150 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 int low_level_ret = -(-ret & 0x007f);
152 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100153 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100155
156 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
157 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200159 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
160 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
161 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
162 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
163 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200165 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200167 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100175
176 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180
Gilles Peskine26869f22019-05-06 15:25:00 +0200181 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200183
184 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200188
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
205 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100206 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
207 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
211 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100215#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100216
217 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100219
Gilles Peskinee59236f2018-01-27 23:32:46 +0100220 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
221 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
222 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
225 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200227 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100234 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
235 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100236 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100238 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
239 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100241 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243#endif
244
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200294 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
311 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200319
itayzafrir5c753392018-05-08 11:18:38 +0300320 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300321 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
328 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300330 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100332 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100334
Paul Elliott588f8ed2022-12-02 18:10:26 +0000335 case MBEDTLS_ERR_ECP_IN_PROGRESS:
336 return PSA_OPERATION_INCOMPLETE;
337
Janos Follatha13b9052019-11-22 12:48:59 +0000338 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300340
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100343 }
344}
345
Paul Elliottdc42ca82023-02-24 18:11:59 +0000346/**
347 * \brief For output buffers which contain "tags"
348 * (outputs that may be checked for validity like
349 * hashes, MACs and signatures), fill the unused
350 * part of the output buffer (the whole buffer on
351 * error, the trailing part on success) with
352 * something that isn't a valid tag (barring an
353 * attack on the tag and deliberately-crafted
354 * input), in case the caller doesn't check the
355 * return status properly.
356 *
357 * \param output_buffer Pointer to buffer to wipe. May not be NULL
358 * unless \p output_buffer_size is zero.
359 * \param status Status of function called to generate
360 * output_buffer originally
361 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
362 * could be NULL.
363 * \param output_buffer_length Length of data written to output_buffer, must be
364 * less than \p output_buffer_size
365 */
366static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000367 size_t output_buffer_size, size_t output_buffer_length)
368{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000369 size_t offset = 0;
370
371 if (output_buffer_size == 0) {
372 /* If output_buffer_size is 0 then we have nothing to do. We must not
373 call memset because output_buffer may be NULL in this case */
374 return;
375 }
376
377 if (status == PSA_SUCCESS) {
378 offset = output_buffer_length;
379 }
380
381 memset(output_buffer + offset, '!', output_buffer_size - offset);
382}
383
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200384
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200385
386
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100387/****************************************************************/
388/* Key management */
389/****************************************************************/
390
Valerio Settia9aab1a2023-06-19 13:39:54 +0200391#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200392psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
393 size_t *bits)
394{
395 switch (grpid) {
396 case MBEDTLS_ECP_DP_SECP192R1:
397 *bits = 192;
398 return PSA_ECC_FAMILY_SECP_R1;
399 case MBEDTLS_ECP_DP_SECP224R1:
400 *bits = 224;
401 return PSA_ECC_FAMILY_SECP_R1;
402 case MBEDTLS_ECP_DP_SECP256R1:
403 *bits = 256;
404 return PSA_ECC_FAMILY_SECP_R1;
405 case MBEDTLS_ECP_DP_SECP384R1:
406 *bits = 384;
407 return PSA_ECC_FAMILY_SECP_R1;
408 case MBEDTLS_ECP_DP_SECP521R1:
409 *bits = 521;
410 return PSA_ECC_FAMILY_SECP_R1;
411 case MBEDTLS_ECP_DP_BP256R1:
412 *bits = 256;
413 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
414 case MBEDTLS_ECP_DP_BP384R1:
415 *bits = 384;
416 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
417 case MBEDTLS_ECP_DP_BP512R1:
418 *bits = 512;
419 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
420 case MBEDTLS_ECP_DP_CURVE25519:
421 *bits = 255;
422 return PSA_ECC_FAMILY_MONTGOMERY;
423 case MBEDTLS_ECP_DP_SECP192K1:
424 *bits = 192;
425 return PSA_ECC_FAMILY_SECP_K1;
426 case MBEDTLS_ECP_DP_SECP224K1:
427 *bits = 224;
428 return PSA_ECC_FAMILY_SECP_K1;
429 case MBEDTLS_ECP_DP_SECP256K1:
430 *bits = 256;
431 return PSA_ECC_FAMILY_SECP_K1;
432 case MBEDTLS_ECP_DP_CURVE448:
433 *bits = 448;
434 return PSA_ECC_FAMILY_MONTGOMERY;
435 default:
436 *bits = 0;
437 return 0;
438 }
439}
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
442 size_t bits,
443 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444{
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100446 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100448#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100451#endif
452#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100453 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100455#endif
456#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100459#endif
460#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100461 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100463#endif
464#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100467 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (bits_is_sloppy) {
469 return MBEDTLS_ECP_DP_SECP521R1;
470 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100471 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100472#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100473 }
474 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100475
Paul Elliott8ff510a2020-06-02 17:19:28 +0100476 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100478#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100479 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100481#endif
482#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100483 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100485#endif
486#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100487 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100489#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100490 }
491 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100492
Paul Elliott8ff510a2020-06-02 17:19:28 +0100493 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100495#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100496 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100498 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (bits_is_sloppy) {
500 return MBEDTLS_ECP_DP_CURVE25519;
501 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100502 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100503#endif
504#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100505 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100507#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100508 }
509 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100510
Paul Elliott8ff510a2020-06-02 17:19:28 +0100511 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100513#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100514 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100516#endif
517#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100518 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100520#endif
521#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100522 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100524#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100525 }
526 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200527 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100528
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100529 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200531}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200532#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
535 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200536{
537 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200541 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200542 case PSA_KEY_TYPE_PASSWORD:
543 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200544 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100545#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200546 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (bits != 128 && bits != 192 && bits != 256) {
548 return PSA_ERROR_INVALID_ARGUMENT;
549 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200550 break;
551#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200552#if defined(PSA_WANT_KEY_TYPE_ARIA)
553 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (bits != 128 && bits != 192 && bits != 256) {
555 return PSA_ERROR_INVALID_ARGUMENT;
556 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200557 break;
558#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100559#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200560 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (bits != 128 && bits != 192 && bits != 256) {
562 return PSA_ERROR_INVALID_ARGUMENT;
563 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200564 break;
565#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100566#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200567 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (bits != 64 && bits != 128 && bits != 192) {
569 return PSA_ERROR_INVALID_ARGUMENT;
570 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200571 break;
572#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100573#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200574 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (bits != 256) {
576 return PSA_ERROR_INVALID_ARGUMENT;
577 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200578 break;
579#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200580 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200582 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (bits % 8 != 0) {
584 return PSA_ERROR_INVALID_ARGUMENT;
585 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200588}
589
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100590/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100591 *
Steven Cooremancd640932021-03-08 12:00:27 +0100592 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
593 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100594 *
595 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100596 * \param[in] key_type The key type of the key to be used with the
597 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100598 *
599 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100600 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100601 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100602 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100603 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100604MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100605 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100607{
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (PSA_ALG_IS_HMAC(algorithm)) {
609 if (key_type == PSA_KEY_TYPE_HMAC) {
610 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100611 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100612 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
615 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
616 * key. */
617 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
618 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
619 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
620 * the block length (larger than 1) for block ciphers. */
621 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
622 return PSA_SUCCESS;
623 }
624 }
625 }
626
627 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100628}
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
631 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200632{
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (slot->key.data != NULL) {
634 return PSA_ERROR_ALREADY_EXISTS;
635 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 slot->key.data = mbedtls_calloc(1, buffer_length);
638 if (slot->key.data == NULL) {
639 return PSA_ERROR_INSUFFICIENT_MEMORY;
640 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200641
Ronald Cronea0f8a62020-11-25 17:52:23 +0100642 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200644}
645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
647 const uint8_t *data,
648 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200649{
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 psa_status_t status = psa_allocate_buffer_to_slot(slot,
651 data_length);
652 if (status != PSA_SUCCESS) {
653 return status;
654 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 memcpy(slot->key.data, data, data_length);
657 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200658}
659
Ronald Crona0fe59f2020-11-29 11:14:53 +0100660psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100661 const psa_key_attributes_t *attributes,
662 const uint8_t *data, size_t data_length,
663 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
667 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200669 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (data_length == 0) {
671 return PSA_ERROR_NOT_SUPPORTED;
672 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (key_type_is_raw_bytes(type)) {
675 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
678 *bits);
679 if (status != PSA_SUCCESS) {
680 return status;
681 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200682
Ronald Crondd04d422020-11-28 15:14:42 +0100683 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100685 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return PSA_SUCCESS;
689 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +0200690#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200691 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100692 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200693 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100694 return PSA_ERROR_INVALID_ARGUMENT;
695 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200696 return mbedtls_psa_ffdh_import_key(attributes,
697 data, data_length,
698 key_buffer, key_buffer_size,
699 key_buffer_length,
700 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100701 }
Valerio Setti8bb57632023-05-26 13:48:07 +0200702#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200703 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti8bb57632023-05-26 13:48:07 +0200704#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
706 if (PSA_KEY_TYPE_IS_ECC(type)) {
707 return mbedtls_psa_ecp_import_key(attributes,
708 data, data_length,
709 key_buffer, key_buffer_size,
710 key_buffer_length,
711 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100712 }
Valerio Setti8bb57632023-05-26 13:48:07 +0200713#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -0800714 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Setti8bb57632023-05-26 13:48:07 +0200715#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
717 if (PSA_KEY_TYPE_IS_RSA(type)) {
718 return mbedtls_psa_rsa_import_key(attributes,
719 data, data_length,
720 key_buffer, key_buffer_size,
721 key_buffer_length,
722 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200723 }
Valerio Setti8bb57632023-05-26 13:48:07 +0200724#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -0800725 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100726 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100729}
730
Gilles Peskinef603c712019-01-19 13:40:11 +0100731/** Calculate the intersection of two algorithm usage policies.
732 *
733 * Return 0 (which allows no operation) on incompatibility.
734 */
735static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100736 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100737 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100739{
Gilles Peskine549ea862019-05-22 11:45:59 +0200740 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (alg1 == alg2) {
742 return alg1;
743 }
Steven Cooreman03488022021-02-18 12:07:20 +0100744 /* If the policies are from the same hash-and-sign family, check
745 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
747 PSA_ALG_IS_SIGN_HASH(alg2) &&
748 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
749 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
750 return alg2;
751 }
752 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
753 return alg1;
754 }
Steven Cooreman03488022021-02-18 12:07:20 +0100755 }
756 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100757 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100758 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
760 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
761 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
762 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
763 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100764 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100765
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100766 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
768 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
769 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
770 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100771 }
772 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
774 (alg1_len <= alg2_len)) {
775 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100776 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
778 (alg2_len <= alg1_len)) {
779 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100780 }
781 }
782 /* If the policies are from the same MAC family, check whether one
783 * of them is a minimum-MAC-length policy. Calculate the most
784 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
786 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
787 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100788 /* Validate the combination of key type and algorithm. Since the base
789 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
791 return 0;
792 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100793
Steven Cooreman31a876d2021-03-03 20:47:40 +0100794 /* Get the (exact or at-least) output lengths for both sides of the
795 * requested intersection. None of the currently supported algorithms
796 * have an output length dependent on the actual key size, so setting it
797 * to a bogus value of 0 is currently OK.
798 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100799 * Note that for at-least-this-length wildcard algorithms, the output
800 * length is set to the shortest allowed length, which allows us to
801 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
803 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100804 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100805
Steven Cooremana1d83222021-02-25 10:20:29 +0100806 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
808 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
809 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100810 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100811
812 /* If only one is an at-least-this-length policy, the intersection would
813 * be the other (fixed-length) policy as long as said fixed length is
814 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
816 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100817 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
819 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100820 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100821
Steven Cooremancd640932021-03-08 12:00:27 +0100822 /* If none of them are wildcards, check whether they define the same tag
823 * length. This is still possible here when one is default-length and
824 * the other specific-length. Ensure to always return the
825 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (alg1_len == alg2_len) {
827 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
828 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100829 }
830 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100832}
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834static int psa_key_algorithm_permits(psa_key_type_t key_type,
835 psa_algorithm_t policy_alg,
836 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200837{
Gilles Peskine549ea862019-05-22 11:45:59 +0200838 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (requested_alg == policy_alg) {
840 return 1;
841 }
Steven Cooreman03488022021-02-18 12:07:20 +0100842 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
843 * and requested_alg is the same hash-and-sign family with any hash,
844 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
846 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
847 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
848 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100849 }
850 /* If policy_alg is a wildcard AEAD algorithm of the same base as
851 * the requested algorithm, check the requested tag length to be
852 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (PSA_ALG_IS_AEAD(policy_alg) &&
854 PSA_ALG_IS_AEAD(requested_alg) &&
855 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
856 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
857 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
858 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
859 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100860 }
Steven Cooremancd640932021-03-08 12:00:27 +0100861 /* If policy_alg is a MAC algorithm of the same base as the requested
862 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (PSA_ALG_IS_MAC(policy_alg) &&
864 PSA_ALG_IS_MAC(requested_alg) &&
865 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
866 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100867 /* Validate the combination of key type and algorithm. Since the policy
868 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
870 return 0;
871 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100872
Steven Cooreman31a876d2021-03-03 20:47:40 +0100873 /* Get both the requested output length for the algorithm which is to be
874 * verified, and the default output length for the base algorithm.
875 * Note that none of the currently supported algorithms have an output
876 * length dependent on actual key size, so setting it to a bogus value
877 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100878 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100880 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 key_type, 0,
882 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100883
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100884 /* If the policy is default-length, only allow an algorithm with
885 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
887 return requested_output_length == default_output_length;
888 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100889
890 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100891 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
893 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
894 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100895 }
896
Steven Cooremancd640932021-03-08 12:00:27 +0100897 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
898 * check for the requested MAC length to be equal to or longer than the
899 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
901 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
902 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100903 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200904 }
Steven Cooremance48e852020-10-05 16:02:45 +0200905 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200906 * a key derivation with that key agreement should also be allowed. This
907 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
909 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
910 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
911 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200912 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100913 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200915}
916
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100917/** Test whether a policy permits an algorithm.
918 *
919 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100920 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100921 * \note This function requires providing the key type for which the policy is
922 * being validated, since some algorithm policy definitions (e.g. MAC)
923 * have different properties depending on what kind of cipher it is
924 * combined with.
925 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100926 * \retval PSA_SUCCESS When \p alg is a specific algorithm
927 * allowed by the \p policy.
928 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
929 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
930 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100931 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
933 psa_key_type_t key_type,
934 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100935{
Steven Cooremand788fab2021-02-25 11:29:17 +0100936 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (alg == 0) {
938 return PSA_ERROR_INVALID_ARGUMENT;
939 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100940
Steven Cooreman7e39f052021-02-23 14:18:32 +0100941 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (PSA_ALG_IS_WILDCARD(alg)) {
943 return PSA_ERROR_INVALID_ARGUMENT;
944 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
947 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
948 return PSA_SUCCESS;
949 } else {
950 return PSA_ERROR_NOT_PERMITTED;
951 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100952}
953
Gilles Peskinef603c712019-01-19 13:40:11 +0100954/** Restrict a key policy based on a constraint.
955 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100956 * \note This function requires providing the key type for which the policy is
957 * being restricted, since some algorithm policy definitions (e.g. MAC)
958 * have different properties depending on what kind of cipher it is
959 * combined with.
960 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100961 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100962 * \param[in,out] policy The policy to restrict.
963 * \param[in] constraint The policy constraint to apply.
964 *
965 * \retval #PSA_SUCCESS
966 * \c *policy contains the intersection of the original value of
967 * \c *policy and \c *constraint.
968 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100969 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100970 * \c *policy is unchanged.
971 */
972static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100973 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100974 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100976{
977 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 psa_key_policy_algorithm_intersection(key_type, policy->alg,
979 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200980 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
982 constraint->alg2);
983 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
984 return PSA_ERROR_INVALID_ARGUMENT;
985 }
986 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
987 return PSA_ERROR_INVALID_ARGUMENT;
988 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100989 policy->usage &= constraint->usage;
990 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200991 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100993}
994
Przemek Stekiel061f6942022-11-30 10:51:35 +0100995/** Get the description of a key given its identifier and policy constraints
996 * and lock it.
997 *
998 * The key must have allow all the usage flags set in \p usage. If \p alg is
999 * nonzero, the key must allow operations with this algorithm. If \p alg is
1000 * zero, the algorithm is not checked.
1001 *
1002 * In case of a persistent key, the function loads the description of the key
1003 * into a key slot if not already done.
1004 *
1005 * On success, the returned key slot is locked. It is the responsibility of
1006 * the caller to unlock the key slot when it does not access it anymore.
1007 */
1008static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001009 mbedtls_svc_key_id_t key,
1010 psa_key_slot_t **p_slot,
1011 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001013{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001015 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 status = psa_get_and_lock_key_slot(key, p_slot);
1018 if (status != PSA_SUCCESS) {
1019 return status;
1020 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001021 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001022
1023 /* Enforce that usage policy for the key slot contains all the flags
1024 * required by the usage parameter. There is one exception: public
1025 * keys can always be exported, so we treat public key objects as
1026 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001028 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001032 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001033 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001034 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001035
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001036 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if (alg != 0) {
1038 status = psa_key_policy_permits(&slot->attr.policy,
1039 slot->attr.type,
1040 alg);
1041 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001042 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001044 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001047
1048error:
1049 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001053}
Darryl Green940d72c2018-07-13 13:18:51 +01001054
Ronald Cron5c522922020-11-14 16:35:34 +01001055/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001056 *
1057 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001058 * available, as opposed to a key in a secure element and/or to be used
1059 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001060 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001061 * This is a temporary function that may be used instead of
1062 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1063 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001064 *
Ronald Cron5c522922020-11-14 16:35:34 +01001065 * On success, the returned key slot is locked. It is the responsibility of the
1066 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001067 */
Ronald Cron5c522922020-11-14 16:35:34 +01001068static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1069 mbedtls_svc_key_id_t key,
1070 psa_key_slot_t **p_slot,
1071 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001073{
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1075 usage, alg);
1076 if (status != PSA_SUCCESS) {
1077 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001078 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1081 psa_unlock_key_slot(*p_slot);
1082 *p_slot = NULL;
1083 return PSA_ERROR_NOT_SUPPORTED;
1084 }
1085
1086 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001087}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001090{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001091 /* Data pointer will always be either a valid pointer or NULL in an
1092 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if (slot->key.data != NULL) {
1094 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1095 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001098 slot->key.data = NULL;
1099 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001102}
1103
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001104/** Completely wipe a slot in memory, including its policy.
1105 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001106psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001107{
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 /*
1111 * As the return error code may not be handled in case of multiple errors,
1112 * do our best to report an unexpected lock counter. Assert with
1113 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1114 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1115 * function is called as part of the execution of a test suite, the
1116 * execution of the test suite is stopped in error if the assertion fails.
1117 */
1118 if (slot->lock_count != 1) {
1119 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001120 status = PSA_ERROR_CORRUPTION_DETECTED;
1121 }
1122
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001123 /* Multipart operations may still be using the key. This is safe
1124 * because all multipart operation objects are independent from
1125 * the key slot: if they need to access the key after the setup
1126 * phase, they have a copy of the key. Note that this means that
1127 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001128 /* At this point, key material and other type-specific content has
1129 * been wiped. Clear remaining metadata. We can call memset and not
1130 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 memset(slot, 0, sizeof(*slot));
1132 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001133}
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001136{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001137 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001138 psa_status_t status; /* status of the last operation */
1139 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001140#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1141 psa_se_drv_table_entry_t *driver;
1142#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (mbedtls_svc_key_id_is_null(key)) {
1145 return PSA_SUCCESS;
1146 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001147
Ronald Cronf2911112020-10-29 17:51:10 +01001148 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001149 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001150 * key, this will load the key description from persistent memory if not
1151 * done yet. We cannot avoid this loading as without it we don't know if
1152 * the key is operated by an SE or not and this information is needed by
1153 * the current implementation.
1154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 status = psa_get_and_lock_key_slot(key, &slot);
1156 if (status != PSA_SUCCESS) {
1157 return status;
1158 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001159
Ronald Cronf2911112020-10-29 17:51:10 +01001160 /*
1161 * If the key slot containing the key description is under access by the
1162 * library (apart from the present access), the key cannot be destroyed
1163 * yet. For the time being, just return in error. Eventually (to be
1164 * implemented), the key should be destroyed when all accesses have
1165 * stopped.
1166 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 if (slot->lock_count > 1) {
1168 psa_unlock_key_slot(slot);
1169 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001170 }
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001173 /* Refuse the destruction of a read-only key (which may or may not work
1174 * if we attempt it, depending on whether the key is merely read-only
1175 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001176 * Just do the best we can, which is to wipe the copy in memory
1177 * (done in this function's cleanup code). */
1178 overall_status = PSA_ERROR_NOT_PERMITTED;
1179 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001180 }
1181
Gilles Peskine354f7672019-07-12 23:46:38 +02001182#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1184 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001185 /* For a key in a secure element, we need to do three things:
1186 * remove the key file in internal storage, destroy the
1187 * key inside the secure element, and update the driver's
1188 * persistent data. Start a transaction that will encompass these
1189 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001191 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001193 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 status = psa_crypto_save_transaction();
1195 if (status != PSA_SUCCESS) {
1196 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001197 /* We should still try to destroy the key in the secure
1198 * element and the key metadata in storage. This is especially
1199 * important if the error is that the storage is full.
1200 * But how to do it exactly without risking an inconsistent
1201 * state after a reset?
1202 * https://github.com/ARMmbed/mbed-crypto/issues/215
1203 */
1204 overall_status = status;
1205 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001206 }
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 status = psa_destroy_se_key(driver,
1209 psa_key_slot_get_slot_number(slot));
1210 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001211 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001213 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001214#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1215
Darryl Greend49a4992018-06-18 17:27:26 +01001216#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1218 status = psa_destroy_persistent_key(slot->attr.id);
1219 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001220 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001222
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001223 /* TODO: other slots may have a copy of the same key. We should
1224 * invalidate them.
1225 * https://github.com/ARMmbed/mbed-crypto/issues/214
1226 */
Darryl Greend49a4992018-06-18 17:27:26 +01001227 }
1228#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001229
Gilles Peskinefc762652019-07-22 19:30:34 +02001230#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (driver != NULL) {
1232 status = psa_save_se_persistent_data(driver);
1233 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001234 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 }
1236 status = psa_crypto_stop_transaction();
1237 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001238 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001240 }
1241#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1242
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001243exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001245 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001247 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 }
1249 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001250}
1251
Valerio Setti8bb57632023-05-26 13:48:07 +02001252#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
John Durkop0e005192020-10-31 22:06:54 -07001253 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001254static psa_status_t psa_get_rsa_public_exponent(
1255 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001257{
1258 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001260 uint8_t *buffer = NULL;
1261 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1265 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001266 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 }
1268 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001269 /* It's the default value, which is reported as an empty string,
1270 * so there's nothing to do. */
1271 goto exit;
1272 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 buflen = mbedtls_mpi_size(&mpi);
1275 buffer = mbedtls_calloc(1, buflen);
1276 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001277 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1278 goto exit;
1279 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1281 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001282 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001284 attributes->domain_parameters = buffer;
1285 attributes->domain_parameters_size = buflen;
1286
1287exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 mbedtls_mpi_free(&mpi);
1289 if (ret != 0) {
1290 mbedtls_free(buffer);
1291 }
1292 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001293}
Valerio Setti8bb57632023-05-26 13:48:07 +02001294#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001295 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001296
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001297/** Retrieve all the publicly-accessible attributes of a key.
1298 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001299psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1300 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001301{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001302 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001303 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001304 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1309 if (status != PSA_SUCCESS) {
1310 return status;
1311 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001312
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001313 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1315 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001316
1317#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1319 psa_set_key_slot_number(attributes,
1320 psa_key_slot_get_slot_number(slot));
1321 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001322#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 switch (slot->attr.type) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001325#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
John Durkop0e005192020-10-31 22:06:54 -07001326 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001327 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001328 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001329 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001330 * is not yet implemented.
1331 * https://github.com/ARMmbed/mbed-crypto/issues/216
1332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001334 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001335
Ronald Cron90857082020-11-25 14:58:33 +01001336 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 slot->attr.type,
1338 slot->key.data,
1339 slot->key.bytes,
1340 &rsa);
1341 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001342 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 status = psa_get_rsa_public_exponent(rsa,
1346 attributes);
1347 mbedtls_rsa_free(rsa);
1348 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001349 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001350 break;
Valerio Setti8bb57632023-05-26 13:48:07 +02001351#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001352 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001353 default:
1354 /* Nothing else to do. */
1355 break;
1356 }
1357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (status != PSA_SUCCESS) {
1359 psa_reset_key_attributes(attributes);
1360 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001365}
1366
Gilles Peskinec8000c02019-08-02 20:15:51 +02001367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1368psa_status_t psa_get_key_slot_number(
1369 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001371{
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001373 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 return PSA_SUCCESS;
1375 } else {
1376 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001377 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001378}
1379#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1382 size_t key_buffer_size,
1383 uint8_t *data,
1384 size_t data_size,
1385 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001386{
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 if (key_buffer_size > data_size) {
1388 return PSA_ERROR_BUFFER_TOO_SMALL;
1389 }
1390 memcpy(data, key_buffer, key_buffer_size);
1391 memset(data + key_buffer_size, 0,
1392 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001393 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001395}
1396
Ronald Cron7285cda2020-11-26 14:46:37 +01001397psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001398 const psa_key_attributes_t *attributes,
1399 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001401{
Ronald Crond18b5f82020-11-25 16:46:05 +01001402 psa_key_type_t type = attributes->core.type;
1403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 if (key_type_is_raw_bytes(type) ||
1405 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001406 PSA_KEY_TYPE_IS_ECC(type) ||
1407 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 return psa_export_key_buffer_internal(
1409 key_buffer, key_buffer_size,
1410 data, data_size, data_length);
1411 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001412 /* This shouldn't happen in the reference implementation, but
1413 it is valid for a special-purpose implementation to omit
1414 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001416 }
1417}
1418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1420 uint8_t *data,
1421 size_t data_size,
1422 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001423{
1424 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1425 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1426 psa_key_slot_t *slot;
1427
Ronald Crone907e552021-01-18 13:22:38 +01001428 /* Reject a zero-length output buffer now, since this can never be a
1429 * valid key representation. This way we know that data must be a valid
1430 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (data_size == 0) {
1432 return PSA_ERROR_BUFFER_TOO_SMALL;
1433 }
Ronald Crone907e552021-01-18 13:22:38 +01001434
Ronald Cron9486f9d2020-11-26 13:36:23 +01001435 /* Set the key to empty now, so that even when there are errors, we always
1436 * set data_length to a value between 0 and data_size. On error, setting
1437 * the key to empty is a good choice because an empty key representation is
1438 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001439 *data_length = 0;
1440
Ronald Cron9486f9d2020-11-26 13:36:23 +01001441 /* Export requires the EXPORT flag. There is an exception for public keys,
1442 * which don't require any flag, but
1443 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1444 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1446 PSA_KEY_USAGE_EXPORT, 0);
1447 if (status != PSA_SUCCESS) {
1448 return status;
1449 }
mohammad160306e79202018-03-28 13:17:44 +03001450
Ronald Crond18b5f82020-11-25 16:46:05 +01001451 psa_key_attributes_t attributes = {
1452 .core = slot->attr
1453 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 status = psa_driver_wrapper_export_key(&attributes,
1455 slot->key.data, slot->key.bytes,
1456 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001461}
1462
Ronald Cron7285cda2020-11-26 14:46:37 +01001463psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001464 const psa_key_attributes_t *attributes,
1465 const uint8_t *key_buffer,
1466 size_t key_buffer_size,
1467 uint8_t *data,
1468 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001470{
Ronald Crond18b5f82020-11-25 16:46:05 +01001471 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001472
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001473 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001474 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1475 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001476 /* Exporting public -> public */
1477 return psa_export_key_buffer_internal(
1478 key_buffer, key_buffer_size,
1479 data, data_size, data_length);
1480 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001481#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001482 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1483 return mbedtls_psa_rsa_export_public_key(attributes,
1484 key_buffer,
1485 key_buffer_size,
1486 data,
1487 data_size,
1488 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001489#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001490 /* We don't know how to convert a private RSA key to public. */
1491 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti8bb57632023-05-26 13:48:07 +02001492#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001493 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001494 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001495#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001496 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1497 return mbedtls_psa_ecp_export_public_key(attributes,
1498 key_buffer,
1499 key_buffer_size,
1500 data,
1501 data_size,
1502 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001503#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001504 /* We don't know how to convert a private ECC key to public */
1505 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti8bb57632023-05-26 13:48:07 +02001506#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001507 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001508 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001509#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001510 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001511 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001512 key_buffer,
1513 key_buffer_size,
1514 data, data_size,
1515 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001516#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001517 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti8bb57632023-05-26 13:48:07 +02001518#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001519 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001521 (void) key_buffer;
1522 (void) key_buffer_size;
1523 (void) data;
1524 (void) data_size;
1525 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001527 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001528}
Ronald Crond18b5f82020-11-25 16:46:05 +01001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1531 uint8_t *data,
1532 size_t data_size,
1533 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001534{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001535 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001536 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001537 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001538
Ronald Crone907e552021-01-18 13:22:38 +01001539 /* Reject a zero-length output buffer now, since this can never be a
1540 * valid key representation. This way we know that data must be a valid
1541 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (data_size == 0) {
1543 return PSA_ERROR_BUFFER_TOO_SMALL;
1544 }
Ronald Crone907e552021-01-18 13:22:38 +01001545
Darryl Greendd8fb772018-11-07 16:00:44 +00001546 /* Set the key to empty now, so that even when there are errors, we always
1547 * set data_length to a value between 0 and data_size. On error, setting
1548 * the key to empty is a good choice because an empty key representation is
1549 * unlikely to be accepted anywhere. */
1550 *data_length = 0;
1551
1552 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1554 if (status != PSA_SUCCESS) {
1555 return status;
1556 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1559 status = PSA_ERROR_INVALID_ARGUMENT;
1560 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001561 }
1562
Ronald Crond18b5f82020-11-25 16:46:05 +01001563 psa_key_attributes_t attributes = {
1564 .core = slot->attr
1565 };
Ronald Cron67227982020-11-26 15:16:05 +01001566 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001567 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001569
1570exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001574}
1575
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001576MBEDTLS_STATIC_ASSERT(
1577 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1578 "One or more key attribute flag is listed as both external-only and dual-use")
1579MBEDTLS_STATIC_ASSERT(
1580 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1581 "One or more key attribute flag is listed as both internal-only and dual-use")
1582MBEDTLS_STATIC_ASSERT(
1583 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1584 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001585
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001586/** Validate that a key policy is internally well-formed.
1587 *
1588 * This function only rejects invalid policies. It does not validate the
1589 * consistency of the policy with respect to other attributes of the key
1590 * such as the key type.
1591 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001592static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001593{
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1595 PSA_KEY_USAGE_COPY |
1596 PSA_KEY_USAGE_ENCRYPT |
1597 PSA_KEY_USAGE_DECRYPT |
1598 PSA_KEY_USAGE_SIGN_MESSAGE |
1599 PSA_KEY_USAGE_VERIFY_MESSAGE |
1600 PSA_KEY_USAGE_SIGN_HASH |
1601 PSA_KEY_USAGE_VERIFY_HASH |
1602 PSA_KEY_USAGE_VERIFY_DERIVATION |
1603 PSA_KEY_USAGE_DERIVE)) != 0) {
1604 return PSA_ERROR_INVALID_ARGUMENT;
1605 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001608}
1609
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001610/** Validate the internal consistency of key attributes.
1611 *
1612 * This function only rejects invalid attribute values. If does not
1613 * validate the consistency of the attributes with any key data that may
1614 * be involved in the creation of the key.
1615 *
1616 * Call this function early in the key creation process.
1617 *
1618 * \param[in] attributes Key attributes for the new key.
1619 * \param[out] p_drv On any return, the driver for the key, if any.
1620 * NULL for a transparent key.
1621 *
1622 */
1623static psa_status_t psa_validate_key_attributes(
1624 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001626{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001627 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1629 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 status = psa_validate_key_location(lifetime, p_drv);
1632 if (status != PSA_SUCCESS) {
1633 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001634 }
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 status = psa_validate_key_persistence(lifetime);
1637 if (status != PSA_SUCCESS) {
1638 return status;
1639 }
1640
1641 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1642 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1643 return PSA_ERROR_INVALID_ARGUMENT;
1644 }
1645 } else {
1646 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1647 return PSA_ERROR_INVALID_ARGUMENT;
1648 }
1649 }
1650
1651 status = psa_validate_key_policy(&attributes->core.policy);
1652 if (status != PSA_SUCCESS) {
1653 return status;
1654 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001655
1656 /* Refuse to create overly large keys.
1657 * Note that this doesn't trigger on import if the attributes don't
1658 * explicitly specify a size (so psa_get_key_bits returns 0), so
1659 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1661 return PSA_ERROR_NOT_SUPPORTED;
1662 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001663
Gilles Peskine91e8c332019-08-02 19:19:39 +02001664 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1666 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1667 return PSA_ERROR_INVALID_ARGUMENT;
1668 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001671}
1672
Gilles Peskine4747d192019-04-17 15:05:45 +02001673/** Prepare a key slot to receive key material.
1674 *
1675 * This function allocates a key slot and sets its metadata.
1676 *
1677 * If this function fails, call psa_fail_key_creation().
1678 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001679 * This function is intended to be used as follows:
1680 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001681 * it with the specified attributes, and in case of a volatile key assign it
1682 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001683 * -# Populate the slot with the key material.
1684 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1685 * In case of failure at any step, stop the sequence and call
1686 * psa_fail_key_creation().
1687 *
Ronald Cron5c522922020-11-14 16:35:34 +01001688 * On success, the key slot is locked. It is the responsibility of the caller
1689 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001690 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001691 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001692 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001693 * \param[out] p_slot On success, a pointer to the prepared slot.
1694 * \param[out] p_drv On any return, the driver for the key, if any.
1695 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001696 *
1697 * \retval #PSA_SUCCESS
1698 * The key slot is ready to receive key material.
1699 * \return If this function fails, the key slot is an invalid state.
1700 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001701 */
1702static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001703 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001704 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001705 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001707{
1708 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001709 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001710 psa_key_slot_t *slot;
1711
Gilles Peskinedf179142019-07-15 22:02:14 +02001712 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001713 *p_drv = NULL;
1714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 status = psa_validate_key_attributes(attributes, p_drv);
1716 if (status != PSA_SUCCESS) {
1717 return status;
1718 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1721 if (status != PSA_SUCCESS) {
1722 return status;
1723 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001724 slot = *p_slot;
1725
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001726 /* We're storing the declared bit-size of the key. It's up to each
1727 * creation mechanism to verify that this information is correct.
1728 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001729 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001730 * is optional (import, copy). In case of a volatile key, assign it the
1731 * volatile key identifier associated to the slot returned to contain its
1732 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001733
1734 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001736#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1737 slot->attr.id = volatile_key_id;
1738#else
1739 slot->attr.id.key_id = volatile_key_id;
1740#endif
1741 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001742
Gilles Peskine91e8c332019-08-02 19:19:39 +02001743 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001744 * external-only flags, query `attributes`. Thanks to the check
1745 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001746 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001747 * may have set. */
1748 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001749
Gilles Peskinecbaff462019-07-12 23:46:04 +02001750#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001751 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001752 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001753 * create the key file in internal storage, create the
1754 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001755 * persistent data. This is done by starting a transaction that will
1756 * encompass these three actions.
1757 * For registering a volatile key, we just need to find an appropriate
1758 * slot number inside the SE. Since the key is designated volatile, creating
1759 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001760 /* The first thing to do is to find a slot number for the new key.
1761 * We save the slot number in persistent storage as part of the
1762 * transaction data. It will be needed to recover if the power
1763 * fails during the key creation process, to clean up on the secure
1764 * element side after restarting. Obtaining a slot number from the
1765 * secure element driver updates its persistent state, but we do not yet
1766 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001767 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001769 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1771 &slot_number);
1772 if (status != PSA_SUCCESS) {
1773 return status;
1774 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1777 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001778 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001779 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001780 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 status = psa_crypto_save_transaction();
1782 if (status != PSA_SUCCESS) {
1783 (void) psa_crypto_stop_transaction();
1784 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001785 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001786 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001787
1788 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001790 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001793 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001795 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001796#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001799}
Gilles Peskine4747d192019-04-17 15:05:45 +02001800
1801/** Finalize the creation of a key once its key material has been set.
1802 *
1803 * This entails writing the key to persistent storage.
1804 *
1805 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001806 * See the documentation of psa_start_key_creation() for the intended use
1807 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001808 *
Ronald Cron5c522922020-11-14 16:35:34 +01001809 * If the finalization succeeds, the function unlocks the key slot (it was
1810 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1811 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001812 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001813 * \param[in,out] slot Pointer to the slot with key material.
1814 * \param[in] driver The secure element driver for the key,
1815 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001816 * \param[out] key On success, identifier of the key. Note that the
1817 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001818 *
1819 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001820 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001821 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1822 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1823 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1824 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1825 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1826 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001827 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001828 * \return If this function fails, the key slot is an invalid state.
1829 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001830 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001831static psa_status_t psa_finish_key_creation(
1832 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001833 psa_se_drv_table_entry_t *driver,
1834 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001835{
1836 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001837 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001838 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001839
1840#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001842#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001844 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001845 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001847
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001848 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1849 sizeof(data.slot_number),
1850 "Slot number size does not match psa_se_key_data_storage_t");
1851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1853 status = psa_save_persistent_key(&slot->attr,
1854 (uint8_t *) &data,
1855 sizeof(data));
1856 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001857#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1858 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001859 /* Key material is saved in export representation in the slot, so
1860 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 status = psa_save_persistent_key(&slot->attr,
1862 slot->key.data,
1863 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001864 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001865 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001866#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1867
Gilles Peskinecbaff462019-07-12 23:46:04 +02001868#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001869 /* Finish the transaction for a key creation. This does not
1870 * happen when registering an existing key. Detect this case
1871 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001872 * creation of a persistent key in a secure element requires a transaction,
1873 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 if (driver != NULL &&
1875 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1876 status = psa_save_se_persistent_data(driver);
1877 if (status != PSA_SUCCESS) {
1878 psa_destroy_persistent_key(slot->attr.id);
1879 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001880 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001882 }
1883#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001886 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 status = psa_unlock_key_slot(slot);
1888 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001889 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001891 }
Ronald Cron50972942020-11-14 11:28:25 +01001892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001894}
1895
1896/** Abort the creation of a key.
1897 *
1898 * You may call this function after calling psa_start_key_creation(),
1899 * or after psa_finish_key_creation() fails. In other circumstances, this
1900 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001901 * See the documentation of psa_start_key_creation() for the intended use
1902 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001903 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001904 * \param[in,out] slot Pointer to the slot with key material.
1905 * \param[in] driver The secure element driver for the key,
1906 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001908static void psa_fail_key_creation(psa_key_slot_t *slot,
1909 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001910{
Gilles Peskine011e4282019-06-26 18:34:38 +02001911 (void) driver;
1912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001914 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001916
1917#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001918 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001919 * element, and the failure happened later (when saving metadata
1920 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001921 * element.
1922 * https://github.com/ARMmbed/mbed-crypto/issues/217
1923 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001924
Gilles Peskined7729582019-08-05 15:55:54 +02001925 /* Abort the ongoing transaction if any (there may not be one if
1926 * the creation process failed before starting one, or if the
1927 * key creation is a registration of a key in a secure element).
1928 * Earlier functions must already have done what it takes to undo any
1929 * partial creation. All that's left is to update the transaction data
1930 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001932#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001935}
1936
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001937/** Validate optional attributes during key creation.
1938 *
1939 * Some key attributes are optional during key creation. If they are
1940 * specified in the attributes structure, check that they are consistent
1941 * with the data in the slot.
1942 *
1943 * This function should be called near the end of key creation, after
1944 * the slot in memory is fully populated but before saving persistent data.
1945 */
1946static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949{
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (attributes->core.type != 0) {
1951 if (attributes->core.type != slot->attr.type) {
1952 return PSA_ERROR_INVALID_ARGUMENT;
1953 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001954 }
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (attributes->domain_parameters_size != 0) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001957#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1959 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001960 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001961 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001962 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001963
Ronald Cron90857082020-11-25 14:58:33 +01001964 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 slot->attr.type,
1966 slot->key.data,
1967 slot->key.bytes,
1968 &rsa);
1969 if (status != PSA_SUCCESS) {
1970 return status;
1971 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 mbedtls_mpi_init(&actual);
1974 mbedtls_mpi_init(&required);
1975 ret = mbedtls_rsa_export(rsa,
1976 NULL, NULL, NULL, NULL, &actual);
1977 mbedtls_rsa_free(rsa);
1978 mbedtls_free(rsa);
1979 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001980 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 }
1982 ret = mbedtls_mpi_read_binary(&required,
1983 attributes->domain_parameters,
1984 attributes->domain_parameters_size);
1985 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001986 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 }
1988 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 }
1991rsa_exit:
1992 mbedtls_mpi_free(&actual);
1993 mbedtls_mpi_free(&required);
1994 if (ret != 0) {
1995 return mbedtls_to_psa_error(ret);
1996 }
1997 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02001998#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001999 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002000 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002002 }
2003 }
2004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 if (attributes->core.bits != 0) {
2006 if (attributes->core.bits != slot->attr.bits) {
2007 return PSA_ERROR_INVALID_ARGUMENT;
2008 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009 }
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002012}
2013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2015 const uint8_t *data,
2016 size_t data_length,
2017 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002018{
2019 psa_status_t status;
2020 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002021 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002022 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302023 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002024
Ronald Cron81709fc2020-11-14 12:10:32 +01002025 *key = MBEDTLS_SVC_KEY_ID_INIT;
2026
Gilles Peskine0f84d622019-09-12 19:03:13 +02002027 /* Reject zero-length symmetric keys (including raw data key objects).
2028 * This also rejects any key which might be encoded as an empty string,
2029 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 if (data_length == 0) {
2031 return PSA_ERROR_INVALID_ARGUMENT;
2032 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002033
Archana449608b2021-09-08 15:36:05 +05302034 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 if (data_length > SIZE_MAX / 8) {
2036 return PSA_ERROR_NOT_SUPPORTED;
2037 }
Archana6ed4bda2021-08-04 10:47:15 +05302038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2040 &slot, &driver);
2041 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002042 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002044
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002045 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302046 * storage ( thus not in the case of importing a key in a secure element
2047 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2048 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (slot->key.data == NULL) {
2050 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302051 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 attributes, data, data_length, &storage_size);
2053 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302054 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 }
Archanad8a83dc2021-06-14 10:04:16 +05302056 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 status = psa_allocate_buffer_to_slot(slot, storage_size);
2058 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002059 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002061 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002062
2063 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 status = psa_driver_wrapper_import_key(attributes,
2065 data, data_length,
2066 slot->key.data,
2067 slot->key.bytes,
2068 &slot->key.bytes, &bits);
2069 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002070 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002074 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002076 status = PSA_ERROR_INVALID_ARGUMENT;
2077 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002078 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002079
Archana6ed4bda2021-08-04 10:47:15 +05302080 /* Enforce a size limit, and in particular ensure that the bit
2081 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302083 status = PSA_ERROR_NOT_SUPPORTED;
2084 goto exit;
2085 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 status = psa_validate_optional_attributes(slot, attributes);
2087 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002088 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002092exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 if (status != PSA_SUCCESS) {
2094 psa_fail_key_creation(slot, driver);
2095 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002098}
2099
Gilles Peskined7729582019-08-05 15:55:54 +02002100#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2101psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002103{
2104 psa_status_t status;
2105 psa_key_slot_t *slot = NULL;
2106 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002107 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002108
2109 /* Leaving attributes unspecified is not currently supported.
2110 * It could make sense to query the key type and size from the
2111 * secure element, but not all secure elements support this
2112 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2114 return PSA_ERROR_NOT_SUPPORTED;
2115 }
2116 if (psa_get_key_bits(attributes) == 0) {
2117 return PSA_ERROR_NOT_SUPPORTED;
2118 }
Gilles Peskined7729582019-08-05 15:55:54 +02002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2121 &slot, &driver);
2122 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 }
Gilles Peskined7729582019-08-05 15:55:54 +02002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002127
2128exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if (status != PSA_SUCCESS) {
2130 psa_fail_key_creation(slot, driver);
2131 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002132
Gilles Peskined7729582019-08-05 15:55:54 +02002133 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 psa_close_key(key);
2135 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002136}
2137#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2140 const psa_key_attributes_t *specified_attributes,
2141 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002142{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002143 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002144 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002145 psa_key_slot_t *source_slot = NULL;
2146 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002147 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002148 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302149 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002150
Ronald Cron81709fc2020-11-14 12:10:32 +01002151 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2152
Archana8a180362021-07-05 02:18:48 +05302153 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2155 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002156 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 status = psa_validate_optional_attributes(source_slot,
2160 specified_attributes);
2161 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002164
Archana9d17bf42021-09-10 06:22:44 +05302165 /* The target key type and number of bits have been validated by
2166 * psa_validate_optional_attributes() to be either equal to zero or
2167 * equal to the ones of the source key. So it is safe to inherit
2168 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302169 * */
Archana9d17bf42021-09-10 06:22:44 +05302170 actual_attributes.core.bits = source_slot->attr.bits;
2171 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302172
2173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 status = psa_restrict_key_policy(source_slot->attr.type,
2175 &actual_attributes.core.policy,
2176 &source_slot->attr.policy);
2177 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2182 &target_slot, &driver);
2183 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002184 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 }
2186 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2187 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302188 /*
Archana9d17bf42021-09-10 06:22:44 +05302189 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302190 * the source key would need to be exported as plaintext and re-imported
2191 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302192 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302193 * appropriate API invocations from the application, if needed.
2194 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002195 status = PSA_ERROR_NOT_SUPPORTED;
2196 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002197 }
Archana8a180362021-07-05 02:18:48 +05302198 /*
2199 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302200 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302201 * - For opaque keys this translates to an invocation of the drivers'
2202 * copy_key entry point through the dispatch layer.
2203 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2205 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2206 &storage_size);
2207 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 }
Archana374fe5b2021-09-08 15:50:28 +05302210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2212 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 }
Archana374fe5b2021-09-08 15:50:28 +05302215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 status = psa_driver_wrapper_copy_key(&actual_attributes,
2217 source_slot->key.data,
2218 source_slot->key.bytes,
2219 target_slot->key.data,
2220 target_slot->key.bytes,
2221 &target_slot->key.bytes);
2222 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302223 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 }
2225 } else {
2226 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302227 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 source_slot->key.bytes);
2229 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 }
Archana8a180362021-07-05 02:18:48 +05302232 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 if (status != PSA_SUCCESS) {
2236 psa_fail_key_creation(target_slot, driver);
2237 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002242}
2243
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002244
2245
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002246/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002247/* Message digests */
2248/****************************************************************/
2249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002252 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if (operation->id == 0) {
2254 return PSA_SUCCESS;
2255 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002258 operation->id = 0;
2259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002261}
2262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2264 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002266 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002267
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002268 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002270 status = PSA_ERROR_BAD_STATE;
2271 goto exit;
2272 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002275 status = PSA_ERROR_INVALID_ARGUMENT;
2276 goto exit;
2277 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002278
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002279 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2280 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002284
2285exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if (status != PSA_SUCCESS) {
2287 psa_hash_abort(operation);
2288 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002289
2290 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002291}
2292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2294 const uint8_t *input,
2295 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002296{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002300 status = PSA_ERROR_BAD_STATE;
2301 goto exit;
2302 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002303
Steven Cooremanc8288352021-03-04 14:02:19 +01002304 /* Don't require hash implementations to behave correctly on a
2305 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 if (input_length == 0) {
2307 return PSA_SUCCESS;
2308 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002309
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002311
2312exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if (status != PSA_SUCCESS) {
2314 psa_hash_abort(operation);
2315 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318}
2319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2321 uint8_t *hash,
2322 size_t hash_size,
2323 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002325 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (operation->id == 0) {
2327 return PSA_ERROR_BAD_STATE;
2328 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329
Steven Cooreman1e582352021-02-18 17:24:37 +01002330 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 operation, hash, hash_size, hash_length);
2332 psa_hash_abort(operation);
2333 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334}
2335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2337 const uint8_t *hash,
2338 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339{
Ronald Cron69a63422021-10-18 09:47:58 +02002340 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002342 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 operation,
2344 actual_hash, sizeof(actual_hash),
2345 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002348 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002352 status = PSA_ERROR_INVALID_SIGNATURE;
2353 goto exit;
2354 }
2355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002357 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002359
2360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2362 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002363 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002367}
2368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369psa_status_t psa_hash_compute(psa_algorithm_t alg,
2370 const uint8_t *input, size_t input_length,
2371 uint8_t *hash, size_t hash_size,
2372 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002373{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002374 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 if (!PSA_ALG_IS_HASH(alg)) {
2376 return PSA_ERROR_INVALID_ARGUMENT;
2377 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2380 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002381}
2382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383psa_status_t psa_hash_compare(psa_algorithm_t alg,
2384 const uint8_t *input, size_t input_length,
2385 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002386{
Ronald Cron69a63422021-10-18 09:47:58 +02002387 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002388 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (!PSA_ALG_IS_HASH(alg)) {
2391 return PSA_ERROR_INVALID_ARGUMENT;
2392 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002393
Steven Cooreman1e582352021-02-18 17:24:37 +01002394 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 alg, input, input_length,
2396 actual_hash, sizeof(actual_hash),
2397 &actual_hash_length);
2398 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002399 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 }
2401 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002402 status = PSA_ERROR_INVALID_SIGNATURE;
2403 goto exit;
2404 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002406 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002408
2409exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2411 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002412}
2413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2415 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002416{
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (source_operation->id == 0 ||
2418 target_operation->id != 0) {
2419 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002420 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2423 target_operation);
2424 if (status != PSA_SUCCESS) {
2425 psa_hash_abort(target_operation);
2426 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002429}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002430
2431
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002432/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433/* MAC */
2434/****************************************************************/
2435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002437{
Steven Cooremane6804192021-03-19 18:28:56 +01002438 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (operation->id == 0) {
2440 return PSA_SUCCESS;
2441 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002444 operation->mac_size = 0;
2445 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002446 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449}
2450
Ronald Cron2dff3b22021-06-17 16:33:22 +02002451static psa_status_t psa_mac_finalize_alg_and_key_validation(
2452 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002453 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002455{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002456 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 psa_key_type_t key_type = psa_get_key_type(attributes);
2458 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (!PSA_ALG_IS_MAC(alg)) {
2461 return PSA_ERROR_INVALID_ARGUMENT;
2462 }
Steven Cooremane6804192021-03-19 18:28:56 +01002463
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002464 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 status = psa_mac_key_can_do(alg, key_type);
2466 if (status != PSA_SUCCESS) {
2467 return status;
2468 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002469
Steven Cooremand1a68f12021-05-10 11:13:41 +02002470 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002474 /* A very short MAC is too short for security since it can be
2475 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2476 * so we make this our minimum, even though 32 bits is still
2477 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002479 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2482 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002483 /* It's impossible to "truncate" to a larger length than the full length
2484 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002486 }
2487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002489 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2490 * that is disabled in the compile-time configuration. The result can
2491 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2492 * configuration into account. In this case, force a return of
2493 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2494 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2495 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2496 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2497 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002499 }
2500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002502}
2503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2505 mbedtls_svc_key_id_t key,
2506 psa_algorithm_t alg,
2507 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002508{
2509 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2510 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002511 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512
2513 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002515 status = PSA_ERROR_BAD_STATE;
2516 goto exit;
2517 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518
2519 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 key,
2521 &slot,
2522 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2523 alg);
2524 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002525 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002527
2528 psa_key_attributes_t attributes = {
2529 .core = slot->attr
2530 };
2531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2533 &operation->mac_size);
2534 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537
2538 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002539 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (is_sign) {
2541 status = psa_driver_wrapper_mac_sign_setup(operation,
2542 &attributes,
2543 slot->key.data,
2544 slot->key.bytes,
2545 alg);
2546 } else {
2547 status = psa_driver_wrapper_mac_verify_setup(operation,
2548 &attributes,
2549 slot->key.data,
2550 slot->key.bytes,
2551 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002553
Gilles Peskinefbfac682018-07-08 20:51:54 +02002554exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (status != PSA_SUCCESS) {
2556 psa_mac_abort(operation);
2557 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002562}
2563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2565 mbedtls_svc_key_id_t key,
2566 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002567{
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002569}
2570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2572 mbedtls_svc_key_id_t key,
2573 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002574{
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002576}
2577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2579 const uint8_t *input,
2580 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581{
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (operation->id == 0) {
2583 return PSA_ERROR_BAD_STATE;
2584 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002586 /* Don't require hash implementations to behave correctly on a
2587 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (input_length == 0) {
2589 return PSA_SUCCESS;
2590 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2593 input, input_length);
2594 if (status != PSA_SUCCESS) {
2595 psa_mac_abort(operation);
2596 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599}
2600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2602 uint8_t *mac,
2603 size_t mac_size,
2604 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002605{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002606 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2607 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002610 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002611 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002612 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002615 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002616 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002617 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002618
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002619 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2620 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002622 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002623 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002624 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002627 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002628 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002629 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 status = psa_driver_wrapper_mac_sign_finish(operation,
2632 mac, operation->mac_size,
2633 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002634
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002635exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002636 /* In case of success, set the potential excess room in the output buffer
2637 * to an invalid value, to avoid potentially leaking a longer MAC.
2638 * In case of error, set the output length and content to a safe default,
2639 * such that in case the caller misses an error check, the output would be
2640 * an unachievable MAC.
2641 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002643 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002644 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002645 }
mohammad16036df908f2018-04-02 08:34:15 -07002646
Paul Elliottdc42ca82023-02-24 18:11:59 +00002647 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002652}
2653
Gilles Peskine449bd832023-01-11 14:50:10 +01002654psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2655 const uint8_t *mac,
2656 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002658 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2659 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002662 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002663 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002664 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002667 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002668 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002669 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002672 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002673 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002674 }
2675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 status = psa_driver_wrapper_mac_verify_finish(operation,
2677 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002678
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002679exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002683}
2684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2686 psa_algorithm_t alg,
2687 const uint8_t *input,
2688 size_t input_length,
2689 uint8_t *mac,
2690 size_t mac_size,
2691 size_t *mac_length,
2692 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002693{
2694 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002695 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2696 psa_key_slot_t *slot;
2697 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002698
Ronald Cron51131b52021-06-17 17:17:20 +02002699 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 key,
2701 &slot,
2702 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2703 alg);
2704 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002705 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002707
Ronald Cron51131b52021-06-17 17:17:20 +02002708 psa_key_attributes_t attributes = {
2709 .core = slot->attr
2710 };
2711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2713 &operation_mac_size);
2714 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002715 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002719 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002720 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002721 }
2722
2723 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 &attributes,
2725 slot->key.data, slot->key.bytes,
2726 alg,
2727 input, input_length,
2728 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002729
2730exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002731 /* In case of success, set the potential excess room in the output buffer
2732 * to an invalid value, to avoid potentially leaking a longer MAC.
2733 * In case of error, set the output length and content to a safe default,
2734 * such that in case the caller misses an error check, the output would be
2735 * an unachievable MAC.
2736 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002738 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002739 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002740 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002741
2742 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002747}
2748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002750 psa_algorithm_t alg,
2751 const uint8_t *input,
2752 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 uint8_t *mac,
2754 size_t mac_size,
2755 size_t *mac_length)
2756{
2757 return psa_mac_compute_internal(key, alg,
2758 input, input_length,
2759 mac, mac_size, mac_length, 1);
2760}
2761
2762psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2763 psa_algorithm_t alg,
2764 const uint8_t *input,
2765 size_t input_length,
2766 const uint8_t *mac,
2767 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002768{
2769 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002770 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2771 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 status = psa_mac_compute_internal(key, alg,
2774 input, input_length,
2775 actual_mac, sizeof(actual_mac),
2776 &actual_mac_length, 0);
2777 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002778 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002782 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002783 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002784 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002786 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002787 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002788 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002789
2790exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002794}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002795
Gilles Peskinee59236f2018-01-27 23:32:46 +01002796/****************************************************************/
2797/* Asymmetric cryptography */
2798/****************************************************************/
2799
Gilles Peskine449bd832023-01-11 14:50:10 +01002800static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2801 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002802{
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (input_is_message) {
2804 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2805 return PSA_ERROR_INVALID_ARGUMENT;
2806 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002807
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2809 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2810 return PSA_ERROR_INVALID_ARGUMENT;
2811 }
2812 }
2813 } else {
2814 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2815 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002816 }
2817 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002820}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2823 int input_is_message,
2824 psa_algorithm_t alg,
2825 const uint8_t *input,
2826 size_t input_length,
2827 uint8_t *signature,
2828 size_t signature_size,
2829 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002830{
2831 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2832 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2833 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002834
2835 *signature_length = 0;
2836
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 status = psa_sign_verify_check_alg(input_is_message, alg);
2838 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002839 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002841
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002842 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002843 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002844 * buffer can in principle be empty since it doesn't actually have
2845 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 if (signature_size == 0) {
2847 return PSA_ERROR_BUFFER_TOO_SMALL;
2848 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002849
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002850 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 key, &slot,
2852 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2853 PSA_KEY_USAGE_SIGN_HASH,
2854 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002855
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002857 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002861 status = PSA_ERROR_INVALID_ARGUMENT;
2862 goto exit;
2863 }
2864
2865 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002867 };
2868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002870 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002871 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002872 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 signature, signature_size, signature_length);
2874 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002875
2876 status = psa_driver_wrapper_sign_hash(
2877 &attributes, slot->key.data, slot->key.bytes,
2878 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002880 }
2881
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002882
2883exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002884 psa_wipe_tag_output_buffer(signature, status, signature_size,
2885 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002888
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002890}
2891
Gilles Peskine449bd832023-01-11 14:50:10 +01002892static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2893 int input_is_message,
2894 psa_algorithm_t alg,
2895 const uint8_t *input,
2896 size_t input_length,
2897 const uint8_t *signature,
2898 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002899{
2900 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2901 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2902 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 status = psa_sign_verify_check_alg(input_is_message, alg);
2905 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002906 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002908
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002909 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 key, &slot,
2911 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2912 PSA_KEY_USAGE_VERIFY_HASH,
2913 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 if (status != PSA_SUCCESS) {
2916 return status;
2917 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002918
2919 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002921 };
2922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002924 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002925 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002926 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 signature, signature_length);
2928 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002929 status = psa_driver_wrapper_verify_hash(
2930 &attributes, slot->key.data, slot->key.bytes,
2931 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002933 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002938
2939}
2940
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002941psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002942 const psa_key_attributes_t *attributes,
2943 const uint8_t *key_buffer,
2944 size_t key_buffer_size,
2945 psa_algorithm_t alg,
2946 const uint8_t *input,
2947 size_t input_length,
2948 uint8_t *signature,
2949 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002951{
2952 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002955 size_t hash_length;
2956 uint8_t hash[PSA_HASH_MAX_SIZE];
2957
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002958 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 PSA_ALG_SIGN_GET_HASH(alg),
2960 input, input_length,
2961 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002964 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002966
2967 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 attributes, key_buffer, key_buffer_size,
2969 alg, hash, hash_length,
2970 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002971 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002974}
2975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2977 psa_algorithm_t alg,
2978 const uint8_t *input,
2979 size_t input_length,
2980 uint8_t *signature,
2981 size_t signature_size,
2982 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002983{
2984 return psa_sign_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_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002987}
2988
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002989psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002990 const psa_key_attributes_t *attributes,
2991 const uint8_t *key_buffer,
2992 size_t key_buffer_size,
2993 psa_algorithm_t alg,
2994 const uint8_t *input,
2995 size_t input_length,
2996 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002998{
2999 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003002 size_t hash_length;
3003 uint8_t hash[PSA_HASH_MAX_SIZE];
3004
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003005 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 PSA_ALG_SIGN_GET_HASH(alg),
3007 input, input_length,
3008 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003011 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003013
3014 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 attributes, key_buffer, key_buffer_size,
3016 alg, hash, hash_length,
3017 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003018 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003021}
3022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3024 psa_algorithm_t alg,
3025 const uint8_t *input,
3026 size_t input_length,
3027 const uint8_t *signature,
3028 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003029{
3030 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003031 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003033}
3034
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003035psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003036 const psa_key_attributes_t *attributes,
3037 const uint8_t *key_buffer, size_t key_buffer_size,
3038 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003040{
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3042 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3043 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003044#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3046 return mbedtls_psa_rsa_sign_hash(
3047 attributes,
3048 key_buffer, key_buffer_size,
3049 alg, hash, hash_length,
3050 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003051#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3052 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 } else {
3054 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003055 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3057 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003058#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3060 return mbedtls_psa_ecdsa_sign_hash(
3061 attributes,
3062 key_buffer, key_buffer_size,
3063 alg, hash, hash_length,
3064 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003065#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3066 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 } else {
3068 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003069 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003070 }
Ronald Cron4501c982021-03-19 20:09:32 +01003071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 (void) key_buffer;
3073 (void) key_buffer_size;
3074 (void) hash;
3075 (void) hash_length;
3076 (void) signature;
3077 (void) signature_size;
3078 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003081}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3084 psa_algorithm_t alg,
3085 const uint8_t *hash,
3086 size_t hash_length,
3087 uint8_t *signature,
3088 size_t signature_size,
3089 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003090{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003091 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003092 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003094}
3095
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003096psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003097 const psa_key_attributes_t *attributes,
3098 const uint8_t *key_buffer, size_t key_buffer_size,
3099 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003101{
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3103 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3104 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003105#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3107 return mbedtls_psa_rsa_verify_hash(
3108 attributes,
3109 key_buffer, key_buffer_size,
3110 alg, hash, hash_length,
3111 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003112#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3113 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 } else {
3115 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003116 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3118 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003119#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3121 return mbedtls_psa_ecdsa_verify_hash(
3122 attributes,
3123 key_buffer, key_buffer_size,
3124 alg, hash, hash_length,
3125 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003126#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3127 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 } else {
3129 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003130 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003131 }
Ronald Cron4501c982021-03-19 20:09:32 +01003132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 (void) key_buffer;
3134 (void) key_buffer_size;
3135 (void) hash;
3136 (void) hash_length;
3137 (void) signature;
3138 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003141}
3142
Gilles Peskine449bd832023-01-11 14:50:10 +01003143psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3144 psa_algorithm_t alg,
3145 const uint8_t *hash,
3146 size_t hash_length,
3147 const uint8_t *signature,
3148 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003149{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003150 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003151 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003153}
3154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3156 psa_algorithm_t alg,
3157 const uint8_t *input,
3158 size_t input_length,
3159 const uint8_t *salt,
3160 size_t salt_length,
3161 uint8_t *output,
3162 size_t output_size,
3163 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003164{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003166 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003167 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003168
Darryl Green5cc689a2018-07-24 15:34:10 +01003169 (void) input;
3170 (void) input_length;
3171 (void) salt;
3172 (void) output;
3173 (void) output_size;
3174
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003175 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003176
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3178 return PSA_ERROR_INVALID_ARGUMENT;
3179 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003180
Ronald Cron5c522922020-11-14 16:35:34 +01003181 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3183 if (status != PSA_SUCCESS) {
3184 return status;
3185 }
3186 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3187 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003188 status = PSA_ERROR_INVALID_ARGUMENT;
3189 goto exit;
3190 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003192 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003194 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003195
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003196 status = psa_driver_wrapper_asymmetric_encrypt(
3197 &attributes, slot->key.data, slot->key.bytes,
3198 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003204}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003205
Gilles Peskine449bd832023-01-11 14:50:10 +01003206psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3207 psa_algorithm_t alg,
3208 const uint8_t *input,
3209 size_t input_length,
3210 const uint8_t *salt,
3211 size_t salt_length,
3212 uint8_t *output,
3213 size_t output_size,
3214 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003215{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003217 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003218 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003219
Darryl Green5cc689a2018-07-24 15:34:10 +01003220 (void) input;
3221 (void) input_length;
3222 (void) salt;
3223 (void) output;
3224 (void) output_size;
3225
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003226 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003227
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3229 return PSA_ERROR_INVALID_ARGUMENT;
3230 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003231
Ronald Cron5c522922020-11-14 16:35:34 +01003232 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3234 if (status != PSA_SUCCESS) {
3235 return status;
3236 }
3237 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003238 status = PSA_ERROR_INVALID_ARGUMENT;
3239 goto exit;
3240 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003241
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003242 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003244 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003245
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003246 status = psa_driver_wrapper_asymmetric_decrypt(
3247 &attributes, slot->key.data, slot->key.bytes,
3248 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003250
3251exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003253
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003255}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003256
Paul Elliott9fe12f62022-11-30 19:16:02 +00003257/****************************************************************/
3258/* Asymmetric interruptible cryptography */
3259/****************************************************************/
3260
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003261static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3262
Paul Elliott9fe12f62022-11-30 19:16:02 +00003263void psa_interruptible_set_max_ops(uint32_t max_ops)
3264{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003265 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003266}
3267
3268uint32_t psa_interruptible_get_max_ops(void)
3269{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003270 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003271}
3272
Paul Elliott9fe12f62022-11-30 19:16:02 +00003273uint32_t psa_sign_hash_get_num_ops(
3274 const psa_sign_hash_interruptible_operation_t *operation)
3275{
Paul Elliott296ede92022-12-15 17:00:30 +00003276 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003277}
3278
3279uint32_t psa_verify_hash_get_num_ops(
3280 const psa_verify_hash_interruptible_operation_t *operation)
3281{
Paul Elliott296ede92022-12-15 17:00:30 +00003282 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003283}
3284
Paul Elliott59ad9452022-12-18 15:09:02 +00003285static psa_status_t psa_sign_hash_abort_internal(
3286 psa_sign_hash_interruptible_operation_t *operation)
3287{
3288 if (operation->id == 0) {
3289 /* The object has (apparently) been initialized but it is not (yet)
3290 * in use. It's ok to call abort on such an object, and there's
3291 * nothing to do. */
3292 return PSA_SUCCESS;
3293 }
3294
3295 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3296
3297 status = psa_driver_wrapper_sign_hash_abort(operation);
3298
3299 operation->id = 0;
3300
Paul Elliotte6145dc2023-02-07 12:51:21 +00003301 /* Do not clear either the error_occurred or num_ops elements here as they
3302 * only want to be cleared by the application calling abort, not by abort
3303 * being called at completion of an operation. */
3304
Paul Elliott59ad9452022-12-18 15:09:02 +00003305 return status;
3306}
3307
Paul Elliott9fe12f62022-11-30 19:16:02 +00003308psa_status_t psa_sign_hash_start(
3309 psa_sign_hash_interruptible_operation_t *operation,
3310 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3311 const uint8_t *hash, size_t hash_length)
3312{
3313 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3314 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3315 psa_key_slot_t *slot;
3316
Paul Elliottc9774412023-02-06 15:14:07 +00003317 /* Check that start has not been previously called, or operation has not
3318 * previously errored. */
3319 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003320 return PSA_ERROR_BAD_STATE;
3321 }
3322
Paul Elliott9fe12f62022-11-30 19:16:02 +00003323 status = psa_sign_verify_check_alg(0, alg);
3324 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003325 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003326 return status;
3327 }
3328
3329 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3330 PSA_KEY_USAGE_SIGN_HASH,
3331 alg);
3332
3333 if (status != PSA_SUCCESS) {
3334 goto exit;
3335 }
3336
3337 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3338 status = PSA_ERROR_INVALID_ARGUMENT;
3339 goto exit;
3340 }
3341
3342 psa_key_attributes_t attributes = {
3343 .core = slot->attr
3344 };
3345
Paul Elliott296ede92022-12-15 17:00:30 +00003346 /* Ensure ops count gets reset, in case of operation re-use. */
3347 operation->num_ops = 0;
3348
Paul Elliott9fe12f62022-11-30 19:16:02 +00003349 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3350 slot->key.data,
3351 slot->key.bytes, alg,
3352 hash, hash_length);
3353exit:
3354
3355 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003356 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003357 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003358 }
3359
3360 unlock_status = psa_unlock_key_slot(slot);
3361
Paul Elliottc9774412023-02-06 15:14:07 +00003362 if (unlock_status != PSA_SUCCESS) {
3363 operation->error_occurred = 1;
3364 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003365
Paul Elliottc9774412023-02-06 15:14:07 +00003366 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003367}
3368
3369
3370psa_status_t psa_sign_hash_complete(
3371 psa_sign_hash_interruptible_operation_t *operation,
3372 uint8_t *signature, size_t signature_size,
3373 size_t *signature_length)
3374{
3375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3376
3377 *signature_length = 0;
3378
Paul Elliottc9774412023-02-06 15:14:07 +00003379 /* Check that start has been called first, and that operation has not
3380 * previously errored. */
3381 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003382 status = PSA_ERROR_BAD_STATE;
3383 goto exit;
3384 }
3385
Paul Elliott096abc42023-02-03 18:33:23 +00003386 /* Immediately reject a zero-length signature buffer. This guarantees that
3387 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003388 if (signature_size == 0) {
3389 status = PSA_ERROR_BUFFER_TOO_SMALL;
3390 goto exit;
3391 }
3392
3393 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3394 signature_size,
3395 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003396
Paul Elliott296ede92022-12-15 17:00:30 +00003397 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003398 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003399
Paul Elliottebe225c2023-02-10 14:32:53 +00003400exit:
3401
Paul Elliott59200a22023-02-21 15:07:40 +00003402 psa_wipe_tag_output_buffer(signature, status, signature_size,
3403 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003404
Paul Elliott53bb3122023-02-10 14:22:22 +00003405 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003406 if (status != PSA_SUCCESS) {
3407 operation->error_occurred = 1;
3408 }
3409
Paul Elliott59ad9452022-12-18 15:09:02 +00003410 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003411 }
3412
3413 return status;
3414}
3415
3416psa_status_t psa_sign_hash_abort(
3417 psa_sign_hash_interruptible_operation_t *operation)
3418{
Paul Elliott59ad9452022-12-18 15:09:02 +00003419 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3420
3421 status = psa_sign_hash_abort_internal(operation);
3422
3423 /* We clear the number of ops done here, so that it is not cleared when
3424 * the operation fails or succeeds, only on manual abort. */
3425 operation->num_ops = 0;
3426
Paul Elliottc9774412023-02-06 15:14:07 +00003427 /* Likewise, failure state. */
3428 operation->error_occurred = 0;
3429
Paul Elliott59ad9452022-12-18 15:09:02 +00003430 return status;
3431}
3432
3433static psa_status_t psa_verify_hash_abort_internal(
3434 psa_verify_hash_interruptible_operation_t *operation)
3435{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003436 if (operation->id == 0) {
3437 /* The object has (apparently) been initialized but it is not (yet)
3438 * in use. It's ok to call abort on such an object, and there's
3439 * nothing to do. */
3440 return PSA_SUCCESS;
3441 }
3442
Paul Elliott59ad9452022-12-18 15:09:02 +00003443 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3444
3445 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003446
3447 operation->id = 0;
3448
Paul Elliotte6145dc2023-02-07 12:51:21 +00003449 /* Do not clear either the error_occurred or num_ops elements here as they
3450 * only want to be cleared by the application calling abort, not by abort
3451 * being called at completion of an operation. */
3452
Paul Elliott59ad9452022-12-18 15:09:02 +00003453 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003454}
3455
3456psa_status_t psa_verify_hash_start(
3457 psa_verify_hash_interruptible_operation_t *operation,
3458 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3459 const uint8_t *hash, size_t hash_length,
3460 const uint8_t *signature, size_t signature_length)
3461{
3462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3463 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3464 psa_key_slot_t *slot;
3465
Paul Elliottc9774412023-02-06 15:14:07 +00003466 /* Check that start has not been previously called, or operation has not
3467 * previously errored. */
3468 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003469 return PSA_ERROR_BAD_STATE;
3470 }
3471
3472 status = psa_sign_verify_check_alg(0, alg);
3473 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003474 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475 return status;
3476 }
3477
3478 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3479 PSA_KEY_USAGE_VERIFY_HASH,
3480 alg);
3481
3482 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003483 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003484 return status;
3485 }
3486
3487 psa_key_attributes_t attributes = {
3488 .core = slot->attr
3489 };
3490
Paul Elliott296ede92022-12-15 17:00:30 +00003491 /* Ensure ops count gets reset, in case of operation re-use. */
3492 operation->num_ops = 0;
3493
Paul Elliott9fe12f62022-11-30 19:16:02 +00003494 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3495 slot->key.data,
3496 slot->key.bytes,
3497 alg, hash, hash_length,
3498 signature, signature_length);
3499
3500 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003501 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003502 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003503 }
3504
3505 unlock_status = psa_unlock_key_slot(slot);
3506
Paul Elliottc9774412023-02-06 15:14:07 +00003507 if (unlock_status != PSA_SUCCESS) {
3508 operation->error_occurred = 1;
3509 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003510
Paul Elliottc9774412023-02-06 15:14:07 +00003511 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003512}
3513
3514psa_status_t psa_verify_hash_complete(
3515 psa_verify_hash_interruptible_operation_t *operation)
3516{
3517 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3518
Paul Elliottc9774412023-02-06 15:14:07 +00003519 /* Check that start has been called first, and that operation has not
3520 * previously errored. */
3521 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003522 status = PSA_ERROR_BAD_STATE;
3523 goto exit;
3524 }
3525
3526 status = psa_driver_wrapper_verify_hash_complete(operation);
3527
Paul Elliott296ede92022-12-15 17:00:30 +00003528 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003529 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003530 operation);
3531
Paul Elliottebe225c2023-02-10 14:32:53 +00003532exit:
3533
Paul Elliott9fe12f62022-11-30 19:16:02 +00003534 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003535 if (status != PSA_SUCCESS) {
3536 operation->error_occurred = 1;
3537 }
3538
Paul Elliott59ad9452022-12-18 15:09:02 +00003539 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003540 }
3541
3542 return status;
3543}
3544
3545psa_status_t psa_verify_hash_abort(
3546 psa_verify_hash_interruptible_operation_t *operation)
3547{
Paul Elliott59ad9452022-12-18 15:09:02 +00003548 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003549
Paul Elliott59ad9452022-12-18 15:09:02 +00003550 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003551
Paul Elliott59ad9452022-12-18 15:09:02 +00003552 /* We clear the number of ops done here, so that it is not cleared when
3553 * the operation fails or succeeds, only on manual abort. */
3554 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003555
Paul Elliottc9774412023-02-06 15:14:07 +00003556 /* Likewise, failure state. */
3557 operation->error_occurred = 0;
3558
Paul Elliott59ad9452022-12-18 15:09:02 +00003559 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003560}
3561
Paul Elliott588f8ed2022-12-02 18:10:26 +00003562/****************************************************************/
3563/* Asymmetric interruptible cryptography internal */
3564/* implementations */
3565/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003566
Paul Elliott588f8ed2022-12-02 18:10:26 +00003567void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3568{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003569
Paul Elliott588f8ed2022-12-02 18:10:26 +00003570#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3571 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3572 defined(MBEDTLS_ECP_RESTARTABLE)
3573
3574 /* Internal implementation uses zero to indicate infinite number max ops,
3575 * therefore avoid this value, and set to minimum possible. */
3576 if (max_ops == 0) {
3577 max_ops = 1;
3578 }
3579
Paul Elliott588f8ed2022-12-02 18:10:26 +00003580 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003581#else
3582 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003583#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3584 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3585 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3586}
3587
Paul Elliott588f8ed2022-12-02 18:10:26 +00003588uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003589 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003590{
3591#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3592 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3593 defined(MBEDTLS_ECP_RESTARTABLE)
3594
Paul Elliott93d9ca82023-02-15 18:14:21 +00003595 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003596#else
3597 (void) operation;
3598 return 0;
3599#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3600 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3601 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3602}
3603
3604uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003605 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003606{
3607 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3608 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3609 defined(MBEDTLS_ECP_RESTARTABLE)
3610
Paul Elliott93d9ca82023-02-15 18:14:21 +00003611 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003612#else
3613 (void) operation;
3614 return 0;
3615#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3616 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3617 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3618}
3619
3620psa_status_t mbedtls_psa_sign_hash_start(
3621 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3622 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3623 size_t key_buffer_size, psa_algorithm_t alg,
3624 const uint8_t *hash, size_t hash_length)
3625{
3626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003627 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003628
Paul Elliott068fe072023-01-16 13:59:15 +00003629 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3630 return PSA_ERROR_NOT_SUPPORTED;
3631 }
3632
3633 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003634 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003635 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003636
3637#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003638 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3639 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640
Paul Elliotta1c94092023-02-15 16:38:04 +00003641 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3642
Paul Elliott93d9ca82023-02-15 18:14:21 +00003643 /* Ensure num_ops is zero'ed in case of context re-use. */
3644 operation->num_ops = 0;
3645
Paul Elliott068fe072023-01-16 13:59:15 +00003646 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3647 attributes->core.bits,
3648 key_buffer,
3649 key_buffer_size,
3650 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003651
Paul Elliott068fe072023-01-16 13:59:15 +00003652 if (status != PSA_SUCCESS) {
3653 return status;
3654 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003655
Paul Elliott1bc59df2023-02-05 13:41:57 +00003656 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003657 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003658
Paul Elliott068fe072023-01-16 13:59:15 +00003659 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003660 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003661 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003662
Paul Elliott0290a762023-02-09 14:30:24 +00003663 /* We only need to store the same length of hash as the private key size
3664 * here, it would be truncated by the internal implementation anyway. */
3665 required_hash_length = (hash_length < operation->coordinate_bytes ?
3666 hash_length : operation->coordinate_bytes);
3667
Paul Elliottba70ad42023-02-15 18:23:53 +00003668 if (required_hash_length > sizeof(operation->hash)) {
3669 /* Shouldn't happen, but better safe than sorry. */
3670 return PSA_ERROR_CORRUPTION_DETECTED;
3671 }
3672
Paul Elliott0290a762023-02-09 14:30:24 +00003673 memcpy(operation->hash, hash, required_hash_length);
3674 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003675
3676 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003677
3678#else
Paul Elliott068fe072023-01-16 13:59:15 +00003679 (void) operation;
3680 (void) key_buffer;
3681 (void) key_buffer_size;
3682 (void) alg;
3683 (void) hash;
3684 (void) hash_length;
3685 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003686 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003687
Paul Elliott068fe072023-01-16 13:59:15 +00003688 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3690 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3691 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692}
3693
3694psa_status_t mbedtls_psa_sign_hash_complete(
3695 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3696 uint8_t *signature, size_t signature_size,
3697 size_t *signature_length)
3698{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003699#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3700 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3701 defined(MBEDTLS_ECP_RESTARTABLE)
3702
Paul Elliotta1c94092023-02-15 16:38:04 +00003703 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003704 mbedtls_mpi r;
3705 mbedtls_mpi s;
3706
Paul Elliott46845252023-02-03 14:59:11 +00003707 mbedtls_mpi_init(&r);
3708 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003710 /* Ensure max_ops is set to the current value (or default). */
3711 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3712
Paul Elliotta1c94092023-02-15 16:38:04 +00003713 if (signature_size < 2 * operation->coordinate_bytes) {
3714 status = PSA_ERROR_BUFFER_TOO_SMALL;
3715 goto exit;
3716 }
3717
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003719
Paul Elliott588f8ed2022-12-02 18:10:26 +00003720#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3721 status = mbedtls_to_psa_error(
3722 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003723 &r,
3724 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725 &operation->ctx->d,
3726 operation->hash,
3727 operation->hash_length,
3728 operation->md_alg,
3729 mbedtls_psa_get_random,
3730 MBEDTLS_PSA_RANDOM_STATE,
3731 &operation->restart_ctx));
3732#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003733 status = PSA_ERROR_NOT_SUPPORTED;
3734 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003735#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3736 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003737 status = mbedtls_to_psa_error(
3738 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003739 &r,
3740 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741 &operation->ctx->d,
3742 operation->hash,
3743 operation->hash_length,
3744 mbedtls_psa_get_random,
3745 MBEDTLS_PSA_RANDOM_STATE,
3746 mbedtls_psa_get_random,
3747 MBEDTLS_PSA_RANDOM_STATE,
3748 &operation->restart_ctx));
3749 }
3750
Paul Elliottf8e5b562023-02-19 18:43:45 +00003751 /* Hide the fact that the restart context only holds a delta of number of
3752 * ops done during the last operation, not an absolute value. */
3753 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3754
Paul Elliott724bd252023-02-08 12:35:08 +00003755 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003756 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003757 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003758 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003759 operation->coordinate_bytes)
3760 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003761
3762 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003763 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003764 }
3765
3766 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003767 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003768 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003769 operation->coordinate_bytes,
3770 operation->coordinate_bytes)
3771 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003772
3773 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003774 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003775 }
3776
Paul Elliott1bc59df2023-02-05 13:41:57 +00003777 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003778
Paul Elliott724bd252023-02-08 12:35:08 +00003779 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003780 }
Paul Elliott724bd252023-02-08 12:35:08 +00003781
3782exit:
3783
3784 mbedtls_mpi_free(&r);
3785 mbedtls_mpi_free(&s);
3786 return status;
3787
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788 #else
3789
3790 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003791 (void) signature;
3792 (void) signature_size;
3793 (void) signature_length;
3794
3795 return PSA_ERROR_NOT_SUPPORTED;
3796
3797#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3798 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3799 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3800}
3801
3802psa_status_t mbedtls_psa_sign_hash_abort(
3803 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3804{
3805
3806#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3807 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3808 defined(MBEDTLS_ECP_RESTARTABLE)
3809
3810 if (operation->ctx) {
3811 mbedtls_ecdsa_free(operation->ctx);
3812 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003813 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003814 }
3815
3816 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3817
Paul Elliott93d9ca82023-02-15 18:14:21 +00003818 operation->num_ops = 0;
3819
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820 return PSA_SUCCESS;
3821
3822#else
3823
3824 (void) operation;
3825
3826 return PSA_ERROR_NOT_SUPPORTED;
3827
3828#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3829 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3830 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3831}
3832
3833psa_status_t mbedtls_psa_verify_hash_start(
3834 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3835 const psa_key_attributes_t *attributes,
3836 const uint8_t *key_buffer, size_t key_buffer_size,
3837 psa_algorithm_t alg,
3838 const uint8_t *hash, size_t hash_length,
3839 const uint8_t *signature, size_t signature_length)
3840{
3841 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003842 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003843 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844
Paul Elliott068fe072023-01-16 13:59:15 +00003845 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3846 return PSA_ERROR_NOT_SUPPORTED;
3847 }
3848
3849 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003850 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003851 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003852
3853#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003854 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3855 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856
Paul Elliotta1c94092023-02-15 16:38:04 +00003857 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3858 mbedtls_mpi_init(&operation->r);
3859 mbedtls_mpi_init(&operation->s);
3860
Paul Elliott93d9ca82023-02-15 18:14:21 +00003861 /* Ensure num_ops is zero'ed in case of context re-use. */
3862 operation->num_ops = 0;
3863
Paul Elliott068fe072023-01-16 13:59:15 +00003864 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3865 attributes->core.bits,
3866 key_buffer,
3867 key_buffer_size,
3868 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003869
Paul Elliott068fe072023-01-16 13:59:15 +00003870 if (status != PSA_SUCCESS) {
3871 return status;
3872 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003873
Paul Elliottc569fc22023-02-10 13:02:54 +00003874 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003875
Paul Elliott1bc59df2023-02-05 13:41:57 +00003876 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003877 return PSA_ERROR_INVALID_SIGNATURE;
3878 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003879
Paul Elliott068fe072023-01-16 13:59:15 +00003880 status = mbedtls_to_psa_error(
3881 mbedtls_mpi_read_binary(&operation->r,
3882 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003883 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884
Paul Elliott068fe072023-01-16 13:59:15 +00003885 if (status != PSA_SUCCESS) {
3886 return status;
3887 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003888
Paul Elliott068fe072023-01-16 13:59:15 +00003889 status = mbedtls_to_psa_error(
3890 mbedtls_mpi_read_binary(&operation->s,
3891 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003892 coordinate_bytes,
3893 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003894
Paul Elliott068fe072023-01-16 13:59:15 +00003895 if (status != PSA_SUCCESS) {
3896 return status;
3897 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003898
Paul Elliott2c9843f2023-02-15 17:32:42 +00003899 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003900
Paul Elliott2c9843f2023-02-15 17:32:42 +00003901 if (status != PSA_SUCCESS) {
3902 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003903 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003904
Paul Elliott0290a762023-02-09 14:30:24 +00003905 /* We only need to store the same length of hash as the private key size
3906 * here, it would be truncated by the internal implementation anyway. */
3907 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3908 coordinate_bytes);
3909
Paul Elliottba70ad42023-02-15 18:23:53 +00003910 if (required_hash_length > sizeof(operation->hash)) {
3911 /* Shouldn't happen, but better safe than sorry. */
3912 return PSA_ERROR_CORRUPTION_DETECTED;
3913 }
3914
Paul Elliott0290a762023-02-09 14:30:24 +00003915 memcpy(operation->hash, hash, required_hash_length);
3916 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003917
3918 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003919#else
Paul Elliott068fe072023-01-16 13:59:15 +00003920 (void) operation;
3921 (void) key_buffer;
3922 (void) key_buffer_size;
3923 (void) alg;
3924 (void) hash;
3925 (void) hash_length;
3926 (void) signature;
3927 (void) signature_length;
3928 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003929 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003930 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003931
Paul Elliott068fe072023-01-16 13:59:15 +00003932 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003933#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3934 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3935 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003936}
3937
3938psa_status_t mbedtls_psa_verify_hash_complete(
3939 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3940{
3941
3942#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3943 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3944 defined(MBEDTLS_ECP_RESTARTABLE)
3945
Paul Elliottf8e5b562023-02-19 18:43:45 +00003946 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3947
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003948 /* Ensure max_ops is set to the current value (or default). */
3949 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3950
Paul Elliottf8e5b562023-02-19 18:43:45 +00003951 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003952 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3953 operation->hash,
3954 operation->hash_length,
3955 &operation->ctx->Q,
3956 &operation->r,
3957 &operation->s,
3958 &operation->restart_ctx));
3959
Paul Elliottf8e5b562023-02-19 18:43:45 +00003960 /* Hide the fact that the restart context only holds a delta of number of
3961 * ops done during the last operation, not an absolute value. */
3962 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3963
3964 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003965#else
3966 (void) operation;
3967
3968 return PSA_ERROR_NOT_SUPPORTED;
3969
3970#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3971 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3972 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3973}
3974
3975psa_status_t mbedtls_psa_verify_hash_abort(
3976 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3977{
3978
3979#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3980 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3981 defined(MBEDTLS_ECP_RESTARTABLE)
3982
3983 if (operation->ctx) {
3984 mbedtls_ecdsa_free(operation->ctx);
3985 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003986 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003987 }
3988
3989 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3990
Paul Elliott93d9ca82023-02-15 18:14:21 +00003991 operation->num_ops = 0;
3992
Paul Elliott588f8ed2022-12-02 18:10:26 +00003993 mbedtls_mpi_free(&operation->r);
3994 mbedtls_mpi_free(&operation->s);
3995
3996 return PSA_SUCCESS;
3997
3998#else
3999 (void) operation;
4000
4001 return PSA_ERROR_NOT_SUPPORTED;
4002
4003#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4004 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4005 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4006}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004007
mohammad1603503973b2018-03-12 15:59:30 +02004008/****************************************************************/
4009/* Symmetric cryptography */
4010/****************************************************************/
4011
Gilles Peskine449bd832023-01-11 14:50:10 +01004012static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4013 mbedtls_svc_key_id_t key,
4014 psa_algorithm_t alg,
4015 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004016{
4017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4018 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004019 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4021 PSA_KEY_USAGE_ENCRYPT :
4022 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004023
4024 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004026 status = PSA_ERROR_BAD_STATE;
4027 goto exit;
4028 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004031 status = PSA_ERROR_INVALID_ARGUMENT;
4032 goto exit;
4033 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4036 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004037 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004039
Ronald Cron49fafa92021-03-10 08:34:23 +01004040 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004041 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004042 * so we only set it (in the driver wrapper) after resources have been
4043 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004044 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004046 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004048 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 }
4050 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004051
Ronald Crona4af55f2020-12-14 14:36:06 +01004052 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004054 };
4055
Ronald Cronab99ac22020-10-01 16:38:28 +02004056 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 if (cipher_operation == MBEDTLS_ENCRYPT) {
4058 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4059 &attributes,
4060 slot->key.data,
4061 slot->key.bytes,
4062 alg);
4063 } else {
4064 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4065 &attributes,
4066 slot->key.data,
4067 slot->key.bytes,
4068 alg);
4069 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004070
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004071exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 if (status != PSA_SUCCESS) {
4073 psa_cipher_abort(operation);
4074 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004077
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004079}
4080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4082 mbedtls_svc_key_id_t key,
4083 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004084{
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004086}
4087
Gilles Peskine449bd832023-01-11 14:50:10 +01004088psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4089 mbedtls_svc_key_id_t key,
4090 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004091{
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004093}
4094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4096 uint8_t *iv,
4097 size_t iv_size,
4098 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004099{
Ronald Cron6d051732020-10-01 14:10:20 +02004100 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004101 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4102 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004105 status = PSA_ERROR_BAD_STATE;
4106 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004107 }
4108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004110 status = PSA_ERROR_BAD_STATE;
4111 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004112 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004113
Ronald Cron2fb90522021-07-05 12:31:44 +02004114 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004116 status = PSA_ERROR_BUFFER_TOO_SMALL;
4117 goto exit;
4118 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004119
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004121 status = PSA_ERROR_GENERIC_ERROR;
4122 goto exit;
4123 }
4124
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 status = psa_generate_random(local_iv, default_iv_length);
4126 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 }
Ronald Cron5618a392021-03-26 09:52:26 +01004129
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 status = psa_driver_wrapper_cipher_set_iv(operation,
4131 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004132
4133exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 if (status == PSA_SUCCESS) {
4135 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004136 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004137 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004139 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004141 }
Ronald Cron6d051732020-10-01 14:10:20 +02004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004144}
4145
Gilles Peskine449bd832023-01-11 14:50:10 +01004146psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4147 const uint8_t *iv,
4148 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004149{
Ronald Cron6d051732020-10-01 14:10:20 +02004150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004153 status = PSA_ERROR_BAD_STATE;
4154 goto exit;
4155 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004158 status = PSA_ERROR_BAD_STATE;
4159 goto exit;
4160 }
Ronald Crona0d68172021-03-26 10:15:08 +01004161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004163 status = PSA_ERROR_INVALID_ARGUMENT;
4164 goto exit;
4165 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 status = psa_driver_wrapper_cipher_set_iv(operation,
4168 iv,
4169 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004170
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004171exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004173 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 } else {
4175 psa_cipher_abort(operation);
4176 }
4177 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004178}
4179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4181 const uint8_t *input,
4182 size_t input_length,
4183 uint8_t *output,
4184 size_t output_size,
4185 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004186{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004190 status = PSA_ERROR_BAD_STATE;
4191 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004192 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004195 status = PSA_ERROR_BAD_STATE;
4196 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004197 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 status = psa_driver_wrapper_cipher_update(operation,
4200 input,
4201 input_length,
4202 output,
4203 output_size,
4204 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004205
4206exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 if (status != PSA_SUCCESS) {
4208 psa_cipher_abort(operation);
4209 }
Ronald Cron6d051732020-10-01 14:10:20 +02004210
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004212}
4213
Gilles Peskine449bd832023-01-11 14:50:10 +01004214psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4215 uint8_t *output,
4216 size_t output_size,
4217 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004218{
David Saadab4ecc272019-02-14 13:48:10 +02004219 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004222 status = PSA_ERROR_BAD_STATE;
4223 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004224 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004227 status = PSA_ERROR_BAD_STATE;
4228 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004229 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004230
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 status = psa_driver_wrapper_cipher_finish(operation,
4232 output,
4233 output_size,
4234 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004235
4236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 if (status == PSA_SUCCESS) {
4238 return psa_cipher_abort(operation);
4239 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004240 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004242
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004244 }
mohammad1603503973b2018-03-12 15:59:30 +02004245}
4246
Gilles Peskine449bd832023-01-11 14:50:10 +01004247psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004248{
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004250 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004251 * in use. It's ok to call abort on such an object, and there's
4252 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004254 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004257
Ronald Cron49fafa92021-03-10 08:34:23 +01004258 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004259 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004260 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004263}
4264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4266 psa_algorithm_t alg,
4267 const uint8_t *input,
4268 size_t input_length,
4269 uint8_t *output,
4270 size_t output_size,
4271 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004272{
4273 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004274 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004275 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004276 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4277 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004280 status = PSA_ERROR_INVALID_ARGUMENT;
4281 goto exit;
4282 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4285 PSA_KEY_USAGE_ENCRYPT,
4286 alg);
4287 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004288 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004290
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004291 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004293 };
4294
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4296 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004297 status = PSA_ERROR_GENERIC_ERROR;
4298 goto exit;
4299 }
4300
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 if (default_iv_length > 0) {
4302 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004303 status = PSA_ERROR_BUFFER_TOO_SMALL;
4304 goto exit;
4305 }
4306
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 status = psa_generate_random(local_iv, default_iv_length);
4308 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004309 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004311 }
4312
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004313 status = psa_driver_wrapper_cipher_encrypt(
4314 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004315 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004316 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004318
4319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 unlock_status = psa_unlock_key_slot(slot);
4321 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004322 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004323 }
Ronald Cron23919522021-07-08 19:00:07 +02004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 if (status == PSA_SUCCESS) {
4326 if (default_iv_length > 0) {
4327 memcpy(output, local_iv, default_iv_length);
4328 }
4329 *output_length += default_iv_length;
4330 } else {
4331 *output_length = 0;
4332 }
4333
4334 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004335}
4336
Gilles Peskine449bd832023-01-11 14:50:10 +01004337psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4338 psa_algorithm_t alg,
4339 const uint8_t *input,
4340 size_t input_length,
4341 uint8_t *output,
4342 size_t output_size,
4343 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004344{
4345 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004346 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004347 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004350 status = PSA_ERROR_INVALID_ARGUMENT;
4351 goto exit;
4352 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4355 PSA_KEY_USAGE_DECRYPT,
4356 alg);
4357 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004358 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004360
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004361 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004363 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4366 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004367 status = PSA_ERROR_INVALID_ARGUMENT;
4368 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004370 status = PSA_ERROR_INVALID_ARGUMENT;
4371 goto exit;
4372 }
4373
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004374 status = psa_driver_wrapper_cipher_decrypt(
4375 &attributes, slot->key.data, slot->key.bytes,
4376 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004378
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004379exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 unlock_status = psa_unlock_key_slot(slot);
4381 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004382 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004386 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 }
Ronald Cron23919522021-07-08 19:00:07 +02004388
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004390}
4391
4392
mohammad16035955c982018-04-26 00:53:03 +03004393/****************************************************************/
4394/* AEAD */
4395/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004396
Paul Elliottbaff51c2021-09-28 17:44:45 +01004397/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004398static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004399{
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004401}
4402
4403/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004404static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4405 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004406{
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004410#if defined(PSA_WANT_ALG_GCM)
4411 case PSA_ALG_GCM:
4412 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 * arbitrarily large nonces. Please note that we do not generally
4414 * recommend the usage of nonces of greater length than
4415 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4416 * size, which can then lead to collisions if you encrypt a very
4417 * large number of messages.*/
4418 if (nonce_length != 0) {
4419 return PSA_SUCCESS;
4420 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004421 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004422#endif /* PSA_WANT_ALG_GCM */
4423#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004424 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 if (nonce_length >= 7 && nonce_length <= 13) {
4426 return PSA_SUCCESS;
4427 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004428 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004429#endif /* PSA_WANT_ALG_CCM */
4430#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004431 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 if (nonce_length == 12) {
4433 return PSA_SUCCESS;
4434 } else if (nonce_length == 8) {
4435 return PSA_ERROR_NOT_SUPPORTED;
4436 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004437 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004438#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004439 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004440 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004442 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004445}
4446
Gilles Peskine449bd832023-01-11 14:50:10 +01004447static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004448{
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4450 return PSA_ERROR_INVALID_ARGUMENT;
4451 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004454}
4455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4457 psa_algorithm_t alg,
4458 const uint8_t *nonce,
4459 size_t nonce_length,
4460 const uint8_t *additional_data,
4461 size_t additional_data_length,
4462 const uint8_t *plaintext,
4463 size_t plaintext_length,
4464 uint8_t *ciphertext,
4465 size_t ciphertext_size,
4466 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004467{
Ronald Cron215633c2021-03-16 17:15:37 +01004468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4469 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004470
mohammad1603f08a5502018-06-03 15:05:47 +03004471 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 status = psa_aead_check_algorithm(alg);
4474 if (status != PSA_SUCCESS) {
4475 return status;
4476 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004477
Ronald Cron9a986162021-03-26 12:40:07 +01004478 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4480 if (status != PSA_SUCCESS) {
4481 return status;
4482 }
mohammad16035955c982018-04-26 00:53:03 +03004483
Ronald Cron215633c2021-03-16 17:15:37 +01004484 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004486 };
mohammad16035955c982018-04-26 00:53:03 +03004487
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 status = psa_aead_check_nonce_length(alg, nonce_length);
4489 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004490 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004492
Ronald Cronde822812021-03-17 16:08:20 +01004493 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004494 &attributes, slot->key.data, slot->key.bytes,
4495 alg,
4496 nonce, nonce_length,
4497 additional_data, additional_data_length,
4498 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004500
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4502 memset(ciphertext, 0, ciphertext_size);
4503 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004504
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004505exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004507
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004509}
4510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4512 psa_algorithm_t alg,
4513 const uint8_t *nonce,
4514 size_t nonce_length,
4515 const uint8_t *additional_data,
4516 size_t additional_data_length,
4517 const uint8_t *ciphertext,
4518 size_t ciphertext_length,
4519 uint8_t *plaintext,
4520 size_t plaintext_size,
4521 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004522{
Ronald Cron215633c2021-03-16 17:15:37 +01004523 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4524 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004525
Gilles Peskineee652a32018-06-01 19:23:52 +02004526 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004527
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 status = psa_aead_check_algorithm(alg);
4529 if (status != PSA_SUCCESS) {
4530 return status;
4531 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004532
Ronald Cron9a986162021-03-26 12:40:07 +01004533 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4535 if (status != PSA_SUCCESS) {
4536 return status;
4537 }
mohammad16035955c982018-04-26 00:53:03 +03004538
Ronald Cron215633c2021-03-16 17:15:37 +01004539 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004541 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 status = psa_aead_check_nonce_length(alg, nonce_length);
4544 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004545 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004547
Ronald Cronde822812021-03-17 16:08:20 +01004548 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004549 &attributes, slot->key.data, slot->key.bytes,
4550 alg,
4551 nonce, nonce_length,
4552 additional_data, additional_data_length,
4553 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004555
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 if (status != PSA_SUCCESS && plaintext_size != 0) {
4557 memset(plaintext, 0, plaintext_size);
4558 }
mohammad160360a64d02018-06-03 17:20:42 +03004559
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004560exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 return status;
mohammad16035955c982018-04-26 00:53:03 +03004564}
4565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4567{
4568 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004571#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004573 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4575 return PSA_ERROR_INVALID_ARGUMENT;
4576 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004577 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004578#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004579
Przemek Stekiel86679c72022-10-06 17:06:56 +02004580#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004582 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4584 return PSA_ERROR_INVALID_ARGUMENT;
4585 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004586 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004587#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004588
Przemek Stekiel86679c72022-10-06 17:06:56 +02004589#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004591 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 if (tag_len != 16) {
4593 return PSA_ERROR_INVALID_ARGUMENT;
4594 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004595 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004596#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004597
4598 default:
4599 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004601 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004603}
4604
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004605/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004606static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4607 int is_encrypt,
4608 mbedtls_svc_key_id_t key,
4609 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004610{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004611 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4612 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4613 psa_key_slot_t *slot = NULL;
4614 psa_key_usage_t key_usage = 0;
4615
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 status = psa_aead_check_algorithm(alg);
4617 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004618 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004622 status = PSA_ERROR_BAD_STATE;
4623 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004624 }
4625
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 if (operation->nonce_set || operation->lengths_set ||
4627 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004628 status = PSA_ERROR_BAD_STATE;
4629 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004630 }
4631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004633 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004635 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4639 alg);
4640 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004641 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004643
4644 psa_key_attributes_t attributes = {
4645 .core = slot->attr
4646 };
4647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004649 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 if (is_encrypt) {
4653 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4654 &attributes,
4655 slot->key.data,
4656 slot->key.bytes,
4657 alg);
4658 } else {
4659 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4660 &attributes,
4661 slot->key.data,
4662 slot->key.bytes,
4663 alg);
4664 }
4665 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004670
4671exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004675 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004677 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 } else {
4679 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004680 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004683}
4684
Paul Elliott302ff6b2021-04-20 18:10:30 +01004685/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004686psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4687 mbedtls_svc_key_id_t key,
4688 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004689{
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004691}
4692
4693/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004694psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4695 mbedtls_svc_key_id_t key,
4696 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004697{
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004699}
4700
4701/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004702psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4703 uint8_t *nonce,
4704 size_t nonce_size,
4705 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004706{
4707 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004708 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004709 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004710
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004711 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004712
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004714 status = PSA_ERROR_BAD_STATE;
4715 goto exit;
4716 }
4717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004719 status = PSA_ERROR_BAD_STATE;
4720 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004721 }
4722
Paul Elliotte193ea82021-10-01 13:00:16 +01004723 /* For CCM, this size may not be correct according to the PSA
4724 * specification. The PSA Crypto 1.0.1 specification states:
4725 *
4726 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4727 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4728 *
4729 * However this restriction that L has to be the smallest integer is not
4730 * applied in practice, and it is not implementable here since the
4731 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4733 operation->alg);
4734 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004735 status = PSA_ERROR_BUFFER_TOO_SMALL;
4736 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004737 }
4738
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 status = psa_generate_random(local_nonce, required_nonce_size);
4740 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004741 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004745
Paul Elliott39dc6b82021-05-11 19:16:09 +01004746exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 if (status == PSA_SUCCESS) {
4748 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004749 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 } else {
4751 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004752 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004755}
4756
4757/* Set the nonce for a multipart authenticated encryption or decryption
4758 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004759psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4760 const uint8_t *nonce,
4761 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004762{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004763 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004766 status = PSA_ERROR_BAD_STATE;
4767 goto exit;
4768 }
4769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004771 status = PSA_ERROR_BAD_STATE;
4772 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004773 }
4774
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4776 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004777 status = PSA_ERROR_INVALID_ARGUMENT;
4778 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004779 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004780
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4782 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004783
Paul Elliott39dc6b82021-05-11 19:16:09 +01004784exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004786 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 } else {
4788 psa_aead_abort(operation);
4789 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004792}
4793
4794/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004795psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4796 size_t ad_length,
4797 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004798{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004799 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004802 status = PSA_ERROR_BAD_STATE;
4803 goto exit;
4804 }
4805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 if (operation->lengths_set || operation->ad_started ||
4807 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004808 status = PSA_ERROR_BAD_STATE;
4809 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004810 }
4811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004813#if defined(PSA_WANT_ALG_GCM)
4814 case PSA_ALG_GCM:
4815 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 * bits. Without the guard this code will generate warnings on 32bit
4817 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004818#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 if (((uint64_t) ad_length) >> 61 != 0 ||
4820 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004821 status = PSA_ERROR_INVALID_ARGUMENT;
4822 goto exit;
4823 }
Paul Elliott325d3742021-09-27 17:56:28 +01004824#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004825 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004826#endif /* PSA_WANT_ALG_GCM */
4827#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004828 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004830 status = PSA_ERROR_INVALID_ARGUMENT;
4831 goto exit;
4832 }
4833 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004834#endif /* PSA_WANT_ALG_CCM */
4835#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004836 case PSA_ALG_CHACHA20_POLY1305:
4837 /* No length restrictions for ChaChaPoly. */
4838 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004839#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004840 default:
4841 break;
4842 }
Paul Elliott325d3742021-09-27 17:56:28 +01004843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4845 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004846
Paul Elliott39dc6b82021-05-11 19:16:09 +01004847exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004849 operation->ad_remaining = ad_length;
4850 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 } else {
4853 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004854 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004855
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004857}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004858
4859/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004860psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4861 const uint8_t *input,
4862 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004863{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004864 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4865
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004867 status = PSA_ERROR_BAD_STATE;
4868 goto exit;
4869 }
4870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004872 status = PSA_ERROR_BAD_STATE;
4873 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004874 }
4875
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 if (operation->lengths_set) {
4877 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004878 status = PSA_ERROR_INVALID_ARGUMENT;
4879 goto exit;
4880 }
4881
4882 operation->ad_remaining -= input_length;
4883 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004884#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004886 status = PSA_ERROR_BAD_STATE;
4887 goto exit;
4888 }
4889#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 status = psa_driver_wrapper_aead_update_ad(operation, input,
4892 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004893
Paul Elliott39dc6b82021-05-11 19:16:09 +01004894exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004896 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 } else {
4898 psa_aead_abort(operation);
4899 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004900
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004902}
4903
4904/* Encrypt or decrypt a message fragment in an active multipart AEAD
4905 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004906psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4907 const uint8_t *input,
4908 size_t input_length,
4909 uint8_t *output,
4910 size_t output_size,
4911 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004912{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004913 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004914
4915 *output_length = 0;
4916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004918 status = PSA_ERROR_BAD_STATE;
4919 goto exit;
4920 }
4921
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004923 status = PSA_ERROR_BAD_STATE;
4924 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004925 }
4926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004928 /* Additional data length was supplied, but not all the additional
4929 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004931 status = PSA_ERROR_INVALID_ARGUMENT;
4932 goto exit;
4933 }
4934
4935 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004937 status = PSA_ERROR_INVALID_ARGUMENT;
4938 goto exit;
4939 }
4940
4941 operation->body_remaining -= input_length;
4942 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004943#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004945 status = PSA_ERROR_BAD_STATE;
4946 goto exit;
4947 }
4948#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4951 output, output_size,
4952 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004953
Paul Elliott39dc6b82021-05-11 19:16:09 +01004954exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004956 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 } else {
4958 psa_aead_abort(operation);
4959 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004962}
4963
Gilles Peskine449bd832023-01-11 14:50:10 +01004964static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004965{
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 if (operation->id == 0 || !operation->nonce_set) {
4967 return PSA_ERROR_BAD_STATE;
4968 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4971 operation->body_remaining != 0)) {
4972 return PSA_ERROR_INVALID_ARGUMENT;
4973 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004974
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004976}
4977
Paul Elliott302ff6b2021-04-20 18:10:30 +01004978/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004979psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4980 uint8_t *ciphertext,
4981 size_t ciphertext_size,
4982 size_t *ciphertext_length,
4983 uint8_t *tag,
4984 size_t tag_size,
4985 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004986{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004987 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4988
Paul Elliott302ff6b2021-04-20 18:10:30 +01004989 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004990 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004991
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 status = psa_aead_final_checks(operation);
4993 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004994 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004998 status = PSA_ERROR_BAD_STATE;
4999 goto exit;
5000 }
5001
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5003 ciphertext_size,
5004 ciphertext_length,
5005 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005006
Paul Elliott39dc6b82021-05-11 19:16:09 +01005007exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005008
5009
Paul Elliottf47b0952021-05-21 18:02:33 +01005010 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005011 * the zero tag size, make sure the tag is set to something implausible.
5012 * Even if the operation succeeds, make sure we clear the rest of the
5013 * buffer to prevent potential leakage of anything previously placed in
5014 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005015 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005016
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005020}
5021
5022/* Finish authenticating and decrypting a message in a multipart AEAD
5023 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005024psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5025 uint8_t *plaintext,
5026 size_t plaintext_size,
5027 size_t *plaintext_length,
5028 const uint8_t *tag,
5029 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005030{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005031 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5032
Paul Elliott302ff6b2021-04-20 18:10:30 +01005033 *plaintext_length = 0;
5034
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 status = psa_aead_final_checks(operation);
5036 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005037 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005039
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005041 status = PSA_ERROR_BAD_STATE;
5042 goto exit;
5043 }
5044
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5046 plaintext_size,
5047 plaintext_length,
5048 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005049
5050exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005052
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005054}
5055
5056/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005057psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005058{
5059 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5060
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005062 /* The object has (apparently) been initialized but it is not (yet)
5063 * in use. It's ok to call abort on such an object, and there's
5064 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005066 }
5067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005069
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005073}
5074
Gilles Peskinee59236f2018-01-27 23:32:46 +01005075/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005076/* Generators */
5077/****************************************************************/
5078
Przemek Stekiel69c46792022-06-10 12:59:51 +02005079#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005080 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005081 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305082 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05305083 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) || \
5084 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
John Durkop07cc04a2020-11-16 22:08:34 -08005085#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005086#endif /* At least one builtin KDF */
5087
Przemek Stekiel69c46792022-06-10 12:59:51 +02005088#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005089 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5090 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5091static psa_status_t psa_key_derivation_start_hmac(
5092 psa_mac_operation_t *operation,
5093 psa_algorithm_t hash_alg,
5094 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005096{
5097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5098 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5100 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5101 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005102
Steven Cooreman72f736a2021-05-07 14:14:37 +02005103 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 status = psa_driver_wrapper_mac_sign_setup(operation,
5107 &attributes,
5108 hmac_key, hmac_key_length,
5109 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 psa_reset_key_attributes(&attributes);
5112 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005113}
5114#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005115
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005116#define HKDF_STATE_INIT 0 /* no input yet */
5117#define HKDF_STATE_STARTED 1 /* got salt */
5118#define HKDF_STATE_KEYED 2 /* got key */
5119#define HKDF_STATE_OUTPUT 3 /* output started */
5120
Gilles Peskinecbe66502019-05-16 16:59:18 +02005121static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005123{
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5125 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5126 } else {
5127 return operation->alg;
5128 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005129}
5130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005132{
5133 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5135 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005136 /* The object has (apparently) been initialized but it is not
5137 * in use. It's ok to call abort on such an object, and there's
5138 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005140#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5142 mbedtls_free(operation->ctx.hkdf.info);
5143 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5144 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005145#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005146#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5147 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5149 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5150 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5151 if (operation->ctx.tls12_prf.secret != NULL) {
5152 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5153 operation->ctx.tls12_prf.secret_length);
5154 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005155 }
5156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 if (operation->ctx.tls12_prf.seed != NULL) {
5158 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5159 operation->ctx.tls12_prf.seed_length);
5160 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005161 }
5162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if (operation->ctx.tls12_prf.label != NULL) {
5164 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5165 operation->ctx.tls12_prf.label_length);
5166 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005167 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005168#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if (operation->ctx.tls12_prf.other_secret != NULL) {
5170 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5171 operation->ctx.tls12_prf.other_secret_length);
5172 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005173 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005174#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005175 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005176
5177 /* We leave the fields Ai and output_block to be erased safely by the
5178 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005180#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5181 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005182#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5184 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5185 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5186 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005187#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05305188#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) || \
5189 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
5190 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg) ||
5191 kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305192 if (operation->ctx.pbkdf2.salt != NULL) {
5193 mbedtls_platform_zeroize(operation->ctx.pbkdf2.salt,
5194 operation->ctx.pbkdf2.salt_length);
5195 mbedtls_free(operation->ctx.pbkdf2.salt);
5196 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305197
5198 status = PSA_SUCCESS;
5199 } else
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05305200#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) ||
5201 * defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005202 {
5203 status = PSA_ERROR_BAD_STATE;
5204 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 mbedtls_platform_zeroize(operation, sizeof(*operation));
5206 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005207}
5208
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005211{
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005215 }
5216
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005219}
5220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5222 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005223{
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 if (operation->alg == 0) {
5225 return PSA_ERROR_BAD_STATE;
5226 }
5227 if (capacity > operation->capacity) {
5228 return PSA_ERROR_INVALID_ARGUMENT;
5229 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005232}
5233
Przemek Stekiel69c46792022-06-10 12:59:51 +02005234#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005235/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005236static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5237 psa_algorithm_t kdf_alg,
5238 uint8_t *output,
5239 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005240{
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5242 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005243 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005244 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005245#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005247#else
5248 const uint8_t last_block = 0xff;
5249#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005250
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 if (hkdf->state < HKDF_STATE_KEYED ||
5252 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005253#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005255#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 )) {
5257 return PSA_ERROR_BAD_STATE;
5258 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005259 hkdf->state = HKDF_STATE_OUTPUT;
5260
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005262 /* Copy what remains of the current block */
5263 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005265 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 }
5267 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005268 output += n;
5269 output_length -= n;
5270 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005272 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005274 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005275 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005277 * object was corrupted or if this function is called directly
5278 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (hkdf->block_number == last_block) {
5280 return PSA_ERROR_BAD_STATE;
5281 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005282
5283 /* We need a new block */
5284 ++hkdf->block_number;
5285 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005286
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5288 hash_alg,
5289 hkdf->prk,
5290 hash_length);
5291 if (status != PSA_SUCCESS) {
5292 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005293 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005294
5295 if (hkdf->block_number != 1) {
5296 status = psa_mac_update(&hkdf->hmac,
5297 hkdf->output_block,
5298 hash_length);
5299 if (status != PSA_SUCCESS) {
5300 return status;
5301 }
5302 }
5303 status = psa_mac_update(&hkdf->hmac,
5304 hkdf->info,
5305 hkdf->info_length);
5306 if (status != PSA_SUCCESS) {
5307 return status;
5308 }
5309 status = psa_mac_update(&hkdf->hmac,
5310 &hkdf->block_number, 1);
5311 if (status != PSA_SUCCESS) {
5312 return status;
5313 }
5314 status = psa_mac_sign_finish(&hkdf->hmac,
5315 hkdf->output_block,
5316 sizeof(hkdf->output_block),
5317 &hmac_output_length);
5318 if (status != PSA_SUCCESS) {
5319 return status;
5320 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005321 }
5322
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005324}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005325#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005326
John Durkop07cc04a2020-11-16 22:08:34 -08005327#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5328 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005329static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5330 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005332{
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5334 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005335 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5336 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005337 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005338
Janos Follath7742fee2019-06-17 12:58:10 +01005339 /* We can't be wanting more output after block 0xff, otherwise
5340 * the capacity check in psa_key_derivation_output_bytes() would have
5341 * prevented this call. It could happen only if the operation
5342 * object was corrupted or if this function is called directly
5343 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 if (tls12_prf->block_number == 0xff) {
5345 return PSA_ERROR_CORRUPTION_DETECTED;
5346 }
Janos Follath7742fee2019-06-17 12:58:10 +01005347
5348 /* We need a new block */
5349 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005350 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005351
5352 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5353 *
5354 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5355 *
5356 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5357 * HMAC_hash(secret, A(2) + seed) +
5358 * HMAC_hash(secret, A(3) + seed) + ...
5359 *
5360 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005361 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005362 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005363 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005364 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005365 * is currently extracted as `output_block` and where i is
5366 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005367 */
5368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 status = psa_key_derivation_start_hmac(&hmac,
5370 hash_alg,
5371 tls12_prf->secret,
5372 tls12_prf->secret_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
5377 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005379 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5380 * the variable seed and in this instance means it in the context of the
5381 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 status = psa_mac_update(&hmac,
5383 tls12_prf->label,
5384 tls12_prf->label_length);
5385 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005386 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 }
5388 status = psa_mac_update(&hmac,
5389 tls12_prf->seed,
5390 tls12_prf->seed_length);
5391 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005392 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 }
5394 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005395 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5397 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005398 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005400 }
5401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 status = psa_mac_sign_finish(&hmac,
5403 tls12_prf->Ai, hash_length,
5404 &hmac_output_length);
5405 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005406 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 }
5408 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005409 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005411
5412 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 status = psa_key_derivation_start_hmac(&hmac,
5414 hash_alg,
5415 tls12_prf->secret,
5416 tls12_prf->secret_length);
5417 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005418 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 }
5420 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5421 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005422 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 }
5424 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5425 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005426 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 }
5428 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5429 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005430 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 }
5432 status = psa_mac_sign_finish(&hmac,
5433 tls12_prf->output_block, hash_length,
5434 &hmac_output_length);
5435 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005436 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005438
Janos Follath7742fee2019-06-17 12:58:10 +01005439
5440cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 cleanup_status = psa_mac_abort(&hmac);
5442 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005443 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005445
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005447}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005448
Janos Follath844eb0e2019-06-19 12:10:49 +01005449static psa_status_t psa_key_derivation_tls12_prf_read(
5450 psa_tls12_prf_key_derivation_t *tls12_prf,
5451 psa_algorithm_t alg,
5452 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005454{
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5456 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005457 psa_status_t status;
5458 uint8_t offset, length;
5459
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005461 case PSA_TLS12_PRF_STATE_LABEL_SET:
5462 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5463 break;
5464 case PSA_TLS12_PRF_STATE_OUTPUT:
5465 break;
5466 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005468 }
5469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005471 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 if (tls12_prf->left_in_block == 0) {
5473 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5474 alg);
5475 if (status != PSA_SUCCESS) {
5476 return status;
5477 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005478
5479 continue;
5480 }
5481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005483 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005485 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005487
5488 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005490 output += length;
5491 output_length -= length;
5492 tls12_prf->left_in_block -= length;
5493 }
5494
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005496}
John Durkop07cc04a2020-11-16 22:08:34 -08005497#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5498 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005499
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005500#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5501static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5502 psa_tls12_ecjpake_to_pms_t *ecjpake,
5503 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005505{
5506 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005507 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005508
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 if (output_length != 32) {
5510 return PSA_ERROR_INVALID_ARGUMENT;
5511 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005512
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5514 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5515 &output_size);
5516 if (status != PSA_SUCCESS) {
5517 return status;
5518 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 if (output_size != output_length) {
5521 return PSA_ERROR_GENERIC_ERROR;
5522 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005523
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005525}
5526#endif
5527
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05305528#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) || \
5529 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305530static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5531 psa_pbkdf2_key_derivation_t *pbkdf2,
5532 psa_algorithm_t prf_alg,
5533 uint8_t prf_output_length,
5534 psa_key_attributes_t *attributes)
5535{
5536 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305537 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305538 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305539 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305540 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305541 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305542 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305543
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305544 mac_operation.is_sign = 1;
5545 mac_operation.mac_size = prf_output_length;
5546 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305547
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305548 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5549 attributes,
5550 pbkdf2->password,
5551 pbkdf2->password_length,
5552 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305553 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305554 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305555 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305556 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5557 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305558 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305559 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305560 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305561 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305562 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305563 }
5564 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5565 &mac_output_length);
5566 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305567 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305568 }
5569
5570 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305571 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305572 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305573 }
5574
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305575 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305576
5577 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305578 /* We are passing prf_output_length as mac_size because the driver
5579 * function directly sets mac_output_length as mac_size upon success.
5580 * See #7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305581 status = psa_driver_wrapper_mac_compute(attributes,
5582 pbkdf2->password,
5583 pbkdf2->password_length,
5584 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305585 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305586 &mac_output_length);
5587 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305588 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305589 }
5590
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305591 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305592 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305593
5594cleanup:
5595 /* Zeroise buffers to clear sensitive data from memory. */
5596 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5597 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305598}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305599
5600static psa_status_t psa_key_derivation_pbkdf2_read(
5601 psa_pbkdf2_key_derivation_t *pbkdf2,
5602 psa_algorithm_t kdf_alg,
5603 uint8_t *output,
5604 size_t output_length)
5605{
5606 psa_status_t status;
5607 psa_algorithm_t prf_alg;
5608 uint8_t prf_output_length;
5609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5610 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5611 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5612
5613 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5614 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5615 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5616 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305617 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5618 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05305619 prf_output_length = PSA_AES_CMAC_PRF_128_OUTPUT_SIZE;
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305620 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05305621 } else {
5622 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305623 }
5624
5625 switch (pbkdf2->state) {
5626 case PSA_PBKDF2_STATE_PASSWORD_SET:
5627 /* Initially we need a new block so bytes_used is equal to block size*/
5628 pbkdf2->bytes_used = prf_output_length;
5629 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5630 break;
5631 case PSA_PBKDF2_STATE_OUTPUT:
5632 break;
5633 default:
5634 return PSA_ERROR_BAD_STATE;
5635 }
5636
5637 while (output_length != 0) {
5638 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5639 if (n > output_length) {
5640 n = (uint8_t) output_length;
5641 }
5642 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5643 output += n;
5644 output_length -= n;
5645 pbkdf2->bytes_used += n;
5646
5647 if (output_length == 0) {
5648 break;
5649 }
5650
5651 /* We need a new block */
5652 pbkdf2->bytes_used = 0;
5653 pbkdf2->block_number++;
5654
5655 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5656 prf_output_length,
5657 &attributes);
5658 if (status != PSA_SUCCESS) {
5659 return status;
5660 }
5661 }
5662
5663 return PSA_SUCCESS;
5664}
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05305665#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC ||
5666 * MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305667
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005668psa_status_t psa_key_derivation_output_bytes(
5669 psa_key_derivation_operation_t *operation,
5670 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005672{
5673 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005677 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005679 }
5680
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005683 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005684 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005685 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005686 goto exit;
5687 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005689 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005690 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005691 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5692 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005693 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005694 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005696 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005697 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005698
Przemek Stekiel69c46792022-06-10 12:59:51 +02005699#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5701 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5702 output, output_length);
5703 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005704#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005705#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5706 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5708 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5709 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5710 kdf_alg, output,
5711 output_length);
5712 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005713#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5714 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005715#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005717 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5719 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005720#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05305721#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) || \
5722 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
5723 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg) ||
5724 kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305725 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5726 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305727 } else
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05305728#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC ||
5729 * MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005730
Gilles Peskineeab56e42018-07-12 17:12:33 +02005731 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005732 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005734 }
5735
5736exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005738 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005739 * This allows us to differentiate between exhausted operations and
5740 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5741 * operations. */
5742 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005746 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005748}
5749
David Brown7807bf72021-02-09 16:28:23 -07005750#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005751static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005752{
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 if (data_size >= 8) {
5754 mbedtls_des_key_set_parity(data);
5755 }
5756 if (data_size >= 16) {
5757 mbedtls_des_key_set_parity(data + 8);
5758 }
5759 if (data_size >= 24) {
5760 mbedtls_des_key_set_parity(data + 16);
5761 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005762}
David Brown7807bf72021-02-09 16:28:23 -07005763#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005764
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005765/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 * ECC keys on a Weierstrass elliptic curve require the generation
5767 * of a private key which is an integer
5768 * in the range [1, N - 1], where N is the boundary of the private key domain:
5769 * N is the prime p for Diffie-Hellman, or the order of the
5770 * curve’s base point for ECC.
5771 *
5772 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5773 * This function generates the private key using the following process:
5774 *
5775 * 1. Draw a byte string of length ceiling(m/8) bytes.
5776 * 2. If m is not a multiple of 8, set the most significant
5777 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5778 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5779 * 4. If k > N - 2, discard the result and return to step 1.
5780 * 5. Output k + 1 as the private key.
5781 *
5782 * This method allows compliance to NIST standards, specifically the methods titled
5783 * Key-Pair Generation by Testing Candidates in the following publications:
5784 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5785 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5786 * Diffie-Hellman keys.
5787 *
5788 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5789 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5790 *
5791 * Note: Function allocates memory for *data buffer, so given *data should be
5792 * always NULL.
5793 */
Valerio Setti8bb57632023-05-26 13:48:07 +02005794#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY) || \
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005795 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005796 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5797 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5798 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005799static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005800 psa_key_slot_t *slot,
5801 size_t bits,
5802 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005803 uint8_t **data
5804 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005805{
Valerio Setti52789862023-04-07 12:13:11 +02005806#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005807 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005808 mbedtls_mpi k;
5809 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005811 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 mbedtls_mpi_init(&k);
5814 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005815
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005816 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005818 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005822 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005823 goto cleanup;
5824 }
5825
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005826 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005830
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005831 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005832 /* Let m be the bit size of N. */
5833 size_t m = ecp_group.nbits;
5834
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005836
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005837 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005839
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005840 /* Note: This function is always called with *data == NULL and it
5841 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 *data = mbedtls_calloc(1, m_bytes);
5843 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005844 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005845 goto cleanup;
5846 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005849 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005851 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005853
5854 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005856 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005857 * (8 * ceiling(m/8) - m) bits of the first byte in
5858 * the string to zero.
5859 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005861 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005862 }
5863
5864 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 * big-endian byte string.
5866 */
5867 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005868
5869 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 * Result of comparison is returned. When it indicates error
5871 * then this function is called again.
5872 */
5873 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005874 }
5875
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005876 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5878 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005879cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 if (ret != 0) {
5881 status = mbedtls_to_psa_error(ret);
5882 }
5883 if (status != PSA_SUCCESS) {
5884 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005885 *data = NULL;
5886 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 mbedtls_mpi_free(&k);
5888 mbedtls_mpi_free(&diff_N_2);
5889 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005890#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005891 (void) slot;
5892 (void) bits;
5893 (void) operation;
5894 (void) data;
5895 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005896#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005897}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005898
5899/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5900 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5901 *
5902 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5903 * draw a 32-byte string and process it as specified in
5904 * Elliptic Curves for Security [RFC7748] §5.
5905 *
5906 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5907 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5908 *
5909 * Note: Function allocates memory for *data buffer, so given *data should be
5910 * always NULL.
5911 */
5912
5913static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5914 size_t bits,
5915 psa_key_derivation_operation_t *operation,
5916 uint8_t **data
5917 )
5918{
5919 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005923 case 255:
5924 output_length = 32;
5925 break;
5926 case 448:
5927 output_length = 56;
5928 break;
5929 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005931 break;
5932 }
5933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 if (*data == NULL) {
5937 return PSA_ERROR_INSUFFICIENT_MEMORY;
5938 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005939
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005941
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005943 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005945
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005947 case 255:
5948 (*data)[0] &= 248;
5949 (*data)[31] &= 127;
5950 (*data)[31] |= 64;
5951 break;
5952 case 448:
5953 (*data)[0] &= 252;
5954 (*data)[55] |= 128;
5955 break;
5956 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005958 break;
5959 }
5960
5961 return status;
5962}
Valerio Setti8bb57632023-05-26 13:48:07 +02005963#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY) ||
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005964 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005965 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5966 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5967 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005968
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005969static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005970 psa_key_slot_t *slot,
5971 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005973{
5974 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305976 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005977 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5980 return PSA_ERROR_INVALID_ARGUMENT;
5981 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005982
Valerio Setti8bb57632023-05-26 13:48:07 +02005983#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY) || \
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005984 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005985 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5986 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5987 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5989 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5990 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005991 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5993 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005994 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 }
5996 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005997 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5999 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006000 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006002 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006003 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02006004#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY) ||
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01006005 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01006006 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
6007 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
6008 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 if (key_type_is_raw_bytes(slot->attr.type)) {
6010 if (bits % 8 != 0) {
6011 return PSA_ERROR_INVALID_ARGUMENT;
6012 }
6013 data = mbedtls_calloc(1, bytes);
6014 if (data == NULL) {
6015 return PSA_ERROR_INSUFFICIENT_MEMORY;
6016 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006017
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 status = psa_key_derivation_output_bytes(operation, data, bytes);
6019 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006020 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 }
David Brown12ca5032021-02-11 11:02:00 -07006022#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6024 psa_des_set_key_parity(data, bytes);
6025 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006026#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 } else {
6028 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006029 }
Ronald Crondd04d422020-11-28 15:14:42 +01006030
Ronald Cronbf33c932020-11-28 18:06:53 +01006031 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006032 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006034 };
6035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6037 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6038 &storage_size);
6039 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306040 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 }
Archanad8a83dc2021-06-14 10:04:16 +05306042 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 status = psa_allocate_buffer_to_slot(slot, storage_size);
6044 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306045 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 }
Archanad8a83dc2021-06-14 10:04:16 +05306047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 status = psa_driver_wrapper_import_key(&attributes,
6049 data, bytes,
6050 slot->key.data,
6051 slot->key.bytes,
6052 &slot->key.bytes, &bits);
6053 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006054 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006056
6057exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 mbedtls_free(data);
6059 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006060}
6061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6063 psa_key_derivation_operation_t *operation,
6064 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006065{
6066 psa_status_t status;
6067 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006068 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006069
Ronald Cron81709fc2020-11-14 12:10:32 +01006070 *key = MBEDTLS_SVC_KEY_ID_INIT;
6071
Gilles Peskine0f84d622019-09-12 19:03:13 +02006072 /* Reject any attempt to create a zero-length key so that we don't
6073 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 if (psa_get_key_bits(attributes) == 0) {
6075 return PSA_ERROR_INVALID_ARGUMENT;
6076 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (operation->alg == PSA_ALG_NONE) {
6079 return PSA_ERROR_BAD_STATE;
6080 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006081
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 if (!operation->can_output_key) {
6083 return PSA_ERROR_NOT_PERMITTED;
6084 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006085
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6087 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006088#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006090 /* Deriving a key in a secure element is not implemented yet. */
6091 status = PSA_ERROR_NOT_SUPPORTED;
6092 }
6093#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 if (status == PSA_SUCCESS) {
6095 status = psa_generate_derived_key_internal(slot,
6096 attributes->core.bits,
6097 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006098 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 if (status == PSA_SUCCESS) {
6100 status = psa_finish_key_creation(slot, driver, key);
6101 }
6102 if (status != PSA_SUCCESS) {
6103 psa_fail_key_creation(slot, driver);
6104 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006107}
6108
Gilles Peskineeab56e42018-07-12 17:12:33 +02006109
6110
6111/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006112/* Key derivation */
6113/****************************************************************/
6114
Steven Cooreman932ffb72021-02-15 12:14:32 +01006115#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006116static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006117{
6118#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6120 return 1;
6121 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006122#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006123#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6125 return 1;
6126 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006127#endif
6128#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6130 return 1;
6131 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006132#endif
6133#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6135 return 1;
6136 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006137#endif
6138#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6140 return 1;
6141 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006142#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006143#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6145 return 1;
6146 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006147#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306148#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6149 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6150 return 1;
6151 }
6152#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306153#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6154 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6155 return 1;
6156 }
6157#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006159}
6160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006162{
6163 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 psa_status_t status = psa_hash_setup(&operation, alg);
6165 psa_hash_abort(&operation);
6166 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006167}
6168
Gilles Peskine969c5d62019-01-16 15:53:06 +01006169static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006170 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006172{
Janos Follath5fe19732019-06-20 15:09:30 +01006173 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6174 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006176
Gilles Peskine969c5d62019-01-16 15:53:06 +01006177 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 if (!is_kdf_alg_supported(kdf_alg)) {
6179 return PSA_ERROR_NOT_SUPPORTED;
6180 }
John Durkop07cc04a2020-11-16 22:08:34 -08006181
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006182 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306183 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6185 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306186 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6187 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6188 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306189 hash_size = PSA_AES_CMAC_PRF_128_OUTPUT_SIZE;
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306190 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 if (hash_size == 0) {
6192 return PSA_ERROR_NOT_SUPPORTED;
6193 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006194
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006195 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6196 * we might fail later, which is somewhat unfriendly and potentially
6197 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 psa_status_t status = psa_hash_try_support(hash_alg);
6199 if (status != PSA_SUCCESS) {
6200 return status;
6201 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006202 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6205 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6206 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6207 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006208 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006209#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006210 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6212 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006213 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006215#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6216 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 operation->capacity = 255 * hash_size;
6218 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006219}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006220
Gilles Peskine449bd832023-01-11 14:50:10 +01006221static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006222{
6223#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 if (alg == PSA_ALG_ECDH) {
6225 return PSA_SUCCESS;
6226 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006227#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006228#if defined(PSA_WANT_ALG_FFDH)
6229 if (alg == PSA_ALG_FFDH) {
6230 return PSA_SUCCESS;
6231 }
6232#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006233 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006235}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006236
6237static int psa_key_derivation_allows_free_form_secret_input(
6238 psa_algorithm_t kdf_alg)
6239{
6240#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6241 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6242 return 0;
6243 }
6244#endif
6245 (void) kdf_alg;
6246 return 1;
6247}
John Durkop07cc04a2020-11-16 22:08:34 -08006248#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006249
Gilles Peskine449bd832023-01-11 14:50:10 +01006250psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6251 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006252{
6253 psa_status_t status;
6254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 if (operation->alg != 0) {
6256 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006257 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6260 return PSA_ERROR_INVALID_ARGUMENT;
6261 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6262#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6263 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6264 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6265 status = psa_key_agreement_try_support(ka_alg);
6266 if (status != PSA_SUCCESS) {
6267 return status;
6268 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006269 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6270 return PSA_ERROR_INVALID_ARGUMENT;
6271 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6273#else
6274 return PSA_ERROR_NOT_SUPPORTED;
6275#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6276 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6277#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6278 status = psa_key_derivation_setup_kdf(operation, alg);
6279#else
6280 return PSA_ERROR_NOT_SUPPORTED;
6281#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6282 } else {
6283 return PSA_ERROR_INVALID_ARGUMENT;
6284 }
6285
6286 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006287 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 }
6289 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006290}
6291
Przemek Stekiel69c46792022-06-10 12:59:51 +02006292#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006293static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6294 psa_algorithm_t kdf_alg,
6295 psa_key_derivation_step_t step,
6296 const uint8_t *data,
6297 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006298{
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006300 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006302 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006303#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6305 return PSA_ERROR_INVALID_ARGUMENT;
6306 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006307#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 if (hkdf->state != HKDF_STATE_INIT) {
6309 return PSA_ERROR_BAD_STATE;
6310 } else {
6311 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6312 hash_alg,
6313 data, data_length);
6314 if (status != PSA_SUCCESS) {
6315 return status;
6316 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006317 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006319 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006320 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006321#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006323 /* We shouldn't be in different state as HKDF_EXPAND only allows
6324 * two inputs: SECRET (this case) and INFO which does not modify
6325 * the state. It could happen only if the hkdf
6326 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 if (hkdf->state != HKDF_STATE_INIT) {
6328 return PSA_ERROR_BAD_STATE;
6329 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006330
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006331 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6333 return PSA_ERROR_INVALID_ARGUMENT;
6334 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006335
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 memcpy(hkdf->prk, data, data_length);
6337 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006338#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006339 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006340 /* HKDF: If no salt was provided, use an empty salt.
6341 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006343#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6345 return PSA_ERROR_BAD_STATE;
6346 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006347#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6349 hash_alg,
6350 NULL, 0);
6351 if (status != PSA_SUCCESS) {
6352 return status;
6353 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006354 hkdf->state = HKDF_STATE_STARTED;
6355 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 if (hkdf->state != HKDF_STATE_STARTED) {
6357 return PSA_ERROR_BAD_STATE;
6358 }
6359 status = psa_mac_update(&hkdf->hmac,
6360 data, data_length);
6361 if (status != PSA_SUCCESS) {
6362 return status;
6363 }
6364 status = psa_mac_sign_finish(&hkdf->hmac,
6365 hkdf->prk,
6366 sizeof(hkdf->prk),
6367 &data_length);
6368 if (status != PSA_SUCCESS) {
6369 return status;
6370 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006371 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006372
Gilles Peskine2b522db2019-04-12 15:11:49 +02006373 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006374 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006375#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006377 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006379 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006381#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006382 {
6383 /* Block 0 is empty, and the next block will be
6384 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006386 }
6387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006389 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006390#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6392 return PSA_ERROR_INVALID_ARGUMENT;
6393 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006394#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006395#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6397 hkdf->state == HKDF_STATE_INIT) {
6398 return PSA_ERROR_BAD_STATE;
6399 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006400#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 if (hkdf->state == HKDF_STATE_OUTPUT) {
6402 return PSA_ERROR_BAD_STATE;
6403 }
6404 if (hkdf->info_set) {
6405 return PSA_ERROR_BAD_STATE;
6406 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006407 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 if (data_length != 0) {
6409 hkdf->info = mbedtls_calloc(1, data_length);
6410 if (hkdf->info == NULL) {
6411 return PSA_ERROR_INSUFFICIENT_MEMORY;
6412 }
6413 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006414 }
6415 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006417 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006419 }
6420}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006421#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006422
John Durkop07cc04a2020-11-16 22:08:34 -08006423#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6424 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006425static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6426 const uint8_t *data,
6427 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006428{
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6430 return PSA_ERROR_BAD_STATE;
6431 }
Janos Follathf08e2652019-06-13 09:05:41 +01006432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 if (data_length != 0) {
6434 prf->seed = mbedtls_calloc(1, data_length);
6435 if (prf->seed == NULL) {
6436 return PSA_ERROR_INSUFFICIENT_MEMORY;
6437 }
Janos Follathf08e2652019-06-13 09:05:41 +01006438
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006440 prf->seed_length = data_length;
6441 }
Janos Follathf08e2652019-06-13 09:05:41 +01006442
Gilles Peskine43f958b2020-12-13 14:55:14 +01006443 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006444
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006446}
6447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6449 const uint8_t *data,
6450 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006451{
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6453 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6454 return PSA_ERROR_BAD_STATE;
6455 }
Janos Follath81550542019-06-13 14:26:34 +01006456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 if (data_length != 0) {
6458 prf->secret = mbedtls_calloc(1, data_length);
6459 if (prf->secret == NULL) {
6460 return PSA_ERROR_INSUFFICIENT_MEMORY;
6461 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006464 prf->secret_length = data_length;
6465 }
Janos Follath81550542019-06-13 14:26:34 +01006466
Gilles Peskine43f958b2020-12-13 14:55:14 +01006467 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006470}
6471
Gilles Peskine449bd832023-01-11 14:50:10 +01006472static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6473 const uint8_t *data,
6474 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006475{
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6477 return PSA_ERROR_BAD_STATE;
6478 }
Janos Follath63028dd2019-06-13 09:15:47 +01006479
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 if (data_length != 0) {
6481 prf->label = mbedtls_calloc(1, data_length);
6482 if (prf->label == NULL) {
6483 return PSA_ERROR_INSUFFICIENT_MEMORY;
6484 }
Janos Follath63028dd2019-06-13 09:15:47 +01006485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006487 prf->label_length = data_length;
6488 }
Janos Follath63028dd2019-06-13 09:15:47 +01006489
Gilles Peskine43f958b2020-12-13 14:55:14 +01006490 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006493}
6494
Gilles Peskine449bd832023-01-11 14:50:10 +01006495static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6496 psa_key_derivation_step_t step,
6497 const uint8_t *data,
6498 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006499{
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006501 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006503 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006505 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006507 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006509 }
6510}
John Durkop07cc04a2020-11-16 22:08:34 -08006511#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6512 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6513
6514#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6515static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6516 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006517 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006519{
6520 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6522 4 + data_length + prf->other_secret_length :
6523 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6526 return PSA_ERROR_INVALID_ARGUMENT;
6527 }
John Durkop07cc04a2020-11-16 22:08:34 -08006528
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 uint8_t *pms = mbedtls_calloc(1, pms_len);
6530 if (pms == NULL) {
6531 return PSA_ERROR_INSUFFICIENT_MEMORY;
6532 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006533 uint8_t *cur = pms;
6534
6535 /* pure-PSK:
6536 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006537 *
6538 * The premaster secret is formed as follows: if the PSK is N octets
6539 * long, concatenate a uint16 with the value N, N zero octets, a second
6540 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006541 *
6542 * mixed-PSK:
6543 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6544 * follows: concatenate a uint16 with the length of the other secret,
6545 * the other secret itself, uint16 with the length of PSK, and the
6546 * PSK itself.
6547 * For details please check:
6548 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6549 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6550 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006551 */
6552
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6554 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6555 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6556 if (prf->other_secret_length != 0) {
6557 memcpy(cur, prf->other_secret, prf->other_secret_length);
6558 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006559 cur += prf->other_secret_length;
6560 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 } else {
6562 *cur++ = MBEDTLS_BYTE_1(data_length);
6563 *cur++ = MBEDTLS_BYTE_0(data_length);
6564 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006565 cur += data_length;
6566 }
6567
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 *cur++ = MBEDTLS_BYTE_1(data_length);
6569 *cur++ = MBEDTLS_BYTE_0(data_length);
6570 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006571 cur += data_length;
6572
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006574
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 mbedtls_platform_zeroize(pms, pms_len);
6576 mbedtls_free(pms);
6577 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006578}
Janos Follath6660f0e2019-06-17 08:44:03 +01006579
Przemek Stekiele47201b2022-04-19 13:53:28 +02006580static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6581 psa_tls12_prf_key_derivation_t *prf,
6582 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006584{
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6586 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006587 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006588
6589 if (data_length != 0) {
6590 prf->other_secret = mbedtls_calloc(1, data_length);
6591 if (prf->other_secret == NULL) {
6592 return PSA_ERROR_INSUFFICIENT_MEMORY;
6593 }
6594
6595 memcpy(prf->other_secret, data, data_length);
6596 prf->other_secret_length = data_length;
6597 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006598 prf->other_secret_length = 0;
6599 }
6600
6601 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006604}
6605
Janos Follath6660f0e2019-06-17 08:44:03 +01006606static psa_status_t psa_tls12_prf_psk_to_ms_input(
6607 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006608 psa_key_derivation_step_t step,
6609 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006611{
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006613 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 return psa_tls12_prf_psk_to_ms_set_key(prf,
6615 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006616 break;
6617 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6619 data,
6620 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006621 break;
6622 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006624 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006625
Przemek Stekiele47201b2022-04-19 13:53:28 +02006626 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006627}
John Durkop07cc04a2020-11-16 22:08:34 -08006628#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006629
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006630#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6631static psa_status_t psa_tls12_ecjpake_to_pms_input(
6632 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006633 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006634 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006636{
Gilles Peskine449bd832023-01-11 14:50:10 +01006637 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6638 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6639 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006640 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006641
6642 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 if (data[0] != 0x04) {
6644 return PSA_ERROR_INVALID_ARGUMENT;
6645 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006646
6647 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006649
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006651}
6652#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306653
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05306654#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) || \
6655 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306656static psa_status_t psa_pbkdf2_set_input_cost(
6657 psa_pbkdf2_key_derivation_t *pbkdf2,
6658 psa_key_derivation_step_t step,
6659 uint64_t data)
6660{
6661 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6662 return PSA_ERROR_INVALID_ARGUMENT;
6663 }
6664
6665 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6666 return PSA_ERROR_BAD_STATE;
6667 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306668
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306669 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306670 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306671 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306672
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306673 if (data == 0) {
6674 return PSA_ERROR_INVALID_ARGUMENT;
6675 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306676
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306677 pbkdf2->input_cost = data;
6678 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6679
6680 return PSA_SUCCESS;
6681}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306682
6683static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6684 const uint8_t *data,
6685 size_t data_length)
6686{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306687 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6688 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6689 return PSA_ERROR_BAD_STATE;
6690 }
6691
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306692 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6693 pbkdf2->salt = mbedtls_calloc(1, data_length);
6694 if (pbkdf2->salt == NULL) {
6695 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306696 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306697
6698 memcpy(pbkdf2->salt, data, data_length);
6699 pbkdf2->salt_length = data_length;
6700 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306701 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306702
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306703 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6704 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306705 return PSA_ERROR_INSUFFICIENT_MEMORY;
6706 }
6707
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306708 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6709 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306710 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306711 mbedtls_free(pbkdf2->salt);
6712 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306713 }
6714
6715 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6716
6717 return PSA_SUCCESS;
6718}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306719
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306720static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306721 const uint8_t *input,
6722 size_t input_len,
6723 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306724 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306725{
6726 psa_status_t status = PSA_SUCCESS;
6727 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6728 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306729 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306730 } else {
6731 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306732 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306733 }
6734 return status;
6735}
6736
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306737static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6738 size_t input_len,
6739 uint8_t *output,
6740 size_t *output_len)
6741{
6742 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306743 if (input_len != PSA_AES_CMAC_PRF_128_OUTPUT_SIZE) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306745 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306746 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6747 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6748 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306749 /* Passing PSA_AES_CMAC_PRF_128_OUTPUT_SIZE as mac_size as the driver
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306750 * function sets mac_output_length = mac_size on success. See #7801*/
6751 status = psa_driver_wrapper_mac_compute(&attributes,
6752 zeros, sizeof(zeros),
6753 PSA_ALG_CMAC, input, input_len,
6754 output,
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306755 PSA_AES_CMAC_PRF_128_OUTPUT_SIZE,
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306756 output_len);
6757 } else {
6758 memcpy(output, input, input_len);
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306759 *output_len = PSA_AES_CMAC_PRF_128_OUTPUT_SIZE;
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306760 }
6761 return status;
6762}
6763
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306764static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306765 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306766 const uint8_t *data,
6767 size_t data_length)
6768{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306769 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306770 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6771 return PSA_ERROR_BAD_STATE;
6772 }
6773
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306774 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6775 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6776 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6777 pbkdf2->password,
6778 &pbkdf2->password_length);
6779 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6780 status = psa_pbkdf2_cmac_set_password(data, data_length,
6781 pbkdf2->password,
6782 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306783 }
6784
6785 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6786
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306787 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306788}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306789
6790static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306791 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306792 psa_key_derivation_step_t step,
6793 const uint8_t *data,
6794 size_t data_length)
6795{
6796 switch (step) {
6797 case PSA_KEY_DERIVATION_INPUT_SALT:
6798 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6799 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306800 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306801 default:
6802 return PSA_ERROR_INVALID_ARGUMENT;
6803 }
6804}
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05306805#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC ||
6806 * MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306807
Gilles Peskineb8965192019-09-24 16:21:10 +02006808/** Check whether the given key type is acceptable for the given
6809 * input step of a key derivation.
6810 *
6811 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6812 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6813 * Both secret and non-secret inputs can alternatively have the type
6814 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6815 * that the input was passed as a buffer rather than via a key object.
6816 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006817static int psa_key_derivation_check_input_type(
6818 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006820{
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006822 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 if (key_type == PSA_KEY_TYPE_DERIVE) {
6824 return PSA_SUCCESS;
6825 }
6826 if (key_type == PSA_KEY_TYPE_NONE) {
6827 return PSA_SUCCESS;
6828 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006829 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006830 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 if (key_type == PSA_KEY_TYPE_DERIVE) {
6832 return PSA_SUCCESS;
6833 }
6834 if (key_type == PSA_KEY_TYPE_NONE) {
6835 return PSA_SUCCESS;
6836 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006837 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006838 case PSA_KEY_DERIVATION_INPUT_LABEL:
6839 case PSA_KEY_DERIVATION_INPUT_SALT:
6840 case PSA_KEY_DERIVATION_INPUT_INFO:
6841 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6843 return PSA_SUCCESS;
6844 }
6845 if (key_type == PSA_KEY_TYPE_NONE) {
6846 return PSA_SUCCESS;
6847 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006848 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306849 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6850 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6851 return PSA_SUCCESS;
6852 }
6853 if (key_type == PSA_KEY_TYPE_DERIVE) {
6854 return PSA_SUCCESS;
6855 }
6856 if (key_type == PSA_KEY_TYPE_NONE) {
6857 return PSA_SUCCESS;
6858 }
6859 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006860 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006862}
6863
Janos Follathb80a94e2019-06-12 15:54:46 +01006864static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006865 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006866 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006867 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006868 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006870{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006871 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006873
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 status = psa_key_derivation_check_input_type(step, key_type);
6875 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006876 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006878
Przemek Stekiel69c46792022-06-10 12:59:51 +02006879#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6881 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6882 step, data, data_length);
6883 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006884#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006885#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6887 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6888 step, data, data_length);
6889 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006890#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6891#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6893 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6894 step, data, data_length);
6895 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006896#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006897#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006899 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6901 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006902#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05306903#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) || \
6904 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6905 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg) ||
6906 kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306907 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6908 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306909 } else
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05306910#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC ||
6911 * MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006912 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006913 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006914 (void) data;
6915 (void) data_length;
6916 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006917 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006918 }
6919
Gilles Peskine224b0d62019-09-23 18:13:17 +02006920exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 if (status != PSA_SUCCESS) {
6922 psa_key_derivation_abort(operation);
6923 }
6924 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006925}
6926
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306927static psa_status_t psa_key_derivation_input_integer_internal(
6928 psa_key_derivation_operation_t *operation,
6929 psa_key_derivation_step_t step,
6930 uint64_t value)
6931{
6932 psa_status_t status;
6933 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6934
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05306935#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) || \
6936 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6937 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg) ||
6938 kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306939 status = psa_pbkdf2_set_input_cost(
6940 &operation->ctx.pbkdf2, step, value);
6941 } else
Kusumit Ghoderao3ab146f2023-06-22 15:35:36 +05306942#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC ||
6943 * MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306944 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306945 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306946 (void) value;
6947 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306948 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306949 }
6950
6951 if (status != PSA_SUCCESS) {
6952 psa_key_derivation_abort(operation);
6953 }
6954 return status;
6955}
6956
Janos Follath51f4a0f2019-06-14 11:35:55 +01006957psa_status_t psa_key_derivation_input_bytes(
6958 psa_key_derivation_operation_t *operation,
6959 psa_key_derivation_step_t step,
6960 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006962{
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 return psa_key_derivation_input_internal(operation, step,
6964 PSA_KEY_TYPE_NONE,
6965 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006966}
6967
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306968psa_status_t psa_key_derivation_input_integer(
6969 psa_key_derivation_operation_t *operation,
6970 psa_key_derivation_step_t step,
6971 uint64_t value)
6972{
6973 return psa_key_derivation_input_integer_internal(operation, step, value);
6974}
6975
Janos Follath51f4a0f2019-06-14 11:35:55 +01006976psa_status_t psa_key_derivation_input_key(
6977 psa_key_derivation_operation_t *operation,
6978 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006980{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006981 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006982 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006983 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006984
Ronald Cron5c522922020-11-14 16:35:34 +01006985 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6987 if (status != PSA_SUCCESS) {
6988 psa_key_derivation_abort(operation);
6989 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006990 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006991
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306992 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6993 * permission to output to a key object. */
6994 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6995 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006996 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006998
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 status = psa_key_derivation_input_internal(operation,
7000 step, slot->attr.type,
7001 slot->key.data,
7002 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007003
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007005
Gilles Peskine449bd832023-01-11 14:50:10 +01007006 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007007}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007008
7009
7010
7011/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007012/* Key agreement */
7013/****************************************************************/
7014
Gilles Peskine449bd832023-01-11 14:50:10 +01007015psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7016 const uint8_t *key_buffer,
7017 size_t key_buffer_size,
7018 psa_algorithm_t alg,
7019 const uint8_t *peer_key,
7020 size_t peer_key_length,
7021 uint8_t *shared_secret,
7022 size_t shared_secret_size,
7023 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007024{
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007026#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007027 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7029 key_buffer_size, alg,
7030 peer_key, peer_key_length,
7031 shared_secret,
7032 shared_secret_size,
7033 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007034#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007035
7036#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7037 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007038 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007039 peer_key,
7040 peer_key_length,
7041 key_buffer,
7042 key_buffer_size,
7043 shared_secret,
7044 shared_secret_size,
7045 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007046#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7047
Gilles Peskine01d718c2018-09-18 12:01:02 +02007048 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007049 (void) attributes;
7050 (void) key_buffer;
7051 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007052 (void) peer_key;
7053 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007054 (void) shared_secret;
7055 (void) shared_secret_size;
7056 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007058 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007059}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007060
Aditya Deshpande17845b82022-10-13 17:21:01 +01007061/** Internal function for raw key agreement
7062 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007063 * to the driver's implementation if a driver is present.
7064 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007065 * (psa_key_agreement_raw_builtin).
7066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007067static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7068 psa_key_slot_t *private_key,
7069 const uint8_t *peer_key,
7070 size_t peer_key_length,
7071 uint8_t *shared_secret,
7072 size_t shared_secret_size,
7073 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007074{
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7076 return PSA_ERROR_NOT_SUPPORTED;
7077 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007078
7079 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007081 };
7082
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 return psa_driver_wrapper_key_agreement(&attributes,
7084 private_key->key.data,
7085 private_key->key.bytes, alg,
7086 peer_key, peer_key_length,
7087 shared_secret,
7088 shared_secret_size,
7089 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007090}
7091
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007092/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007093 * to potentially free embedded data structures and wipe confidential data.
7094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007095static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7096 psa_key_derivation_step_t step,
7097 psa_key_slot_t *private_key,
7098 const uint8_t *peer_key,
7099 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007100{
7101 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007102 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007103 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007105
7106 /* Step 1: run the secret agreement algorithm to generate the shared
7107 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 status = psa_key_agreement_raw_internal(ka_alg,
7109 private_key,
7110 peer_key, peer_key_length,
7111 shared_secret,
7112 sizeof(shared_secret),
7113 &shared_secret_length);
7114 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007117
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007118 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007119 * the shared secret. A shared secret is permitted wherever a key
7120 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 status = psa_key_derivation_input_internal(operation, step,
7122 PSA_KEY_TYPE_DERIVE,
7123 shared_secret,
7124 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007125exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7127 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007128}
7129
Gilles Peskine449bd832023-01-11 14:50:10 +01007130psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7131 psa_key_derivation_step_t step,
7132 mbedtls_svc_key_id_t private_key,
7133 const uint8_t *peer_key,
7134 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007135{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007136 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007137 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007138 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007139
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7141 return PSA_ERROR_INVALID_ARGUMENT;
7142 }
Ronald Cron5c522922020-11-14 16:35:34 +01007143 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7145 if (status != PSA_SUCCESS) {
7146 return status;
7147 }
7148 status = psa_key_agreement_internal(operation, step,
7149 slot,
7150 peer_key, peer_key_length);
7151 if (status != PSA_SUCCESS) {
7152 psa_key_derivation_abort(operation);
7153 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007154 /* If a private key has been added as SECRET, we allow the derived
7155 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007156 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007157 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007159 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007160
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007162
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007164}
7165
Gilles Peskine449bd832023-01-11 14:50:10 +01007166psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7167 mbedtls_svc_key_id_t private_key,
7168 const uint8_t *peer_key,
7169 size_t peer_key_length,
7170 uint8_t *output,
7171 size_t output_size,
7172 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007173{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007174 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007175 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007176 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007177
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007179 status = PSA_ERROR_INVALID_ARGUMENT;
7180 goto exit;
7181 }
Ronald Cron5c522922020-11-14 16:35:34 +01007182 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7184 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007185 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007187
Gilles Peskine3e561302022-04-14 00:17:15 +02007188 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7189 * for the output size. The PSA specification only guarantees that this
7190 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7191 * but it might be nice to allow smaller buffers if the output fits.
7192 * At the time of writing this comment, with only ECDH implemented,
7193 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7194 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7195 * be exact for it as well. */
7196 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7198 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007199 status = PSA_ERROR_BUFFER_TOO_SMALL;
7200 goto exit;
7201 }
7202
Gilles Peskine449bd832023-01-11 14:50:10 +01007203 status = psa_key_agreement_raw_internal(alg, slot,
7204 peer_key, peer_key_length,
7205 output, output_size,
7206 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007207
7208exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007210 /* If an error happens and is not handled properly, the output
7211 * may be used as a key to protect sensitive data. Arrange for such
7212 * a key to be random, which is likely to result in decryption or
7213 * verification errors. This is better than filling the buffer with
7214 * some constant data such as zeros, which would result in the data
7215 * being protected with a reproducible, easily knowable key.
7216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007218 *output_length = output_size;
7219 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007222
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007224}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007225
7226
Gilles Peskine30524eb2020-11-13 17:02:26 +01007227
Gilles Peskine86a440b2018-11-12 18:39:40 +01007228/****************************************************************/
7229/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007230/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007231
Gilles Peskine30524eb2020-11-13 17:02:26 +01007232/** Initialize the PSA random generator.
7233 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007234static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007235{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007236#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007238#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7239
Gilles Peskine30524eb2020-11-13 17:02:26 +01007240 /* Set default configuration if
7241 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007243 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 }
7245 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007246 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007247 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007248
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007250#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7251 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7252 /* The PSA entropy injection feature depends on using NV seed as an entropy
7253 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 mbedtls_entropy_add_source(&rng->entropy,
7255 mbedtls_nv_seed_poll, NULL,
7256 MBEDTLS_ENTROPY_BLOCK_SIZE,
7257 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007258#endif
7259
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007261#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007262}
7263
7264/** Deinitialize the PSA random generator.
7265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007266static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007267{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007268#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007270#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7272 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007273#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007274}
7275
7276/** Seed the PSA random generator.
7277 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007278static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007279{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007280#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7281 /* Do nothing: the external RNG seeds itself. */
7282 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007284#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007285 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007286 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7287 drbg_seed, sizeof(drbg_seed) - 1);
7288 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007289#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007290}
7291
Gilles Peskine449bd832023-01-11 14:50:10 +01007292psa_status_t psa_generate_random(uint8_t *output,
7293 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007294{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007295 GUARD_MODULE_INITIALIZED;
7296
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007297#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7298
7299 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7301 output, output_size,
7302 &output_length);
7303 if (status != PSA_SUCCESS) {
7304 return status;
7305 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007306 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007307 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 if (output_length != output_size) {
7309 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7310 }
7311 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007312
7313#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7314
Gilles Peskine449bd832023-01-11 14:50:10 +01007315 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007316 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7318 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7319 output_size);
7320 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7321 output, request_size);
7322 if (ret != 0) {
7323 return mbedtls_to_psa_error(ret);
7324 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007325 output_size -= request_size;
7326 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007327 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007328 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007329#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007330}
7331
Gilles Peskine8814fc42020-12-14 15:33:44 +01007332/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007333 *
7334 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7335 * `psa_generate_random(...)`. The state parameter is ignored since the
7336 * PSA API doesn't support passing an explicit state.
7337 *
7338 * In the non-external case, psa_generate_random() calls an
7339 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7340 * and semantics as mbedtls_psa_get_random(). As an optimization,
7341 * instead of doing this back-and-forth between the PSA API and the
7342 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7343 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007344 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007345#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7346int mbedtls_psa_get_random(void *p_rng,
7347 unsigned char *output,
7348 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007349{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007350 /* This function takes a pointer to the RNG state because that's what
7351 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7352 * its own state internally and doesn't let the caller access that state.
7353 * So we just ignore the state parameter, and in practice we'll pass
7354 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007355 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 psa_status_t status = psa_generate_random(output, output_size);
7357 if (status == PSA_SUCCESS) {
7358 return 0;
7359 } else {
7360 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7361 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007362}
7363#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7364
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007365#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00007366#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007367
Gilles Peskine449bd832023-01-11 14:50:10 +01007368psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7369 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007370{
Gilles Peskine449bd832023-01-11 14:50:10 +01007371 if (global_data.initialized) {
7372 return PSA_ERROR_NOT_PERMITTED;
7373 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007374
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7376 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7377 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7378 return PSA_ERROR_INVALID_ARGUMENT;
7379 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007380
Gilles Peskine449bd832023-01-11 14:50:10 +01007381 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007382}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007383#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007384
Ronald Cron3772afe2021-02-08 16:10:05 +01007385/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007386 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007387 * \param type The key type
7388 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007389 *
7390 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007391 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007392 * \retval #PSA_ERROR_INVALID_ARGUMENT
7393 * The size in bits of the key is not valid.
7394 * \retval #PSA_ERROR_NOT_SUPPORTED
7395 * The type and/or the size in bits of the key or the combination of
7396 * the two is not supported.
7397 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007398static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007400{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007401 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007402
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 if (key_type_is_raw_bytes(type)) {
7404 status = psa_validate_unstructured_key_bit_size(type, bits);
7405 if (status != PSA_SUCCESS) {
7406 return status;
7407 }
7408 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007409#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7411 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7412 return PSA_ERROR_NOT_SUPPORTED;
7413 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007414
Ronald Cron01b2aba2020-10-05 09:42:02 +02007415 /* Accept only byte-aligned keys, for the same reasons as
7416 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007417 if (bits % 8 != 0) {
7418 return PSA_ERROR_NOT_SUPPORTED;
7419 }
7420 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007421#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007422
Valerio Setti8bb57632023-05-26 13:48:07 +02007423#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007425 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007426 return PSA_SUCCESS;
7427 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007428#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_LEGACY) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007429
Valerio Setti8bb57632023-05-26 13:48:07 +02007430#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007431 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007432 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007433 return PSA_ERROR_NOT_SUPPORTED;
7434 }
7435 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007436#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007437 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007439 }
7440
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007442}
7443
Ronald Cron55ed0592020-10-05 10:30:40 +02007444psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007445 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007447{
7448 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007449 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007450
Gilles Peskine449bd832023-01-11 14:50:10 +01007451 if ((attributes->domain_parameters == NULL) &&
7452 (attributes->domain_parameters_size != 0)) {
7453 return PSA_ERROR_INVALID_ARGUMENT;
7454 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007455
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 if (key_type_is_raw_bytes(type)) {
7457 status = psa_generate_random(key_buffer, key_buffer_size);
7458 if (status != PSA_SUCCESS) {
7459 return status;
7460 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007461
David Brown12ca5032021-02-11 11:02:00 -07007462#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 if (type == PSA_KEY_TYPE_DES) {
7464 psa_des_set_key_parity(key_buffer, key_buffer_size);
7465 }
David Brown8107e312021-02-12 08:08:46 -07007466#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007467 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007468
Valerio Setti8bb57632023-05-26 13:48:07 +02007469#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) && \
Jaeden Amero424fa932021-05-14 08:34:32 +01007470 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7472 return mbedtls_psa_rsa_generate_key(attributes,
7473 key_buffer,
7474 key_buffer_size,
7475 key_buffer_length);
7476 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007477#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY)
Jaeden Amero424fa932021-05-14 08:34:32 +01007478 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007479
Valerio Setti8bb57632023-05-26 13:48:07 +02007480#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7482 return mbedtls_psa_ecp_generate_key(attributes,
7483 key_buffer,
7484 key_buffer_size,
7485 key_buffer_length);
7486 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007487#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_LEGACY) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007488
Valerio Setti8bb57632023-05-26 13:48:07 +02007489#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007490 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7491 return mbedtls_psa_ffdh_generate_key(attributes,
7492 key_buffer,
7493 key_buffer_size,
7494 key_buffer_length);
7495 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007496#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Steven Cooreman29149862020-08-05 15:43:42 +02007497 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 (void) key_buffer_length;
7499 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007500 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007501
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007503}
Darryl Green0c6575a2018-11-07 16:05:30 +00007504
Gilles Peskine449bd832023-01-11 14:50:10 +01007505psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7506 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007507{
7508 psa_status_t status;
7509 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007510 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007511 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007512
Ronald Cron81709fc2020-11-14 12:10:32 +01007513 *key = MBEDTLS_SVC_KEY_ID_INIT;
7514
Gilles Peskine0f84d622019-09-12 19:03:13 +02007515 /* Reject any attempt to create a zero-length key so that we don't
7516 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 if (psa_get_key_bits(attributes) == 0) {
7518 return PSA_ERROR_INVALID_ARGUMENT;
7519 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007520
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007521 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007522 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7523 return PSA_ERROR_INVALID_ARGUMENT;
7524 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007525
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7527 &slot, &driver);
7528 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007529 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 }
Gilles Peskine11792082019-08-06 18:36:36 +02007531
Ronald Cron977c2472020-10-13 08:32:21 +02007532 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307533 * storage ( thus not in the case of generating a key in a secure element
7534 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7535 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 if (slot->key.data == NULL) {
7537 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7538 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007539 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007540 attributes->core.type, attributes->core.bits);
7541 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007542 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007544
7545 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 attributes->core.type,
7547 attributes->core.bits);
7548 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007549 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 attributes, &key_buffer_size);
7551 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007552 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 }
Ronald Cron977c2472020-10-13 08:32:21 +02007554 }
Ronald Cron977c2472020-10-13 08:32:21 +02007555
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7557 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007558 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 }
Ronald Cron977c2472020-10-13 08:32:21 +02007560 }
7561
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 status = psa_driver_wrapper_generate_key(attributes,
7563 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007564
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 if (status != PSA_SUCCESS) {
7566 psa_remove_key_data_from_memory(slot);
7567 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007568
Gilles Peskine11792082019-08-06 18:36:36 +02007569exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007570 if (status == PSA_SUCCESS) {
7571 status = psa_finish_key_creation(slot, driver, key);
7572 }
7573 if (status != PSA_SUCCESS) {
7574 psa_fail_key_creation(slot, driver);
7575 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007576
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007578}
7579
Gilles Peskinee59236f2018-01-27 23:32:46 +01007580/****************************************************************/
7581/* Module setup */
7582/****************************************************************/
7583
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007584#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007585psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 void (* entropy_init)(mbedtls_entropy_context *ctx),
7587 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007588{
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7590 return PSA_ERROR_BAD_STATE;
7591 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007592 global_data.rng.entropy_init = entropy_init;
7593 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007595}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007596#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007597
Gilles Peskine449bd832023-01-11 14:50:10 +01007598void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007599{
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 psa_wipe_all_key_slots();
7601 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7602 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007603 }
7604 /* Wipe all remaining data, including configuration.
7605 * In particular, this sets all state indicator to the value
7606 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007608
7609 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007611}
7612
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007613#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7614/** Recover a transaction that was interrupted by a power failure.
7615 *
7616 * This function is called during initialization, before psa_crypto_init()
7617 * returns. If this function returns a failure status, the initialization
7618 * fails.
7619 */
7620static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007622{
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007624 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7625 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 /* TODO - fall through to the failure case until this
7627 * is implemented.
7628 * https://github.com/ARMmbed/mbed-crypto/issues/218
7629 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007630 default:
7631 /* We found an unsupported transaction in the storage.
7632 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007634 }
7635}
7636#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7637
Gilles Peskine449bd832023-01-11 14:50:10 +01007638psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007639{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007640 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007641
Gilles Peskinec6b69072018-11-20 21:42:52 +01007642 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 if (global_data.initialized != 0) {
7644 return PSA_SUCCESS;
7645 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007646
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007647 /* Init drivers */
7648 status = psa_driver_wrapper_init();
7649 if (status != PSA_SUCCESS) {
7650 goto exit;
7651 }
7652 global_data.drivers_initialized = 1;
7653
Gilles Peskine30524eb2020-11-13 17:02:26 +01007654 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007655 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007656 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007657 status = mbedtls_psa_random_seed(&global_data.rng);
7658 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007659 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007660 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007661 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007662
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 status = psa_initialize_key_slots();
7664 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007665 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007667
Gilles Peskine4b734222019-07-24 15:56:31 +02007668#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 status = psa_crypto_load_transaction();
7670 if (status == PSA_SUCCESS) {
7671 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7672 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007673 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 }
7675 status = psa_crypto_stop_transaction();
7676 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007677 /* There's no transaction to complete. It's all good. */
7678 status = PSA_SUCCESS;
7679 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007680#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007681
Gilles Peskinec6b69072018-11-20 21:42:52 +01007682 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007683 global_data.initialized = 1;
7684
7685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007686 if (status != PSA_SUCCESS) {
7687 mbedtls_psa_crypto_free();
7688 }
7689 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007690}
7691
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007692psa_status_t psa_crypto_driver_pake_get_password_len(
7693 const psa_crypto_driver_pake_inputs_t *inputs,
7694 size_t *password_len)
7695{
7696 if (inputs->password_len == 0) {
7697 return PSA_ERROR_BAD_STATE;
7698 }
7699
7700 *password_len = inputs->password_len;
7701
7702 return PSA_SUCCESS;
7703}
7704
7705psa_status_t psa_crypto_driver_pake_get_password(
7706 const psa_crypto_driver_pake_inputs_t *inputs,
7707 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7708{
7709 if (inputs->password_len == 0) {
7710 return PSA_ERROR_BAD_STATE;
7711 }
7712
7713 if (buffer_size < inputs->password_len) {
7714 return PSA_ERROR_BUFFER_TOO_SMALL;
7715 }
7716
7717 memcpy(buffer, inputs->password, inputs->password_len);
7718 *buffer_length = inputs->password_len;
7719
7720 return PSA_SUCCESS;
7721}
7722
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007723psa_status_t psa_crypto_driver_pake_get_user_len(
7724 const psa_crypto_driver_pake_inputs_t *inputs,
7725 size_t *user_len)
7726{
7727 if (inputs->user_len == 0) {
7728 return PSA_ERROR_BAD_STATE;
7729 }
7730
7731 *user_len = inputs->user_len;
7732
7733 return PSA_SUCCESS;
7734}
7735
7736psa_status_t psa_crypto_driver_pake_get_user(
7737 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007738 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007739{
7740 if (inputs->user_len == 0) {
7741 return PSA_ERROR_BAD_STATE;
7742 }
7743
Przemek Stekielc0e62502023-03-14 11:49:36 +01007744 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007745 return PSA_ERROR_BUFFER_TOO_SMALL;
7746 }
7747
Przemek Stekielc0e62502023-03-14 11:49:36 +01007748 memcpy(user_id, inputs->user, inputs->user_len);
7749 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007750
7751 return PSA_SUCCESS;
7752}
7753
7754psa_status_t psa_crypto_driver_pake_get_peer_len(
7755 const psa_crypto_driver_pake_inputs_t *inputs,
7756 size_t *peer_len)
7757{
7758 if (inputs->peer_len == 0) {
7759 return PSA_ERROR_BAD_STATE;
7760 }
7761
7762 *peer_len = inputs->peer_len;
7763
7764 return PSA_SUCCESS;
7765}
7766
7767psa_status_t psa_crypto_driver_pake_get_peer(
7768 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007769 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007770{
7771 if (inputs->peer_len == 0) {
7772 return PSA_ERROR_BAD_STATE;
7773 }
7774
Przemek Stekielc0e62502023-03-14 11:49:36 +01007775 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007776 return PSA_ERROR_BUFFER_TOO_SMALL;
7777 }
7778
Przemek Stekielc0e62502023-03-14 11:49:36 +01007779 memcpy(peer_id, inputs->peer, inputs->peer_len);
7780 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007781
7782 return PSA_SUCCESS;
7783}
7784
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007785psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7786 const psa_crypto_driver_pake_inputs_t *inputs,
7787 psa_pake_cipher_suite_t *cipher_suite)
7788{
7789 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7790 return PSA_ERROR_BAD_STATE;
7791 }
7792
7793 *cipher_suite = inputs->cipher_suite;
7794
7795 return PSA_SUCCESS;
7796}
7797
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01007798#if defined(PSA_WANT_ALG_SOME_PAKE)
Neil Armstronga7d08c32022-06-01 18:21:20 +02007799psa_status_t psa_pake_setup(
7800 psa_pake_operation_t *operation,
7801 const psa_pake_cipher_suite_t *cipher_suite)
7802{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007803 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007804
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007805 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007806 status = PSA_ERROR_BAD_STATE;
7807 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007808 }
7809
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007810 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007811 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007812 status = PSA_ERROR_INVALID_ARGUMENT;
7813 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007814 }
7815
Przemek Stekiel51eac532022-12-07 11:04:51 +01007816 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7817
Przemek Stekiele12ed362022-12-21 12:54:46 +01007818 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007819 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7820 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007821 operation->data.inputs.cipher_suite = *cipher_suite;
7822
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007823#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007824 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007825 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007826 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007827
David Horstmann16f01512023-06-14 17:21:07 +01007828 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007829 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007830 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007831#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007832 {
7833 status = PSA_ERROR_NOT_SUPPORTED;
7834 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007835 }
7836
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007837 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7838
Przemek Stekiel51eac532022-12-07 11:04:51 +01007839 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007840exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007841 psa_pake_abort(operation);
7842 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007843}
7844
7845psa_status_t psa_pake_set_password_key(
7846 psa_pake_operation_t *operation,
7847 mbedtls_svc_key_id_t password)
7848{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007849 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007850 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7851 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007852
Przemek Stekiel51eac532022-12-07 11:04:51 +01007853 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007854 status = PSA_ERROR_BAD_STATE;
7855 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007856 }
7857
Przemek Stekiel6c764412022-11-22 14:05:12 +01007858 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7859 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007860 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007861 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007862 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007863 }
7864
Przemek Stekiel6c764412022-11-22 14:05:12 +01007865 psa_key_attributes_t attributes = {
7866 .core = slot->attr
7867 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007868
Przemek Stekiel51eac532022-12-07 11:04:51 +01007869 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007870
Przemek Stekiel51eac532022-12-07 11:04:51 +01007871 if (type != PSA_KEY_TYPE_PASSWORD &&
7872 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7873 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007874 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007875 }
7876
Przemek Stekiel51eac532022-12-07 11:04:51 +01007877 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7878 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007879 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007880 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007881 }
7882
7883 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7884 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007885 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007886exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007887 if (status != PSA_SUCCESS) {
7888 psa_pake_abort(operation);
7889 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007890 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007891 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007892}
7893
7894psa_status_t psa_pake_set_user(
7895 psa_pake_operation_t *operation,
7896 const uint8_t *user_id,
7897 size_t user_id_len)
7898{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007900
7901 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007902 status = PSA_ERROR_BAD_STATE;
7903 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007904 }
7905
Przemek Stekiel51eac532022-12-07 11:04:51 +01007906 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007907 status = PSA_ERROR_INVALID_ARGUMENT;
7908 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007909 }
7910
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007911 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007912 status = PSA_ERROR_BAD_STATE;
7913 goto exit;
7914 }
7915
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007916 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7917 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007918 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7919 goto exit;
7920 }
7921
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007922 memcpy(operation->data.inputs.user, user_id, user_id_len);
7923 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007924
7925 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007926exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007927 psa_pake_abort(operation);
7928 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007929}
7930
7931psa_status_t psa_pake_set_peer(
7932 psa_pake_operation_t *operation,
7933 const uint8_t *peer_id,
7934 size_t peer_id_len)
7935{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007936 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007937
7938 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007939 status = PSA_ERROR_BAD_STATE;
7940 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007941 }
7942
Przemek Stekiel51eac532022-12-07 11:04:51 +01007943 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007944 status = PSA_ERROR_INVALID_ARGUMENT;
7945 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007946 }
7947
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007948 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007949 status = PSA_ERROR_BAD_STATE;
7950 goto exit;
7951 }
7952
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007953 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7954 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007955 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7956 goto exit;
7957 }
7958
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007959 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7960 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007961
7962 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007963exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007964 psa_pake_abort(operation);
7965 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007966}
7967
7968psa_status_t psa_pake_set_role(
7969 psa_pake_operation_t *operation,
7970 psa_pake_role_t role)
7971{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007972 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007973
Przemek Stekiel51eac532022-12-07 11:04:51 +01007974 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007975 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007976 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007977 }
7978
Przemek Stekiel09104b82023-03-06 14:11:51 +01007979 switch (operation->alg) {
7980#if defined(PSA_WANT_ALG_JPAKE)
7981 case PSA_ALG_JPAKE:
7982 if (role == PSA_PAKE_ROLE_NONE) {
7983 return PSA_SUCCESS;
7984 }
7985 status = PSA_ERROR_INVALID_ARGUMENT;
7986 break;
7987#endif
7988 default:
7989 (void) role;
7990 status = PSA_ERROR_NOT_SUPPORTED;
7991 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007992 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007993exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007994 psa_pake_abort(operation);
7995 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007996}
7997
David Horstmanne7f21e62023-05-12 18:17:21 +01007998/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007999#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008000static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008001 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008002{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008003 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008004 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008005 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008006
David Horstmann024e5c52023-06-14 15:48:21 +01008007 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008008 is_x1 = (stage->outputs < 1);
8009 } else {
8010 is_x1 = (stage->inputs < 1);
8011 }
8012
David Horstmann74a3d8c2023-06-14 18:28:19 +01008013 key_share_step = is_x1 ?
8014 PSA_JPAKE_X1_STEP_KEY_SHARE :
8015 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008016 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008017 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8018 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8019 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8020 } else {
8021 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008022 }
David Horstmann74a3d8c2023-06-14 18:28:19 +01008023 return key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008024}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008025#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008026
Przemek Stekiel2797d372022-12-22 11:19:22 +01008027static psa_status_t psa_pake_complete_inputs(
8028 psa_pake_operation_t *operation)
8029{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008030 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008031 /* Create copy of the inputs on stack as inputs share memory
8032 with the driver context which will be setup by the driver. */
8033 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008034
Przemek Stekielfde11282023-03-13 16:06:09 +01008035 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008036 return PSA_ERROR_BAD_STATE;
8037 }
8038
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008039 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008040 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8041 return PSA_ERROR_BAD_STATE;
8042 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008043 }
8044
Przemek Stekiel18620a32023-01-17 16:34:52 +01008045 /* Clear driver context */
8046 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8047
8048 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008049
8050 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008051 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
8052 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008053
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008054 /* User and peer are translated to role. */
8055 mbedtls_free(inputs.user);
8056 mbedtls_free(inputs.peer);
8057
Przemek Stekiel2797d372022-12-22 11:19:22 +01008058 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008059#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008060 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008061 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008062 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008063#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008064 {
8065 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008066 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008067 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008068 return status;
8069}
8070
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008071#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008072static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008073 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008074 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008075 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008076{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008077 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8078 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8079 step != PSA_PAKE_STEP_ZK_PROOF) {
8080 return PSA_ERROR_INVALID_ARGUMENT;
8081 }
8082
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008083 psa_jpake_computation_stage_t *computation_stage =
8084 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008085
David Horstmann5da95602023-06-08 15:37:12 +01008086 if (computation_stage->round != PSA_JPAKE_FIRST &&
8087 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008088 return PSA_ERROR_BAD_STATE;
8089 }
8090
David Horstmanne7f21e62023-05-12 18:17:21 +01008091 /* Check that the step we are given is the one we were expecting */
8092 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008093 return PSA_ERROR_BAD_STATE;
8094 }
8095
David Horstmanne7f21e62023-05-12 18:17:21 +01008096 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8097 computation_stage->inputs == 0 &&
8098 computation_stage->outputs == 0) {
8099 /* Start of the round, so function decides whether we are inputting
8100 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008101 computation_stage->io_mode = io_mode;
8102 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008103 /* Middle of the round so the mode we are in must match the function
8104 * called by the user */
8105 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008106 }
8107
8108 return PSA_SUCCESS;
8109}
8110
David Horstmanne7f21e62023-05-12 18:17:21 +01008111static psa_status_t psa_jpake_epilogue(
8112 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008113 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008114{
David Horstmanne7f21e62023-05-12 18:17:21 +01008115 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008116 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008117
David Horstmanne7f21e62023-05-12 18:17:21 +01008118 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8119 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008120 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008121 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008122 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008123 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008124 }
8125 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008126 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008127 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008128 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008129 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008130 }
8131 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008132 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8133 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008134 /* End of a round, move to the next round */
8135 stage->inputs = 0;
8136 stage->outputs = 0;
8137 stage->round++;
8138 }
8139 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008140 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008141 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008142 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008143 return PSA_SUCCESS;
8144}
David Horstmanne7f21e62023-05-12 18:17:21 +01008145
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008146#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008147
Neil Armstronga7d08c32022-06-01 18:21:20 +02008148psa_status_t psa_pake_output(
8149 psa_pake_operation_t *operation,
8150 psa_pake_step_t step,
8151 uint8_t *output,
8152 size_t output_size,
8153 size_t *output_length)
8154{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008155 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008156 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008157 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008158
8159 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008160 status = psa_pake_complete_inputs(operation);
8161 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008162 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008163 }
8164 }
8165
8166 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008167 status = PSA_ERROR_BAD_STATE;
8168 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008169 }
8170
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008171 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008172 status = PSA_ERROR_INVALID_ARGUMENT;
8173 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008174 }
8175
Przemek Stekiele12ed362022-12-21 12:54:46 +01008176 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008177#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008178 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008179 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008180 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008181 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008182 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008183 driver_step = convert_jpake_computation_stage_to_driver_step(
8184 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008185 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008186#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008187 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008188 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008189 status = PSA_ERROR_NOT_SUPPORTED;
8190 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008191 }
8192
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008193 status = psa_driver_wrapper_pake_output(operation, driver_step,
8194 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008195
8196 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008197 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008198 }
8199
8200 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008201#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008202 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008203 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008204 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008205 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008206 }
8207 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008208#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008209 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008210 status = PSA_ERROR_NOT_SUPPORTED;
8211 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008212 }
8213
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008214 return PSA_SUCCESS;
8215exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008216 psa_pake_abort(operation);
8217 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008218}
8219
8220psa_status_t psa_pake_input(
8221 psa_pake_operation_t *operation,
8222 psa_pake_step_t step,
8223 const uint8_t *input,
8224 size_t input_length)
8225{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008227 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008228 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8229 operation->primitive,
8230 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008231
8232 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008233 status = psa_pake_complete_inputs(operation);
8234 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008235 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008236 }
8237 }
8238
8239 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008240 status = PSA_ERROR_BAD_STATE;
8241 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008242 }
8243
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008244 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008245 status = PSA_ERROR_INVALID_ARGUMENT;
8246 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008247 }
8248
Przemek Stekiele12ed362022-12-21 12:54:46 +01008249 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008250#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008251 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008252 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008253 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008254 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008255 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008256 driver_step = convert_jpake_computation_stage_to_driver_step(
8257 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008259#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008260 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008261 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008262 status = PSA_ERROR_NOT_SUPPORTED;
8263 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008264 }
8265
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008266 status = psa_driver_wrapper_pake_input(operation, driver_step,
8267 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008268
8269 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008270 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008271 }
8272
8273 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008274#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008275 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008276 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008277 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008278 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008279 }
8280 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008281#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008282 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008283 status = PSA_ERROR_NOT_SUPPORTED;
8284 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008285 }
8286
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008287 return PSA_SUCCESS;
8288exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008289 psa_pake_abort(operation);
8290 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008291}
8292
8293psa_status_t psa_pake_get_implicit_key(
8294 psa_pake_operation_t *operation,
8295 psa_key_derivation_operation_t *output)
8296{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008298 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008299 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008300 size_t shared_key_len = 0;
8301
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008302 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008303 status = PSA_ERROR_BAD_STATE;
8304 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008305 }
8306
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008307#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008308 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008309 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008310 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008311 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008312 status = PSA_ERROR_BAD_STATE;
8313 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008314 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008315 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008316#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008317 {
8318 status = PSA_ERROR_NOT_SUPPORTED;
8319 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008320 }
8321
Przemek Stekiel0c781802022-11-29 14:53:13 +01008322 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8323 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008324 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008325 &shared_key_len);
8326
8327 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008328 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008329 }
8330
8331 status = psa_key_derivation_input_bytes(output,
8332 PSA_KEY_DERIVATION_INPUT_SECRET,
8333 shared_key,
8334 shared_key_len);
8335
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008336 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008337exit:
8338 abort_status = psa_pake_abort(operation);
8339 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008340}
8341
8342psa_status_t psa_pake_abort(
8343 psa_pake_operation_t *operation)
8344{
Przemek Stekield69dca92023-01-31 19:59:20 +01008345 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008346
Przemek Stekield69dca92023-01-31 19:59:20 +01008347 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008348 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008349 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008350
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008351 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8352 if (operation->data.inputs.password != NULL) {
8353 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008354 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008355 mbedtls_free(operation->data.inputs.password);
8356 }
8357 if (operation->data.inputs.user != NULL) {
8358 mbedtls_free(operation->data.inputs.user);
8359 }
8360 if (operation->data.inputs.peer != NULL) {
8361 mbedtls_free(operation->data.inputs.peer);
8362 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008363 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008364 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008365
Przemek Stekield69dca92023-01-31 19:59:20 +01008366 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008367}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008368#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008369
Gilles Peskinee59236f2018-01-27 23:32:46 +01008370#endif /* MBEDTLS_PSA_CRYPTO_C */