blob: f6ad775ad6f190b3e490a184180c7e75f3f47947 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010038#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010039#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010040#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010042#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020044#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020045#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010046#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010047/* Include internal declarations that are useful for implementing persistently
48 * stored keys. */
49#include "psa_crypto_storage.h"
50
Gilles Peskineb2b64d32020-12-14 16:43:58 +010051#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010052
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020062#include "mbedtls/chacha20.h"
63#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020068#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/error.h"
72#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010074#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000075#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010076#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000077#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010078#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000079#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/sha1.h"
83#include "mbedtls/sha256.h"
84#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020085#include "md_psa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020090#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020091#endif
92
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010093/****************************************************************/
94/* Global data, support functions and library management */
95/****************************************************************/
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100}
101
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100102/* Values for psa_global_data_t::rng_state */
103#define RNG_NOT_INITIALIZED 0
104#define RNG_INITIALIZED 1
105#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100110 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100111 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112} psa_global_data_t;
113
114static psa_global_data_t global_data;
115
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100116#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
117mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
118 &global_data.rng.drbg;
119#endif
120
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (global_data.initialized == 0) \
123 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100125int psa_can_do_hash(psa_algorithm_t hash_alg)
126{
127 (void) hash_alg;
128 return global_data.drivers_initialized;
129}
Valerio Setti8bb57632023-05-26 13:48:07 +0200130#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200131 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Setti8bb57632023-05-26 13:48:07 +0200132 defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200133static int psa_is_dh_key_size_valid(size_t bits)
134{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200135 if (bits != 2048 && bits != 3072 && bits != 4096 &&
136 bits != 6144 && bits != 8192) {
137 return 0;
138 }
139
140 return 1;
141}
Valerio Setti8bb57632023-05-26 13:48:07 +0200142#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200143 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Setti8bb57632023-05-26 13:48:07 +0200144 MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100148 /* Mbed TLS error codes can combine a high-level error code and a
149 * low-level error code. The low-level error usually reflects the
150 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 int low_level_ret = -(-ret & 0x007f);
152 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100153 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100155
156 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
157 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200159 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
160 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
161 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
162 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
163 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200165 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200167 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100175
176 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180
Gilles Peskine26869f22019-05-06 15:25:00 +0200181 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200183
184 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200188
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
205 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100206 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
207 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
211 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100215#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100216
217 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100219
Gilles Peskinee59236f2018-01-27 23:32:46 +0100220 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
221 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
222 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
225 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200227 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100234 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
235 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100236 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100238 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
239 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100241 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243#endif
244
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200294 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
311 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200319
itayzafrir5c753392018-05-08 11:18:38 +0300320 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300321 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
328 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300330 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100332 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100334
Paul Elliott588f8ed2022-12-02 18:10:26 +0000335 case MBEDTLS_ERR_ECP_IN_PROGRESS:
336 return PSA_OPERATION_INCOMPLETE;
337
Janos Follatha13b9052019-11-22 12:48:59 +0000338 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300340
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100343 }
344}
345
Paul Elliottdc42ca82023-02-24 18:11:59 +0000346/**
347 * \brief For output buffers which contain "tags"
348 * (outputs that may be checked for validity like
349 * hashes, MACs and signatures), fill the unused
350 * part of the output buffer (the whole buffer on
351 * error, the trailing part on success) with
352 * something that isn't a valid tag (barring an
353 * attack on the tag and deliberately-crafted
354 * input), in case the caller doesn't check the
355 * return status properly.
356 *
357 * \param output_buffer Pointer to buffer to wipe. May not be NULL
358 * unless \p output_buffer_size is zero.
359 * \param status Status of function called to generate
360 * output_buffer originally
361 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
362 * could be NULL.
363 * \param output_buffer_length Length of data written to output_buffer, must be
364 * less than \p output_buffer_size
365 */
366static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000367 size_t output_buffer_size, size_t output_buffer_length)
368{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000369 size_t offset = 0;
370
371 if (output_buffer_size == 0) {
372 /* If output_buffer_size is 0 then we have nothing to do. We must not
373 call memset because output_buffer may be NULL in this case */
374 return;
375 }
376
377 if (status == PSA_SUCCESS) {
378 offset = output_buffer_length;
379 }
380
381 memset(output_buffer + offset, '!', output_buffer_size - offset);
382}
383
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200384
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200385
386
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100387/****************************************************************/
388/* Key management */
389/****************************************************************/
390
Valerio Settia9aab1a2023-06-19 13:39:54 +0200391#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200392psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
393 size_t *bits)
394{
395 switch (grpid) {
396 case MBEDTLS_ECP_DP_SECP192R1:
397 *bits = 192;
398 return PSA_ECC_FAMILY_SECP_R1;
399 case MBEDTLS_ECP_DP_SECP224R1:
400 *bits = 224;
401 return PSA_ECC_FAMILY_SECP_R1;
402 case MBEDTLS_ECP_DP_SECP256R1:
403 *bits = 256;
404 return PSA_ECC_FAMILY_SECP_R1;
405 case MBEDTLS_ECP_DP_SECP384R1:
406 *bits = 384;
407 return PSA_ECC_FAMILY_SECP_R1;
408 case MBEDTLS_ECP_DP_SECP521R1:
409 *bits = 521;
410 return PSA_ECC_FAMILY_SECP_R1;
411 case MBEDTLS_ECP_DP_BP256R1:
412 *bits = 256;
413 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
414 case MBEDTLS_ECP_DP_BP384R1:
415 *bits = 384;
416 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
417 case MBEDTLS_ECP_DP_BP512R1:
418 *bits = 512;
419 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
420 case MBEDTLS_ECP_DP_CURVE25519:
421 *bits = 255;
422 return PSA_ECC_FAMILY_MONTGOMERY;
423 case MBEDTLS_ECP_DP_SECP192K1:
424 *bits = 192;
425 return PSA_ECC_FAMILY_SECP_K1;
426 case MBEDTLS_ECP_DP_SECP224K1:
427 *bits = 224;
428 return PSA_ECC_FAMILY_SECP_K1;
429 case MBEDTLS_ECP_DP_SECP256K1:
430 *bits = 256;
431 return PSA_ECC_FAMILY_SECP_K1;
432 case MBEDTLS_ECP_DP_CURVE448:
433 *bits = 448;
434 return PSA_ECC_FAMILY_MONTGOMERY;
435 default:
436 *bits = 0;
437 return 0;
438 }
439}
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
442 size_t bits,
443 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444{
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100446 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100448#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100451#endif
452#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100453 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100455#endif
456#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100459#endif
460#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100461 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100463#endif
464#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100467 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (bits_is_sloppy) {
469 return MBEDTLS_ECP_DP_SECP521R1;
470 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100471 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100472#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100473 }
474 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100475
Paul Elliott8ff510a2020-06-02 17:19:28 +0100476 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100478#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100479 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100481#endif
482#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100483 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100485#endif
486#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100487 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100489#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100490 }
491 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100492
Paul Elliott8ff510a2020-06-02 17:19:28 +0100493 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100495#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100496 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100498 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (bits_is_sloppy) {
500 return MBEDTLS_ECP_DP_CURVE25519;
501 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100502 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100503#endif
504#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100505 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100507#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100508 }
509 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100510
Paul Elliott8ff510a2020-06-02 17:19:28 +0100511 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100513#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100514 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100516#endif
517#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100518 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100520#endif
521#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100522 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100524#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100525 }
526 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200527 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100528
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100529 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200531}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200532#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
535 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200536{
537 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200541 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200542 case PSA_KEY_TYPE_PASSWORD:
543 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200544 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100545#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200546 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (bits != 128 && bits != 192 && bits != 256) {
548 return PSA_ERROR_INVALID_ARGUMENT;
549 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200550 break;
551#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200552#if defined(PSA_WANT_KEY_TYPE_ARIA)
553 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (bits != 128 && bits != 192 && bits != 256) {
555 return PSA_ERROR_INVALID_ARGUMENT;
556 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200557 break;
558#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100559#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200560 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (bits != 128 && bits != 192 && bits != 256) {
562 return PSA_ERROR_INVALID_ARGUMENT;
563 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200564 break;
565#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100566#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200567 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (bits != 64 && bits != 128 && bits != 192) {
569 return PSA_ERROR_INVALID_ARGUMENT;
570 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200571 break;
572#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100573#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200574 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (bits != 256) {
576 return PSA_ERROR_INVALID_ARGUMENT;
577 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200578 break;
579#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200580 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200582 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (bits % 8 != 0) {
584 return PSA_ERROR_INVALID_ARGUMENT;
585 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200588}
589
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100590/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100591 *
Steven Cooremancd640932021-03-08 12:00:27 +0100592 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
593 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100594 *
595 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100596 * \param[in] key_type The key type of the key to be used with the
597 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100598 *
599 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100600 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100601 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100602 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100603 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100604MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100605 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100607{
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (PSA_ALG_IS_HMAC(algorithm)) {
609 if (key_type == PSA_KEY_TYPE_HMAC) {
610 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100611 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100612 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
615 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
616 * key. */
617 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
618 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
619 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
620 * the block length (larger than 1) for block ciphers. */
621 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
622 return PSA_SUCCESS;
623 }
624 }
625 }
626
627 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100628}
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
631 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200632{
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (slot->key.data != NULL) {
634 return PSA_ERROR_ALREADY_EXISTS;
635 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 slot->key.data = mbedtls_calloc(1, buffer_length);
638 if (slot->key.data == NULL) {
639 return PSA_ERROR_INSUFFICIENT_MEMORY;
640 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200641
Ronald Cronea0f8a62020-11-25 17:52:23 +0100642 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200644}
645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
647 const uint8_t *data,
648 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200649{
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 psa_status_t status = psa_allocate_buffer_to_slot(slot,
651 data_length);
652 if (status != PSA_SUCCESS) {
653 return status;
654 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 memcpy(slot->key.data, data, data_length);
657 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200658}
659
Ronald Crona0fe59f2020-11-29 11:14:53 +0100660psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100661 const psa_key_attributes_t *attributes,
662 const uint8_t *data, size_t data_length,
663 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
667 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200669 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (data_length == 0) {
671 return PSA_ERROR_NOT_SUPPORTED;
672 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (key_type_is_raw_bytes(type)) {
675 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
678 *bits);
679 if (status != PSA_SUCCESS) {
680 return status;
681 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200682
Ronald Crondd04d422020-11-28 15:14:42 +0100683 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100685 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return PSA_SUCCESS;
689 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +0200690#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200691 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100692 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200693 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100694 return PSA_ERROR_INVALID_ARGUMENT;
695 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200696 return mbedtls_psa_ffdh_import_key(attributes,
697 data, data_length,
698 key_buffer, key_buffer_size,
699 key_buffer_length,
700 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100701 }
Valerio Setti8bb57632023-05-26 13:48:07 +0200702#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200703 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200704#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
706 if (PSA_KEY_TYPE_IS_ECC(type)) {
707 return mbedtls_psa_ecp_import_key(attributes,
708 data, data_length,
709 key_buffer, key_buffer_size,
710 key_buffer_length,
711 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100712 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200713#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800714 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio 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{
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001092 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001094
Ronald Cronea0f8a62020-11-25 17:52:23 +01001095 slot->key.data = NULL;
1096 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001099}
1100
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001101/** Completely wipe a slot in memory, including its policy.
1102 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001103psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001104{
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 /*
1108 * As the return error code may not be handled in case of multiple errors,
1109 * do our best to report an unexpected lock counter. Assert with
1110 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1111 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1112 * function is called as part of the execution of a test suite, the
1113 * execution of the test suite is stopped in error if the assertion fails.
1114 */
1115 if (slot->lock_count != 1) {
1116 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001117 status = PSA_ERROR_CORRUPTION_DETECTED;
1118 }
1119
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001120 /* Multipart operations may still be using the key. This is safe
1121 * because all multipart operation objects are independent from
1122 * the key slot: if they need to access the key after the setup
1123 * phase, they have a copy of the key. Note that this means that
1124 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001125 /* At this point, key material and other type-specific content has
1126 * been wiped. Clear remaining metadata. We can call memset and not
1127 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 memset(slot, 0, sizeof(*slot));
1129 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001130}
1131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001133{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001134 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001135 psa_status_t status; /* status of the last operation */
1136 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001137#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1138 psa_se_drv_table_entry_t *driver;
1139#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (mbedtls_svc_key_id_is_null(key)) {
1142 return PSA_SUCCESS;
1143 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001144
Ronald Cronf2911112020-10-29 17:51:10 +01001145 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001146 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001147 * key, this will load the key description from persistent memory if not
1148 * done yet. We cannot avoid this loading as without it we don't know if
1149 * the key is operated by an SE or not and this information is needed by
1150 * the current implementation.
1151 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 status = psa_get_and_lock_key_slot(key, &slot);
1153 if (status != PSA_SUCCESS) {
1154 return status;
1155 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001156
Ronald Cronf2911112020-10-29 17:51:10 +01001157 /*
1158 * If the key slot containing the key description is under access by the
1159 * library (apart from the present access), the key cannot be destroyed
1160 * yet. For the time being, just return in error. Eventually (to be
1161 * implemented), the key should be destroyed when all accesses have
1162 * stopped.
1163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (slot->lock_count > 1) {
1165 psa_unlock_key_slot(slot);
1166 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001167 }
1168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001170 /* Refuse the destruction of a read-only key (which may or may not work
1171 * if we attempt it, depending on whether the key is merely read-only
1172 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001173 * Just do the best we can, which is to wipe the copy in memory
1174 * (done in this function's cleanup code). */
1175 overall_status = PSA_ERROR_NOT_PERMITTED;
1176 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001177 }
1178
Gilles Peskine354f7672019-07-12 23:46:38 +02001179#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1181 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001182 /* For a key in a secure element, we need to do three things:
1183 * remove the key file in internal storage, destroy the
1184 * key inside the secure element, and update the driver's
1185 * persistent data. Start a transaction that will encompass these
1186 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001188 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001190 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 status = psa_crypto_save_transaction();
1192 if (status != PSA_SUCCESS) {
1193 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001194 /* We should still try to destroy the key in the secure
1195 * element and the key metadata in storage. This is especially
1196 * important if the error is that the storage is full.
1197 * But how to do it exactly without risking an inconsistent
1198 * state after a reset?
1199 * https://github.com/ARMmbed/mbed-crypto/issues/215
1200 */
1201 overall_status = status;
1202 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001203 }
1204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 status = psa_destroy_se_key(driver,
1206 psa_key_slot_get_slot_number(slot));
1207 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001208 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001210 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001211#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1212
Darryl Greend49a4992018-06-18 17:27:26 +01001213#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1215 status = psa_destroy_persistent_key(slot->attr.id);
1216 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001217 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001219
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001220 /* TODO: other slots may have a copy of the same key. We should
1221 * invalidate them.
1222 * https://github.com/ARMmbed/mbed-crypto/issues/214
1223 */
Darryl Greend49a4992018-06-18 17:27:26 +01001224 }
1225#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001226
Gilles Peskinefc762652019-07-22 19:30:34 +02001227#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if (driver != NULL) {
1229 status = psa_save_se_persistent_data(driver);
1230 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001231 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 }
1233 status = psa_crypto_stop_transaction();
1234 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001235 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001237 }
1238#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1239
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001240exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001242 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001244 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 }
1246 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001247}
1248
Valerio Setti8bb57632023-05-26 13:48:07 +02001249#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
John Durkop0e005192020-10-31 22:06:54 -07001250 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001251static psa_status_t psa_get_rsa_public_exponent(
1252 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001254{
1255 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001257 uint8_t *buffer = NULL;
1258 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1262 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001263 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 }
1265 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001266 /* It's the default value, which is reported as an empty string,
1267 * so there's nothing to do. */
1268 goto exit;
1269 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 buflen = mbedtls_mpi_size(&mpi);
1272 buffer = mbedtls_calloc(1, buflen);
1273 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001274 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1275 goto exit;
1276 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1278 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001279 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001281 attributes->domain_parameters = buffer;
1282 attributes->domain_parameters_size = buflen;
1283
1284exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 mbedtls_mpi_free(&mpi);
1286 if (ret != 0) {
1287 mbedtls_free(buffer);
1288 }
1289 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001290}
Valerio Setti8bb57632023-05-26 13:48:07 +02001291#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001292 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001293
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001294/** Retrieve all the publicly-accessible attributes of a key.
1295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1297 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001298{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001299 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001300 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001301 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1306 if (status != PSA_SUCCESS) {
1307 return status;
1308 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001309
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001310 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1312 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001313
1314#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1316 psa_set_key_slot_number(attributes,
1317 psa_key_slot_get_slot_number(slot));
1318 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001319#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 switch (slot->attr.type) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001322#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
John Durkop0e005192020-10-31 22:06:54 -07001323 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001324 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001325 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001326 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001327 * is not yet implemented.
1328 * https://github.com/ARMmbed/mbed-crypto/issues/216
1329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001331 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001332
Ronald Cron90857082020-11-25 14:58:33 +01001333 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 slot->attr.type,
1335 slot->key.data,
1336 slot->key.bytes,
1337 &rsa);
1338 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001339 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 status = psa_get_rsa_public_exponent(rsa,
1343 attributes);
1344 mbedtls_rsa_free(rsa);
1345 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001346 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001347 break;
Valerio Setti8bb57632023-05-26 13:48:07 +02001348#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001349 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001350 default:
1351 /* Nothing else to do. */
1352 break;
1353 }
1354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 if (status != PSA_SUCCESS) {
1356 psa_reset_key_attributes(attributes);
1357 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001362}
1363
Gilles Peskinec8000c02019-08-02 20:15:51 +02001364#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1365psa_status_t psa_get_key_slot_number(
1366 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001368{
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001370 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 return PSA_SUCCESS;
1372 } else {
1373 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001374 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001375}
1376#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1379 size_t key_buffer_size,
1380 uint8_t *data,
1381 size_t data_size,
1382 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001383{
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (key_buffer_size > data_size) {
1385 return PSA_ERROR_BUFFER_TOO_SMALL;
1386 }
1387 memcpy(data, key_buffer, key_buffer_size);
1388 memset(data + key_buffer_size, 0,
1389 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001390 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001392}
1393
Ronald Cron7285cda2020-11-26 14:46:37 +01001394psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001395 const psa_key_attributes_t *attributes,
1396 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001398{
Ronald Crond18b5f82020-11-25 16:46:05 +01001399 psa_key_type_t type = attributes->core.type;
1400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if (key_type_is_raw_bytes(type) ||
1402 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001403 PSA_KEY_TYPE_IS_ECC(type) ||
1404 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 return psa_export_key_buffer_internal(
1406 key_buffer, key_buffer_size,
1407 data, data_size, data_length);
1408 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001409 /* This shouldn't happen in the reference implementation, but
1410 it is valid for a special-purpose implementation to omit
1411 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001413 }
1414}
1415
Gilles Peskine449bd832023-01-11 14:50:10 +01001416psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1417 uint8_t *data,
1418 size_t data_size,
1419 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001420{
1421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1422 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1423 psa_key_slot_t *slot;
1424
Ronald Crone907e552021-01-18 13:22:38 +01001425 /* Reject a zero-length output buffer now, since this can never be a
1426 * valid key representation. This way we know that data must be a valid
1427 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (data_size == 0) {
1429 return PSA_ERROR_BUFFER_TOO_SMALL;
1430 }
Ronald Crone907e552021-01-18 13:22:38 +01001431
Ronald Cron9486f9d2020-11-26 13:36:23 +01001432 /* Set the key to empty now, so that even when there are errors, we always
1433 * set data_length to a value between 0 and data_size. On error, setting
1434 * the key to empty is a good choice because an empty key representation is
1435 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001436 *data_length = 0;
1437
Ronald Cron9486f9d2020-11-26 13:36:23 +01001438 /* Export requires the EXPORT flag. There is an exception for public keys,
1439 * which don't require any flag, but
1440 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1443 PSA_KEY_USAGE_EXPORT, 0);
1444 if (status != PSA_SUCCESS) {
1445 return status;
1446 }
mohammad160306e79202018-03-28 13:17:44 +03001447
Ronald Crond18b5f82020-11-25 16:46:05 +01001448 psa_key_attributes_t attributes = {
1449 .core = slot->attr
1450 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 status = psa_driver_wrapper_export_key(&attributes,
1452 slot->key.data, slot->key.bytes,
1453 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001458}
1459
Ronald Cron7285cda2020-11-26 14:46:37 +01001460psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001461 const psa_key_attributes_t *attributes,
1462 const uint8_t *key_buffer,
1463 size_t key_buffer_size,
1464 uint8_t *data,
1465 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001467{
Ronald Crond18b5f82020-11-25 16:46:05 +01001468 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001469
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001470 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001471 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1472 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001473 /* Exporting public -> public */
1474 return psa_export_key_buffer_internal(
1475 key_buffer, key_buffer_size,
1476 data, data_size, data_length);
1477 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001478#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001479 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1480 return mbedtls_psa_rsa_export_public_key(attributes,
1481 key_buffer,
1482 key_buffer_size,
1483 data,
1484 data_size,
1485 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001486#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001487 /* We don't know how to convert a private RSA key to public. */
1488 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti8bb57632023-05-26 13:48:07 +02001489#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001490 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001491 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001492#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001493 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1494 return mbedtls_psa_ecp_export_public_key(attributes,
1495 key_buffer,
1496 key_buffer_size,
1497 data,
1498 data_size,
1499 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001500#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001501 /* We don't know how to convert a private ECC key to public */
1502 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001503#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001504 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001505 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001506#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001507 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001508 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001509 key_buffer,
1510 key_buffer_size,
1511 data, data_size,
1512 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001513#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001514 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti8bb57632023-05-26 13:48:07 +02001515#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001516 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001518 (void) key_buffer;
1519 (void) key_buffer_size;
1520 (void) data;
1521 (void) data_size;
1522 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001524 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001525}
Ronald Crond18b5f82020-11-25 16:46:05 +01001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1528 uint8_t *data,
1529 size_t data_size,
1530 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001531{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001532 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001533 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001534 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001535
Ronald Crone907e552021-01-18 13:22:38 +01001536 /* Reject a zero-length output buffer now, since this can never be a
1537 * valid key representation. This way we know that data must be a valid
1538 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (data_size == 0) {
1540 return PSA_ERROR_BUFFER_TOO_SMALL;
1541 }
Ronald Crone907e552021-01-18 13:22:38 +01001542
Darryl Greendd8fb772018-11-07 16:00:44 +00001543 /* Set the key to empty now, so that even when there are errors, we always
1544 * set data_length to a value between 0 and data_size. On error, setting
1545 * the key to empty is a good choice because an empty key representation is
1546 * unlikely to be accepted anywhere. */
1547 *data_length = 0;
1548
1549 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1551 if (status != PSA_SUCCESS) {
1552 return status;
1553 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1556 status = PSA_ERROR_INVALID_ARGUMENT;
1557 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001558 }
1559
Ronald Crond18b5f82020-11-25 16:46:05 +01001560 psa_key_attributes_t attributes = {
1561 .core = slot->attr
1562 };
Ronald Cron67227982020-11-26 15:16:05 +01001563 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001564 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001566
1567exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001571}
1572
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001573MBEDTLS_STATIC_ASSERT(
1574 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1575 "One or more key attribute flag is listed as both external-only and dual-use")
1576MBEDTLS_STATIC_ASSERT(
1577 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1578 "One or more key attribute flag is listed as both internal-only and dual-use")
1579MBEDTLS_STATIC_ASSERT(
1580 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1581 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001582
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001583/** Validate that a key policy is internally well-formed.
1584 *
1585 * This function only rejects invalid policies. It does not validate the
1586 * consistency of the policy with respect to other attributes of the key
1587 * such as the key type.
1588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001589static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001590{
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1592 PSA_KEY_USAGE_COPY |
1593 PSA_KEY_USAGE_ENCRYPT |
1594 PSA_KEY_USAGE_DECRYPT |
1595 PSA_KEY_USAGE_SIGN_MESSAGE |
1596 PSA_KEY_USAGE_VERIFY_MESSAGE |
1597 PSA_KEY_USAGE_SIGN_HASH |
1598 PSA_KEY_USAGE_VERIFY_HASH |
1599 PSA_KEY_USAGE_VERIFY_DERIVATION |
1600 PSA_KEY_USAGE_DERIVE)) != 0) {
1601 return PSA_ERROR_INVALID_ARGUMENT;
1602 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001605}
1606
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001607/** Validate the internal consistency of key attributes.
1608 *
1609 * This function only rejects invalid attribute values. If does not
1610 * validate the consistency of the attributes with any key data that may
1611 * be involved in the creation of the key.
1612 *
1613 * Call this function early in the key creation process.
1614 *
1615 * \param[in] attributes Key attributes for the new key.
1616 * \param[out] p_drv On any return, the driver for the key, if any.
1617 * NULL for a transparent key.
1618 *
1619 */
1620static psa_status_t psa_validate_key_attributes(
1621 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001623{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001624 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1626 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 status = psa_validate_key_location(lifetime, p_drv);
1629 if (status != PSA_SUCCESS) {
1630 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001631 }
1632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 status = psa_validate_key_persistence(lifetime);
1634 if (status != PSA_SUCCESS) {
1635 return status;
1636 }
1637
1638 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1639 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1640 return PSA_ERROR_INVALID_ARGUMENT;
1641 }
1642 } else {
1643 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1644 return PSA_ERROR_INVALID_ARGUMENT;
1645 }
1646 }
1647
1648 status = psa_validate_key_policy(&attributes->core.policy);
1649 if (status != PSA_SUCCESS) {
1650 return status;
1651 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001652
1653 /* Refuse to create overly large keys.
1654 * Note that this doesn't trigger on import if the attributes don't
1655 * explicitly specify a size (so psa_get_key_bits returns 0), so
1656 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1658 return PSA_ERROR_NOT_SUPPORTED;
1659 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001660
Gilles Peskine91e8c332019-08-02 19:19:39 +02001661 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1663 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1664 return PSA_ERROR_INVALID_ARGUMENT;
1665 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001668}
1669
Gilles Peskine4747d192019-04-17 15:05:45 +02001670/** Prepare a key slot to receive key material.
1671 *
1672 * This function allocates a key slot and sets its metadata.
1673 *
1674 * If this function fails, call psa_fail_key_creation().
1675 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001676 * This function is intended to be used as follows:
1677 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001678 * it with the specified attributes, and in case of a volatile key assign it
1679 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001680 * -# Populate the slot with the key material.
1681 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1682 * In case of failure at any step, stop the sequence and call
1683 * psa_fail_key_creation().
1684 *
Ronald Cron5c522922020-11-14 16:35:34 +01001685 * On success, the key slot is locked. It is the responsibility of the caller
1686 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001687 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001688 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001689 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001690 * \param[out] p_slot On success, a pointer to the prepared slot.
1691 * \param[out] p_drv On any return, the driver for the key, if any.
1692 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001693 *
1694 * \retval #PSA_SUCCESS
1695 * The key slot is ready to receive key material.
1696 * \return If this function fails, the key slot is an invalid state.
1697 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001698 */
1699static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001700 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001701 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001702 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001704{
1705 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001706 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001707 psa_key_slot_t *slot;
1708
Gilles Peskinedf179142019-07-15 22:02:14 +02001709 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001710 *p_drv = NULL;
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 status = psa_validate_key_attributes(attributes, p_drv);
1713 if (status != PSA_SUCCESS) {
1714 return status;
1715 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1718 if (status != PSA_SUCCESS) {
1719 return status;
1720 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001721 slot = *p_slot;
1722
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001723 /* We're storing the declared bit-size of the key. It's up to each
1724 * creation mechanism to verify that this information is correct.
1725 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001726 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001727 * is optional (import, copy). In case of a volatile key, assign it the
1728 * volatile key identifier associated to the slot returned to contain its
1729 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001730
1731 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001733#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1734 slot->attr.id = volatile_key_id;
1735#else
1736 slot->attr.id.key_id = volatile_key_id;
1737#endif
1738 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001739
Gilles Peskine91e8c332019-08-02 19:19:39 +02001740 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001741 * external-only flags, query `attributes`. Thanks to the check
1742 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001743 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001744 * may have set. */
1745 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001746
Gilles Peskinecbaff462019-07-12 23:46:04 +02001747#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001748 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001749 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001750 * create the key file in internal storage, create the
1751 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001752 * persistent data. This is done by starting a transaction that will
1753 * encompass these three actions.
1754 * For registering a volatile key, we just need to find an appropriate
1755 * slot number inside the SE. Since the key is designated volatile, creating
1756 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001757 /* The first thing to do is to find a slot number for the new key.
1758 * We save the slot number in persistent storage as part of the
1759 * transaction data. It will be needed to recover if the power
1760 * fails during the key creation process, to clean up on the secure
1761 * element side after restarting. Obtaining a slot number from the
1762 * secure element driver updates its persistent state, but we do not yet
1763 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001764 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001766 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1768 &slot_number);
1769 if (status != PSA_SUCCESS) {
1770 return status;
1771 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1774 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001775 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001776 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001777 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 status = psa_crypto_save_transaction();
1779 if (status != PSA_SUCCESS) {
1780 (void) psa_crypto_stop_transaction();
1781 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001782 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001783 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001784
1785 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001787 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001790 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001792 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001793#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001796}
Gilles Peskine4747d192019-04-17 15:05:45 +02001797
1798/** Finalize the creation of a key once its key material has been set.
1799 *
1800 * This entails writing the key to persistent storage.
1801 *
1802 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001803 * See the documentation of psa_start_key_creation() for the intended use
1804 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001805 *
Ronald Cron5c522922020-11-14 16:35:34 +01001806 * If the finalization succeeds, the function unlocks the key slot (it was
1807 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1808 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001809 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001810 * \param[in,out] slot Pointer to the slot with key material.
1811 * \param[in] driver The secure element driver for the key,
1812 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001813 * \param[out] key On success, identifier of the key. Note that the
1814 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001815 *
1816 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001817 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001818 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1819 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1820 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1821 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1822 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1823 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001824 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001825 * \return If this function fails, the key slot is an invalid state.
1826 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001827 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001828static psa_status_t psa_finish_key_creation(
1829 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001830 psa_se_drv_table_entry_t *driver,
1831 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001832{
1833 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001834 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001835 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001836
1837#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001839#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001841 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001842 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001844
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001845 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1846 sizeof(data.slot_number),
1847 "Slot number size does not match psa_se_key_data_storage_t");
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1850 status = psa_save_persistent_key(&slot->attr,
1851 (uint8_t *) &data,
1852 sizeof(data));
1853 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001854#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1855 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001856 /* Key material is saved in export representation in the slot, so
1857 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 status = psa_save_persistent_key(&slot->attr,
1859 slot->key.data,
1860 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001861 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001862 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001863#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1864
Gilles Peskinecbaff462019-07-12 23:46:04 +02001865#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001866 /* Finish the transaction for a key creation. This does not
1867 * happen when registering an existing key. Detect this case
1868 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001869 * creation of a persistent key in a secure element requires a transaction,
1870 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (driver != NULL &&
1872 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1873 status = psa_save_se_persistent_data(driver);
1874 if (status != PSA_SUCCESS) {
1875 psa_destroy_persistent_key(slot->attr.id);
1876 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001877 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001879 }
1880#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1881
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001883 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 status = psa_unlock_key_slot(slot);
1885 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001886 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001888 }
Ronald Cron50972942020-11-14 11:28:25 +01001889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001891}
1892
1893/** Abort the creation of a key.
1894 *
1895 * You may call this function after calling psa_start_key_creation(),
1896 * or after psa_finish_key_creation() fails. In other circumstances, this
1897 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001898 * See the documentation of psa_start_key_creation() for the intended use
1899 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001900 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001901 * \param[in,out] slot Pointer to the slot with key material.
1902 * \param[in] driver The secure element driver for the key,
1903 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001904 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001905static void psa_fail_key_creation(psa_key_slot_t *slot,
1906 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001907{
Gilles Peskine011e4282019-06-26 18:34:38 +02001908 (void) driver;
1909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001911 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001913
1914#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001915 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001916 * element, and the failure happened later (when saving metadata
1917 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001918 * element.
1919 * https://github.com/ARMmbed/mbed-crypto/issues/217
1920 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001921
Gilles Peskined7729582019-08-05 15:55:54 +02001922 /* Abort the ongoing transaction if any (there may not be one if
1923 * the creation process failed before starting one, or if the
1924 * key creation is a registration of a key in a secure element).
1925 * Earlier functions must already have done what it takes to undo any
1926 * partial creation. All that's left is to update the transaction data
1927 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001929#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001932}
1933
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001934/** Validate optional attributes during key creation.
1935 *
1936 * Some key attributes are optional during key creation. If they are
1937 * specified in the attributes structure, check that they are consistent
1938 * with the data in the slot.
1939 *
1940 * This function should be called near the end of key creation, after
1941 * the slot in memory is fully populated but before saving persistent data.
1942 */
1943static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001944 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001946{
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (attributes->core.type != 0) {
1948 if (attributes->core.type != slot->attr.type) {
1949 return PSA_ERROR_INVALID_ARGUMENT;
1950 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 }
1952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if (attributes->domain_parameters_size != 0) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001954#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1956 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001957 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001958 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001960
Ronald Cron90857082020-11-25 14:58:33 +01001961 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 slot->attr.type,
1963 slot->key.data,
1964 slot->key.bytes,
1965 &rsa);
1966 if (status != PSA_SUCCESS) {
1967 return status;
1968 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 mbedtls_mpi_init(&actual);
1971 mbedtls_mpi_init(&required);
1972 ret = mbedtls_rsa_export(rsa,
1973 NULL, NULL, NULL, NULL, &actual);
1974 mbedtls_rsa_free(rsa);
1975 mbedtls_free(rsa);
1976 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001977 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 }
1979 ret = mbedtls_mpi_read_binary(&required,
1980 attributes->domain_parameters,
1981 attributes->domain_parameters_size);
1982 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001983 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 }
1985 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001986 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 }
1988rsa_exit:
1989 mbedtls_mpi_free(&actual);
1990 mbedtls_mpi_free(&required);
1991 if (ret != 0) {
1992 return mbedtls_to_psa_error(ret);
1993 }
1994 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02001995#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) ||
John Durkop9814fa22020-11-04 12:28:15 -08001996 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001997 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001999 }
2000 }
2001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 if (attributes->core.bits != 0) {
2003 if (attributes->core.bits != slot->attr.bits) {
2004 return PSA_ERROR_INVALID_ARGUMENT;
2005 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002006 }
2007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009}
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2012 const uint8_t *data,
2013 size_t data_length,
2014 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002015{
2016 psa_status_t status;
2017 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002018 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002019 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302020 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002021
Ronald Cron81709fc2020-11-14 12:10:32 +01002022 *key = MBEDTLS_SVC_KEY_ID_INIT;
2023
Gilles Peskine0f84d622019-09-12 19:03:13 +02002024 /* Reject zero-length symmetric keys (including raw data key objects).
2025 * This also rejects any key which might be encoded as an empty string,
2026 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (data_length == 0) {
2028 return PSA_ERROR_INVALID_ARGUMENT;
2029 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002030
Archana449608b2021-09-08 15:36:05 +05302031 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 if (data_length > SIZE_MAX / 8) {
2033 return PSA_ERROR_NOT_SUPPORTED;
2034 }
Archana6ed4bda2021-08-04 10:47:15 +05302035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2037 &slot, &driver);
2038 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002042 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302043 * storage ( thus not in the case of importing a key in a secure element
2044 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2045 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (slot->key.data == NULL) {
2047 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302048 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 attributes, data, data_length, &storage_size);
2050 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302051 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 }
Archanad8a83dc2021-06-14 10:04:16 +05302053 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 status = psa_allocate_buffer_to_slot(slot, storage_size);
2055 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002056 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002058 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002059
2060 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 status = psa_driver_wrapper_import_key(attributes,
2062 data, data_length,
2063 slot->key.data,
2064 slot->key.bytes,
2065 &slot->key.bytes, &bits);
2066 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002067 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002071 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002073 status = PSA_ERROR_INVALID_ARGUMENT;
2074 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002075 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002076
Archana6ed4bda2021-08-04 10:47:15 +05302077 /* Enforce a size limit, and in particular ensure that the bit
2078 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302080 status = PSA_ERROR_NOT_SUPPORTED;
2081 goto exit;
2082 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 status = psa_validate_optional_attributes(slot, attributes);
2084 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002085 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002089exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (status != PSA_SUCCESS) {
2091 psa_fail_key_creation(slot, driver);
2092 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002095}
2096
Gilles Peskined7729582019-08-05 15:55:54 +02002097#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2098psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002100{
2101 psa_status_t status;
2102 psa_key_slot_t *slot = NULL;
2103 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002104 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002105
2106 /* Leaving attributes unspecified is not currently supported.
2107 * It could make sense to query the key type and size from the
2108 * secure element, but not all secure elements support this
2109 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2111 return PSA_ERROR_NOT_SUPPORTED;
2112 }
2113 if (psa_get_key_bits(attributes) == 0) {
2114 return PSA_ERROR_NOT_SUPPORTED;
2115 }
Gilles Peskined7729582019-08-05 15:55:54 +02002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2118 &slot, &driver);
2119 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 }
Gilles Peskined7729582019-08-05 15:55:54 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002124
2125exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 if (status != PSA_SUCCESS) {
2127 psa_fail_key_creation(slot, driver);
2128 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002129
Gilles Peskined7729582019-08-05 15:55:54 +02002130 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 psa_close_key(key);
2132 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002133}
2134#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2137 const psa_key_attributes_t *specified_attributes,
2138 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002139{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002140 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002141 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002142 psa_key_slot_t *source_slot = NULL;
2143 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002144 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002145 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302146 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002147
Ronald Cron81709fc2020-11-14 12:10:32 +01002148 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2149
Archana8a180362021-07-05 02:18:48 +05302150 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2152 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002153 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 status = psa_validate_optional_attributes(source_slot,
2157 specified_attributes);
2158 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002159 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002161
Archana9d17bf42021-09-10 06:22:44 +05302162 /* The target key type and number of bits have been validated by
2163 * psa_validate_optional_attributes() to be either equal to zero or
2164 * equal to the ones of the source key. So it is safe to inherit
2165 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302166 * */
Archana9d17bf42021-09-10 06:22:44 +05302167 actual_attributes.core.bits = source_slot->attr.bits;
2168 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302169
2170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 status = psa_restrict_key_policy(source_slot->attr.type,
2172 &actual_attributes.core.policy,
2173 &source_slot->attr.policy);
2174 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002175 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2179 &target_slot, &driver);
2180 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 }
2183 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2184 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302185 /*
Archana9d17bf42021-09-10 06:22:44 +05302186 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302187 * the source key would need to be exported as plaintext and re-imported
2188 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302189 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302190 * appropriate API invocations from the application, if needed.
2191 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002192 status = PSA_ERROR_NOT_SUPPORTED;
2193 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002194 }
Archana8a180362021-07-05 02:18:48 +05302195 /*
2196 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302197 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302198 * - For opaque keys this translates to an invocation of the drivers'
2199 * copy_key entry point through the dispatch layer.
2200 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2202 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2203 &storage_size);
2204 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302205 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 }
Archana374fe5b2021-09-08 15:50:28 +05302207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2209 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302210 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 }
Archana374fe5b2021-09-08 15:50:28 +05302212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 status = psa_driver_wrapper_copy_key(&actual_attributes,
2214 source_slot->key.data,
2215 source_slot->key.bytes,
2216 target_slot->key.data,
2217 target_slot->key.bytes,
2218 &target_slot->key.bytes);
2219 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 }
2222 } else {
2223 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302224 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 source_slot->key.bytes);
2226 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302227 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 }
Archana8a180362021-07-05 02:18:48 +05302229 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 if (status != PSA_SUCCESS) {
2233 psa_fail_key_creation(target_slot, driver);
2234 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002239}
2240
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002241
2242
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002243/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002244/* Message digests */
2245/****************************************************************/
2246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002248{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002249 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if (operation->id == 0) {
2251 return PSA_SUCCESS;
2252 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002255 operation->id = 0;
2256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258}
2259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2261 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002262{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002264
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002265 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002267 status = PSA_ERROR_BAD_STATE;
2268 goto exit;
2269 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002272 status = PSA_ERROR_INVALID_ARGUMENT;
2273 goto exit;
2274 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002275
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002276 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2277 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002281
2282exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 if (status != PSA_SUCCESS) {
2284 psa_hash_abort(operation);
2285 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002286
2287 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288}
2289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2291 const uint8_t *input,
2292 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002293{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002294 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002297 status = PSA_ERROR_BAD_STATE;
2298 goto exit;
2299 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002300
Steven Cooremanc8288352021-03-04 14:02:19 +01002301 /* Don't require hash implementations to behave correctly on a
2302 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (input_length == 0) {
2304 return PSA_SUCCESS;
2305 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002308
2309exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (status != PSA_SUCCESS) {
2311 psa_hash_abort(operation);
2312 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002315}
2316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2318 uint8_t *hash,
2319 size_t hash_size,
2320 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002322 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 if (operation->id == 0) {
2324 return PSA_ERROR_BAD_STATE;
2325 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326
Steven Cooreman1e582352021-02-18 17:24:37 +01002327 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 operation, hash, hash_size, hash_length);
2329 psa_hash_abort(operation);
2330 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331}
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2334 const uint8_t *hash,
2335 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336{
Ronald Cron69a63422021-10-18 09:47:58 +02002337 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002338 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002339 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 operation,
2341 actual_hash, sizeof(actual_hash),
2342 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002345 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002349 status = PSA_ERROR_INVALID_SIGNATURE;
2350 goto exit;
2351 }
2352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002354 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002356
2357exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2359 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002360 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364}
2365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366psa_status_t psa_hash_compute(psa_algorithm_t alg,
2367 const uint8_t *input, size_t input_length,
2368 uint8_t *hash, size_t hash_size,
2369 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002370{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002371 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 if (!PSA_ALG_IS_HASH(alg)) {
2373 return PSA_ERROR_INVALID_ARGUMENT;
2374 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2377 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002378}
2379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380psa_status_t psa_hash_compare(psa_algorithm_t alg,
2381 const uint8_t *input, size_t input_length,
2382 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002383{
Ronald Cron69a63422021-10-18 09:47:58 +02002384 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002385 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (!PSA_ALG_IS_HASH(alg)) {
2388 return PSA_ERROR_INVALID_ARGUMENT;
2389 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002390
Steven Cooreman1e582352021-02-18 17:24:37 +01002391 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 alg, input, input_length,
2393 actual_hash, sizeof(actual_hash),
2394 &actual_hash_length);
2395 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002396 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 }
2398 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002399 status = PSA_ERROR_INVALID_SIGNATURE;
2400 goto exit;
2401 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002403 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002405
2406exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2408 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002409}
2410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2412 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002413{
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if (source_operation->id == 0 ||
2415 target_operation->id != 0) {
2416 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002417 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2420 target_operation);
2421 if (status != PSA_SUCCESS) {
2422 psa_hash_abort(target_operation);
2423 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002426}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002427
2428
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002429/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430/* MAC */
2431/****************************************************************/
2432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002434{
Steven Cooremane6804192021-03-19 18:28:56 +01002435 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if (operation->id == 0) {
2437 return PSA_SUCCESS;
2438 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002441 operation->mac_size = 0;
2442 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002443 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002446}
2447
Ronald Cron2dff3b22021-06-17 16:33:22 +02002448static psa_status_t psa_mac_finalize_alg_and_key_validation(
2449 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002450 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002452{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002453 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 psa_key_type_t key_type = psa_get_key_type(attributes);
2455 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if (!PSA_ALG_IS_MAC(alg)) {
2458 return PSA_ERROR_INVALID_ARGUMENT;
2459 }
Steven Cooremane6804192021-03-19 18:28:56 +01002460
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002461 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 status = psa_mac_key_can_do(alg, key_type);
2463 if (status != PSA_SUCCESS) {
2464 return status;
2465 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002466
Steven Cooremand1a68f12021-05-10 11:13:41 +02002467 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002471 /* A very short MAC is too short for security since it can be
2472 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2473 * so we make this our minimum, even though 32 bits is still
2474 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002476 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2479 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002480 /* It's impossible to "truncate" to a larger length than the full length
2481 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002483 }
2484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002486 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2487 * that is disabled in the compile-time configuration. The result can
2488 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2489 * configuration into account. In this case, force a return of
2490 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2491 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2492 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2493 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2494 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002496 }
2497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002499}
2500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2502 mbedtls_svc_key_id_t key,
2503 psa_algorithm_t alg,
2504 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002505{
2506 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2507 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002508 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509
2510 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002512 status = PSA_ERROR_BAD_STATE;
2513 goto exit;
2514 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002515
2516 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 key,
2518 &slot,
2519 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2520 alg);
2521 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002522 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524
2525 psa_key_attributes_t attributes = {
2526 .core = slot->attr
2527 };
2528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2530 &operation->mac_size);
2531 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534
2535 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002536 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 if (is_sign) {
2538 status = psa_driver_wrapper_mac_sign_setup(operation,
2539 &attributes,
2540 slot->key.data,
2541 slot->key.bytes,
2542 alg);
2543 } else {
2544 status = psa_driver_wrapper_mac_verify_setup(operation,
2545 &attributes,
2546 slot->key.data,
2547 slot->key.bytes,
2548 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002550
Gilles Peskinefbfac682018-07-08 20:51:54 +02002551exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 if (status != PSA_SUCCESS) {
2553 psa_mac_abort(operation);
2554 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559}
2560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2562 mbedtls_svc_key_id_t key,
2563 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002564{
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002566}
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2569 mbedtls_svc_key_id_t key,
2570 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002571{
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002573}
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2576 const uint8_t *input,
2577 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002578{
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (operation->id == 0) {
2580 return PSA_ERROR_BAD_STATE;
2581 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002583 /* Don't require hash implementations to behave correctly on a
2584 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (input_length == 0) {
2586 return PSA_SUCCESS;
2587 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2590 input, input_length);
2591 if (status != PSA_SUCCESS) {
2592 psa_mac_abort(operation);
2593 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002594
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002596}
2597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2599 uint8_t *mac,
2600 size_t mac_size,
2601 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002602{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002603 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2604 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002607 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002608 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002609 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002612 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002613 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002614 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002615
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002616 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2617 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002619 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002620 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002621 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002624 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002625 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002626 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 status = psa_driver_wrapper_mac_sign_finish(operation,
2629 mac, operation->mac_size,
2630 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002631
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002632exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002633 /* In case of success, set the potential excess room in the output buffer
2634 * to an invalid value, to avoid potentially leaking a longer MAC.
2635 * In case of error, set the output length and content to a safe default,
2636 * such that in case the caller misses an error check, the output would be
2637 * an unachievable MAC.
2638 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002640 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002641 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002642 }
mohammad16036df908f2018-04-02 08:34:15 -07002643
Paul Elliottdc42ca82023-02-24 18:11:59 +00002644 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002647
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002649}
2650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2652 const uint8_t *mac,
2653 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002654{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002655 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2656 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002659 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002660 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002661 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002664 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002665 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002666 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002669 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002670 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002671 }
2672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 status = psa_driver_wrapper_mac_verify_finish(operation,
2674 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002675
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002676exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002680}
2681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2683 psa_algorithm_t alg,
2684 const uint8_t *input,
2685 size_t input_length,
2686 uint8_t *mac,
2687 size_t mac_size,
2688 size_t *mac_length,
2689 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002690{
2691 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002692 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2693 psa_key_slot_t *slot;
2694 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695
Ronald Cron51131b52021-06-17 17:17:20 +02002696 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 key,
2698 &slot,
2699 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2700 alg);
2701 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002702 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002704
Ronald Cron51131b52021-06-17 17:17:20 +02002705 psa_key_attributes_t attributes = {
2706 .core = slot->attr
2707 };
2708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2710 &operation_mac_size);
2711 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002712 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002716 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002717 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002718 }
2719
2720 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 &attributes,
2722 slot->key.data, slot->key.bytes,
2723 alg,
2724 input, input_length,
2725 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002726
2727exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002728 /* In case of success, set the potential excess room in the output buffer
2729 * to an invalid value, to avoid potentially leaking a longer MAC.
2730 * In case of error, set the output length and content to a safe default,
2731 * such that in case the caller misses an error check, the output would be
2732 * an unachievable MAC.
2733 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002735 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002736 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002737 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002738
2739 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002744}
2745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002747 psa_algorithm_t alg,
2748 const uint8_t *input,
2749 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 uint8_t *mac,
2751 size_t mac_size,
2752 size_t *mac_length)
2753{
2754 return psa_mac_compute_internal(key, alg,
2755 input, input_length,
2756 mac, mac_size, mac_length, 1);
2757}
2758
2759psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2760 psa_algorithm_t alg,
2761 const uint8_t *input,
2762 size_t input_length,
2763 const uint8_t *mac,
2764 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002765{
2766 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002767 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2768 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 status = psa_mac_compute_internal(key, alg,
2771 input, input_length,
2772 actual_mac, sizeof(actual_mac),
2773 &actual_mac_length, 0);
2774 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002775 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002779 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002780 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002781 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002783 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002784 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002785 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002786
2787exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002791}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002792
Gilles Peskinee59236f2018-01-27 23:32:46 +01002793/****************************************************************/
2794/* Asymmetric cryptography */
2795/****************************************************************/
2796
Gilles Peskine449bd832023-01-11 14:50:10 +01002797static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2798 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002799{
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 if (input_is_message) {
2801 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2802 return PSA_ERROR_INVALID_ARGUMENT;
2803 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2806 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2807 return PSA_ERROR_INVALID_ARGUMENT;
2808 }
2809 }
2810 } else {
2811 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2812 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002813 }
2814 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002817}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2820 int input_is_message,
2821 psa_algorithm_t alg,
2822 const uint8_t *input,
2823 size_t input_length,
2824 uint8_t *signature,
2825 size_t signature_size,
2826 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002827{
2828 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2829 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2830 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002831
2832 *signature_length = 0;
2833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 status = psa_sign_verify_check_alg(input_is_message, alg);
2835 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002836 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002838
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002839 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002840 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002841 * buffer can in principle be empty since it doesn't actually have
2842 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 if (signature_size == 0) {
2844 return PSA_ERROR_BUFFER_TOO_SMALL;
2845 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002846
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002847 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 key, &slot,
2849 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2850 PSA_KEY_USAGE_SIGN_HASH,
2851 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002854 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002858 status = PSA_ERROR_INVALID_ARGUMENT;
2859 goto exit;
2860 }
2861
2862 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002864 };
2865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002867 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002868 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002869 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 signature, signature_size, signature_length);
2871 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002872
2873 status = psa_driver_wrapper_sign_hash(
2874 &attributes, slot->key.data, slot->key.bytes,
2875 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002877 }
2878
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002879
2880exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002881 psa_wipe_tag_output_buffer(signature, status, signature_size,
2882 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002883
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002887}
2888
Gilles Peskine449bd832023-01-11 14:50:10 +01002889static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2890 int input_is_message,
2891 psa_algorithm_t alg,
2892 const uint8_t *input,
2893 size_t input_length,
2894 const uint8_t *signature,
2895 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002896{
2897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2898 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2899 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 status = psa_sign_verify_check_alg(input_is_message, alg);
2902 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002903 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002905
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002906 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 key, &slot,
2908 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2909 PSA_KEY_USAGE_VERIFY_HASH,
2910 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002911
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 if (status != PSA_SUCCESS) {
2913 return status;
2914 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002915
2916 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002918 };
2919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002921 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002922 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002923 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 signature, signature_length);
2925 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002926 status = psa_driver_wrapper_verify_hash(
2927 &attributes, slot->key.data, slot->key.bytes,
2928 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002930 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002935
2936}
2937
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002938psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002939 const psa_key_attributes_t *attributes,
2940 const uint8_t *key_buffer,
2941 size_t key_buffer_size,
2942 psa_algorithm_t alg,
2943 const uint8_t *input,
2944 size_t input_length,
2945 uint8_t *signature,
2946 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002948{
2949 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002952 size_t hash_length;
2953 uint8_t hash[PSA_HASH_MAX_SIZE];
2954
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002955 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 PSA_ALG_SIGN_GET_HASH(alg),
2957 input, input_length,
2958 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002961 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002963
2964 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 attributes, key_buffer, key_buffer_size,
2966 alg, hash, hash_length,
2967 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002968 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002971}
2972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2974 psa_algorithm_t alg,
2975 const uint8_t *input,
2976 size_t input_length,
2977 uint8_t *signature,
2978 size_t signature_size,
2979 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002980{
2981 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002982 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002984}
2985
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002986psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002987 const psa_key_attributes_t *attributes,
2988 const uint8_t *key_buffer,
2989 size_t key_buffer_size,
2990 psa_algorithm_t alg,
2991 const uint8_t *input,
2992 size_t input_length,
2993 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002995{
2996 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002999 size_t hash_length;
3000 uint8_t hash[PSA_HASH_MAX_SIZE];
3001
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003002 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 PSA_ALG_SIGN_GET_HASH(alg),
3004 input, input_length,
3005 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003008 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003010
3011 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 attributes, key_buffer, key_buffer_size,
3013 alg, hash, hash_length,
3014 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003015 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003018}
3019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3021 psa_algorithm_t alg,
3022 const uint8_t *input,
3023 size_t input_length,
3024 const uint8_t *signature,
3025 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003026{
3027 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003028 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003030}
3031
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003032psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003033 const psa_key_attributes_t *attributes,
3034 const uint8_t *key_buffer, size_t key_buffer_size,
3035 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003037{
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3039 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3040 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003041#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3043 return mbedtls_psa_rsa_sign_hash(
3044 attributes,
3045 key_buffer, key_buffer_size,
3046 alg, hash, hash_length,
3047 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003048#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3049 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 } else {
3051 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003052 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3054 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003055#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3057 return mbedtls_psa_ecdsa_sign_hash(
3058 attributes,
3059 key_buffer, key_buffer_size,
3060 alg, hash, hash_length,
3061 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003062#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3063 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 } else {
3065 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003066 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003067 }
Ronald Cron4501c982021-03-19 20:09:32 +01003068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 (void) key_buffer;
3070 (void) key_buffer_size;
3071 (void) hash;
3072 (void) hash_length;
3073 (void) signature;
3074 (void) signature_size;
3075 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003078}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3081 psa_algorithm_t alg,
3082 const uint8_t *hash,
3083 size_t hash_length,
3084 uint8_t *signature,
3085 size_t signature_size,
3086 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003087{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003088 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003089 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003091}
3092
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003093psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003094 const psa_key_attributes_t *attributes,
3095 const uint8_t *key_buffer, size_t key_buffer_size,
3096 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003098{
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3100 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3101 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003102#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3104 return mbedtls_psa_rsa_verify_hash(
3105 attributes,
3106 key_buffer, key_buffer_size,
3107 alg, hash, hash_length,
3108 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003109#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3110 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 } else {
3112 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003113 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3115 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003116#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3118 return mbedtls_psa_ecdsa_verify_hash(
3119 attributes,
3120 key_buffer, key_buffer_size,
3121 alg, hash, hash_length,
3122 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003123#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3124 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 } else {
3126 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003127 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003128 }
Ronald Cron4501c982021-03-19 20:09:32 +01003129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 (void) key_buffer;
3131 (void) key_buffer_size;
3132 (void) hash;
3133 (void) hash_length;
3134 (void) signature;
3135 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003138}
3139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3141 psa_algorithm_t alg,
3142 const uint8_t *hash,
3143 size_t hash_length,
3144 const uint8_t *signature,
3145 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003146{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003147 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003148 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003150}
3151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3153 psa_algorithm_t alg,
3154 const uint8_t *input,
3155 size_t input_length,
3156 const uint8_t *salt,
3157 size_t salt_length,
3158 uint8_t *output,
3159 size_t output_size,
3160 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003161{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003162 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003163 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003164 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003165
Darryl Green5cc689a2018-07-24 15:34:10 +01003166 (void) input;
3167 (void) input_length;
3168 (void) salt;
3169 (void) output;
3170 (void) output_size;
3171
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003172 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003173
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3175 return PSA_ERROR_INVALID_ARGUMENT;
3176 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003177
Ronald Cron5c522922020-11-14 16:35:34 +01003178 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3180 if (status != PSA_SUCCESS) {
3181 return status;
3182 }
3183 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3184 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003185 status = PSA_ERROR_INVALID_ARGUMENT;
3186 goto exit;
3187 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003188
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003189 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003191 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003192
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003193 status = psa_driver_wrapper_asymmetric_encrypt(
3194 &attributes, slot->key.data, slot->key.bytes,
3195 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003199
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003201}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3204 psa_algorithm_t alg,
3205 const uint8_t *input,
3206 size_t input_length,
3207 const uint8_t *salt,
3208 size_t salt_length,
3209 uint8_t *output,
3210 size_t output_size,
3211 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003214 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003215 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003216
Darryl Green5cc689a2018-07-24 15:34:10 +01003217 (void) input;
3218 (void) input_length;
3219 (void) salt;
3220 (void) output;
3221 (void) output_size;
3222
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003223 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3226 return PSA_ERROR_INVALID_ARGUMENT;
3227 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003228
Ronald Cron5c522922020-11-14 16:35:34 +01003229 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3231 if (status != PSA_SUCCESS) {
3232 return status;
3233 }
3234 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003235 status = PSA_ERROR_INVALID_ARGUMENT;
3236 goto exit;
3237 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003239 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003241 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003243 status = psa_driver_wrapper_asymmetric_decrypt(
3244 &attributes, slot->key.data, slot->key.bytes,
3245 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003247
3248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003250
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003252}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003253
Paul Elliott9fe12f62022-11-30 19:16:02 +00003254/****************************************************************/
3255/* Asymmetric interruptible cryptography */
3256/****************************************************************/
3257
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003258static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3259
Paul Elliott9fe12f62022-11-30 19:16:02 +00003260void psa_interruptible_set_max_ops(uint32_t max_ops)
3261{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003262 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003263}
3264
3265uint32_t psa_interruptible_get_max_ops(void)
3266{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003267 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003268}
3269
Paul Elliott9fe12f62022-11-30 19:16:02 +00003270uint32_t psa_sign_hash_get_num_ops(
3271 const psa_sign_hash_interruptible_operation_t *operation)
3272{
Paul Elliott296ede92022-12-15 17:00:30 +00003273 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003274}
3275
3276uint32_t psa_verify_hash_get_num_ops(
3277 const psa_verify_hash_interruptible_operation_t *operation)
3278{
Paul Elliott296ede92022-12-15 17:00:30 +00003279 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003280}
3281
Paul Elliott59ad9452022-12-18 15:09:02 +00003282static psa_status_t psa_sign_hash_abort_internal(
3283 psa_sign_hash_interruptible_operation_t *operation)
3284{
3285 if (operation->id == 0) {
3286 /* The object has (apparently) been initialized but it is not (yet)
3287 * in use. It's ok to call abort on such an object, and there's
3288 * nothing to do. */
3289 return PSA_SUCCESS;
3290 }
3291
3292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3293
3294 status = psa_driver_wrapper_sign_hash_abort(operation);
3295
3296 operation->id = 0;
3297
Paul Elliotte6145dc2023-02-07 12:51:21 +00003298 /* Do not clear either the error_occurred or num_ops elements here as they
3299 * only want to be cleared by the application calling abort, not by abort
3300 * being called at completion of an operation. */
3301
Paul Elliott59ad9452022-12-18 15:09:02 +00003302 return status;
3303}
3304
Paul Elliott9fe12f62022-11-30 19:16:02 +00003305psa_status_t psa_sign_hash_start(
3306 psa_sign_hash_interruptible_operation_t *operation,
3307 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3308 const uint8_t *hash, size_t hash_length)
3309{
3310 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3311 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3312 psa_key_slot_t *slot;
3313
Paul Elliottc9774412023-02-06 15:14:07 +00003314 /* Check that start has not been previously called, or operation has not
3315 * previously errored. */
3316 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003317 return PSA_ERROR_BAD_STATE;
3318 }
3319
Paul Elliott9fe12f62022-11-30 19:16:02 +00003320 status = psa_sign_verify_check_alg(0, alg);
3321 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003322 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003323 return status;
3324 }
3325
3326 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3327 PSA_KEY_USAGE_SIGN_HASH,
3328 alg);
3329
3330 if (status != PSA_SUCCESS) {
3331 goto exit;
3332 }
3333
3334 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3335 status = PSA_ERROR_INVALID_ARGUMENT;
3336 goto exit;
3337 }
3338
3339 psa_key_attributes_t attributes = {
3340 .core = slot->attr
3341 };
3342
Paul Elliott296ede92022-12-15 17:00:30 +00003343 /* Ensure ops count gets reset, in case of operation re-use. */
3344 operation->num_ops = 0;
3345
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3347 slot->key.data,
3348 slot->key.bytes, alg,
3349 hash, hash_length);
3350exit:
3351
3352 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003353 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003354 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003355 }
3356
3357 unlock_status = psa_unlock_key_slot(slot);
3358
Paul Elliottc9774412023-02-06 15:14:07 +00003359 if (unlock_status != PSA_SUCCESS) {
3360 operation->error_occurred = 1;
3361 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003362
Paul Elliottc9774412023-02-06 15:14:07 +00003363 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003364}
3365
3366
3367psa_status_t psa_sign_hash_complete(
3368 psa_sign_hash_interruptible_operation_t *operation,
3369 uint8_t *signature, size_t signature_size,
3370 size_t *signature_length)
3371{
3372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3373
3374 *signature_length = 0;
3375
Paul Elliottc9774412023-02-06 15:14:07 +00003376 /* Check that start has been called first, and that operation has not
3377 * previously errored. */
3378 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003379 status = PSA_ERROR_BAD_STATE;
3380 goto exit;
3381 }
3382
Paul Elliott096abc42023-02-03 18:33:23 +00003383 /* Immediately reject a zero-length signature buffer. This guarantees that
3384 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003385 if (signature_size == 0) {
3386 status = PSA_ERROR_BUFFER_TOO_SMALL;
3387 goto exit;
3388 }
3389
3390 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3391 signature_size,
3392 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003393
Paul Elliott296ede92022-12-15 17:00:30 +00003394 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003395 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003396
Paul Elliottebe225c2023-02-10 14:32:53 +00003397exit:
3398
Paul Elliott59200a22023-02-21 15:07:40 +00003399 psa_wipe_tag_output_buffer(signature, status, signature_size,
3400 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003401
Paul Elliott53bb3122023-02-10 14:22:22 +00003402 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003403 if (status != PSA_SUCCESS) {
3404 operation->error_occurred = 1;
3405 }
3406
Paul Elliott59ad9452022-12-18 15:09:02 +00003407 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003408 }
3409
3410 return status;
3411}
3412
3413psa_status_t psa_sign_hash_abort(
3414 psa_sign_hash_interruptible_operation_t *operation)
3415{
Paul Elliott59ad9452022-12-18 15:09:02 +00003416 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3417
3418 status = psa_sign_hash_abort_internal(operation);
3419
3420 /* We clear the number of ops done here, so that it is not cleared when
3421 * the operation fails or succeeds, only on manual abort. */
3422 operation->num_ops = 0;
3423
Paul Elliottc9774412023-02-06 15:14:07 +00003424 /* Likewise, failure state. */
3425 operation->error_occurred = 0;
3426
Paul Elliott59ad9452022-12-18 15:09:02 +00003427 return status;
3428}
3429
3430static psa_status_t psa_verify_hash_abort_internal(
3431 psa_verify_hash_interruptible_operation_t *operation)
3432{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003433 if (operation->id == 0) {
3434 /* The object has (apparently) been initialized but it is not (yet)
3435 * in use. It's ok to call abort on such an object, and there's
3436 * nothing to do. */
3437 return PSA_SUCCESS;
3438 }
3439
Paul Elliott59ad9452022-12-18 15:09:02 +00003440 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3441
3442 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003443
3444 operation->id = 0;
3445
Paul Elliotte6145dc2023-02-07 12:51:21 +00003446 /* Do not clear either the error_occurred or num_ops elements here as they
3447 * only want to be cleared by the application calling abort, not by abort
3448 * being called at completion of an operation. */
3449
Paul Elliott59ad9452022-12-18 15:09:02 +00003450 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003451}
3452
3453psa_status_t psa_verify_hash_start(
3454 psa_verify_hash_interruptible_operation_t *operation,
3455 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3456 const uint8_t *hash, size_t hash_length,
3457 const uint8_t *signature, size_t signature_length)
3458{
3459 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3460 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3461 psa_key_slot_t *slot;
3462
Paul Elliottc9774412023-02-06 15:14:07 +00003463 /* Check that start has not been previously called, or operation has not
3464 * previously errored. */
3465 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003466 return PSA_ERROR_BAD_STATE;
3467 }
3468
3469 status = psa_sign_verify_check_alg(0, alg);
3470 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003471 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003472 return status;
3473 }
3474
3475 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3476 PSA_KEY_USAGE_VERIFY_HASH,
3477 alg);
3478
3479 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003480 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003481 return status;
3482 }
3483
3484 psa_key_attributes_t attributes = {
3485 .core = slot->attr
3486 };
3487
Paul Elliott296ede92022-12-15 17:00:30 +00003488 /* Ensure ops count gets reset, in case of operation re-use. */
3489 operation->num_ops = 0;
3490
Paul Elliott9fe12f62022-11-30 19:16:02 +00003491 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3492 slot->key.data,
3493 slot->key.bytes,
3494 alg, hash, hash_length,
3495 signature, signature_length);
3496
3497 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003498 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003499 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003500 }
3501
3502 unlock_status = psa_unlock_key_slot(slot);
3503
Paul Elliottc9774412023-02-06 15:14:07 +00003504 if (unlock_status != PSA_SUCCESS) {
3505 operation->error_occurred = 1;
3506 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003507
Paul Elliottc9774412023-02-06 15:14:07 +00003508 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003509}
3510
3511psa_status_t psa_verify_hash_complete(
3512 psa_verify_hash_interruptible_operation_t *operation)
3513{
3514 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3515
Paul Elliottc9774412023-02-06 15:14:07 +00003516 /* Check that start has been called first, and that operation has not
3517 * previously errored. */
3518 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003519 status = PSA_ERROR_BAD_STATE;
3520 goto exit;
3521 }
3522
3523 status = psa_driver_wrapper_verify_hash_complete(operation);
3524
Paul Elliott296ede92022-12-15 17:00:30 +00003525 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003526 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003527 operation);
3528
Paul Elliottebe225c2023-02-10 14:32:53 +00003529exit:
3530
Paul Elliott9fe12f62022-11-30 19:16:02 +00003531 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003532 if (status != PSA_SUCCESS) {
3533 operation->error_occurred = 1;
3534 }
3535
Paul Elliott59ad9452022-12-18 15:09:02 +00003536 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003537 }
3538
3539 return status;
3540}
3541
3542psa_status_t psa_verify_hash_abort(
3543 psa_verify_hash_interruptible_operation_t *operation)
3544{
Paul Elliott59ad9452022-12-18 15:09:02 +00003545 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003546
Paul Elliott59ad9452022-12-18 15:09:02 +00003547 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003548
Paul Elliott59ad9452022-12-18 15:09:02 +00003549 /* We clear the number of ops done here, so that it is not cleared when
3550 * the operation fails or succeeds, only on manual abort. */
3551 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003552
Paul Elliottc9774412023-02-06 15:14:07 +00003553 /* Likewise, failure state. */
3554 operation->error_occurred = 0;
3555
Paul Elliott59ad9452022-12-18 15:09:02 +00003556 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003557}
3558
Paul Elliott588f8ed2022-12-02 18:10:26 +00003559/****************************************************************/
3560/* Asymmetric interruptible cryptography internal */
3561/* implementations */
3562/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003563
Paul Elliott588f8ed2022-12-02 18:10:26 +00003564void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3565{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003566
Paul Elliott588f8ed2022-12-02 18:10:26 +00003567#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3568 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3569 defined(MBEDTLS_ECP_RESTARTABLE)
3570
3571 /* Internal implementation uses zero to indicate infinite number max ops,
3572 * therefore avoid this value, and set to minimum possible. */
3573 if (max_ops == 0) {
3574 max_ops = 1;
3575 }
3576
Paul Elliott588f8ed2022-12-02 18:10:26 +00003577 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003578#else
3579 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003580#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3581 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3582 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3583}
3584
Paul Elliott588f8ed2022-12-02 18:10:26 +00003585uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003586 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587{
3588#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3589 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3590 defined(MBEDTLS_ECP_RESTARTABLE)
3591
Paul Elliott93d9ca82023-02-15 18:14:21 +00003592 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003593#else
3594 (void) operation;
3595 return 0;
3596#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3597 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3598 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3599}
3600
3601uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003602 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003603{
3604 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3605 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3606 defined(MBEDTLS_ECP_RESTARTABLE)
3607
Paul Elliott93d9ca82023-02-15 18:14:21 +00003608 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609#else
3610 (void) operation;
3611 return 0;
3612#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3613 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3614 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3615}
3616
3617psa_status_t mbedtls_psa_sign_hash_start(
3618 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3619 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3620 size_t key_buffer_size, psa_algorithm_t alg,
3621 const uint8_t *hash, size_t hash_length)
3622{
3623 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003624 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003625
Paul Elliott068fe072023-01-16 13:59:15 +00003626 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3627 return PSA_ERROR_NOT_SUPPORTED;
3628 }
3629
3630 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003631 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003632 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003633
3634#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003635 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3636 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003637
Paul Elliotta1c94092023-02-15 16:38:04 +00003638 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3639
Paul Elliott93d9ca82023-02-15 18:14:21 +00003640 /* Ensure num_ops is zero'ed in case of context re-use. */
3641 operation->num_ops = 0;
3642
Paul Elliott068fe072023-01-16 13:59:15 +00003643 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3644 attributes->core.bits,
3645 key_buffer,
3646 key_buffer_size,
3647 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003648
Paul Elliott068fe072023-01-16 13:59:15 +00003649 if (status != PSA_SUCCESS) {
3650 return status;
3651 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003652
Paul Elliott1bc59df2023-02-05 13:41:57 +00003653 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003654 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003655
Paul Elliott068fe072023-01-16 13:59:15 +00003656 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003657 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003658 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003659
Paul Elliott0290a762023-02-09 14:30:24 +00003660 /* We only need to store the same length of hash as the private key size
3661 * here, it would be truncated by the internal implementation anyway. */
3662 required_hash_length = (hash_length < operation->coordinate_bytes ?
3663 hash_length : operation->coordinate_bytes);
3664
Paul Elliottba70ad42023-02-15 18:23:53 +00003665 if (required_hash_length > sizeof(operation->hash)) {
3666 /* Shouldn't happen, but better safe than sorry. */
3667 return PSA_ERROR_CORRUPTION_DETECTED;
3668 }
3669
Paul Elliott0290a762023-02-09 14:30:24 +00003670 memcpy(operation->hash, hash, required_hash_length);
3671 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003672
3673 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674
3675#else
Paul Elliott068fe072023-01-16 13:59:15 +00003676 (void) operation;
3677 (void) key_buffer;
3678 (void) key_buffer_size;
3679 (void) alg;
3680 (void) hash;
3681 (void) hash_length;
3682 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003683 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003684
Paul Elliott068fe072023-01-16 13:59:15 +00003685 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3687 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3688 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689}
3690
3691psa_status_t mbedtls_psa_sign_hash_complete(
3692 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3693 uint8_t *signature, size_t signature_size,
3694 size_t *signature_length)
3695{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003696#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3697 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3698 defined(MBEDTLS_ECP_RESTARTABLE)
3699
Paul Elliotta1c94092023-02-15 16:38:04 +00003700 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003701 mbedtls_mpi r;
3702 mbedtls_mpi s;
3703
Paul Elliott46845252023-02-03 14:59:11 +00003704 mbedtls_mpi_init(&r);
3705 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003706
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003707 /* Ensure max_ops is set to the current value (or default). */
3708 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3709
Paul Elliotta1c94092023-02-15 16:38:04 +00003710 if (signature_size < 2 * operation->coordinate_bytes) {
3711 status = PSA_ERROR_BUFFER_TOO_SMALL;
3712 goto exit;
3713 }
3714
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003716
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3718 status = mbedtls_to_psa_error(
3719 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003720 &r,
3721 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722 &operation->ctx->d,
3723 operation->hash,
3724 operation->hash_length,
3725 operation->md_alg,
3726 mbedtls_psa_get_random,
3727 MBEDTLS_PSA_RANDOM_STATE,
3728 &operation->restart_ctx));
3729#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003730 status = PSA_ERROR_NOT_SUPPORTED;
3731 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003732#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3733 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734 status = mbedtls_to_psa_error(
3735 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003736 &r,
3737 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738 &operation->ctx->d,
3739 operation->hash,
3740 operation->hash_length,
3741 mbedtls_psa_get_random,
3742 MBEDTLS_PSA_RANDOM_STATE,
3743 mbedtls_psa_get_random,
3744 MBEDTLS_PSA_RANDOM_STATE,
3745 &operation->restart_ctx));
3746 }
3747
Paul Elliottf8e5b562023-02-19 18:43:45 +00003748 /* Hide the fact that the restart context only holds a delta of number of
3749 * ops done during the last operation, not an absolute value. */
3750 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3751
Paul Elliott724bd252023-02-08 12:35:08 +00003752 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003753 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003754 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003755 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003756 operation->coordinate_bytes)
3757 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003758
3759 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003760 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003761 }
3762
3763 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003764 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003766 operation->coordinate_bytes,
3767 operation->coordinate_bytes)
3768 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003769
3770 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003771 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003772 }
3773
Paul Elliott1bc59df2023-02-05 13:41:57 +00003774 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003775
Paul Elliott724bd252023-02-08 12:35:08 +00003776 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003777 }
Paul Elliott724bd252023-02-08 12:35:08 +00003778
3779exit:
3780
3781 mbedtls_mpi_free(&r);
3782 mbedtls_mpi_free(&s);
3783 return status;
3784
Paul Elliott588f8ed2022-12-02 18:10:26 +00003785 #else
3786
3787 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788 (void) signature;
3789 (void) signature_size;
3790 (void) signature_length;
3791
3792 return PSA_ERROR_NOT_SUPPORTED;
3793
3794#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3795 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3796 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3797}
3798
3799psa_status_t mbedtls_psa_sign_hash_abort(
3800 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3801{
3802
3803#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3804 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3805 defined(MBEDTLS_ECP_RESTARTABLE)
3806
3807 if (operation->ctx) {
3808 mbedtls_ecdsa_free(operation->ctx);
3809 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003810 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003811 }
3812
3813 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3814
Paul Elliott93d9ca82023-02-15 18:14:21 +00003815 operation->num_ops = 0;
3816
Paul Elliott588f8ed2022-12-02 18:10:26 +00003817 return PSA_SUCCESS;
3818
3819#else
3820
3821 (void) operation;
3822
3823 return PSA_ERROR_NOT_SUPPORTED;
3824
3825#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3826 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3827 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3828}
3829
3830psa_status_t mbedtls_psa_verify_hash_start(
3831 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3832 const psa_key_attributes_t *attributes,
3833 const uint8_t *key_buffer, size_t key_buffer_size,
3834 psa_algorithm_t alg,
3835 const uint8_t *hash, size_t hash_length,
3836 const uint8_t *signature, size_t signature_length)
3837{
3838 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003839 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003840 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841
Paul Elliott068fe072023-01-16 13:59:15 +00003842 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3843 return PSA_ERROR_NOT_SUPPORTED;
3844 }
3845
3846 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003847 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003848 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849
3850#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003851 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3852 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853
Paul Elliotta1c94092023-02-15 16:38:04 +00003854 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3855 mbedtls_mpi_init(&operation->r);
3856 mbedtls_mpi_init(&operation->s);
3857
Paul Elliott93d9ca82023-02-15 18:14:21 +00003858 /* Ensure num_ops is zero'ed in case of context re-use. */
3859 operation->num_ops = 0;
3860
Paul Elliott068fe072023-01-16 13:59:15 +00003861 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3862 attributes->core.bits,
3863 key_buffer,
3864 key_buffer_size,
3865 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003866
Paul Elliott068fe072023-01-16 13:59:15 +00003867 if (status != PSA_SUCCESS) {
3868 return status;
3869 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003870
Paul Elliottc569fc22023-02-10 13:02:54 +00003871 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003872
Paul Elliott1bc59df2023-02-05 13:41:57 +00003873 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003874 return PSA_ERROR_INVALID_SIGNATURE;
3875 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003876
Paul Elliott068fe072023-01-16 13:59:15 +00003877 status = mbedtls_to_psa_error(
3878 mbedtls_mpi_read_binary(&operation->r,
3879 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003880 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003881
Paul Elliott068fe072023-01-16 13:59:15 +00003882 if (status != PSA_SUCCESS) {
3883 return status;
3884 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885
Paul Elliott068fe072023-01-16 13:59:15 +00003886 status = mbedtls_to_psa_error(
3887 mbedtls_mpi_read_binary(&operation->s,
3888 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003889 coordinate_bytes,
3890 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003891
Paul Elliott068fe072023-01-16 13:59:15 +00003892 if (status != PSA_SUCCESS) {
3893 return status;
3894 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003895
Paul Elliott2c9843f2023-02-15 17:32:42 +00003896 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003897
Paul Elliott2c9843f2023-02-15 17:32:42 +00003898 if (status != PSA_SUCCESS) {
3899 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003900 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003901
Paul Elliott0290a762023-02-09 14:30:24 +00003902 /* We only need to store the same length of hash as the private key size
3903 * here, it would be truncated by the internal implementation anyway. */
3904 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3905 coordinate_bytes);
3906
Paul Elliottba70ad42023-02-15 18:23:53 +00003907 if (required_hash_length > sizeof(operation->hash)) {
3908 /* Shouldn't happen, but better safe than sorry. */
3909 return PSA_ERROR_CORRUPTION_DETECTED;
3910 }
3911
Paul Elliott0290a762023-02-09 14:30:24 +00003912 memcpy(operation->hash, hash, required_hash_length);
3913 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003914
3915 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003916#else
Paul Elliott068fe072023-01-16 13:59:15 +00003917 (void) operation;
3918 (void) key_buffer;
3919 (void) key_buffer_size;
3920 (void) alg;
3921 (void) hash;
3922 (void) hash_length;
3923 (void) signature;
3924 (void) signature_length;
3925 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003926 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003927 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003928
Paul Elliott068fe072023-01-16 13:59:15 +00003929 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003930#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3931 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3932 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003933}
3934
3935psa_status_t mbedtls_psa_verify_hash_complete(
3936 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3937{
3938
3939#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3940 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3941 defined(MBEDTLS_ECP_RESTARTABLE)
3942
Paul Elliottf8e5b562023-02-19 18:43:45 +00003943 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3944
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003945 /* Ensure max_ops is set to the current value (or default). */
3946 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3947
Paul Elliottf8e5b562023-02-19 18:43:45 +00003948 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003949 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3950 operation->hash,
3951 operation->hash_length,
3952 &operation->ctx->Q,
3953 &operation->r,
3954 &operation->s,
3955 &operation->restart_ctx));
3956
Paul Elliottf8e5b562023-02-19 18:43:45 +00003957 /* Hide the fact that the restart context only holds a delta of number of
3958 * ops done during the last operation, not an absolute value. */
3959 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3960
3961 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003962#else
3963 (void) operation;
3964
3965 return PSA_ERROR_NOT_SUPPORTED;
3966
3967#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3968 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3969 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3970}
3971
3972psa_status_t mbedtls_psa_verify_hash_abort(
3973 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3974{
3975
3976#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3977 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3978 defined(MBEDTLS_ECP_RESTARTABLE)
3979
3980 if (operation->ctx) {
3981 mbedtls_ecdsa_free(operation->ctx);
3982 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003983 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003984 }
3985
3986 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3987
Paul Elliott93d9ca82023-02-15 18:14:21 +00003988 operation->num_ops = 0;
3989
Paul Elliott588f8ed2022-12-02 18:10:26 +00003990 mbedtls_mpi_free(&operation->r);
3991 mbedtls_mpi_free(&operation->s);
3992
3993 return PSA_SUCCESS;
3994
3995#else
3996 (void) operation;
3997
3998 return PSA_ERROR_NOT_SUPPORTED;
3999
4000#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4001 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4002 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4003}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004004
mohammad1603503973b2018-03-12 15:59:30 +02004005/****************************************************************/
4006/* Symmetric cryptography */
4007/****************************************************************/
4008
Gilles Peskine449bd832023-01-11 14:50:10 +01004009static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4010 mbedtls_svc_key_id_t key,
4011 psa_algorithm_t alg,
4012 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004013{
4014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4015 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004016 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4018 PSA_KEY_USAGE_ENCRYPT :
4019 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004020
4021 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004023 status = PSA_ERROR_BAD_STATE;
4024 goto exit;
4025 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004028 status = PSA_ERROR_INVALID_ARGUMENT;
4029 goto exit;
4030 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4033 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004034 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004036
Ronald Cron49fafa92021-03-10 08:34:23 +01004037 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004038 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004039 * so we only set it (in the driver wrapper) after resources have been
4040 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004041 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004043 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004045 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 }
4047 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004048
Ronald Crona4af55f2020-12-14 14:36:06 +01004049 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004051 };
4052
Ronald Cronab99ac22020-10-01 16:38:28 +02004053 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 if (cipher_operation == MBEDTLS_ENCRYPT) {
4055 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4056 &attributes,
4057 slot->key.data,
4058 slot->key.bytes,
4059 alg);
4060 } else {
4061 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4062 &attributes,
4063 slot->key.data,
4064 slot->key.bytes,
4065 alg);
4066 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004067
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004068exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (status != PSA_SUCCESS) {
4070 psa_cipher_abort(operation);
4071 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004072
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004074
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004076}
4077
Gilles Peskine449bd832023-01-11 14:50:10 +01004078psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4079 mbedtls_svc_key_id_t key,
4080 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004081{
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004083}
4084
Gilles Peskine449bd832023-01-11 14:50:10 +01004085psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4086 mbedtls_svc_key_id_t key,
4087 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004088{
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004090}
4091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4093 uint8_t *iv,
4094 size_t iv_size,
4095 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004096{
Ronald Cron6d051732020-10-01 14:10:20 +02004097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004098 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4099 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004102 status = PSA_ERROR_BAD_STATE;
4103 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004104 }
4105
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004107 status = PSA_ERROR_BAD_STATE;
4108 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004109 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004110
Ronald Cron2fb90522021-07-05 12:31:44 +02004111 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004113 status = PSA_ERROR_BUFFER_TOO_SMALL;
4114 goto exit;
4115 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004116
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004118 status = PSA_ERROR_GENERIC_ERROR;
4119 goto exit;
4120 }
4121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 status = psa_generate_random(local_iv, default_iv_length);
4123 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 }
Ronald Cron5618a392021-03-26 09:52:26 +01004126
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 status = psa_driver_wrapper_cipher_set_iv(operation,
4128 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004129
4130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004131 if (status == PSA_SUCCESS) {
4132 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004133 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004134 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004136 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004138 }
Ronald Cron6d051732020-10-01 14:10:20 +02004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004141}
4142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4144 const uint8_t *iv,
4145 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004146{
Ronald Cron6d051732020-10-01 14:10:20 +02004147 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004148
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004150 status = PSA_ERROR_BAD_STATE;
4151 goto exit;
4152 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004155 status = PSA_ERROR_BAD_STATE;
4156 goto exit;
4157 }
Ronald Crona0d68172021-03-26 10:15:08 +01004158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004160 status = PSA_ERROR_INVALID_ARGUMENT;
4161 goto exit;
4162 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 status = psa_driver_wrapper_cipher_set_iv(operation,
4165 iv,
4166 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004167
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004170 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 } else {
4172 psa_cipher_abort(operation);
4173 }
4174 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004175}
4176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4178 const uint8_t *input,
4179 size_t input_length,
4180 uint8_t *output,
4181 size_t output_size,
4182 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004183{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004187 status = PSA_ERROR_BAD_STATE;
4188 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004189 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004192 status = PSA_ERROR_BAD_STATE;
4193 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004194 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 status = psa_driver_wrapper_cipher_update(operation,
4197 input,
4198 input_length,
4199 output,
4200 output_size,
4201 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004202
4203exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 if (status != PSA_SUCCESS) {
4205 psa_cipher_abort(operation);
4206 }
Ronald Cron6d051732020-10-01 14:10:20 +02004207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004209}
4210
Gilles Peskine449bd832023-01-11 14:50:10 +01004211psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4212 uint8_t *output,
4213 size_t output_size,
4214 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004215{
David Saadab4ecc272019-02-14 13:48:10 +02004216 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004219 status = PSA_ERROR_BAD_STATE;
4220 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004221 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004224 status = PSA_ERROR_BAD_STATE;
4225 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004226 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 status = psa_driver_wrapper_cipher_finish(operation,
4229 output,
4230 output_size,
4231 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004232
4233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 if (status == PSA_SUCCESS) {
4235 return psa_cipher_abort(operation);
4236 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004237 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004241 }
mohammad1603503973b2018-03-12 15:59:30 +02004242}
4243
Gilles Peskine449bd832023-01-11 14:50:10 +01004244psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004245{
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004247 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004248 * in use. It's ok to call abort on such an object, and there's
4249 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004251 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004252
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004254
Ronald Cron49fafa92021-03-10 08:34:23 +01004255 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004256 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004257 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004258
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004260}
4261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4263 psa_algorithm_t alg,
4264 const uint8_t *input,
4265 size_t input_length,
4266 uint8_t *output,
4267 size_t output_size,
4268 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004269{
4270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004271 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004272 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004273 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4274 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004277 status = PSA_ERROR_INVALID_ARGUMENT;
4278 goto exit;
4279 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004280
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4282 PSA_KEY_USAGE_ENCRYPT,
4283 alg);
4284 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004285 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004287
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004288 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004290 };
4291
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4293 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004294 status = PSA_ERROR_GENERIC_ERROR;
4295 goto exit;
4296 }
4297
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 if (default_iv_length > 0) {
4299 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004300 status = PSA_ERROR_BUFFER_TOO_SMALL;
4301 goto exit;
4302 }
4303
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 status = psa_generate_random(local_iv, default_iv_length);
4305 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004306 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004308 }
4309
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004310 status = psa_driver_wrapper_cipher_encrypt(
4311 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004312 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004313 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004315
4316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 unlock_status = psa_unlock_key_slot(slot);
4318 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004319 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004320 }
Ronald Cron23919522021-07-08 19:00:07 +02004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 if (status == PSA_SUCCESS) {
4323 if (default_iv_length > 0) {
4324 memcpy(output, local_iv, default_iv_length);
4325 }
4326 *output_length += default_iv_length;
4327 } else {
4328 *output_length = 0;
4329 }
4330
4331 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004332}
4333
Gilles Peskine449bd832023-01-11 14:50:10 +01004334psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4335 psa_algorithm_t alg,
4336 const uint8_t *input,
4337 size_t input_length,
4338 uint8_t *output,
4339 size_t output_size,
4340 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004341{
4342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004343 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004344 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004345
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004347 status = PSA_ERROR_INVALID_ARGUMENT;
4348 goto exit;
4349 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4352 PSA_KEY_USAGE_DECRYPT,
4353 alg);
4354 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004355 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004357
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004358 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004360 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004361
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4363 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004364 status = PSA_ERROR_INVALID_ARGUMENT;
4365 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004367 status = PSA_ERROR_INVALID_ARGUMENT;
4368 goto exit;
4369 }
4370
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004371 status = psa_driver_wrapper_cipher_decrypt(
4372 &attributes, slot->key.data, slot->key.bytes,
4373 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004375
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004376exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 unlock_status = psa_unlock_key_slot(slot);
4378 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004379 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004381
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004383 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 }
Ronald Cron23919522021-07-08 19:00:07 +02004385
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004387}
4388
4389
mohammad16035955c982018-04-26 00:53:03 +03004390/****************************************************************/
4391/* AEAD */
4392/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004393
Paul Elliottbaff51c2021-09-28 17:44:45 +01004394/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004395static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004396{
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004398}
4399
4400/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004401static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4402 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004403{
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004407#if defined(PSA_WANT_ALG_GCM)
4408 case PSA_ALG_GCM:
4409 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 * arbitrarily large nonces. Please note that we do not generally
4411 * recommend the usage of nonces of greater length than
4412 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4413 * size, which can then lead to collisions if you encrypt a very
4414 * large number of messages.*/
4415 if (nonce_length != 0) {
4416 return PSA_SUCCESS;
4417 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004418 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004419#endif /* PSA_WANT_ALG_GCM */
4420#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004421 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 if (nonce_length >= 7 && nonce_length <= 13) {
4423 return PSA_SUCCESS;
4424 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004425 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004426#endif /* PSA_WANT_ALG_CCM */
4427#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004428 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 if (nonce_length == 12) {
4430 return PSA_SUCCESS;
4431 } else if (nonce_length == 8) {
4432 return PSA_ERROR_NOT_SUPPORTED;
4433 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004434 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004435#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004436 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004437 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004439 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004442}
4443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004445{
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4447 return PSA_ERROR_INVALID_ARGUMENT;
4448 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004451}
4452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4454 psa_algorithm_t alg,
4455 const uint8_t *nonce,
4456 size_t nonce_length,
4457 const uint8_t *additional_data,
4458 size_t additional_data_length,
4459 const uint8_t *plaintext,
4460 size_t plaintext_length,
4461 uint8_t *ciphertext,
4462 size_t ciphertext_size,
4463 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004464{
Ronald Cron215633c2021-03-16 17:15:37 +01004465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4466 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004467
mohammad1603f08a5502018-06-03 15:05:47 +03004468 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 status = psa_aead_check_algorithm(alg);
4471 if (status != PSA_SUCCESS) {
4472 return status;
4473 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004474
Ronald Cron9a986162021-03-26 12:40:07 +01004475 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4477 if (status != PSA_SUCCESS) {
4478 return status;
4479 }
mohammad16035955c982018-04-26 00:53:03 +03004480
Ronald Cron215633c2021-03-16 17:15:37 +01004481 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004483 };
mohammad16035955c982018-04-26 00:53:03 +03004484
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 status = psa_aead_check_nonce_length(alg, nonce_length);
4486 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004487 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004489
Ronald Cronde822812021-03-17 16:08:20 +01004490 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004491 &attributes, slot->key.data, slot->key.bytes,
4492 alg,
4493 nonce, nonce_length,
4494 additional_data, additional_data_length,
4495 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004497
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4499 memset(ciphertext, 0, ciphertext_size);
4500 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004501
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004502exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004504
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004506}
4507
Gilles Peskine449bd832023-01-11 14:50:10 +01004508psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4509 psa_algorithm_t alg,
4510 const uint8_t *nonce,
4511 size_t nonce_length,
4512 const uint8_t *additional_data,
4513 size_t additional_data_length,
4514 const uint8_t *ciphertext,
4515 size_t ciphertext_length,
4516 uint8_t *plaintext,
4517 size_t plaintext_size,
4518 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004519{
Ronald Cron215633c2021-03-16 17:15:37 +01004520 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4521 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004522
Gilles Peskineee652a32018-06-01 19:23:52 +02004523 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004524
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 status = psa_aead_check_algorithm(alg);
4526 if (status != PSA_SUCCESS) {
4527 return status;
4528 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004529
Ronald Cron9a986162021-03-26 12:40:07 +01004530 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4532 if (status != PSA_SUCCESS) {
4533 return status;
4534 }
mohammad16035955c982018-04-26 00:53:03 +03004535
Ronald Cron215633c2021-03-16 17:15:37 +01004536 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004538 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 status = psa_aead_check_nonce_length(alg, nonce_length);
4541 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004542 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004544
Ronald Cronde822812021-03-17 16:08:20 +01004545 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004546 &attributes, slot->key.data, slot->key.bytes,
4547 alg,
4548 nonce, nonce_length,
4549 additional_data, additional_data_length,
4550 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 if (status != PSA_SUCCESS && plaintext_size != 0) {
4554 memset(plaintext, 0, plaintext_size);
4555 }
mohammad160360a64d02018-06-03 17:20:42 +03004556
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004557exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004559
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 return status;
mohammad16035955c982018-04-26 00:53:03 +03004561}
4562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4564{
4565 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004568#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004570 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4572 return PSA_ERROR_INVALID_ARGUMENT;
4573 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004574 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004575#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004576
Przemek Stekiel86679c72022-10-06 17:06:56 +02004577#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004579 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4581 return PSA_ERROR_INVALID_ARGUMENT;
4582 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004583 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004584#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004585
Przemek Stekiel86679c72022-10-06 17:06:56 +02004586#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004588 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if (tag_len != 16) {
4590 return PSA_ERROR_INVALID_ARGUMENT;
4591 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004592 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004593#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004594
4595 default:
4596 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004598 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004600}
4601
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004602/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004603static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4604 int is_encrypt,
4605 mbedtls_svc_key_id_t key,
4606 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004607{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4609 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4610 psa_key_slot_t *slot = NULL;
4611 psa_key_usage_t key_usage = 0;
4612
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 status = psa_aead_check_algorithm(alg);
4614 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004615 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004617
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004619 status = PSA_ERROR_BAD_STATE;
4620 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004621 }
4622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 if (operation->nonce_set || operation->lengths_set ||
4624 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004625 status = PSA_ERROR_BAD_STATE;
4626 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004627 }
4628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004630 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004632 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4636 alg);
4637 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004640
4641 psa_key_attributes_t attributes = {
4642 .core = slot->attr
4643 };
4644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004646 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004648
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 if (is_encrypt) {
4650 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4651 &attributes,
4652 slot->key.data,
4653 slot->key.bytes,
4654 alg);
4655 } else {
4656 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4657 &attributes,
4658 slot->key.data,
4659 slot->key.bytes,
4660 alg);
4661 }
4662 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004663 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004667
4668exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004670
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004672 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004674 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 } else {
4676 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004677 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004680}
4681
Paul Elliott302ff6b2021-04-20 18:10:30 +01004682/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004683psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4684 mbedtls_svc_key_id_t key,
4685 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004686{
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004688}
4689
4690/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004691psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4692 mbedtls_svc_key_id_t key,
4693 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004694{
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004696}
4697
4698/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004699psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4700 uint8_t *nonce,
4701 size_t nonce_size,
4702 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004703{
4704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004705 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004706 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004707
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004708 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004711 status = PSA_ERROR_BAD_STATE;
4712 goto exit;
4713 }
4714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004716 status = PSA_ERROR_BAD_STATE;
4717 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004718 }
4719
Paul Elliotte193ea82021-10-01 13:00:16 +01004720 /* For CCM, this size may not be correct according to the PSA
4721 * specification. The PSA Crypto 1.0.1 specification states:
4722 *
4723 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4724 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4725 *
4726 * However this restriction that L has to be the smallest integer is not
4727 * applied in practice, and it is not implementable here since the
4728 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4730 operation->alg);
4731 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004732 status = PSA_ERROR_BUFFER_TOO_SMALL;
4733 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004734 }
4735
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 status = psa_generate_random(local_nonce, required_nonce_size);
4737 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004738 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004742
Paul Elliott39dc6b82021-05-11 19:16:09 +01004743exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if (status == PSA_SUCCESS) {
4745 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004746 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 } else {
4748 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004749 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004752}
4753
4754/* Set the nonce for a multipart authenticated encryption or decryption
4755 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004756psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4757 const uint8_t *nonce,
4758 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004759{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004760 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004763 status = PSA_ERROR_BAD_STATE;
4764 goto exit;
4765 }
4766
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004768 status = PSA_ERROR_BAD_STATE;
4769 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004770 }
4771
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4773 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004774 status = PSA_ERROR_INVALID_ARGUMENT;
4775 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004776 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004777
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4779 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004780
Paul Elliott39dc6b82021-05-11 19:16:09 +01004781exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004783 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 } else {
4785 psa_aead_abort(operation);
4786 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004787
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004789}
4790
4791/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004792psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4793 size_t ad_length,
4794 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004795{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004796 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004799 status = PSA_ERROR_BAD_STATE;
4800 goto exit;
4801 }
4802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 if (operation->lengths_set || operation->ad_started ||
4804 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004805 status = PSA_ERROR_BAD_STATE;
4806 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004807 }
4808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004810#if defined(PSA_WANT_ALG_GCM)
4811 case PSA_ALG_GCM:
4812 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 * bits. Without the guard this code will generate warnings on 32bit
4814 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004815#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 if (((uint64_t) ad_length) >> 61 != 0 ||
4817 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004818 status = PSA_ERROR_INVALID_ARGUMENT;
4819 goto exit;
4820 }
Paul Elliott325d3742021-09-27 17:56:28 +01004821#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004822 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004823#endif /* PSA_WANT_ALG_GCM */
4824#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004825 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004827 status = PSA_ERROR_INVALID_ARGUMENT;
4828 goto exit;
4829 }
4830 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004831#endif /* PSA_WANT_ALG_CCM */
4832#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004833 case PSA_ALG_CHACHA20_POLY1305:
4834 /* No length restrictions for ChaChaPoly. */
4835 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004836#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004837 default:
4838 break;
4839 }
Paul Elliott325d3742021-09-27 17:56:28 +01004840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4842 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004843
Paul Elliott39dc6b82021-05-11 19:16:09 +01004844exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004846 operation->ad_remaining = ad_length;
4847 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004848 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 } else {
4850 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004851 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004854}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004855
4856/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004857psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4858 const uint8_t *input,
4859 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004860{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004861 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004864 status = PSA_ERROR_BAD_STATE;
4865 goto exit;
4866 }
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004869 status = PSA_ERROR_BAD_STATE;
4870 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004871 }
4872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 if (operation->lengths_set) {
4874 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004875 status = PSA_ERROR_INVALID_ARGUMENT;
4876 goto exit;
4877 }
4878
4879 operation->ad_remaining -= input_length;
4880 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004881#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004883 status = PSA_ERROR_BAD_STATE;
4884 goto exit;
4885 }
4886#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 status = psa_driver_wrapper_aead_update_ad(operation, input,
4889 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004890
Paul Elliott39dc6b82021-05-11 19:16:09 +01004891exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004893 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 } else {
4895 psa_aead_abort(operation);
4896 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004897
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004899}
4900
4901/* Encrypt or decrypt a message fragment in an active multipart AEAD
4902 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004903psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4904 const uint8_t *input,
4905 size_t input_length,
4906 uint8_t *output,
4907 size_t output_size,
4908 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004909{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004910 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004911
4912 *output_length = 0;
4913
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004915 status = PSA_ERROR_BAD_STATE;
4916 goto exit;
4917 }
4918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004920 status = PSA_ERROR_BAD_STATE;
4921 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004922 }
4923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004925 /* Additional data length was supplied, but not all the additional
4926 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004928 status = PSA_ERROR_INVALID_ARGUMENT;
4929 goto exit;
4930 }
4931
4932 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004934 status = PSA_ERROR_INVALID_ARGUMENT;
4935 goto exit;
4936 }
4937
4938 operation->body_remaining -= input_length;
4939 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004940#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004942 status = PSA_ERROR_BAD_STATE;
4943 goto exit;
4944 }
4945#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004946
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4948 output, output_size,
4949 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004950
Paul Elliott39dc6b82021-05-11 19:16:09 +01004951exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004953 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 } else {
4955 psa_aead_abort(operation);
4956 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004957
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004959}
4960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004962{
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 if (operation->id == 0 || !operation->nonce_set) {
4964 return PSA_ERROR_BAD_STATE;
4965 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004966
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4968 operation->body_remaining != 0)) {
4969 return PSA_ERROR_INVALID_ARGUMENT;
4970 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004971
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004973}
4974
Paul Elliott302ff6b2021-04-20 18:10:30 +01004975/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004976psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4977 uint8_t *ciphertext,
4978 size_t ciphertext_size,
4979 size_t *ciphertext_length,
4980 uint8_t *tag,
4981 size_t tag_size,
4982 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004984 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4985
Paul Elliott302ff6b2021-04-20 18:10:30 +01004986 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004987 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004988
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 status = psa_aead_final_checks(operation);
4990 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004991 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004993
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004995 status = PSA_ERROR_BAD_STATE;
4996 goto exit;
4997 }
4998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5000 ciphertext_size,
5001 ciphertext_length,
5002 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005003
Paul Elliott39dc6b82021-05-11 19:16:09 +01005004exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005005
5006
Paul Elliottf47b0952021-05-21 18:02:33 +01005007 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005008 * the zero tag size, make sure the tag is set to something implausible.
5009 * Even if the operation succeeds, make sure we clear the rest of the
5010 * buffer to prevent potential leakage of anything previously placed in
5011 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005012 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005013
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005015
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005017}
5018
5019/* Finish authenticating and decrypting a message in a multipart AEAD
5020 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005021psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5022 uint8_t *plaintext,
5023 size_t plaintext_size,
5024 size_t *plaintext_length,
5025 const uint8_t *tag,
5026 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005027{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005028 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5029
Paul Elliott302ff6b2021-04-20 18:10:30 +01005030 *plaintext_length = 0;
5031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 status = psa_aead_final_checks(operation);
5033 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005034 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005036
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005038 status = PSA_ERROR_BAD_STATE;
5039 goto exit;
5040 }
5041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5043 plaintext_size,
5044 plaintext_length,
5045 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005046
5047exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005049
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005051}
5052
5053/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005054psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005055{
5056 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005059 /* The object has (apparently) been initialized but it is not (yet)
5060 * in use. It's ok to call abort on such an object, and there's
5061 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005063 }
5064
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005066
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005070}
5071
Gilles Peskinee59236f2018-01-27 23:32:46 +01005072/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005073/* Generators */
5074/****************************************************************/
5075
Przemek Stekiel69c46792022-06-10 12:59:51 +02005076#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005077 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005078 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305079 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
5080 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
John Durkop07cc04a2020-11-16 22:08:34 -08005081#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005082#endif /* At least one builtin KDF */
5083
Przemek Stekiel69c46792022-06-10 12:59:51 +02005084#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005085 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5086 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5087static psa_status_t psa_key_derivation_start_hmac(
5088 psa_mac_operation_t *operation,
5089 psa_algorithm_t hash_alg,
5090 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005092{
5093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5096 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5097 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005098
Steven Cooreman72f736a2021-05-07 14:14:37 +02005099 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005101
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 status = psa_driver_wrapper_mac_sign_setup(operation,
5103 &attributes,
5104 hmac_key, hmac_key_length,
5105 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 psa_reset_key_attributes(&attributes);
5108 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005109}
5110#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005111
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112#define HKDF_STATE_INIT 0 /* no input yet */
5113#define HKDF_STATE_STARTED 1 /* got salt */
5114#define HKDF_STATE_KEYED 2 /* got key */
5115#define HKDF_STATE_OUTPUT 3 /* output started */
5116
Gilles Peskinecbe66502019-05-16 16:59:18 +02005117static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005119{
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5121 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5122 } else {
5123 return operation->alg;
5124 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005125}
5126
Gilles Peskine449bd832023-01-11 14:50:10 +01005127psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005128{
5129 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5131 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005132 /* The object has (apparently) been initialized but it is not
5133 * in use. It's ok to call abort on such an object, and there's
5134 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005136#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5138 mbedtls_free(operation->ctx.hkdf.info);
5139 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5140 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005141#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005142#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5143 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5145 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5146 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5147 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005148 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005150 }
5151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005153 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005155 }
5156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005158 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005160 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005161#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005163 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005165 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005166#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005167 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005168
5169 /* We leave the fields Ai and output_block to be erased safely by the
5170 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005172#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5173 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005174#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5176 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5177 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5178 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005179#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305180#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5181 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305182 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005183 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305184 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305185 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305186
5187 status = PSA_SUCCESS;
5188 } else
5189#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005190 {
5191 status = PSA_ERROR_BAD_STATE;
5192 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 mbedtls_platform_zeroize(operation, sizeof(*operation));
5194 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005195}
5196
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005199{
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005201 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005203 }
5204
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005205 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005207}
5208
Gilles Peskine449bd832023-01-11 14:50:10 +01005209psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5210 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005211{
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 if (operation->alg == 0) {
5213 return PSA_ERROR_BAD_STATE;
5214 }
5215 if (capacity > operation->capacity) {
5216 return PSA_ERROR_INVALID_ARGUMENT;
5217 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005220}
5221
Przemek Stekiel69c46792022-06-10 12:59:51 +02005222#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005223/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005224static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5225 psa_algorithm_t kdf_alg,
5226 uint8_t *output,
5227 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005228{
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5230 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005231 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005232 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005233#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005235#else
5236 const uint8_t last_block = 0xff;
5237#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005238
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 if (hkdf->state < HKDF_STATE_KEYED ||
5240 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005241#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005243#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 )) {
5245 return PSA_ERROR_BAD_STATE;
5246 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005247 hkdf->state = HKDF_STATE_OUTPUT;
5248
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005250 /* Copy what remains of the current block */
5251 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005253 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 }
5255 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005256 output += n;
5257 output_length -= n;
5258 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005260 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005262 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005263 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005264 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005265 * object was corrupted or if this function is called directly
5266 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 if (hkdf->block_number == last_block) {
5268 return PSA_ERROR_BAD_STATE;
5269 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005270
5271 /* We need a new block */
5272 ++hkdf->block_number;
5273 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005274
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5276 hash_alg,
5277 hkdf->prk,
5278 hash_length);
5279 if (status != PSA_SUCCESS) {
5280 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005281 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005282
5283 if (hkdf->block_number != 1) {
5284 status = psa_mac_update(&hkdf->hmac,
5285 hkdf->output_block,
5286 hash_length);
5287 if (status != PSA_SUCCESS) {
5288 return status;
5289 }
5290 }
5291 status = psa_mac_update(&hkdf->hmac,
5292 hkdf->info,
5293 hkdf->info_length);
5294 if (status != PSA_SUCCESS) {
5295 return status;
5296 }
5297 status = psa_mac_update(&hkdf->hmac,
5298 &hkdf->block_number, 1);
5299 if (status != PSA_SUCCESS) {
5300 return status;
5301 }
5302 status = psa_mac_sign_finish(&hkdf->hmac,
5303 hkdf->output_block,
5304 sizeof(hkdf->output_block),
5305 &hmac_output_length);
5306 if (status != PSA_SUCCESS) {
5307 return status;
5308 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005309 }
5310
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005312}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005313#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005314
John Durkop07cc04a2020-11-16 22:08:34 -08005315#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5316 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005317static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5318 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005320{
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5322 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005323 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5324 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005325 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005326
Janos Follath7742fee2019-06-17 12:58:10 +01005327 /* We can't be wanting more output after block 0xff, otherwise
5328 * the capacity check in psa_key_derivation_output_bytes() would have
5329 * prevented this call. It could happen only if the operation
5330 * object was corrupted or if this function is called directly
5331 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 if (tls12_prf->block_number == 0xff) {
5333 return PSA_ERROR_CORRUPTION_DETECTED;
5334 }
Janos Follath7742fee2019-06-17 12:58:10 +01005335
5336 /* We need a new block */
5337 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005338 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005339
5340 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5341 *
5342 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5343 *
5344 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5345 * HMAC_hash(secret, A(2) + seed) +
5346 * HMAC_hash(secret, A(3) + seed) + ...
5347 *
5348 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005349 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005350 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005351 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005352 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005353 * is currently extracted as `output_block` and where i is
5354 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005355 */
5356
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 status = psa_key_derivation_start_hmac(&hmac,
5358 hash_alg,
5359 tls12_prf->secret,
5360 tls12_prf->secret_length);
5361 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005362 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005364
5365 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005367 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5368 * the variable seed and in this instance means it in the context of the
5369 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 status = psa_mac_update(&hmac,
5371 tls12_prf->label,
5372 tls12_prf->label_length);
5373 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005374 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 }
5376 status = psa_mac_update(&hmac,
5377 tls12_prf->seed,
5378 tls12_prf->seed_length);
5379 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005380 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 }
5382 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005383 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5385 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005386 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005388 }
5389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 status = psa_mac_sign_finish(&hmac,
5391 tls12_prf->Ai, hash_length,
5392 &hmac_output_length);
5393 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005394 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 }
5396 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005397 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005399
5400 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 status = psa_key_derivation_start_hmac(&hmac,
5402 hash_alg,
5403 tls12_prf->secret,
5404 tls12_prf->secret_length);
5405 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005406 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 }
5408 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5409 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005410 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 }
5412 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5413 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005414 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 }
5416 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_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_sign_finish(&hmac,
5421 tls12_prf->output_block, hash_length,
5422 &hmac_output_length);
5423 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005424 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005426
Janos Follath7742fee2019-06-17 12:58:10 +01005427
5428cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 cleanup_status = psa_mac_abort(&hmac);
5430 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005431 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005435}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005436
Janos Follath844eb0e2019-06-19 12:10:49 +01005437static psa_status_t psa_key_derivation_tls12_prf_read(
5438 psa_tls12_prf_key_derivation_t *tls12_prf,
5439 psa_algorithm_t alg,
5440 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005442{
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5444 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005445 psa_status_t status;
5446 uint8_t offset, length;
5447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005449 case PSA_TLS12_PRF_STATE_LABEL_SET:
5450 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5451 break;
5452 case PSA_TLS12_PRF_STATE_OUTPUT:
5453 break;
5454 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005456 }
5457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005459 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 if (tls12_prf->left_in_block == 0) {
5461 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5462 alg);
5463 if (status != PSA_SUCCESS) {
5464 return status;
5465 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005466
5467 continue;
5468 }
5469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005471 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005473 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005475
5476 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005478 output += length;
5479 output_length -= length;
5480 tls12_prf->left_in_block -= length;
5481 }
5482
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005484}
John Durkop07cc04a2020-11-16 22:08:34 -08005485#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5486 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005487
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005488#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5489static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5490 psa_tls12_ecjpake_to_pms_t *ecjpake,
5491 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005493{
5494 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005495 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005496
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 if (output_length != 32) {
5498 return PSA_ERROR_INVALID_ARGUMENT;
5499 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005500
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5502 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5503 &output_size);
5504 if (status != PSA_SUCCESS) {
5505 return status;
5506 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005507
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 if (output_size != output_length) {
5509 return PSA_ERROR_GENERIC_ERROR;
5510 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005511
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005513}
5514#endif
5515
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305516#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5517static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5518 psa_pbkdf2_key_derivation_t *pbkdf2,
5519 psa_algorithm_t prf_alg,
5520 uint8_t prf_output_length,
5521 psa_key_attributes_t *attributes)
5522{
5523 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305524 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305525 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305526 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305527 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305528 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305529 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305530
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305531 mac_operation.is_sign = 1;
5532 mac_operation.mac_size = prf_output_length;
5533 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305534
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305535 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5536 attributes,
5537 pbkdf2->password,
5538 pbkdf2->password_length,
5539 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305540 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305541 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305542 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305543 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5544 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305545 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305546 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305547 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305548 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305549 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305550 }
5551 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5552 &mac_output_length);
5553 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305554 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305555 }
5556
5557 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305558 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305559 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305560 }
5561
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305562 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305563
5564 for (i = 1; i < pbkdf2->input_cost; i++) {
5565 status = psa_driver_wrapper_mac_compute(attributes,
5566 pbkdf2->password,
5567 pbkdf2->password_length,
5568 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305569 U_i, sizeof(U_i),
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305570 &mac_output_length);
5571 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305572 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305573 }
5574
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305575 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305576 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305577
5578cleanup:
5579 /* Zeroise buffers to clear sensitive data from memory. */
5580 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5581 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305582}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305583
5584static psa_status_t psa_key_derivation_pbkdf2_read(
5585 psa_pbkdf2_key_derivation_t *pbkdf2,
5586 psa_algorithm_t kdf_alg,
5587 uint8_t *output,
5588 size_t output_length)
5589{
5590 psa_status_t status;
5591 psa_algorithm_t prf_alg;
5592 uint8_t prf_output_length;
5593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5594 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5595 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5596
5597 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5598 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5599 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5600 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305601 } else {
5602 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305603 }
5604
5605 switch (pbkdf2->state) {
5606 case PSA_PBKDF2_STATE_PASSWORD_SET:
5607 /* Initially we need a new block so bytes_used is equal to block size*/
5608 pbkdf2->bytes_used = prf_output_length;
5609 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5610 break;
5611 case PSA_PBKDF2_STATE_OUTPUT:
5612 break;
5613 default:
5614 return PSA_ERROR_BAD_STATE;
5615 }
5616
5617 while (output_length != 0) {
5618 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5619 if (n > output_length) {
5620 n = (uint8_t) output_length;
5621 }
5622 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5623 output += n;
5624 output_length -= n;
5625 pbkdf2->bytes_used += n;
5626
5627 if (output_length == 0) {
5628 break;
5629 }
5630
5631 /* We need a new block */
5632 pbkdf2->bytes_used = 0;
5633 pbkdf2->block_number++;
5634
5635 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5636 prf_output_length,
5637 &attributes);
5638 if (status != PSA_SUCCESS) {
5639 return status;
5640 }
5641 }
5642
5643 return PSA_SUCCESS;
5644}
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305645#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
5646
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005647psa_status_t psa_key_derivation_output_bytes(
5648 psa_key_derivation_operation_t *operation,
5649 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005651{
5652 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005656 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005658 }
5659
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005661 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005662 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005663 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005664 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005665 goto exit;
5666 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005668 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005669 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005670 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5671 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005672 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005673 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005675 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005676 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005677
Przemek Stekiel69c46792022-06-10 12:59:51 +02005678#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5680 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5681 output, output_length);
5682 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005683#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005684#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5685 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5687 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5688 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5689 kdf_alg, output,
5690 output_length);
5691 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005692#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5693 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005694#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005696 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5698 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005699#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305700#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5701 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305702 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5703 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305704 } else
5705#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005706
Gilles Peskineeab56e42018-07-12 17:12:33 +02005707 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005708 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005710 }
5711
5712exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005714 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005715 * This allows us to differentiate between exhausted operations and
5716 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5717 * operations. */
5718 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005722 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005724}
5725
David Brown7807bf72021-02-09 16:28:23 -07005726#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005727static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005728{
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 if (data_size >= 8) {
5730 mbedtls_des_key_set_parity(data);
5731 }
5732 if (data_size >= 16) {
5733 mbedtls_des_key_set_parity(data + 8);
5734 }
5735 if (data_size >= 24) {
5736 mbedtls_des_key_set_parity(data + 16);
5737 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005738}
David Brown7807bf72021-02-09 16:28:23 -07005739#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005740
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005741/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 * ECC keys on a Weierstrass elliptic curve require the generation
5743 * of a private key which is an integer
5744 * in the range [1, N - 1], where N is the boundary of the private key domain:
5745 * N is the prime p for Diffie-Hellman, or the order of the
5746 * curve’s base point for ECC.
5747 *
5748 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5749 * This function generates the private key using the following process:
5750 *
5751 * 1. Draw a byte string of length ceiling(m/8) bytes.
5752 * 2. If m is not a multiple of 8, set the most significant
5753 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5754 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5755 * 4. If k > N - 2, discard the result and return to step 1.
5756 * 5. Output k + 1 as the private key.
5757 *
5758 * This method allows compliance to NIST standards, specifically the methods titled
5759 * Key-Pair Generation by Testing Candidates in the following publications:
5760 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5761 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5762 * Diffie-Hellman keys.
5763 *
5764 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5765 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5766 *
5767 * Note: Function allocates memory for *data buffer, so given *data should be
5768 * always NULL.
5769 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005770#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5771#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005772static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005773 psa_key_slot_t *slot,
5774 size_t bits,
5775 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005776 uint8_t **data
5777 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005778{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005779 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005780 mbedtls_mpi k;
5781 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005783 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 mbedtls_mpi_init(&k);
5786 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005787
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005788 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005790 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005792
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005794 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005795 goto cleanup;
5796 }
5797
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005798 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005802
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005803 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005804 /* Let m be the bit size of N. */
5805 size_t m = ecp_group.nbits;
5806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005808
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005809 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005811
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005812 /* Note: This function is always called with *data == NULL and it
5813 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 *data = mbedtls_calloc(1, m_bytes);
5815 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005816 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005817 goto cleanup;
5818 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005821 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005823 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005825
5826 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005828 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005829 * (8 * ceiling(m/8) - m) bits of the first byte in
5830 * the string to zero.
5831 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005833 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005834 }
5835
5836 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 * big-endian byte string.
5838 */
5839 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005840
5841 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 * Result of comparison is returned. When it indicates error
5843 * then this function is called again.
5844 */
5845 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005846 }
5847
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005848 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5850 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005851cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 if (ret != 0) {
5853 status = mbedtls_to_psa_error(ret);
5854 }
5855 if (status != PSA_SUCCESS) {
5856 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005857 *data = NULL;
5858 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 mbedtls_mpi_free(&k);
5860 mbedtls_mpi_free(&diff_N_2);
5861 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005862}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005863
5864/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5865 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5866 *
5867 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5868 * draw a 32-byte string and process it as specified in
5869 * Elliptic Curves for Security [RFC7748] §5.
5870 *
5871 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5872 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5873 *
5874 * Note: Function allocates memory for *data buffer, so given *data should be
5875 * always NULL.
5876 */
5877
5878static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5879 size_t bits,
5880 psa_key_derivation_operation_t *operation,
5881 uint8_t **data
5882 )
5883{
5884 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005885 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005888 case 255:
5889 output_length = 32;
5890 break;
5891 case 448:
5892 output_length = 56;
5893 break;
5894 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005896 break;
5897 }
5898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005900
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 if (*data == NULL) {
5902 return PSA_ERROR_INSUFFICIENT_MEMORY;
5903 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005904
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005908 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005912 case 255:
5913 (*data)[0] &= 248;
5914 (*data)[31] &= 127;
5915 (*data)[31] |= 64;
5916 break;
5917 case 448:
5918 (*data)[0] &= 252;
5919 (*data)[55] |= 128;
5920 break;
5921 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005923 break;
5924 }
5925
5926 return status;
5927}
Valerio Setti86587ab2023-06-27 13:09:30 +02005928#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5929static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5930 psa_key_slot_t *slot, size_t bits,
5931 psa_key_derivation_operation_t *operation, uint8_t **data)
5932{
5933 (void) slot;
5934 (void) bits;
5935 (void) operation;
5936 (void) data;
5937 return PSA_ERROR_NOT_SUPPORTED;
5938}
5939
5940static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5941 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5942{
5943 (void) bits;
5944 (void) operation;
5945 (void) data;
5946 return PSA_ERROR_NOT_SUPPORTED;
5947}
5948#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5949#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005950
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005951static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005952 psa_key_slot_t *slot,
5953 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005955{
5956 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305958 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005960
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5962 return PSA_ERROR_INVALID_ARGUMENT;
5963 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005964
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005965#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005966 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5968 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5969 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005970 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5972 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005973 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 }
5975 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005976 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5978 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005979 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005981 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005982 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005983#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005984 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (key_type_is_raw_bytes(slot->attr.type)) {
5986 if (bits % 8 != 0) {
5987 return PSA_ERROR_INVALID_ARGUMENT;
5988 }
5989 data = mbedtls_calloc(1, bytes);
5990 if (data == NULL) {
5991 return PSA_ERROR_INSUFFICIENT_MEMORY;
5992 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 status = psa_key_derivation_output_bytes(operation, data, bytes);
5995 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005996 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 }
David Brown12ca5032021-02-11 11:02:00 -07005998#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6000 psa_des_set_key_parity(data, bytes);
6001 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006002#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 } else {
6004 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006005 }
Ronald Crondd04d422020-11-28 15:14:42 +01006006
Ronald Cronbf33c932020-11-28 18:06:53 +01006007 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006008 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006010 };
6011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6013 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6014 &storage_size);
6015 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306016 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 }
Archanad8a83dc2021-06-14 10:04:16 +05306018 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 status = psa_allocate_buffer_to_slot(slot, storage_size);
6020 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306021 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 }
Archanad8a83dc2021-06-14 10:04:16 +05306023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 status = psa_driver_wrapper_import_key(&attributes,
6025 data, bytes,
6026 slot->key.data,
6027 slot->key.bytes,
6028 &slot->key.bytes, &bits);
6029 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006030 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006032
6033exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 mbedtls_free(data);
6035 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006036}
6037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6039 psa_key_derivation_operation_t *operation,
6040 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006041{
6042 psa_status_t status;
6043 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006044 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006045
Ronald Cron81709fc2020-11-14 12:10:32 +01006046 *key = MBEDTLS_SVC_KEY_ID_INIT;
6047
Gilles Peskine0f84d622019-09-12 19:03:13 +02006048 /* Reject any attempt to create a zero-length key so that we don't
6049 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 if (psa_get_key_bits(attributes) == 0) {
6051 return PSA_ERROR_INVALID_ARGUMENT;
6052 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006053
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 if (operation->alg == PSA_ALG_NONE) {
6055 return PSA_ERROR_BAD_STATE;
6056 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 if (!operation->can_output_key) {
6059 return PSA_ERROR_NOT_PERMITTED;
6060 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6063 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006064#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006066 /* Deriving a key in a secure element is not implemented yet. */
6067 status = PSA_ERROR_NOT_SUPPORTED;
6068 }
6069#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 if (status == PSA_SUCCESS) {
6071 status = psa_generate_derived_key_internal(slot,
6072 attributes->core.bits,
6073 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006074 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 if (status == PSA_SUCCESS) {
6076 status = psa_finish_key_creation(slot, driver, key);
6077 }
6078 if (status != PSA_SUCCESS) {
6079 psa_fail_key_creation(slot, driver);
6080 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006081
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006083}
6084
Gilles Peskineeab56e42018-07-12 17:12:33 +02006085
6086
6087/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006088/* Key derivation */
6089/****************************************************************/
6090
Steven Cooreman932ffb72021-02-15 12:14:32 +01006091#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006092static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006093{
6094#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6096 return 1;
6097 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006098#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006099#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6101 return 1;
6102 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006103#endif
6104#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6106 return 1;
6107 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006108#endif
6109#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6111 return 1;
6112 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006113#endif
6114#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6116 return 1;
6117 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006118#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006119#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6121 return 1;
6122 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006123#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306124#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6125 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6126 return 1;
6127 }
6128#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006130}
6131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006133{
6134 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 psa_status_t status = psa_hash_setup(&operation, alg);
6136 psa_hash_abort(&operation);
6137 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006138}
6139
Gilles Peskine969c5d62019-01-16 15:53:06 +01006140static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006141 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006143{
Janos Follath5fe19732019-06-20 15:09:30 +01006144 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6145 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006147
Gilles Peskine969c5d62019-01-16 15:53:06 +01006148 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 if (!is_kdf_alg_supported(kdf_alg)) {
6150 return PSA_ERROR_NOT_SUPPORTED;
6151 }
John Durkop07cc04a2020-11-16 22:08:34 -08006152
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006153 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006154 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6156 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6157 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6158 if (hash_size == 0) {
6159 return PSA_ERROR_NOT_SUPPORTED;
6160 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006161
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006162 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6163 * we might fail later, which is somewhat unfriendly and potentially
6164 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 psa_status_t status = psa_hash_try_support(hash_alg);
6166 if (status != PSA_SUCCESS) {
6167 return status;
6168 }
6169 } else {
6170 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006171 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6174 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6175 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6176 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006177 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006178#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006179 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6181 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006182 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006184#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6185 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 operation->capacity = 255 * hash_size;
6187 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006188}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006191{
6192#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 if (alg == PSA_ALG_ECDH) {
6194 return PSA_SUCCESS;
6195 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006196#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006197#if defined(PSA_WANT_ALG_FFDH)
6198 if (alg == PSA_ALG_FFDH) {
6199 return PSA_SUCCESS;
6200 }
6201#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006202 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006204}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006205
6206static int psa_key_derivation_allows_free_form_secret_input(
6207 psa_algorithm_t kdf_alg)
6208{
6209#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6210 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6211 return 0;
6212 }
6213#endif
6214 (void) kdf_alg;
6215 return 1;
6216}
John Durkop07cc04a2020-11-16 22:08:34 -08006217#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6220 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006221{
6222 psa_status_t status;
6223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 if (operation->alg != 0) {
6225 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006226 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6229 return PSA_ERROR_INVALID_ARGUMENT;
6230 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6231#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6232 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6233 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6234 status = psa_key_agreement_try_support(ka_alg);
6235 if (status != PSA_SUCCESS) {
6236 return status;
6237 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006238 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6239 return PSA_ERROR_INVALID_ARGUMENT;
6240 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6242#else
6243 return PSA_ERROR_NOT_SUPPORTED;
6244#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6245 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6246#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6247 status = psa_key_derivation_setup_kdf(operation, alg);
6248#else
6249 return PSA_ERROR_NOT_SUPPORTED;
6250#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6251 } else {
6252 return PSA_ERROR_INVALID_ARGUMENT;
6253 }
6254
6255 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006256 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 }
6258 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006259}
6260
Przemek Stekiel69c46792022-06-10 12:59:51 +02006261#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006262static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6263 psa_algorithm_t kdf_alg,
6264 psa_key_derivation_step_t step,
6265 const uint8_t *data,
6266 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006267{
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006269 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006271 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006272#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6274 return PSA_ERROR_INVALID_ARGUMENT;
6275 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006276#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 if (hkdf->state != HKDF_STATE_INIT) {
6278 return PSA_ERROR_BAD_STATE;
6279 } else {
6280 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6281 hash_alg,
6282 data, data_length);
6283 if (status != PSA_SUCCESS) {
6284 return status;
6285 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006286 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006288 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006289 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006290#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006292 /* We shouldn't be in different state as HKDF_EXPAND only allows
6293 * two inputs: SECRET (this case) and INFO which does not modify
6294 * the state. It could happen only if the hkdf
6295 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 if (hkdf->state != HKDF_STATE_INIT) {
6297 return PSA_ERROR_BAD_STATE;
6298 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006299
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006300 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6302 return PSA_ERROR_INVALID_ARGUMENT;
6303 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 memcpy(hkdf->prk, data, data_length);
6306 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006307#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006308 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006309 /* HKDF: If no salt was provided, use an empty salt.
6310 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006312#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6314 return PSA_ERROR_BAD_STATE;
6315 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006316#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6318 hash_alg,
6319 NULL, 0);
6320 if (status != PSA_SUCCESS) {
6321 return status;
6322 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006323 hkdf->state = HKDF_STATE_STARTED;
6324 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 if (hkdf->state != HKDF_STATE_STARTED) {
6326 return PSA_ERROR_BAD_STATE;
6327 }
6328 status = psa_mac_update(&hkdf->hmac,
6329 data, data_length);
6330 if (status != PSA_SUCCESS) {
6331 return status;
6332 }
6333 status = psa_mac_sign_finish(&hkdf->hmac,
6334 hkdf->prk,
6335 sizeof(hkdf->prk),
6336 &data_length);
6337 if (status != PSA_SUCCESS) {
6338 return status;
6339 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006340 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006341
Gilles Peskine2b522db2019-04-12 15:11:49 +02006342 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006343 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006344#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006346 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006348 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006350#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006351 {
6352 /* Block 0 is empty, and the next block will be
6353 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006355 }
6356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006358 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006359#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6361 return PSA_ERROR_INVALID_ARGUMENT;
6362 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006363#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006364#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6366 hkdf->state == HKDF_STATE_INIT) {
6367 return PSA_ERROR_BAD_STATE;
6368 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006369#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 if (hkdf->state == HKDF_STATE_OUTPUT) {
6371 return PSA_ERROR_BAD_STATE;
6372 }
6373 if (hkdf->info_set) {
6374 return PSA_ERROR_BAD_STATE;
6375 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006376 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 if (data_length != 0) {
6378 hkdf->info = mbedtls_calloc(1, data_length);
6379 if (hkdf->info == NULL) {
6380 return PSA_ERROR_INSUFFICIENT_MEMORY;
6381 }
6382 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006383 }
6384 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006386 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006388 }
6389}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006390#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006391
John Durkop07cc04a2020-11-16 22:08:34 -08006392#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6393 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006394static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6395 const uint8_t *data,
6396 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006397{
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6399 return PSA_ERROR_BAD_STATE;
6400 }
Janos Follathf08e2652019-06-13 09:05:41 +01006401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 if (data_length != 0) {
6403 prf->seed = mbedtls_calloc(1, data_length);
6404 if (prf->seed == NULL) {
6405 return PSA_ERROR_INSUFFICIENT_MEMORY;
6406 }
Janos Follathf08e2652019-06-13 09:05:41 +01006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006409 prf->seed_length = data_length;
6410 }
Janos Follathf08e2652019-06-13 09:05:41 +01006411
Gilles Peskine43f958b2020-12-13 14:55:14 +01006412 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006413
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006415}
6416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6418 const uint8_t *data,
6419 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006420{
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6422 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6423 return PSA_ERROR_BAD_STATE;
6424 }
Janos Follath81550542019-06-13 14:26:34 +01006425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 if (data_length != 0) {
6427 prf->secret = mbedtls_calloc(1, data_length);
6428 if (prf->secret == NULL) {
6429 return PSA_ERROR_INSUFFICIENT_MEMORY;
6430 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006433 prf->secret_length = data_length;
6434 }
Janos Follath81550542019-06-13 14:26:34 +01006435
Gilles Peskine43f958b2020-12-13 14:55:14 +01006436 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006439}
6440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6442 const uint8_t *data,
6443 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006444{
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6446 return PSA_ERROR_BAD_STATE;
6447 }
Janos Follath63028dd2019-06-13 09:15:47 +01006448
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (data_length != 0) {
6450 prf->label = mbedtls_calloc(1, data_length);
6451 if (prf->label == NULL) {
6452 return PSA_ERROR_INSUFFICIENT_MEMORY;
6453 }
Janos Follath63028dd2019-06-13 09:15:47 +01006454
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006456 prf->label_length = data_length;
6457 }
Janos Follath63028dd2019-06-13 09:15:47 +01006458
Gilles Peskine43f958b2020-12-13 14:55:14 +01006459 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006460
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006462}
6463
Gilles Peskine449bd832023-01-11 14:50:10 +01006464static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6465 psa_key_derivation_step_t step,
6466 const uint8_t *data,
6467 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006468{
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006470 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006472 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006474 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006476 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006478 }
6479}
John Durkop07cc04a2020-11-16 22:08:34 -08006480#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6481 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6482
6483#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6484static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6485 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006486 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006488{
6489 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6491 4 + data_length + prf->other_secret_length :
6492 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6495 return PSA_ERROR_INVALID_ARGUMENT;
6496 }
John Durkop07cc04a2020-11-16 22:08:34 -08006497
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 uint8_t *pms = mbedtls_calloc(1, pms_len);
6499 if (pms == NULL) {
6500 return PSA_ERROR_INSUFFICIENT_MEMORY;
6501 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006502 uint8_t *cur = pms;
6503
6504 /* pure-PSK:
6505 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006506 *
6507 * The premaster secret is formed as follows: if the PSK is N octets
6508 * long, concatenate a uint16 with the value N, N zero octets, a second
6509 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006510 *
6511 * mixed-PSK:
6512 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6513 * follows: concatenate a uint16 with the length of the other secret,
6514 * the other secret itself, uint16 with the length of PSK, and the
6515 * PSK itself.
6516 * For details please check:
6517 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6518 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6519 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006520 */
6521
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6523 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6524 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6525 if (prf->other_secret_length != 0) {
6526 memcpy(cur, prf->other_secret, prf->other_secret_length);
6527 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006528 cur += prf->other_secret_length;
6529 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 } else {
6531 *cur++ = MBEDTLS_BYTE_1(data_length);
6532 *cur++ = MBEDTLS_BYTE_0(data_length);
6533 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006534 cur += data_length;
6535 }
6536
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 *cur++ = MBEDTLS_BYTE_1(data_length);
6538 *cur++ = MBEDTLS_BYTE_0(data_length);
6539 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006540 cur += data_length;
6541
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006543
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006544 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006546}
Janos Follath6660f0e2019-06-17 08:44:03 +01006547
Przemek Stekiele47201b2022-04-19 13:53:28 +02006548static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6549 psa_tls12_prf_key_derivation_t *prf,
6550 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006552{
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6554 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006555 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006556
6557 if (data_length != 0) {
6558 prf->other_secret = mbedtls_calloc(1, data_length);
6559 if (prf->other_secret == NULL) {
6560 return PSA_ERROR_INSUFFICIENT_MEMORY;
6561 }
6562
6563 memcpy(prf->other_secret, data, data_length);
6564 prf->other_secret_length = data_length;
6565 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006566 prf->other_secret_length = 0;
6567 }
6568
6569 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6570
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006572}
6573
Janos Follath6660f0e2019-06-17 08:44:03 +01006574static psa_status_t psa_tls12_prf_psk_to_ms_input(
6575 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006576 psa_key_derivation_step_t step,
6577 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006579{
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006581 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 return psa_tls12_prf_psk_to_ms_set_key(prf,
6583 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006584 break;
6585 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6587 data,
6588 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006589 break;
6590 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006592 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006593
Przemek Stekiele47201b2022-04-19 13:53:28 +02006594 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006595}
John Durkop07cc04a2020-11-16 22:08:34 -08006596#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006597
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006598#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6599static psa_status_t psa_tls12_ecjpake_to_pms_input(
6600 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006601 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006602 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006604{
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6606 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6607 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006608 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006609
6610 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 if (data[0] != 0x04) {
6612 return PSA_ERROR_INVALID_ARGUMENT;
6613 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006614
6615 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006617
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006619}
6620#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306621
6622#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6623static psa_status_t psa_pbkdf2_set_input_cost(
6624 psa_pbkdf2_key_derivation_t *pbkdf2,
6625 psa_key_derivation_step_t step,
6626 uint64_t data)
6627{
6628 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6629 return PSA_ERROR_INVALID_ARGUMENT;
6630 }
6631
6632 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6633 return PSA_ERROR_BAD_STATE;
6634 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306635
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306636 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306637 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306638 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306639
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306640 if (data == 0) {
6641 return PSA_ERROR_INVALID_ARGUMENT;
6642 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306643
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306644 pbkdf2->input_cost = data;
6645 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6646
6647 return PSA_SUCCESS;
6648}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306649
6650static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6651 const uint8_t *data,
6652 size_t data_length)
6653{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306654 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6655 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6656 return PSA_ERROR_BAD_STATE;
6657 }
6658
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306659 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6660 pbkdf2->salt = mbedtls_calloc(1, data_length);
6661 if (pbkdf2->salt == NULL) {
6662 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306663 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306664
6665 memcpy(pbkdf2->salt, data, data_length);
6666 pbkdf2->salt_length = data_length;
6667 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306668 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306669
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306670 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6671 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306672 return PSA_ERROR_INSUFFICIENT_MEMORY;
6673 }
6674
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306675 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6676 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306677 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306678 mbedtls_free(pbkdf2->salt);
6679 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306680 }
6681
6682 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6683
6684 return PSA_SUCCESS;
6685}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306686
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306687static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306688 const uint8_t *input,
6689 size_t input_len,
6690 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306691 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306692{
6693 psa_status_t status = PSA_SUCCESS;
6694 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6695 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306696 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306697 } else {
6698 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306699 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306700 }
6701 return status;
6702}
6703
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306704static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306705 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306706 const uint8_t *data,
6707 size_t data_length)
6708{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306709 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306710 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6711 return PSA_ERROR_BAD_STATE;
6712 }
6713
6714 if (data_length != 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306715 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6716 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6717 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6718 pbkdf2->password,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306719 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306720 }
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306721 }
6722
6723 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6724
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306725 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306726}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306727
6728static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306729 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306730 psa_key_derivation_step_t step,
6731 const uint8_t *data,
6732 size_t data_length)
6733{
6734 switch (step) {
6735 case PSA_KEY_DERIVATION_INPUT_SALT:
6736 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6737 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306738 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306739 default:
6740 return PSA_ERROR_INVALID_ARGUMENT;
6741 }
6742}
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306743#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6744
Gilles Peskineb8965192019-09-24 16:21:10 +02006745/** Check whether the given key type is acceptable for the given
6746 * input step of a key derivation.
6747 *
6748 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6749 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6750 * Both secret and non-secret inputs can alternatively have the type
6751 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6752 * that the input was passed as a buffer rather than via a key object.
6753 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006754static int psa_key_derivation_check_input_type(
6755 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006757{
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006759 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 if (key_type == PSA_KEY_TYPE_DERIVE) {
6761 return PSA_SUCCESS;
6762 }
6763 if (key_type == PSA_KEY_TYPE_NONE) {
6764 return PSA_SUCCESS;
6765 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006766 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006767 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 if (key_type == PSA_KEY_TYPE_DERIVE) {
6769 return PSA_SUCCESS;
6770 }
6771 if (key_type == PSA_KEY_TYPE_NONE) {
6772 return PSA_SUCCESS;
6773 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006774 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006775 case PSA_KEY_DERIVATION_INPUT_LABEL:
6776 case PSA_KEY_DERIVATION_INPUT_SALT:
6777 case PSA_KEY_DERIVATION_INPUT_INFO:
6778 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6780 return PSA_SUCCESS;
6781 }
6782 if (key_type == PSA_KEY_TYPE_NONE) {
6783 return PSA_SUCCESS;
6784 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006785 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306786 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6787 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6788 return PSA_SUCCESS;
6789 }
6790 if (key_type == PSA_KEY_TYPE_DERIVE) {
6791 return PSA_SUCCESS;
6792 }
6793 if (key_type == PSA_KEY_TYPE_NONE) {
6794 return PSA_SUCCESS;
6795 }
6796 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006797 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006799}
6800
Janos Follathb80a94e2019-06-12 15:54:46 +01006801static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006802 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006803 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006804 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006805 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006807{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006808 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006809 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006810
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 status = psa_key_derivation_check_input_type(step, key_type);
6812 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006813 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006815
Przemek Stekiel69c46792022-06-10 12:59:51 +02006816#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6818 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6819 step, data, data_length);
6820 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006821#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006822#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6824 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6825 step, data, data_length);
6826 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006827#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6828#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6830 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6831 step, data, data_length);
6832 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006833#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006834#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006835 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006836 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6838 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006839#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306840#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6841 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306842 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6843 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306844 } else
6845#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006846 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006847 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006848 (void) data;
6849 (void) data_length;
6850 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006852 }
6853
Gilles Peskine224b0d62019-09-23 18:13:17 +02006854exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 if (status != PSA_SUCCESS) {
6856 psa_key_derivation_abort(operation);
6857 }
6858 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006859}
6860
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306861static psa_status_t psa_key_derivation_input_integer_internal(
6862 psa_key_derivation_operation_t *operation,
6863 psa_key_derivation_step_t step,
6864 uint64_t value)
6865{
6866 psa_status_t status;
6867 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6868
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306869#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6870 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6871 status = psa_pbkdf2_set_input_cost(
6872 &operation->ctx.pbkdf2, step, value);
6873 } else
6874#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306875 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306876 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306877 (void) value;
6878 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306879 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306880 }
6881
6882 if (status != PSA_SUCCESS) {
6883 psa_key_derivation_abort(operation);
6884 }
6885 return status;
6886}
6887
Janos Follath51f4a0f2019-06-14 11:35:55 +01006888psa_status_t psa_key_derivation_input_bytes(
6889 psa_key_derivation_operation_t *operation,
6890 psa_key_derivation_step_t step,
6891 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006893{
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 return psa_key_derivation_input_internal(operation, step,
6895 PSA_KEY_TYPE_NONE,
6896 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006897}
6898
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306899psa_status_t psa_key_derivation_input_integer(
6900 psa_key_derivation_operation_t *operation,
6901 psa_key_derivation_step_t step,
6902 uint64_t value)
6903{
6904 return psa_key_derivation_input_integer_internal(operation, step, value);
6905}
6906
Janos Follath51f4a0f2019-06-14 11:35:55 +01006907psa_status_t psa_key_derivation_input_key(
6908 psa_key_derivation_operation_t *operation,
6909 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006911{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006913 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006914 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006915
Ronald Cron5c522922020-11-14 16:35:34 +01006916 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006917 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6918 if (status != PSA_SUCCESS) {
6919 psa_key_derivation_abort(operation);
6920 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006921 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006922
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306923 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6924 * permission to output to a key object. */
6925 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6926 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006927 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006929
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 status = psa_key_derivation_input_internal(operation,
6931 step, slot->attr.type,
6932 slot->key.data,
6933 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006934
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006936
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006938}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006939
6940
6941
6942/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006943/* Key agreement */
6944/****************************************************************/
6945
Gilles Peskine449bd832023-01-11 14:50:10 +01006946psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6947 const uint8_t *key_buffer,
6948 size_t key_buffer_size,
6949 psa_algorithm_t alg,
6950 const uint8_t *peer_key,
6951 size_t peer_key_length,
6952 uint8_t *shared_secret,
6953 size_t shared_secret_size,
6954 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006955{
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006957#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006958 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6960 key_buffer_size, alg,
6961 peer_key, peer_key_length,
6962 shared_secret,
6963 shared_secret_size,
6964 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006965#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006966
6967#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6968 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006969 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006970 peer_key,
6971 peer_key_length,
6972 key_buffer,
6973 key_buffer_size,
6974 shared_secret,
6975 shared_secret_size,
6976 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006977#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6978
Gilles Peskine01d718c2018-09-18 12:01:02 +02006979 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006980 (void) attributes;
6981 (void) key_buffer;
6982 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006983 (void) peer_key;
6984 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006985 (void) shared_secret;
6986 (void) shared_secret_size;
6987 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006989 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006990}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006991
Aditya Deshpande17845b82022-10-13 17:21:01 +01006992/** Internal function for raw key agreement
6993 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006994 * to the driver's implementation if a driver is present.
6995 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006996 * (psa_key_agreement_raw_builtin).
6997 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006998static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6999 psa_key_slot_t *private_key,
7000 const uint8_t *peer_key,
7001 size_t peer_key_length,
7002 uint8_t *shared_secret,
7003 size_t shared_secret_size,
7004 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007005{
Gilles Peskine449bd832023-01-11 14:50:10 +01007006 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7007 return PSA_ERROR_NOT_SUPPORTED;
7008 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007009
7010 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007012 };
7013
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 return psa_driver_wrapper_key_agreement(&attributes,
7015 private_key->key.data,
7016 private_key->key.bytes, alg,
7017 peer_key, peer_key_length,
7018 shared_secret,
7019 shared_secret_size,
7020 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007021}
7022
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007023/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007024 * to potentially free embedded data structures and wipe confidential data.
7025 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007026static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7027 psa_key_derivation_step_t step,
7028 psa_key_slot_t *private_key,
7029 const uint8_t *peer_key,
7030 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007031{
7032 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007033 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007034 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007036
7037 /* Step 1: run the secret agreement algorithm to generate the shared
7038 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 status = psa_key_agreement_raw_internal(ka_alg,
7040 private_key,
7041 peer_key, peer_key_length,
7042 shared_secret,
7043 sizeof(shared_secret),
7044 &shared_secret_length);
7045 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007046 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007047 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007048
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007049 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007050 * the shared secret. A shared secret is permitted wherever a key
7051 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 status = psa_key_derivation_input_internal(operation, step,
7053 PSA_KEY_TYPE_DERIVE,
7054 shared_secret,
7055 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007056exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7058 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007059}
7060
Gilles Peskine449bd832023-01-11 14:50:10 +01007061psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7062 psa_key_derivation_step_t step,
7063 mbedtls_svc_key_id_t private_key,
7064 const uint8_t *peer_key,
7065 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007066{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007067 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007068 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007069 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007070
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7072 return PSA_ERROR_INVALID_ARGUMENT;
7073 }
Ronald Cron5c522922020-11-14 16:35:34 +01007074 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7076 if (status != PSA_SUCCESS) {
7077 return status;
7078 }
7079 status = psa_key_agreement_internal(operation, step,
7080 slot,
7081 peer_key, peer_key_length);
7082 if (status != PSA_SUCCESS) {
7083 psa_key_derivation_abort(operation);
7084 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007085 /* If a private key has been added as SECRET, we allow the derived
7086 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007088 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007090 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007091
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007093
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007095}
7096
Gilles Peskine449bd832023-01-11 14:50:10 +01007097psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7098 mbedtls_svc_key_id_t private_key,
7099 const uint8_t *peer_key,
7100 size_t peer_key_length,
7101 uint8_t *output,
7102 size_t output_size,
7103 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007104{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007105 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007106 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007107 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007108
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007110 status = PSA_ERROR_INVALID_ARGUMENT;
7111 goto exit;
7112 }
Ronald Cron5c522922020-11-14 16:35:34 +01007113 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7115 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007118
Gilles Peskine3e561302022-04-14 00:17:15 +02007119 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7120 * for the output size. The PSA specification only guarantees that this
7121 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7122 * but it might be nice to allow smaller buffers if the output fits.
7123 * At the time of writing this comment, with only ECDH implemented,
7124 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7125 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7126 * be exact for it as well. */
7127 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7129 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007130 status = PSA_ERROR_BUFFER_TOO_SMALL;
7131 goto exit;
7132 }
7133
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 status = psa_key_agreement_raw_internal(alg, slot,
7135 peer_key, peer_key_length,
7136 output, output_size,
7137 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007138
7139exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007141 /* If an error happens and is not handled properly, the output
7142 * may be used as a key to protect sensitive data. Arrange for such
7143 * a key to be random, which is likely to result in decryption or
7144 * verification errors. This is better than filling the buffer with
7145 * some constant data such as zeros, which would result in the data
7146 * being protected with a reproducible, easily knowable key.
7147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007149 *output_length = output_size;
7150 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007151
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007153
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007155}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007156
7157
Gilles Peskine30524eb2020-11-13 17:02:26 +01007158
Gilles Peskine86a440b2018-11-12 18:39:40 +01007159/****************************************************************/
7160/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007161/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007162
Gilles Peskine30524eb2020-11-13 17:02:26 +01007163/** Initialize the PSA random generator.
7164 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007165static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007166{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007167#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007168 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007169#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7170
Gilles Peskine30524eb2020-11-13 17:02:26 +01007171 /* Set default configuration if
7172 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007174 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 }
7176 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007177 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007179
Gilles Peskine449bd832023-01-11 14:50:10 +01007180 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007181#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7182 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7183 /* The PSA entropy injection feature depends on using NV seed as an entropy
7184 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 mbedtls_entropy_add_source(&rng->entropy,
7186 mbedtls_nv_seed_poll, NULL,
7187 MBEDTLS_ENTROPY_BLOCK_SIZE,
7188 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007189#endif
7190
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007192#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007193}
7194
7195/** Deinitialize the PSA random generator.
7196 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007197static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007198{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007199#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007201#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7203 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007204#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007205}
7206
7207/** Seed the PSA random generator.
7208 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007209static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007210{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007211#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7212 /* Do nothing: the external RNG seeds itself. */
7213 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007215#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007216 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7218 drbg_seed, sizeof(drbg_seed) - 1);
7219 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007220#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007221}
7222
Gilles Peskine449bd832023-01-11 14:50:10 +01007223psa_status_t psa_generate_random(uint8_t *output,
7224 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007225{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007226 GUARD_MODULE_INITIALIZED;
7227
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007228#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7229
7230 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7232 output, output_size,
7233 &output_length);
7234 if (status != PSA_SUCCESS) {
7235 return status;
7236 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007237 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007238 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 if (output_length != output_size) {
7240 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7241 }
7242 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007243
7244#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7245
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007247 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7249 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7250 output_size);
7251 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7252 output, request_size);
7253 if (ret != 0) {
7254 return mbedtls_to_psa_error(ret);
7255 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007256 output_size -= request_size;
7257 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007258 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007260#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007261}
7262
Gilles Peskine8814fc42020-12-14 15:33:44 +01007263/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007264 *
7265 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7266 * `psa_generate_random(...)`. The state parameter is ignored since the
7267 * PSA API doesn't support passing an explicit state.
7268 *
7269 * In the non-external case, psa_generate_random() calls an
7270 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7271 * and semantics as mbedtls_psa_get_random(). As an optimization,
7272 * instead of doing this back-and-forth between the PSA API and the
7273 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7274 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007275 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007276#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7277int mbedtls_psa_get_random(void *p_rng,
7278 unsigned char *output,
7279 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007280{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007281 /* This function takes a pointer to the RNG state because that's what
7282 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7283 * its own state internally and doesn't let the caller access that state.
7284 * So we just ignore the state parameter, and in practice we'll pass
7285 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007286 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 psa_status_t status = psa_generate_random(output, output_size);
7288 if (status == PSA_SUCCESS) {
7289 return 0;
7290 } else {
7291 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7292 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007293}
7294#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7295
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007296#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00007297#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007298
Gilles Peskine449bd832023-01-11 14:50:10 +01007299psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7300 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007301{
Gilles Peskine449bd832023-01-11 14:50:10 +01007302 if (global_data.initialized) {
7303 return PSA_ERROR_NOT_PERMITTED;
7304 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007305
Gilles Peskine449bd832023-01-11 14:50:10 +01007306 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7307 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7308 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7309 return PSA_ERROR_INVALID_ARGUMENT;
7310 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007311
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007313}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007314#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007315
Ronald Cron3772afe2021-02-08 16:10:05 +01007316/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007317 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007318 * \param type The key type
7319 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007320 *
7321 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007322 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007323 * \retval #PSA_ERROR_INVALID_ARGUMENT
7324 * The size in bits of the key is not valid.
7325 * \retval #PSA_ERROR_NOT_SUPPORTED
7326 * The type and/or the size in bits of the key or the combination of
7327 * the two is not supported.
7328 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007329static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007331{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007332 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007333
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 if (key_type_is_raw_bytes(type)) {
7335 status = psa_validate_unstructured_key_bit_size(type, bits);
7336 if (status != PSA_SUCCESS) {
7337 return status;
7338 }
7339 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007340#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7342 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7343 return PSA_ERROR_NOT_SUPPORTED;
7344 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007345
Ronald Cron01b2aba2020-10-05 09:42:02 +02007346 /* Accept only byte-aligned keys, for the same reasons as
7347 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007348 if (bits % 8 != 0) {
7349 return PSA_ERROR_NOT_SUPPORTED;
7350 }
7351 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007352#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007353
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007354#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007355 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007356 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007357 return PSA_SUCCESS;
7358 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007359#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007360
Valerio Setti8bb57632023-05-26 13:48:07 +02007361#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007362 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007363 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007364 return PSA_ERROR_NOT_SUPPORTED;
7365 }
7366 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007367#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007368 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007369 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007370 }
7371
Gilles Peskine449bd832023-01-11 14:50:10 +01007372 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007373}
7374
Ronald Cron55ed0592020-10-05 10:30:40 +02007375psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007376 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007378{
7379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007380 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007381
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 if ((attributes->domain_parameters == NULL) &&
7383 (attributes->domain_parameters_size != 0)) {
7384 return PSA_ERROR_INVALID_ARGUMENT;
7385 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007386
Gilles Peskine449bd832023-01-11 14:50:10 +01007387 if (key_type_is_raw_bytes(type)) {
7388 status = psa_generate_random(key_buffer, key_buffer_size);
7389 if (status != PSA_SUCCESS) {
7390 return status;
7391 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007392
David Brown12ca5032021-02-11 11:02:00 -07007393#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 if (type == PSA_KEY_TYPE_DES) {
7395 psa_des_set_key_parity(key_buffer, key_buffer_size);
7396 }
David Brown8107e312021-02-12 08:08:46 -07007397#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007399
Valerio Setti8bb57632023-05-26 13:48:07 +02007400#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY) && \
Jaeden Amero424fa932021-05-14 08:34:32 +01007401 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7403 return mbedtls_psa_rsa_generate_key(attributes,
7404 key_buffer,
7405 key_buffer_size,
7406 key_buffer_length);
7407 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007408#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_LEGACY)
Jaeden Amero424fa932021-05-14 08:34:32 +01007409 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007410
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007411#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007412 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7413 return mbedtls_psa_ecp_generate_key(attributes,
7414 key_buffer,
7415 key_buffer_size,
7416 key_buffer_length);
7417 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007418#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007419
Valerio Setti8bb57632023-05-26 13:48:07 +02007420#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007421 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7422 return mbedtls_psa_ffdh_generate_key(attributes,
7423 key_buffer,
7424 key_buffer_size,
7425 key_buffer_length);
7426 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007427#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Steven Cooreman29149862020-08-05 15:43:42 +02007428 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 (void) key_buffer_length;
7430 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007431 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007432
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007434}
Darryl Green0c6575a2018-11-07 16:05:30 +00007435
Gilles Peskine449bd832023-01-11 14:50:10 +01007436psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7437 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007438{
7439 psa_status_t status;
7440 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007441 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007442 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007443
Ronald Cron81709fc2020-11-14 12:10:32 +01007444 *key = MBEDTLS_SVC_KEY_ID_INIT;
7445
Gilles Peskine0f84d622019-09-12 19:03:13 +02007446 /* Reject any attempt to create a zero-length key so that we don't
7447 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 if (psa_get_key_bits(attributes) == 0) {
7449 return PSA_ERROR_INVALID_ARGUMENT;
7450 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007451
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007452 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7454 return PSA_ERROR_INVALID_ARGUMENT;
7455 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007456
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7458 &slot, &driver);
7459 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007460 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 }
Gilles Peskine11792082019-08-06 18:36:36 +02007462
Ronald Cron977c2472020-10-13 08:32:21 +02007463 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307464 * storage ( thus not in the case of generating a key in a secure element
7465 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7466 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007467 if (slot->key.data == NULL) {
7468 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7469 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007470 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 attributes->core.type, attributes->core.bits);
7472 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007473 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007475
7476 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 attributes->core.type,
7478 attributes->core.bits);
7479 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007480 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 attributes, &key_buffer_size);
7482 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007483 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 }
Ronald Cron977c2472020-10-13 08:32:21 +02007485 }
Ronald Cron977c2472020-10-13 08:32:21 +02007486
Gilles Peskine449bd832023-01-11 14:50:10 +01007487 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7488 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007489 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 }
Ronald Cron977c2472020-10-13 08:32:21 +02007491 }
7492
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 status = psa_driver_wrapper_generate_key(attributes,
7494 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007495
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 if (status != PSA_SUCCESS) {
7497 psa_remove_key_data_from_memory(slot);
7498 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007499
Gilles Peskine11792082019-08-06 18:36:36 +02007500exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 if (status == PSA_SUCCESS) {
7502 status = psa_finish_key_creation(slot, driver, key);
7503 }
7504 if (status != PSA_SUCCESS) {
7505 psa_fail_key_creation(slot, driver);
7506 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007507
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007509}
7510
Gilles Peskinee59236f2018-01-27 23:32:46 +01007511/****************************************************************/
7512/* Module setup */
7513/****************************************************************/
7514
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007515#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007516psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 void (* entropy_init)(mbedtls_entropy_context *ctx),
7518 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007519{
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7521 return PSA_ERROR_BAD_STATE;
7522 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007523 global_data.rng.entropy_init = entropy_init;
7524 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007526}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007527#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007528
Gilles Peskine449bd832023-01-11 14:50:10 +01007529void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007530{
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 psa_wipe_all_key_slots();
7532 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7533 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007534 }
7535 /* Wipe all remaining data, including configuration.
7536 * In particular, this sets all state indicator to the value
7537 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007539
7540 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007542}
7543
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007544#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7545/** Recover a transaction that was interrupted by a power failure.
7546 *
7547 * This function is called during initialization, before psa_crypto_init()
7548 * returns. If this function returns a failure status, the initialization
7549 * fails.
7550 */
7551static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007553{
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007555 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7556 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007557 /* TODO - fall through to the failure case until this
7558 * is implemented.
7559 * https://github.com/ARMmbed/mbed-crypto/issues/218
7560 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007561 default:
7562 /* We found an unsupported transaction in the storage.
7563 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007565 }
7566}
7567#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7568
Gilles Peskine449bd832023-01-11 14:50:10 +01007569psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007570{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007571 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007572
Gilles Peskinec6b69072018-11-20 21:42:52 +01007573 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 if (global_data.initialized != 0) {
7575 return PSA_SUCCESS;
7576 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007577
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007578 /* Init drivers */
7579 status = psa_driver_wrapper_init();
7580 if (status != PSA_SUCCESS) {
7581 goto exit;
7582 }
7583 global_data.drivers_initialized = 1;
7584
Gilles Peskine30524eb2020-11-13 17:02:26 +01007585 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007587 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 status = mbedtls_psa_random_seed(&global_data.rng);
7589 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007590 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007592 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007593
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 status = psa_initialize_key_slots();
7595 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007596 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007598
Gilles Peskine4b734222019-07-24 15:56:31 +02007599#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 status = psa_crypto_load_transaction();
7601 if (status == PSA_SUCCESS) {
7602 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7603 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007604 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007605 }
7606 status = psa_crypto_stop_transaction();
7607 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007608 /* There's no transaction to complete. It's all good. */
7609 status = PSA_SUCCESS;
7610 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007611#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007612
Gilles Peskinec6b69072018-11-20 21:42:52 +01007613 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007614 global_data.initialized = 1;
7615
7616exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 if (status != PSA_SUCCESS) {
7618 mbedtls_psa_crypto_free();
7619 }
7620 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007621}
7622
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007623psa_status_t psa_crypto_driver_pake_get_password_len(
7624 const psa_crypto_driver_pake_inputs_t *inputs,
7625 size_t *password_len)
7626{
7627 if (inputs->password_len == 0) {
7628 return PSA_ERROR_BAD_STATE;
7629 }
7630
7631 *password_len = inputs->password_len;
7632
7633 return PSA_SUCCESS;
7634}
7635
7636psa_status_t psa_crypto_driver_pake_get_password(
7637 const psa_crypto_driver_pake_inputs_t *inputs,
7638 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7639{
7640 if (inputs->password_len == 0) {
7641 return PSA_ERROR_BAD_STATE;
7642 }
7643
7644 if (buffer_size < inputs->password_len) {
7645 return PSA_ERROR_BUFFER_TOO_SMALL;
7646 }
7647
7648 memcpy(buffer, inputs->password, inputs->password_len);
7649 *buffer_length = inputs->password_len;
7650
7651 return PSA_SUCCESS;
7652}
7653
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007654psa_status_t psa_crypto_driver_pake_get_user_len(
7655 const psa_crypto_driver_pake_inputs_t *inputs,
7656 size_t *user_len)
7657{
7658 if (inputs->user_len == 0) {
7659 return PSA_ERROR_BAD_STATE;
7660 }
7661
7662 *user_len = inputs->user_len;
7663
7664 return PSA_SUCCESS;
7665}
7666
7667psa_status_t psa_crypto_driver_pake_get_user(
7668 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007669 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007670{
7671 if (inputs->user_len == 0) {
7672 return PSA_ERROR_BAD_STATE;
7673 }
7674
Przemek Stekielc0e62502023-03-14 11:49:36 +01007675 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007676 return PSA_ERROR_BUFFER_TOO_SMALL;
7677 }
7678
Przemek Stekielc0e62502023-03-14 11:49:36 +01007679 memcpy(user_id, inputs->user, inputs->user_len);
7680 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007681
7682 return PSA_SUCCESS;
7683}
7684
7685psa_status_t psa_crypto_driver_pake_get_peer_len(
7686 const psa_crypto_driver_pake_inputs_t *inputs,
7687 size_t *peer_len)
7688{
7689 if (inputs->peer_len == 0) {
7690 return PSA_ERROR_BAD_STATE;
7691 }
7692
7693 *peer_len = inputs->peer_len;
7694
7695 return PSA_SUCCESS;
7696}
7697
7698psa_status_t psa_crypto_driver_pake_get_peer(
7699 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007700 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007701{
7702 if (inputs->peer_len == 0) {
7703 return PSA_ERROR_BAD_STATE;
7704 }
7705
Przemek Stekielc0e62502023-03-14 11:49:36 +01007706 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007707 return PSA_ERROR_BUFFER_TOO_SMALL;
7708 }
7709
Przemek Stekielc0e62502023-03-14 11:49:36 +01007710 memcpy(peer_id, inputs->peer, inputs->peer_len);
7711 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007712
7713 return PSA_SUCCESS;
7714}
7715
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007716psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7717 const psa_crypto_driver_pake_inputs_t *inputs,
7718 psa_pake_cipher_suite_t *cipher_suite)
7719{
7720 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7721 return PSA_ERROR_BAD_STATE;
7722 }
7723
7724 *cipher_suite = inputs->cipher_suite;
7725
7726 return PSA_SUCCESS;
7727}
7728
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01007729#if defined(PSA_WANT_ALG_SOME_PAKE)
Neil Armstronga7d08c32022-06-01 18:21:20 +02007730psa_status_t psa_pake_setup(
7731 psa_pake_operation_t *operation,
7732 const psa_pake_cipher_suite_t *cipher_suite)
7733{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007734 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007735
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007736 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007737 status = PSA_ERROR_BAD_STATE;
7738 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007739 }
7740
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007741 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007742 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007743 status = PSA_ERROR_INVALID_ARGUMENT;
7744 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007745 }
7746
Przemek Stekiel51eac532022-12-07 11:04:51 +01007747 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7748
Przemek Stekiele12ed362022-12-21 12:54:46 +01007749 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007750 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7751 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007752 operation->data.inputs.cipher_suite = *cipher_suite;
7753
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007754#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007755 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007756 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007757 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007758
David Horstmann16f01512023-06-14 17:21:07 +01007759 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007760 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007761 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007762#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007763 {
7764 status = PSA_ERROR_NOT_SUPPORTED;
7765 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007766 }
7767
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007768 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7769
Przemek Stekiel51eac532022-12-07 11:04:51 +01007770 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007771exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007772 psa_pake_abort(operation);
7773 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007774}
7775
7776psa_status_t psa_pake_set_password_key(
7777 psa_pake_operation_t *operation,
7778 mbedtls_svc_key_id_t password)
7779{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007780 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007781 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7782 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007783
Przemek Stekiel51eac532022-12-07 11:04:51 +01007784 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007785 status = PSA_ERROR_BAD_STATE;
7786 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007787 }
7788
Przemek Stekiel6c764412022-11-22 14:05:12 +01007789 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7790 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007791 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007792 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007793 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007794 }
7795
Przemek Stekiel6c764412022-11-22 14:05:12 +01007796 psa_key_attributes_t attributes = {
7797 .core = slot->attr
7798 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007799
Przemek Stekiel51eac532022-12-07 11:04:51 +01007800 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007801
Przemek Stekiel51eac532022-12-07 11:04:51 +01007802 if (type != PSA_KEY_TYPE_PASSWORD &&
7803 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7804 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007805 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007806 }
7807
Przemek Stekiel51eac532022-12-07 11:04:51 +01007808 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7809 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007810 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007811 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007812 }
7813
7814 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7815 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007816 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007817exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007818 if (status != PSA_SUCCESS) {
7819 psa_pake_abort(operation);
7820 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007821 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007822 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007823}
7824
7825psa_status_t psa_pake_set_user(
7826 psa_pake_operation_t *operation,
7827 const uint8_t *user_id,
7828 size_t user_id_len)
7829{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007831
7832 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007833 status = PSA_ERROR_BAD_STATE;
7834 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007835 }
7836
Przemek Stekiel51eac532022-12-07 11:04:51 +01007837 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007838 status = PSA_ERROR_INVALID_ARGUMENT;
7839 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007840 }
7841
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007842 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007843 status = PSA_ERROR_BAD_STATE;
7844 goto exit;
7845 }
7846
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007847 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7848 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007849 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7850 goto exit;
7851 }
7852
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007853 memcpy(operation->data.inputs.user, user_id, user_id_len);
7854 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007855
7856 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007857exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007858 psa_pake_abort(operation);
7859 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007860}
7861
7862psa_status_t psa_pake_set_peer(
7863 psa_pake_operation_t *operation,
7864 const uint8_t *peer_id,
7865 size_t peer_id_len)
7866{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007868
7869 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007870 status = PSA_ERROR_BAD_STATE;
7871 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007872 }
7873
Przemek Stekiel51eac532022-12-07 11:04:51 +01007874 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007875 status = PSA_ERROR_INVALID_ARGUMENT;
7876 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007877 }
7878
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007879 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007880 status = PSA_ERROR_BAD_STATE;
7881 goto exit;
7882 }
7883
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007884 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7885 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007886 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7887 goto exit;
7888 }
7889
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007890 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7891 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007892
7893 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007894exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007895 psa_pake_abort(operation);
7896 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007897}
7898
7899psa_status_t psa_pake_set_role(
7900 psa_pake_operation_t *operation,
7901 psa_pake_role_t role)
7902{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007904
Przemek Stekiel51eac532022-12-07 11:04:51 +01007905 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007906 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007907 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007908 }
7909
Przemek Stekiel09104b82023-03-06 14:11:51 +01007910 switch (operation->alg) {
7911#if defined(PSA_WANT_ALG_JPAKE)
7912 case PSA_ALG_JPAKE:
7913 if (role == PSA_PAKE_ROLE_NONE) {
7914 return PSA_SUCCESS;
7915 }
7916 status = PSA_ERROR_INVALID_ARGUMENT;
7917 break;
7918#endif
7919 default:
7920 (void) role;
7921 status = PSA_ERROR_NOT_SUPPORTED;
7922 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007923 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007924exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007925 psa_pake_abort(operation);
7926 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007927}
7928
David Horstmanne7f21e62023-05-12 18:17:21 +01007929/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007930#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007931static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007932 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007933{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007934 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007935 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007936 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007937
David Horstmann024e5c52023-06-14 15:48:21 +01007938 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007939 is_x1 = (stage->outputs < 1);
7940 } else {
7941 is_x1 = (stage->inputs < 1);
7942 }
7943
David Horstmann74a3d8c2023-06-14 18:28:19 +01007944 key_share_step = is_x1 ?
7945 PSA_JPAKE_X1_STEP_KEY_SHARE :
7946 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007947 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007948 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7949 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7950 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7951 } else {
7952 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007953 }
David Horstmann74a3d8c2023-06-14 18:28:19 +01007954 return key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007955}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007956#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007957
Przemek Stekiel2797d372022-12-22 11:19:22 +01007958static psa_status_t psa_pake_complete_inputs(
7959 psa_pake_operation_t *operation)
7960{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007962 /* Create copy of the inputs on stack as inputs share memory
7963 with the driver context which will be setup by the driver. */
7964 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007965
Przemek Stekielfde11282023-03-13 16:06:09 +01007966 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007967 return PSA_ERROR_BAD_STATE;
7968 }
7969
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007970 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007971 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7972 return PSA_ERROR_BAD_STATE;
7973 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007974 }
7975
Przemek Stekiel18620a32023-01-17 16:34:52 +01007976 /* Clear driver context */
7977 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7978
7979 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007980
7981 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007982 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007983
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007984 /* User and peer are translated to role. */
7985 mbedtls_free(inputs.user);
7986 mbedtls_free(inputs.peer);
7987
Przemek Stekiel2797d372022-12-22 11:19:22 +01007988 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007989#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007990 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007991 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007992 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007993#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007994 {
7995 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007996 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007997 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007998 return status;
7999}
8000
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008001#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008002static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008003 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008004 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008005 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008006{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008007 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8008 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8009 step != PSA_PAKE_STEP_ZK_PROOF) {
8010 return PSA_ERROR_INVALID_ARGUMENT;
8011 }
8012
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008013 psa_jpake_computation_stage_t *computation_stage =
8014 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008015
David Horstmann5da95602023-06-08 15:37:12 +01008016 if (computation_stage->round != PSA_JPAKE_FIRST &&
8017 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008018 return PSA_ERROR_BAD_STATE;
8019 }
8020
David Horstmanne7f21e62023-05-12 18:17:21 +01008021 /* Check that the step we are given is the one we were expecting */
8022 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008023 return PSA_ERROR_BAD_STATE;
8024 }
8025
David Horstmanne7f21e62023-05-12 18:17:21 +01008026 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8027 computation_stage->inputs == 0 &&
8028 computation_stage->outputs == 0) {
8029 /* Start of the round, so function decides whether we are inputting
8030 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008031 computation_stage->io_mode = io_mode;
8032 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008033 /* Middle of the round so the mode we are in must match the function
8034 * called by the user */
8035 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008036 }
8037
8038 return PSA_SUCCESS;
8039}
8040
David Horstmanne7f21e62023-05-12 18:17:21 +01008041static psa_status_t psa_jpake_epilogue(
8042 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008043 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008044{
David Horstmanne7f21e62023-05-12 18:17:21 +01008045 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008046 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008047
David Horstmanne7f21e62023-05-12 18:17:21 +01008048 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8049 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008050 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008051 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008052 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008053 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008054 }
8055 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008056 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008057 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008058 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008059 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008060 }
8061 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008062 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8063 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008064 /* End of a round, move to the next round */
8065 stage->inputs = 0;
8066 stage->outputs = 0;
8067 stage->round++;
8068 }
8069 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008070 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008071 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008072 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008073 return PSA_SUCCESS;
8074}
David Horstmanne7f21e62023-05-12 18:17:21 +01008075
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008076#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008077
Neil Armstronga7d08c32022-06-01 18:21:20 +02008078psa_status_t psa_pake_output(
8079 psa_pake_operation_t *operation,
8080 psa_pake_step_t step,
8081 uint8_t *output,
8082 size_t output_size,
8083 size_t *output_length)
8084{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008085 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008086 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008087 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008088
8089 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008090 status = psa_pake_complete_inputs(operation);
8091 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008092 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008093 }
8094 }
8095
8096 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008097 status = PSA_ERROR_BAD_STATE;
8098 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008099 }
8100
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008101 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008102 status = PSA_ERROR_INVALID_ARGUMENT;
8103 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008104 }
8105
Przemek Stekiele12ed362022-12-21 12:54:46 +01008106 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008107#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008108 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008109 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008110 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008111 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008112 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008113 driver_step = convert_jpake_computation_stage_to_driver_step(
8114 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008115 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008116#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008117 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008118 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008119 status = PSA_ERROR_NOT_SUPPORTED;
8120 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008121 }
8122
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008123 status = psa_driver_wrapper_pake_output(operation, driver_step,
8124 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008125
8126 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008127 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008128 }
8129
8130 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008131#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008132 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008133 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008134 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008135 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008136 }
8137 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008138#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008139 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008140 status = PSA_ERROR_NOT_SUPPORTED;
8141 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008142 }
8143
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008144 return PSA_SUCCESS;
8145exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008146 psa_pake_abort(operation);
8147 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008148}
8149
8150psa_status_t psa_pake_input(
8151 psa_pake_operation_t *operation,
8152 psa_pake_step_t step,
8153 const uint8_t *input,
8154 size_t input_length)
8155{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008156 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008157 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008158 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8159 operation->primitive,
8160 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008161
8162 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008163 status = psa_pake_complete_inputs(operation);
8164 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008165 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008166 }
8167 }
8168
8169 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008170 status = PSA_ERROR_BAD_STATE;
8171 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008172 }
8173
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008174 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008175 status = PSA_ERROR_INVALID_ARGUMENT;
8176 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008177 }
8178
Przemek Stekiele12ed362022-12-21 12:54:46 +01008179 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008180#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008181 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008182 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008183 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008184 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008185 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008186 driver_step = convert_jpake_computation_stage_to_driver_step(
8187 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008188 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008189#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008190 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008191 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008192 status = PSA_ERROR_NOT_SUPPORTED;
8193 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008194 }
8195
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008196 status = psa_driver_wrapper_pake_input(operation, driver_step,
8197 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008198
8199 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008200 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008201 }
8202
8203 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008204#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008205 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008206 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008207 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008208 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008209 }
8210 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008211#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008212 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008213 status = PSA_ERROR_NOT_SUPPORTED;
8214 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008215 }
8216
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008217 return PSA_SUCCESS;
8218exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008219 psa_pake_abort(operation);
8220 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008221}
8222
8223psa_status_t psa_pake_get_implicit_key(
8224 psa_pake_operation_t *operation,
8225 psa_key_derivation_operation_t *output)
8226{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008227 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008228 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008229 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008230 size_t shared_key_len = 0;
8231
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008232 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008233 status = PSA_ERROR_BAD_STATE;
8234 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008235 }
8236
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008237#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008238 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008239 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008240 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008241 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008242 status = PSA_ERROR_BAD_STATE;
8243 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008244 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008245 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008246#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008247 {
8248 status = PSA_ERROR_NOT_SUPPORTED;
8249 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008250 }
8251
Przemek Stekiel0c781802022-11-29 14:53:13 +01008252 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8253 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008254 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008255 &shared_key_len);
8256
8257 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008258 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008259 }
8260
8261 status = psa_key_derivation_input_bytes(output,
8262 PSA_KEY_DERIVATION_INPUT_SECRET,
8263 shared_key,
8264 shared_key_len);
8265
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008266 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008267exit:
8268 abort_status = psa_pake_abort(operation);
8269 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008270}
8271
8272psa_status_t psa_pake_abort(
8273 psa_pake_operation_t *operation)
8274{
Przemek Stekield69dca92023-01-31 19:59:20 +01008275 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276
Przemek Stekield69dca92023-01-31 19:59:20 +01008277 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008278 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008279 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008280
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008281 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8282 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008283 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008284 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008285 }
8286 if (operation->data.inputs.user != NULL) {
8287 mbedtls_free(operation->data.inputs.user);
8288 }
8289 if (operation->data.inputs.peer != NULL) {
8290 mbedtls_free(operation->data.inputs.peer);
8291 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008292 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008293 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008294
Przemek Stekield69dca92023-01-31 19:59:20 +01008295 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008296}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008297#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008298
Gilles Peskinee59236f2018-01-27 23:32:46 +01008299#endif /* MBEDTLS_PSA_CRYPTO_C */