blob: 456d4e38f3ae7716b2aef623ae411e9d5c1c587a [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"
Dave Rodgman78701152023-08-29 14:20:18 +010067#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020069#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010071#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/error.h"
73#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010075#include "mbedtls/md.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 {
Dave Rodgman864f5942023-08-16 18:04:44 +0100108 uint8_t initialized;
109 uint8_t rng_state;
110 uint8_t drivers_initialized;
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 Settia55f0422023-07-10 15:34:41 +0200130#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200131 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200132 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
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 Settia55f0422023-07-10 15:34:41 +0200142#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200143 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200144 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
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
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100156#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
158 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100160 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
161 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100162#endif
163
164#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200165 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
166 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
167 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
168 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
169 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200171 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200173 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100175#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200176
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100177#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000178 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100179 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100181#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100182
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100183#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100184 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100188#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100189
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100190#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200191 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100193#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200194
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100195#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200196 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200198 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100200#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200201
Dave Rodgman509b5672023-08-16 19:26:23 +0100202#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100217#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
220 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100221 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
222 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100223 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
226 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100228 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100230#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100232#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100233 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100235#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
Gilles Peskinee59236f2018-01-27 23:32:46 +0100237 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
238 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
239 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100241
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100242#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100243 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200245 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100249#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100250
Gilles Peskine82e57d12020-11-13 21:31:17 +0100251#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100253 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
254 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100255 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
258 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100260 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100262#endif
263
Dave Rodgmandea266f2023-08-31 11:52:43 +0100264#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100265 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100271#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100272 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100274#endif
275#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100276
Dave Rodgman509b5672023-08-16 19:26:23 +0100277#if defined(MBEDTLS_BIGNUM_C)
278#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100279 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100281#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100284 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100286 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100288 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100290 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100292 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100294 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100296#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100297
Dave Rodgman509b5672023-08-16 19:26:23 +0100298#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
302 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100304#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
305 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100308#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
310 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100312 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100314 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
315 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100317 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319 case MBEDTLS_ERR_PK_INVALID_ALG:
320 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
321 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100323 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200325 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100327#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100328
Gilles Peskineff2d2002019-05-06 15:26:23 +0200329 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200331 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200333
Dave Rodgman509b5672023-08-16 19:26:23 +0100334#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100335 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100337 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100339 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100341 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100343 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
344 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100346 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100348 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100352#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200353
Dave Rodgman1dab4452023-09-01 09:59:51 +0100354#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300355 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300356 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300358 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300360 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300362 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
363 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300365 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100367 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100369
Dave Rodgman509b5672023-08-16 19:26:23 +0100370#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000371 case MBEDTLS_ERR_ECP_IN_PROGRESS:
372 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100373#endif
374#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000375
Janos Follatha13b9052019-11-22 12:48:59 +0000376 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300378
Gilles Peskinee59236f2018-01-27 23:32:46 +0100379 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100381 }
382}
383
Paul Elliottdc42ca82023-02-24 18:11:59 +0000384/**
385 * \brief For output buffers which contain "tags"
386 * (outputs that may be checked for validity like
387 * hashes, MACs and signatures), fill the unused
388 * part of the output buffer (the whole buffer on
389 * error, the trailing part on success) with
390 * something that isn't a valid tag (barring an
391 * attack on the tag and deliberately-crafted
392 * input), in case the caller doesn't check the
393 * return status properly.
394 *
395 * \param output_buffer Pointer to buffer to wipe. May not be NULL
396 * unless \p output_buffer_size is zero.
397 * \param status Status of function called to generate
398 * output_buffer originally
399 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
400 * could be NULL.
401 * \param output_buffer_length Length of data written to output_buffer, must be
402 * less than \p output_buffer_size
403 */
404static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000405 size_t output_buffer_size, size_t output_buffer_length)
406{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000407 size_t offset = 0;
408
409 if (output_buffer_size == 0) {
410 /* If output_buffer_size is 0 then we have nothing to do. We must not
411 call memset because output_buffer may be NULL in this case */
412 return;
413 }
414
415 if (status == PSA_SUCCESS) {
416 offset = output_buffer_length;
417 }
418
419 memset(output_buffer + offset, '!', output_buffer_size - offset);
420}
421
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200422
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200423
424
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100425/****************************************************************/
426/* Key management */
427/****************************************************************/
428
Valerio Settia9aab1a2023-06-19 13:39:54 +0200429#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200430psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
431 size_t *bits)
432{
433 switch (grpid) {
Dave Rodgman6f682032023-08-16 18:44:32 +0100434#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200435 case MBEDTLS_ECP_DP_SECP192R1:
436 *bits = 192;
437 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100438#endif
439#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200440 case MBEDTLS_ECP_DP_SECP224R1:
441 *bits = 224;
442 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100443#endif
444#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200445 case MBEDTLS_ECP_DP_SECP256R1:
446 *bits = 256;
447 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100448#endif
449#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200450 case MBEDTLS_ECP_DP_SECP384R1:
451 *bits = 384;
452 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100453#endif
454#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200455 case MBEDTLS_ECP_DP_SECP521R1:
456 *bits = 521;
457 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100458#endif
459#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200460 case MBEDTLS_ECP_DP_BP256R1:
461 *bits = 256;
462 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100463#endif
464#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200465 case MBEDTLS_ECP_DP_BP384R1:
466 *bits = 384;
467 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100468#endif
469#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200470 case MBEDTLS_ECP_DP_BP512R1:
471 *bits = 512;
472 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100473#endif
474#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200475 case MBEDTLS_ECP_DP_CURVE25519:
476 *bits = 255;
477 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100478#endif
479#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200480 case MBEDTLS_ECP_DP_SECP192K1:
481 *bits = 192;
482 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100483#endif
484#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200485 case MBEDTLS_ECP_DP_SECP224K1:
486 *bits = 224;
487 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100488#endif
489#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200490 case MBEDTLS_ECP_DP_SECP256K1:
491 *bits = 256;
492 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100493#endif
494#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200495 case MBEDTLS_ECP_DP_CURVE448:
496 *bits = 448;
497 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100498#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200499 default:
500 *bits = 0;
501 return 0;
502 }
503}
504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
506 size_t bits,
507 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200508{
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100510 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100512#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100513 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100515#endif
516#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100517 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100519#endif
520#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100521 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100523#endif
524#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100525 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100527#endif
528#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100529 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100531 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (bits_is_sloppy) {
533 return MBEDTLS_ECP_DP_SECP521R1;
534 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100535 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100536#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100537 }
538 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100539
Paul Elliott8ff510a2020-06-02 17:19:28 +0100540 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100542#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100543 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100545#endif
546#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100547 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100549#endif
550#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100551 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100553#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100554 }
555 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100556
Paul Elliott8ff510a2020-06-02 17:19:28 +0100557 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100559#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100560 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100562 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (bits_is_sloppy) {
564 return MBEDTLS_ECP_DP_CURVE25519;
565 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100566 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100567#endif
568#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100569 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100571#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100572 }
573 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100574
Paul Elliott8ff510a2020-06-02 17:19:28 +0100575 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100577#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100578 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100580#endif
581#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100582 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100584#endif
585#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100586 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100588#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100589 }
590 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200591 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100592
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100593 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200595}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200596#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
599 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200600{
601 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200603 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200604 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200605 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200606 case PSA_KEY_TYPE_PASSWORD:
607 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200608 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100609#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200610 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (bits != 128 && bits != 192 && bits != 256) {
612 return PSA_ERROR_INVALID_ARGUMENT;
613 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200614 break;
615#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200616#if defined(PSA_WANT_KEY_TYPE_ARIA)
617 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (bits != 128 && bits != 192 && bits != 256) {
619 return PSA_ERROR_INVALID_ARGUMENT;
620 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200621 break;
622#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100623#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200624 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 if (bits != 128 && bits != 192 && bits != 256) {
626 return PSA_ERROR_INVALID_ARGUMENT;
627 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200628 break;
629#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100630#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200631 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (bits != 64 && bits != 128 && bits != 192) {
633 return PSA_ERROR_INVALID_ARGUMENT;
634 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200635 break;
636#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100637#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200638 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (bits != 256) {
640 return PSA_ERROR_INVALID_ARGUMENT;
641 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200642 break;
643#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200644 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200646 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (bits % 8 != 0) {
648 return PSA_ERROR_INVALID_ARGUMENT;
649 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200652}
653
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100654/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100655 *
Steven Cooremancd640932021-03-08 12:00:27 +0100656 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
657 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100658 *
659 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100660 * \param[in] key_type The key type of the key to be used with the
661 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100662 *
663 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100664 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100665 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100666 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100667 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100668MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100669 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100671{
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (PSA_ALG_IS_HMAC(algorithm)) {
673 if (key_type == PSA_KEY_TYPE_HMAC) {
674 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100675 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100676 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
679 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
680 * key. */
681 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
682 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
683 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
684 * the block length (larger than 1) for block ciphers. */
685 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
686 return PSA_SUCCESS;
687 }
688 }
689 }
690
691 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100692}
693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
695 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200696{
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (slot->key.data != NULL) {
698 return PSA_ERROR_ALREADY_EXISTS;
699 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 slot->key.data = mbedtls_calloc(1, buffer_length);
702 if (slot->key.data == NULL) {
703 return PSA_ERROR_INSUFFICIENT_MEMORY;
704 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200705
Ronald Cronea0f8a62020-11-25 17:52:23 +0100706 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200708}
709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
711 const uint8_t *data,
712 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200713{
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 psa_status_t status = psa_allocate_buffer_to_slot(slot,
715 data_length);
716 if (status != PSA_SUCCESS) {
717 return status;
718 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 memcpy(slot->key.data, data, data_length);
721 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200722}
723
Ronald Crona0fe59f2020-11-29 11:14:53 +0100724psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100725 const psa_key_attributes_t *attributes,
726 const uint8_t *data, size_t data_length,
727 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100729{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100730 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
731 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100732
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200733 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (data_length == 0) {
735 return PSA_ERROR_NOT_SUPPORTED;
736 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if (key_type_is_raw_bytes(type)) {
739 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
742 *bits);
743 if (status != PSA_SUCCESS) {
744 return status;
745 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200746
Ronald Crondd04d422020-11-28 15:14:42 +0100747 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100749 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 return PSA_SUCCESS;
753 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200754#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200755 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100756 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200757 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100758 return PSA_ERROR_INVALID_ARGUMENT;
759 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200760 return mbedtls_psa_ffdh_import_key(attributes,
761 data, data_length,
762 key_buffer, key_buffer_size,
763 key_buffer_length,
764 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100765 }
Valerio Settia55f0422023-07-10 15:34:41 +0200766#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200767 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200768#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
770 if (PSA_KEY_TYPE_IS_ECC(type)) {
771 return mbedtls_psa_ecp_import_key(attributes,
772 data, data_length,
773 key_buffer, key_buffer_size,
774 key_buffer_length,
775 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100776 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200777#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800778 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200779#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
780 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
782 if (PSA_KEY_TYPE_IS_RSA(type)) {
783 return mbedtls_psa_rsa_import_key(attributes,
784 data, data_length,
785 key_buffer, key_buffer_size,
786 key_buffer_length,
787 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200788 }
Valerio Settife478902023-07-25 12:27:19 +0200789#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
790 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800791 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100792 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100795}
796
Gilles Peskinef603c712019-01-19 13:40:11 +0100797/** Calculate the intersection of two algorithm usage policies.
798 *
799 * Return 0 (which allows no operation) on incompatibility.
800 */
801static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100802 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100803 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100805{
Gilles Peskine549ea862019-05-22 11:45:59 +0200806 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (alg1 == alg2) {
808 return alg1;
809 }
Steven Cooreman03488022021-02-18 12:07:20 +0100810 /* If the policies are from the same hash-and-sign family, check
811 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
813 PSA_ALG_IS_SIGN_HASH(alg2) &&
814 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
815 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
816 return alg2;
817 }
818 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
819 return alg1;
820 }
Steven Cooreman03488022021-02-18 12:07:20 +0100821 }
822 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100823 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100824 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
826 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
827 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
828 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
829 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100830 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100831
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100832 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
834 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
835 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
836 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100837 }
838 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
840 (alg1_len <= alg2_len)) {
841 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100842 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
844 (alg2_len <= alg1_len)) {
845 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100846 }
847 }
848 /* If the policies are from the same MAC family, check whether one
849 * of them is a minimum-MAC-length policy. Calculate the most
850 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
852 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
853 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100854 /* Validate the combination of key type and algorithm. Since the base
855 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
857 return 0;
858 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100859
Steven Cooreman31a876d2021-03-03 20:47:40 +0100860 /* Get the (exact or at-least) output lengths for both sides of the
861 * requested intersection. None of the currently supported algorithms
862 * have an output length dependent on the actual key size, so setting it
863 * to a bogus value of 0 is currently OK.
864 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100865 * Note that for at-least-this-length wildcard algorithms, the output
866 * length is set to the shortest allowed length, which allows us to
867 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
869 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100870 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100871
Steven Cooremana1d83222021-02-25 10:20:29 +0100872 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
874 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
875 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100876 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100877
878 /* If only one is an at-least-this-length policy, the intersection would
879 * be the other (fixed-length) policy as long as said fixed length is
880 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
882 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100883 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
885 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100886 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100887
Steven Cooremancd640932021-03-08 12:00:27 +0100888 /* If none of them are wildcards, check whether they define the same tag
889 * length. This is still possible here when one is default-length and
890 * the other specific-length. Ensure to always return the
891 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if (alg1_len == alg2_len) {
893 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
894 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100895 }
896 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100898}
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900static int psa_key_algorithm_permits(psa_key_type_t key_type,
901 psa_algorithm_t policy_alg,
902 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200903{
Gilles Peskine549ea862019-05-22 11:45:59 +0200904 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (requested_alg == policy_alg) {
906 return 1;
907 }
Steven Cooreman03488022021-02-18 12:07:20 +0100908 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
909 * and requested_alg is the same hash-and-sign family with any hash,
910 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
912 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
913 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
914 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100915 }
916 /* If policy_alg is a wildcard AEAD algorithm of the same base as
917 * the requested algorithm, check the requested tag length to be
918 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if (PSA_ALG_IS_AEAD(policy_alg) &&
920 PSA_ALG_IS_AEAD(requested_alg) &&
921 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
922 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
923 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
924 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
925 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100926 }
Steven Cooremancd640932021-03-08 12:00:27 +0100927 /* If policy_alg is a MAC algorithm of the same base as the requested
928 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if (PSA_ALG_IS_MAC(policy_alg) &&
930 PSA_ALG_IS_MAC(requested_alg) &&
931 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
932 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100933 /* Validate the combination of key type and algorithm. Since the policy
934 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
936 return 0;
937 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100938
Steven Cooreman31a876d2021-03-03 20:47:40 +0100939 /* Get both the requested output length for the algorithm which is to be
940 * verified, and the default output length for the base algorithm.
941 * Note that none of the currently supported algorithms have an output
942 * length dependent on actual key size, so setting it to a bogus value
943 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100944 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100946 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 key_type, 0,
948 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100949
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100950 /* If the policy is default-length, only allow an algorithm with
951 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
953 return requested_output_length == default_output_length;
954 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100955
956 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100957 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
959 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
960 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100961 }
962
Steven Cooremancd640932021-03-08 12:00:27 +0100963 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
964 * check for the requested MAC length to be equal to or longer than the
965 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
967 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
968 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100969 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200970 }
Steven Cooremance48e852020-10-05 16:02:45 +0200971 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200972 * a key derivation with that key agreement should also be allowed. This
973 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
975 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
976 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
977 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200978 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100979 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200981}
982
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100983/** Test whether a policy permits an algorithm.
984 *
985 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100986 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100987 * \note This function requires providing the key type for which the policy is
988 * being validated, since some algorithm policy definitions (e.g. MAC)
989 * have different properties depending on what kind of cipher it is
990 * combined with.
991 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100992 * \retval PSA_SUCCESS When \p alg is a specific algorithm
993 * allowed by the \p policy.
994 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
995 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
996 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100997 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
999 psa_key_type_t key_type,
1000 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001001{
Steven Cooremand788fab2021-02-25 11:29:17 +01001002 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if (alg == 0) {
1004 return PSA_ERROR_INVALID_ARGUMENT;
1005 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001006
Steven Cooreman7e39f052021-02-23 14:18:32 +01001007 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 if (PSA_ALG_IS_WILDCARD(alg)) {
1009 return PSA_ERROR_INVALID_ARGUMENT;
1010 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1013 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1014 return PSA_SUCCESS;
1015 } else {
1016 return PSA_ERROR_NOT_PERMITTED;
1017 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001018}
1019
Gilles Peskinef603c712019-01-19 13:40:11 +01001020/** Restrict a key policy based on a constraint.
1021 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001022 * \note This function requires providing the key type for which the policy is
1023 * being restricted, since some algorithm policy definitions (e.g. MAC)
1024 * have different properties depending on what kind of cipher it is
1025 * combined with.
1026 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001027 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001028 * \param[in,out] policy The policy to restrict.
1029 * \param[in] constraint The policy constraint to apply.
1030 *
1031 * \retval #PSA_SUCCESS
1032 * \c *policy contains the intersection of the original value of
1033 * \c *policy and \c *constraint.
1034 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001035 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001036 * \c *policy is unchanged.
1037 */
1038static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001039 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001040 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001042{
1043 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1045 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001046 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1048 constraint->alg2);
1049 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1050 return PSA_ERROR_INVALID_ARGUMENT;
1051 }
1052 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1053 return PSA_ERROR_INVALID_ARGUMENT;
1054 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001055 policy->usage &= constraint->usage;
1056 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001057 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001059}
1060
Przemek Stekiel061f6942022-11-30 10:51:35 +01001061/** Get the description of a key given its identifier and policy constraints
1062 * and lock it.
1063 *
1064 * The key must have allow all the usage flags set in \p usage. If \p alg is
1065 * nonzero, the key must allow operations with this algorithm. If \p alg is
1066 * zero, the algorithm is not checked.
1067 *
1068 * In case of a persistent key, the function loads the description of the key
1069 * into a key slot if not already done.
1070 *
1071 * On success, the returned key slot is locked. It is the responsibility of
1072 * the caller to unlock the key slot when it does not access it anymore.
1073 */
1074static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001075 mbedtls_svc_key_id_t key,
1076 psa_key_slot_t **p_slot,
1077 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001079{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001080 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001081 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 status = psa_get_and_lock_key_slot(key, p_slot);
1084 if (status != PSA_SUCCESS) {
1085 return status;
1086 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001087 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001088
1089 /* Enforce that usage policy for the key slot contains all the flags
1090 * required by the usage parameter. There is one exception: public
1091 * keys can always be exported, so we treat public key objects as
1092 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001094 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001098 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001099 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001100 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001101
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001102 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 if (alg != 0) {
1104 status = psa_key_policy_permits(&slot->attr.policy,
1105 slot->attr.type,
1106 alg);
1107 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001108 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001110 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001113
1114error:
1115 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001119}
Darryl Green940d72c2018-07-13 13:18:51 +01001120
Ronald Cron5c522922020-11-14 16:35:34 +01001121/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001122 *
1123 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001124 * available, as opposed to a key in a secure element and/or to be used
1125 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001126 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001127 * This is a temporary function that may be used instead of
1128 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1129 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001130 *
Ronald Cron5c522922020-11-14 16:35:34 +01001131 * On success, the returned key slot is locked. It is the responsibility of the
1132 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001133 */
Ronald Cron5c522922020-11-14 16:35:34 +01001134static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1135 mbedtls_svc_key_id_t key,
1136 psa_key_slot_t **p_slot,
1137 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001139{
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1141 usage, alg);
1142 if (status != PSA_SUCCESS) {
1143 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001144 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1147 psa_unlock_key_slot(*p_slot);
1148 *p_slot = NULL;
1149 return PSA_ERROR_NOT_SUPPORTED;
1150 }
1151
1152 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001153}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001156{
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001158 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001160
Ronald Cronea0f8a62020-11-25 17:52:23 +01001161 slot->key.data = NULL;
1162 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001165}
1166
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001167/** Completely wipe a slot in memory, including its policy.
1168 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001170{
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 /*
1174 * As the return error code may not be handled in case of multiple errors,
1175 * do our best to report an unexpected lock counter. Assert with
1176 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1177 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1178 * function is called as part of the execution of a test suite, the
1179 * execution of the test suite is stopped in error if the assertion fails.
1180 */
1181 if (slot->lock_count != 1) {
1182 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001183 status = PSA_ERROR_CORRUPTION_DETECTED;
1184 }
1185
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001186 /* Multipart operations may still be using the key. This is safe
1187 * because all multipart operation objects are independent from
1188 * the key slot: if they need to access the key after the setup
1189 * phase, they have a copy of the key. Note that this means that
1190 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001191 /* At this point, key material and other type-specific content has
1192 * been wiped. Clear remaining metadata. We can call memset and not
1193 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 memset(slot, 0, sizeof(*slot));
1195 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001196}
1197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001199{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001200 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001201 psa_status_t status; /* status of the last operation */
1202 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001203#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1204 psa_se_drv_table_entry_t *driver;
1205#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (mbedtls_svc_key_id_is_null(key)) {
1208 return PSA_SUCCESS;
1209 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001210
Ronald Cronf2911112020-10-29 17:51:10 +01001211 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001212 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001213 * key, this will load the key description from persistent memory if not
1214 * done yet. We cannot avoid this loading as without it we don't know if
1215 * the key is operated by an SE or not and this information is needed by
1216 * the current implementation.
1217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 status = psa_get_and_lock_key_slot(key, &slot);
1219 if (status != PSA_SUCCESS) {
1220 return status;
1221 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001222
Ronald Cronf2911112020-10-29 17:51:10 +01001223 /*
1224 * If the key slot containing the key description is under access by the
1225 * library (apart from the present access), the key cannot be destroyed
1226 * yet. For the time being, just return in error. Eventually (to be
1227 * implemented), the key should be destroyed when all accesses have
1228 * stopped.
1229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 if (slot->lock_count > 1) {
1231 psa_unlock_key_slot(slot);
1232 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001233 }
1234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001236 /* Refuse the destruction of a read-only key (which may or may not work
1237 * if we attempt it, depending on whether the key is merely read-only
1238 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001239 * Just do the best we can, which is to wipe the copy in memory
1240 * (done in this function's cleanup code). */
1241 overall_status = PSA_ERROR_NOT_PERMITTED;
1242 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001243 }
1244
Gilles Peskine354f7672019-07-12 23:46:38 +02001245#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1247 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001248 /* For a key in a secure element, we need to do three things:
1249 * remove the key file in internal storage, destroy the
1250 * key inside the secure element, and update the driver's
1251 * persistent data. Start a transaction that will encompass these
1252 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001254 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001256 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 status = psa_crypto_save_transaction();
1258 if (status != PSA_SUCCESS) {
1259 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001260 /* We should still try to destroy the key in the secure
1261 * element and the key metadata in storage. This is especially
1262 * important if the error is that the storage is full.
1263 * But how to do it exactly without risking an inconsistent
1264 * state after a reset?
1265 * https://github.com/ARMmbed/mbed-crypto/issues/215
1266 */
1267 overall_status = status;
1268 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001269 }
1270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 status = psa_destroy_se_key(driver,
1272 psa_key_slot_get_slot_number(slot));
1273 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001274 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001276 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001277#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1278
Darryl Greend49a4992018-06-18 17:27:26 +01001279#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1281 status = psa_destroy_persistent_key(slot->attr.id);
1282 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001283 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001285
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001286 /* TODO: other slots may have a copy of the same key. We should
1287 * invalidate them.
1288 * https://github.com/ARMmbed/mbed-crypto/issues/214
1289 */
Darryl Greend49a4992018-06-18 17:27:26 +01001290 }
1291#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001292
Gilles Peskinefc762652019-07-22 19:30:34 +02001293#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if (driver != NULL) {
1295 status = psa_save_se_persistent_data(driver);
1296 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001297 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 }
1299 status = psa_crypto_stop_transaction();
1300 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001301 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001303 }
1304#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1305
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001306exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001308 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001310 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 }
1312 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001313}
1314
Valerio Settib2bcedb2023-07-10 11:24:00 +02001315#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001316 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001317static psa_status_t psa_get_rsa_public_exponent(
1318 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001320{
1321 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001323 uint8_t *buffer = NULL;
1324 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1328 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001329 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 }
1331 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001332 /* It's the default value, which is reported as an empty string,
1333 * so there's nothing to do. */
1334 goto exit;
1335 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 buflen = mbedtls_mpi_size(&mpi);
1338 buffer = mbedtls_calloc(1, buflen);
1339 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001340 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1341 goto exit;
1342 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1344 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001345 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001347 attributes->domain_parameters = buffer;
1348 attributes->domain_parameters_size = buflen;
1349
1350exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 mbedtls_mpi_free(&mpi);
1352 if (ret != 0) {
1353 mbedtls_free(buffer);
1354 }
1355 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001356}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001357#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001358 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001359
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001360/** Retrieve all the publicly-accessible attributes of a key.
1361 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001362psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1363 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001366 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001367 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1372 if (status != PSA_SUCCESS) {
1373 return status;
1374 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001375
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001376 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1378 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001379
1380#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1382 psa_set_key_slot_number(attributes,
1383 psa_key_slot_get_slot_number(slot));
1384 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001385#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001388#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1389 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001390 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001391 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001392 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001393 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001394 * is not yet implemented.
1395 * https://github.com/ARMmbed/mbed-crypto/issues/216
1396 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001398 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001399
Ronald Cron90857082020-11-25 14:58:33 +01001400 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 slot->attr.type,
1402 slot->key.data,
1403 slot->key.bytes,
1404 &rsa);
1405 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001406 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 status = psa_get_rsa_public_exponent(rsa,
1410 attributes);
1411 mbedtls_rsa_free(rsa);
1412 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001413 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001414 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001415#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1416 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001417 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001418 default:
1419 /* Nothing else to do. */
1420 break;
1421 }
1422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if (status != PSA_SUCCESS) {
1424 psa_reset_key_attributes(attributes);
1425 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001430}
1431
Gilles Peskinec8000c02019-08-02 20:15:51 +02001432#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1433psa_status_t psa_get_key_slot_number(
1434 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001436{
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001438 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 return PSA_SUCCESS;
1440 } else {
1441 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001442 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001443}
1444#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1447 size_t key_buffer_size,
1448 uint8_t *data,
1449 size_t data_size,
1450 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001451{
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (key_buffer_size > data_size) {
1453 return PSA_ERROR_BUFFER_TOO_SMALL;
1454 }
1455 memcpy(data, key_buffer, key_buffer_size);
1456 memset(data + key_buffer_size, 0,
1457 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001458 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001460}
1461
Ronald Cron7285cda2020-11-26 14:46:37 +01001462psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001463 const psa_key_attributes_t *attributes,
1464 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001466{
Ronald Crond18b5f82020-11-25 16:46:05 +01001467 psa_key_type_t type = attributes->core.type;
1468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (key_type_is_raw_bytes(type) ||
1470 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001471 PSA_KEY_TYPE_IS_ECC(type) ||
1472 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 return psa_export_key_buffer_internal(
1474 key_buffer, key_buffer_size,
1475 data, data_size, data_length);
1476 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001477 /* This shouldn't happen in the reference implementation, but
1478 it is valid for a special-purpose implementation to omit
1479 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001481 }
1482}
1483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1485 uint8_t *data,
1486 size_t data_size,
1487 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001488{
1489 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1490 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1491 psa_key_slot_t *slot;
1492
Ronald Crone907e552021-01-18 13:22:38 +01001493 /* Reject a zero-length output buffer now, since this can never be a
1494 * valid key representation. This way we know that data must be a valid
1495 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (data_size == 0) {
1497 return PSA_ERROR_BUFFER_TOO_SMALL;
1498 }
Ronald Crone907e552021-01-18 13:22:38 +01001499
Ronald Cron9486f9d2020-11-26 13:36:23 +01001500 /* Set the key to empty now, so that even when there are errors, we always
1501 * set data_length to a value between 0 and data_size. On error, setting
1502 * the key to empty is a good choice because an empty key representation is
1503 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001504 *data_length = 0;
1505
Ronald Cron9486f9d2020-11-26 13:36:23 +01001506 /* Export requires the EXPORT flag. There is an exception for public keys,
1507 * which don't require any flag, but
1508 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1509 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1511 PSA_KEY_USAGE_EXPORT, 0);
1512 if (status != PSA_SUCCESS) {
1513 return status;
1514 }
mohammad160306e79202018-03-28 13:17:44 +03001515
Ronald Crond18b5f82020-11-25 16:46:05 +01001516 psa_key_attributes_t attributes = {
1517 .core = slot->attr
1518 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 status = psa_driver_wrapper_export_key(&attributes,
1520 slot->key.data, slot->key.bytes,
1521 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001526}
1527
Ronald Cron7285cda2020-11-26 14:46:37 +01001528psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001529 const psa_key_attributes_t *attributes,
1530 const uint8_t *key_buffer,
1531 size_t key_buffer_size,
1532 uint8_t *data,
1533 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001535{
Ronald Crond18b5f82020-11-25 16:46:05 +01001536 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001537
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001538 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001539 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1540 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001541 /* Exporting public -> public */
1542 return psa_export_key_buffer_internal(
1543 key_buffer, key_buffer_size,
1544 data, data_size, data_length);
1545 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001546#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001547 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1548 return mbedtls_psa_rsa_export_public_key(attributes,
1549 key_buffer,
1550 key_buffer_size,
1551 data,
1552 data_size,
1553 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001554#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001555 /* We don't know how to convert a private RSA key to public. */
1556 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001557#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001558 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001559 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001560#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001561 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1562 return mbedtls_psa_ecp_export_public_key(attributes,
1563 key_buffer,
1564 key_buffer_size,
1565 data,
1566 data_size,
1567 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001568#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001569 /* We don't know how to convert a private ECC key to public */
1570 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001571#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001572 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001573 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001574#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001575 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001576 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001577 key_buffer,
1578 key_buffer_size,
1579 data, data_size,
1580 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001581#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001582 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001583#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001584 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001586 (void) key_buffer;
1587 (void) key_buffer_size;
1588 (void) data;
1589 (void) data_size;
1590 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001592 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001593}
Ronald Crond18b5f82020-11-25 16:46:05 +01001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1596 uint8_t *data,
1597 size_t data_size,
1598 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001599{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001601 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001602 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001603 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001604
Ronald Crone907e552021-01-18 13:22:38 +01001605 /* Reject a zero-length output buffer now, since this can never be a
1606 * valid key representation. This way we know that data must be a valid
1607 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if (data_size == 0) {
1609 return PSA_ERROR_BUFFER_TOO_SMALL;
1610 }
Ronald Crone907e552021-01-18 13:22:38 +01001611
Darryl Greendd8fb772018-11-07 16:00:44 +00001612 /* Set the key to empty now, so that even when there are errors, we always
1613 * set data_length to a value between 0 and data_size. On error, setting
1614 * the key to empty is a good choice because an empty key representation is
1615 * unlikely to be accepted anywhere. */
1616 *data_length = 0;
1617
1618 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1620 if (status != PSA_SUCCESS) {
1621 return status;
1622 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1625 status = PSA_ERROR_INVALID_ARGUMENT;
1626 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001627 }
1628
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001629 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001630 .core = slot->attr
1631 };
Ronald Cron67227982020-11-26 15:16:05 +01001632 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001633 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001635
1636exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001640}
1641
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001642MBEDTLS_STATIC_ASSERT(
1643 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1644 "One or more key attribute flag is listed as both external-only and dual-use")
1645MBEDTLS_STATIC_ASSERT(
1646 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1647 "One or more key attribute flag is listed as both internal-only and dual-use")
1648MBEDTLS_STATIC_ASSERT(
1649 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1650 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001651
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001652/** Validate that a key policy is internally well-formed.
1653 *
1654 * This function only rejects invalid policies. It does not validate the
1655 * consistency of the policy with respect to other attributes of the key
1656 * such as the key type.
1657 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001658static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001659{
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1661 PSA_KEY_USAGE_COPY |
1662 PSA_KEY_USAGE_ENCRYPT |
1663 PSA_KEY_USAGE_DECRYPT |
1664 PSA_KEY_USAGE_SIGN_MESSAGE |
1665 PSA_KEY_USAGE_VERIFY_MESSAGE |
1666 PSA_KEY_USAGE_SIGN_HASH |
1667 PSA_KEY_USAGE_VERIFY_HASH |
1668 PSA_KEY_USAGE_VERIFY_DERIVATION |
1669 PSA_KEY_USAGE_DERIVE)) != 0) {
1670 return PSA_ERROR_INVALID_ARGUMENT;
1671 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001674}
1675
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001676/** Validate the internal consistency of key attributes.
1677 *
1678 * This function only rejects invalid attribute values. If does not
1679 * validate the consistency of the attributes with any key data that may
1680 * be involved in the creation of the key.
1681 *
1682 * Call this function early in the key creation process.
1683 *
1684 * \param[in] attributes Key attributes for the new key.
1685 * \param[out] p_drv On any return, the driver for the key, if any.
1686 * NULL for a transparent key.
1687 *
1688 */
1689static psa_status_t psa_validate_key_attributes(
1690 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001692{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001693 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1695 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 status = psa_validate_key_location(lifetime, p_drv);
1698 if (status != PSA_SUCCESS) {
1699 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001700 }
1701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 status = psa_validate_key_persistence(lifetime);
1703 if (status != PSA_SUCCESS) {
1704 return status;
1705 }
1706
1707 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1708 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1709 return PSA_ERROR_INVALID_ARGUMENT;
1710 }
1711 } else {
1712 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1713 return PSA_ERROR_INVALID_ARGUMENT;
1714 }
1715 }
1716
1717 status = psa_validate_key_policy(&attributes->core.policy);
1718 if (status != PSA_SUCCESS) {
1719 return status;
1720 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001721
1722 /* Refuse to create overly large keys.
1723 * Note that this doesn't trigger on import if the attributes don't
1724 * explicitly specify a size (so psa_get_key_bits returns 0), so
1725 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1727 return PSA_ERROR_NOT_SUPPORTED;
1728 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001729
Gilles Peskine91e8c332019-08-02 19:19:39 +02001730 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1732 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1733 return PSA_ERROR_INVALID_ARGUMENT;
1734 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001737}
1738
Gilles Peskine4747d192019-04-17 15:05:45 +02001739/** Prepare a key slot to receive key material.
1740 *
1741 * This function allocates a key slot and sets its metadata.
1742 *
1743 * If this function fails, call psa_fail_key_creation().
1744 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001745 * This function is intended to be used as follows:
1746 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001747 * it with the specified attributes, and in case of a volatile key assign it
1748 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001749 * -# Populate the slot with the key material.
1750 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1751 * In case of failure at any step, stop the sequence and call
1752 * psa_fail_key_creation().
1753 *
Ronald Cron5c522922020-11-14 16:35:34 +01001754 * On success, the key slot is locked. It is the responsibility of the caller
1755 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001756 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001757 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001758 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001759 * \param[out] p_slot On success, a pointer to the prepared slot.
1760 * \param[out] p_drv On any return, the driver for the key, if any.
1761 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001762 *
1763 * \retval #PSA_SUCCESS
1764 * The key slot is ready to receive key material.
1765 * \return If this function fails, the key slot is an invalid state.
1766 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001767 */
1768static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001769 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001771 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001773{
1774 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001775 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 psa_key_slot_t *slot;
1777
Gilles Peskinedf179142019-07-15 22:02:14 +02001778 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001779 *p_drv = NULL;
1780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 status = psa_validate_key_attributes(attributes, p_drv);
1782 if (status != PSA_SUCCESS) {
1783 return status;
1784 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1787 if (status != PSA_SUCCESS) {
1788 return status;
1789 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001790 slot = *p_slot;
1791
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001792 /* We're storing the declared bit-size of the key. It's up to each
1793 * creation mechanism to verify that this information is correct.
1794 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001795 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001796 * is optional (import, copy). In case of a volatile key, assign it the
1797 * volatile key identifier associated to the slot returned to contain its
1798 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001799
1800 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001802#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1803 slot->attr.id = volatile_key_id;
1804#else
1805 slot->attr.id.key_id = volatile_key_id;
1806#endif
1807 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001808
Gilles Peskine91e8c332019-08-02 19:19:39 +02001809 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001810 * external-only flags, query `attributes`. Thanks to the check
1811 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001812 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001813 * may have set. */
1814 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001815
Gilles Peskinecbaff462019-07-12 23:46:04 +02001816#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001817 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001818 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001819 * create the key file in internal storage, create the
1820 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001821 * persistent data. This is done by starting a transaction that will
1822 * encompass these three actions.
1823 * For registering a volatile key, we just need to find an appropriate
1824 * slot number inside the SE. Since the key is designated volatile, creating
1825 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001826 /* The first thing to do is to find a slot number for the new key.
1827 * We save the slot number in persistent storage as part of the
1828 * transaction data. It will be needed to recover if the power
1829 * fails during the key creation process, to clean up on the secure
1830 * element side after restarting. Obtaining a slot number from the
1831 * secure element driver updates its persistent state, but we do not yet
1832 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001833 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001835 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1837 &slot_number);
1838 if (status != PSA_SUCCESS) {
1839 return status;
1840 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1843 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001844 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001845 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001846 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 status = psa_crypto_save_transaction();
1848 if (status != PSA_SUCCESS) {
1849 (void) psa_crypto_stop_transaction();
1850 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001851 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001852 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001853
1854 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001856 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001857
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001859 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001861 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001862#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001865}
Gilles Peskine4747d192019-04-17 15:05:45 +02001866
1867/** Finalize the creation of a key once its key material has been set.
1868 *
1869 * This entails writing the key to persistent storage.
1870 *
1871 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001872 * See the documentation of psa_start_key_creation() for the intended use
1873 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001874 *
Ronald Cron5c522922020-11-14 16:35:34 +01001875 * If the finalization succeeds, the function unlocks the key slot (it was
1876 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1877 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001878 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001879 * \param[in,out] slot Pointer to the slot with key material.
1880 * \param[in] driver The secure element driver for the key,
1881 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001882 * \param[out] key On success, identifier of the key. Note that the
1883 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001884 *
1885 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001886 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001887 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1888 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1889 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1890 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1891 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1892 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001893 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001894 * \return If this function fails, the key slot is an invalid state.
1895 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001896 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001897static psa_status_t psa_finish_key_creation(
1898 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001899 psa_se_drv_table_entry_t *driver,
1900 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001901{
1902 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001903 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001904 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001905
1906#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001908#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001910 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001911 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001913
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001914 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1915 sizeof(data.slot_number),
1916 "Slot number size does not match psa_se_key_data_storage_t");
1917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1919 status = psa_save_persistent_key(&slot->attr,
1920 (uint8_t *) &data,
1921 sizeof(data));
1922 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001923#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1924 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001925 /* Key material is saved in export representation in the slot, so
1926 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 status = psa_save_persistent_key(&slot->attr,
1928 slot->key.data,
1929 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001930 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001931 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001932#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1933
Gilles Peskinecbaff462019-07-12 23:46:04 +02001934#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001935 /* Finish the transaction for a key creation. This does not
1936 * happen when registering an existing key. Detect this case
1937 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001938 * creation of a persistent key in a secure element requires a transaction,
1939 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (driver != NULL &&
1941 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1942 status = psa_save_se_persistent_data(driver);
1943 if (status != PSA_SUCCESS) {
1944 psa_destroy_persistent_key(slot->attr.id);
1945 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001946 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001948 }
1949#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001952 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 status = psa_unlock_key_slot(slot);
1954 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001955 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001957 }
Ronald Cron50972942020-11-14 11:28:25 +01001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001960}
1961
1962/** Abort the creation of a key.
1963 *
1964 * You may call this function after calling psa_start_key_creation(),
1965 * or after psa_finish_key_creation() fails. In other circumstances, this
1966 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001967 * See the documentation of psa_start_key_creation() for the intended use
1968 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001969 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001970 * \param[in,out] slot Pointer to the slot with key material.
1971 * \param[in] driver The secure element driver for the key,
1972 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001973 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001974static void psa_fail_key_creation(psa_key_slot_t *slot,
1975 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001976{
Gilles Peskine011e4282019-06-26 18:34:38 +02001977 (void) driver;
1978
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001980 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001982
1983#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001984 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001985 * element, and the failure happened later (when saving metadata
1986 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001987 * element.
1988 * https://github.com/ARMmbed/mbed-crypto/issues/217
1989 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001990
Gilles Peskined7729582019-08-05 15:55:54 +02001991 /* Abort the ongoing transaction if any (there may not be one if
1992 * the creation process failed before starting one, or if the
1993 * key creation is a registration of a key in a secure element).
1994 * Earlier functions must already have done what it takes to undo any
1995 * partial creation. All that's left is to update the transaction data
1996 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001998#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002001}
2002
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002003/** Validate optional attributes during key creation.
2004 *
2005 * Some key attributes are optional during key creation. If they are
2006 * specified in the attributes structure, check that they are consistent
2007 * with the data in the slot.
2008 *
2009 * This function should be called near the end of key creation, after
2010 * the slot in memory is fully populated but before saving persistent data.
2011 */
2012static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002013 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015{
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 if (attributes->core.type != 0) {
2017 if (attributes->core.type != slot->attr.type) {
2018 return PSA_ERROR_INVALID_ARGUMENT;
2019 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020 }
2021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002023#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2024 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2026 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002027 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002028 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002030
Ronald Cron90857082020-11-25 14:58:33 +01002031 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 slot->attr.type,
2033 slot->key.data,
2034 slot->key.bytes,
2035 &rsa);
2036 if (status != PSA_SUCCESS) {
2037 return status;
2038 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 mbedtls_mpi_init(&actual);
2041 mbedtls_mpi_init(&required);
2042 ret = mbedtls_rsa_export(rsa,
2043 NULL, NULL, NULL, NULL, &actual);
2044 mbedtls_rsa_free(rsa);
2045 mbedtls_free(rsa);
2046 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002047 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 }
2049 ret = mbedtls_mpi_read_binary(&required,
2050 attributes->domain_parameters,
2051 attributes->domain_parameters_size);
2052 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002053 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 }
2055 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002056 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 }
2058rsa_exit:
2059 mbedtls_mpi_free(&actual);
2060 mbedtls_mpi_free(&required);
2061 if (ret != 0) {
2062 return mbedtls_to_psa_error(ret);
2063 }
2064 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002065#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2066 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002067 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002068 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002070 }
2071 }
2072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 if (attributes->core.bits != 0) {
2074 if (attributes->core.bits != slot->attr.bits) {
2075 return PSA_ERROR_INVALID_ARGUMENT;
2076 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002077 }
2078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002080}
2081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2083 const uint8_t *data,
2084 size_t data_length,
2085 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002086{
2087 psa_status_t status;
2088 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002089 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002090 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302091 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002092
Ronald Cron81709fc2020-11-14 12:10:32 +01002093 *key = MBEDTLS_SVC_KEY_ID_INIT;
2094
Gilles Peskine0f84d622019-09-12 19:03:13 +02002095 /* Reject zero-length symmetric keys (including raw data key objects).
2096 * This also rejects any key which might be encoded as an empty string,
2097 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (data_length == 0) {
2099 return PSA_ERROR_INVALID_ARGUMENT;
2100 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002101
Archana449608b2021-09-08 15:36:05 +05302102 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 if (data_length > SIZE_MAX / 8) {
2104 return PSA_ERROR_NOT_SUPPORTED;
2105 }
Archana6ed4bda2021-08-04 10:47:15 +05302106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2108 &slot, &driver);
2109 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002112
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002113 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302114 * storage ( thus not in the case of importing a key in a secure element
2115 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2116 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (slot->key.data == NULL) {
2118 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302119 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 attributes, data, data_length, &storage_size);
2121 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302122 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 }
Archanad8a83dc2021-06-14 10:04:16 +05302124 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 status = psa_allocate_buffer_to_slot(slot, storage_size);
2126 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002129 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002130
2131 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 status = psa_driver_wrapper_import_key(attributes,
2133 data, data_length,
2134 slot->key.data,
2135 slot->key.bytes,
2136 &slot->key.bytes, &bits);
2137 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002142 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002144 status = PSA_ERROR_INVALID_ARGUMENT;
2145 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002146 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002147
Archana6ed4bda2021-08-04 10:47:15 +05302148 /* Enforce a size limit, and in particular ensure that the bit
2149 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302151 status = PSA_ERROR_NOT_SUPPORTED;
2152 goto exit;
2153 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 status = psa_validate_optional_attributes(slot, attributes);
2155 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002156 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002160exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 if (status != PSA_SUCCESS) {
2162 psa_fail_key_creation(slot, driver);
2163 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002166}
2167
Gilles Peskined7729582019-08-05 15:55:54 +02002168#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2169psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002171{
2172 psa_status_t status;
2173 psa_key_slot_t *slot = NULL;
2174 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002175 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002176
2177 /* Leaving attributes unspecified is not currently supported.
2178 * It could make sense to query the key type and size from the
2179 * secure element, but not all secure elements support this
2180 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2182 return PSA_ERROR_NOT_SUPPORTED;
2183 }
2184 if (psa_get_key_bits(attributes) == 0) {
2185 return PSA_ERROR_NOT_SUPPORTED;
2186 }
Gilles Peskined7729582019-08-05 15:55:54 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2189 &slot, &driver);
2190 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002191 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 }
Gilles Peskined7729582019-08-05 15:55:54 +02002193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002195
2196exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (status != PSA_SUCCESS) {
2198 psa_fail_key_creation(slot, driver);
2199 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002200
Gilles Peskined7729582019-08-05 15:55:54 +02002201 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 psa_close_key(key);
2203 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002204}
2205#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2208 const psa_key_attributes_t *specified_attributes,
2209 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002210{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002211 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002212 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002213 psa_key_slot_t *source_slot = NULL;
2214 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002215 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002216 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302217 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002218
Ronald Cron81709fc2020-11-14 12:10:32 +01002219 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2220
Archana8a180362021-07-05 02:18:48 +05302221 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2223 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002224 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 status = psa_validate_optional_attributes(source_slot,
2228 specified_attributes);
2229 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002232
Archana9d17bf42021-09-10 06:22:44 +05302233 /* The target key type and number of bits have been validated by
2234 * psa_validate_optional_attributes() to be either equal to zero or
2235 * equal to the ones of the source key. So it is safe to inherit
2236 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302237 * */
Archana9d17bf42021-09-10 06:22:44 +05302238 actual_attributes.core.bits = source_slot->attr.bits;
2239 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302240
2241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 status = psa_restrict_key_policy(source_slot->attr.type,
2243 &actual_attributes.core.policy,
2244 &source_slot->attr.policy);
2245 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002246 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2250 &target_slot, &driver);
2251 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002252 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 }
2254 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2255 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302256 /*
Archana9d17bf42021-09-10 06:22:44 +05302257 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302258 * the source key would need to be exported as plaintext and re-imported
2259 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302260 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302261 * appropriate API invocations from the application, if needed.
2262 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002263 status = PSA_ERROR_NOT_SUPPORTED;
2264 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002265 }
Archana8a180362021-07-05 02:18:48 +05302266 /*
2267 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302268 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302269 * - For opaque keys this translates to an invocation of the drivers'
2270 * copy_key entry point through the dispatch layer.
2271 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2273 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2274 &storage_size);
2275 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302276 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 }
Archana374fe5b2021-09-08 15:50:28 +05302278
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2280 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302281 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 }
Archana374fe5b2021-09-08 15:50:28 +05302283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 status = psa_driver_wrapper_copy_key(&actual_attributes,
2285 source_slot->key.data,
2286 source_slot->key.bytes,
2287 target_slot->key.data,
2288 target_slot->key.bytes,
2289 &target_slot->key.bytes);
2290 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302291 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 }
2293 } else {
2294 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302295 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 source_slot->key.bytes);
2297 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302298 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 }
Archana8a180362021-07-05 02:18:48 +05302300 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (status != PSA_SUCCESS) {
2304 psa_fail_key_creation(target_slot, driver);
2305 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002310}
2311
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002312
2313
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002314/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002315/* Message digests */
2316/****************************************************************/
2317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002320 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 if (operation->id == 0) {
2322 return PSA_SUCCESS;
2323 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002326 operation->id = 0;
2327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329}
2330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2332 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002334 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002335
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002336 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002338 status = PSA_ERROR_BAD_STATE;
2339 goto exit;
2340 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002343 status = PSA_ERROR_INVALID_ARGUMENT;
2344 goto exit;
2345 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002346
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002347 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2348 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002352
2353exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if (status != PSA_SUCCESS) {
2355 psa_hash_abort(operation);
2356 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002357
2358 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002359}
2360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2362 const uint8_t *input,
2363 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002368 status = PSA_ERROR_BAD_STATE;
2369 goto exit;
2370 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002371
Steven Cooremanc8288352021-03-04 14:02:19 +01002372 /* Don't require hash implementations to behave correctly on a
2373 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (input_length == 0) {
2375 return PSA_SUCCESS;
2376 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002379
2380exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (status != PSA_SUCCESS) {
2382 psa_hash_abort(operation);
2383 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386}
2387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2389 uint8_t *hash,
2390 size_t hash_size,
2391 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002392{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002393 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 if (operation->id == 0) {
2395 return PSA_ERROR_BAD_STATE;
2396 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002397
Steven Cooreman1e582352021-02-18 17:24:37 +01002398 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 operation, hash, hash_size, hash_length);
2400 psa_hash_abort(operation);
2401 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002402}
2403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2405 const uint8_t *hash,
2406 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002407{
Ronald Cron69a63422021-10-18 09:47:58 +02002408 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002409 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002410 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 operation,
2412 actual_hash, sizeof(actual_hash),
2413 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002416 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002420 status = PSA_ERROR_INVALID_SIGNATURE;
2421 goto exit;
2422 }
2423
Dave Rodgman78701152023-08-29 14:20:18 +01002424 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002425 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002427
2428exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2430 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002431 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002435}
2436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437psa_status_t psa_hash_compute(psa_algorithm_t alg,
2438 const uint8_t *input, size_t input_length,
2439 uint8_t *hash, size_t hash_size,
2440 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002441{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002442 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 if (!PSA_ALG_IS_HASH(alg)) {
2444 return PSA_ERROR_INVALID_ARGUMENT;
2445 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2448 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002449}
2450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451psa_status_t psa_hash_compare(psa_algorithm_t alg,
2452 const uint8_t *input, size_t input_length,
2453 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002454{
Ronald Cron69a63422021-10-18 09:47:58 +02002455 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002456 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 if (!PSA_ALG_IS_HASH(alg)) {
2459 return PSA_ERROR_INVALID_ARGUMENT;
2460 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002461
Steven Cooreman1e582352021-02-18 17:24:37 +01002462 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 alg, input, input_length,
2464 actual_hash, sizeof(actual_hash),
2465 &actual_hash_length);
2466 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002467 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 }
2469 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002470 status = PSA_ERROR_INVALID_SIGNATURE;
2471 goto exit;
2472 }
Dave Rodgman78701152023-08-29 14:20:18 +01002473 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002474 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002476
2477exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2479 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002480}
2481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2483 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002484{
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (source_operation->id == 0 ||
2486 target_operation->id != 0) {
2487 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002488 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2491 target_operation);
2492 if (status != PSA_SUCCESS) {
2493 psa_hash_abort(target_operation);
2494 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002497}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002498
2499
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002500/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501/* MAC */
2502/****************************************************************/
2503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002505{
Steven Cooremane6804192021-03-19 18:28:56 +01002506 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 if (operation->id == 0) {
2508 return PSA_SUCCESS;
2509 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002512 operation->mac_size = 0;
2513 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002514 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002515
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002517}
2518
Ronald Cron2dff3b22021-06-17 16:33:22 +02002519static psa_status_t psa_mac_finalize_alg_and_key_validation(
2520 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002521 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002524 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 psa_key_type_t key_type = psa_get_key_type(attributes);
2526 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 if (!PSA_ALG_IS_MAC(alg)) {
2529 return PSA_ERROR_INVALID_ARGUMENT;
2530 }
Steven Cooremane6804192021-03-19 18:28:56 +01002531
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002532 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 status = psa_mac_key_can_do(alg, key_type);
2534 if (status != PSA_SUCCESS) {
2535 return status;
2536 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002537
Steven Cooremand1a68f12021-05-10 11:13:41 +02002538 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002542 /* A very short MAC is too short for security since it can be
2543 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2544 * so we make this our minimum, even though 32 bits is still
2545 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002547 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2550 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002551 /* It's impossible to "truncate" to a larger length than the full length
2552 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002554 }
2555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002557 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2558 * that is disabled in the compile-time configuration. The result can
2559 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2560 * configuration into account. In this case, force a return of
2561 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2562 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2563 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2564 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2565 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002567 }
2568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002570}
2571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2573 mbedtls_svc_key_id_t key,
2574 psa_algorithm_t alg,
2575 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002576{
2577 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2578 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002579 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002580 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581
2582 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002584 status = PSA_ERROR_BAD_STATE;
2585 goto exit;
2586 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587
2588 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 key,
2590 &slot,
2591 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2592 alg);
2593 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002594 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002596
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002597 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598 .core = slot->attr
2599 };
2600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2602 &operation->mac_size);
2603 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002604 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606
2607 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002608 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (is_sign) {
2610 status = psa_driver_wrapper_mac_sign_setup(operation,
2611 &attributes,
2612 slot->key.data,
2613 slot->key.bytes,
2614 alg);
2615 } else {
2616 status = psa_driver_wrapper_mac_verify_setup(operation,
2617 &attributes,
2618 slot->key.data,
2619 slot->key.bytes,
2620 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002622
Gilles Peskinefbfac682018-07-08 20:51:54 +02002623exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (status != PSA_SUCCESS) {
2625 psa_mac_abort(operation);
2626 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631}
2632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2634 mbedtls_svc_key_id_t key,
2635 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002636{
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002638}
2639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2641 mbedtls_svc_key_id_t key,
2642 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002643{
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002645}
2646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2648 const uint8_t *input,
2649 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002650{
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if (operation->id == 0) {
2652 return PSA_ERROR_BAD_STATE;
2653 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002654
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002655 /* Don't require hash implementations to behave correctly on a
2656 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 if (input_length == 0) {
2658 return PSA_SUCCESS;
2659 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2662 input, input_length);
2663 if (status != PSA_SUCCESS) {
2664 psa_mac_abort(operation);
2665 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002668}
2669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2671 uint8_t *mac,
2672 size_t mac_size,
2673 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002674{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002675 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2676 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002679 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002680 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002681 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002684 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002685 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002686 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002687
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002688 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2689 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002691 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002692 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002693 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002696 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002697 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002698 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 status = psa_driver_wrapper_mac_sign_finish(operation,
2701 mac, operation->mac_size,
2702 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002703
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002704exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002705 /* In case of success, set the potential excess room in the output buffer
2706 * to an invalid value, to avoid potentially leaking a longer MAC.
2707 * In case of error, set the output length and content to a safe default,
2708 * such that in case the caller misses an error check, the output would be
2709 * an unachievable MAC.
2710 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002712 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002713 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002714 }
mohammad16036df908f2018-04-02 08:34:15 -07002715
Paul Elliottdc42ca82023-02-24 18:11:59 +00002716 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002721}
2722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2724 const uint8_t *mac,
2725 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002726{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002727 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2728 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002731 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002732 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002733 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002736 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002737 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002738 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002741 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002742 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002743 }
2744
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 status = psa_driver_wrapper_mac_verify_finish(operation,
2746 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002747
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002748exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752}
2753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2755 psa_algorithm_t alg,
2756 const uint8_t *input,
2757 size_t input_length,
2758 uint8_t *mac,
2759 size_t mac_size,
2760 size_t *mac_length,
2761 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002762{
2763 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002764 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2765 psa_key_slot_t *slot;
2766 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002767 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002768
Ronald Cron51131b52021-06-17 17:17:20 +02002769 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 key,
2771 &slot,
2772 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2773 alg);
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
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002778 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002779 .core = slot->attr
2780 };
2781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2783 &operation_mac_size);
2784 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002785 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002787
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002789 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002790 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002791 }
2792
2793 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 &attributes,
2795 slot->key.data, slot->key.bytes,
2796 alg,
2797 input, input_length,
2798 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002799
2800exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002801 /* In case of success, set the potential excess room in the output buffer
2802 * to an invalid value, to avoid potentially leaking a longer MAC.
2803 * In case of error, set the output length and content to a safe default,
2804 * such that in case the caller misses an error check, the output would be
2805 * an unachievable MAC.
2806 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002808 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002809 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002810 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002811
2812 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002817}
2818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002820 psa_algorithm_t alg,
2821 const uint8_t *input,
2822 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 uint8_t *mac,
2824 size_t mac_size,
2825 size_t *mac_length)
2826{
2827 return psa_mac_compute_internal(key, alg,
2828 input, input_length,
2829 mac, mac_size, mac_length, 1);
2830}
2831
2832psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2833 psa_algorithm_t alg,
2834 const uint8_t *input,
2835 size_t input_length,
2836 const uint8_t *mac,
2837 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002838{
2839 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002840 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2841 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 status = psa_mac_compute_internal(key, alg,
2844 input, input_length,
2845 actual_mac, sizeof(actual_mac),
2846 &actual_mac_length, 0);
2847 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002848 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002852 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002853 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002854 }
Dave Rodgman78701152023-08-29 14:20:18 +01002855 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002856 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002857 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002858 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002859
2860exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002862
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002864}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002865
Gilles Peskinee59236f2018-01-27 23:32:46 +01002866/****************************************************************/
2867/* Asymmetric cryptography */
2868/****************************************************************/
2869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2871 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002872{
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 if (input_is_message) {
2874 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2875 return PSA_ERROR_INVALID_ARGUMENT;
2876 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2879 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2880 return PSA_ERROR_INVALID_ARGUMENT;
2881 }
2882 }
2883 } else {
2884 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2885 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002886 }
2887 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002888
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002890}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002891
Gilles Peskine449bd832023-01-11 14:50:10 +01002892static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2893 int input_is_message,
2894 psa_algorithm_t alg,
2895 const uint8_t *input,
2896 size_t input_length,
2897 uint8_t *signature,
2898 size_t signature_size,
2899 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002900{
2901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2902 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2903 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002904 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002905
2906 *signature_length = 0;
2907
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 status = psa_sign_verify_check_alg(input_is_message, alg);
2909 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002910 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002912
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002913 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002914 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002915 * buffer can in principle be empty since it doesn't actually have
2916 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 if (signature_size == 0) {
2918 return PSA_ERROR_BUFFER_TOO_SMALL;
2919 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002920
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002921 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 key, &slot,
2923 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2924 PSA_KEY_USAGE_SIGN_HASH,
2925 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002928 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002932 status = PSA_ERROR_INVALID_ARGUMENT;
2933 goto exit;
2934 }
2935
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002936 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002938 };
2939
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002941 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002942 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002943 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 signature, signature_size, signature_length);
2945 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002946
2947 status = psa_driver_wrapper_sign_hash(
2948 &attributes, slot->key.data, slot->key.bytes,
2949 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002951 }
2952
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002953
2954exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002955 psa_wipe_tag_output_buffer(signature, status, signature_size,
2956 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002961}
2962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2964 int input_is_message,
2965 psa_algorithm_t alg,
2966 const uint8_t *input,
2967 size_t input_length,
2968 const uint8_t *signature,
2969 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002970{
2971 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2972 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2973 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 status = psa_sign_verify_check_alg(input_is_message, alg);
2976 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002977 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002979
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002980 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 key, &slot,
2982 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2983 PSA_KEY_USAGE_VERIFY_HASH,
2984 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002985
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 if (status != PSA_SUCCESS) {
2987 return status;
2988 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002989
2990 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002992 };
2993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002995 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002996 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002997 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 signature, signature_length);
2999 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003000 status = psa_driver_wrapper_verify_hash(
3001 &attributes, slot->key.data, slot->key.bytes,
3002 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003004 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003009
3010}
3011
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003012psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003013 const psa_key_attributes_t *attributes,
3014 const uint8_t *key_buffer,
3015 size_t key_buffer_size,
3016 psa_algorithm_t alg,
3017 const uint8_t *input,
3018 size_t input_length,
3019 uint8_t *signature,
3020 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003022{
3023 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003026 size_t hash_length;
3027 uint8_t hash[PSA_HASH_MAX_SIZE];
3028
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003029 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 PSA_ALG_SIGN_GET_HASH(alg),
3031 input, input_length,
3032 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003035 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003037
3038 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 attributes, key_buffer, key_buffer_size,
3040 alg, hash, hash_length,
3041 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003042 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003045}
3046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3048 psa_algorithm_t alg,
3049 const uint8_t *input,
3050 size_t input_length,
3051 uint8_t *signature,
3052 size_t signature_size,
3053 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003054{
3055 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003056 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003058}
3059
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003060psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003061 const psa_key_attributes_t *attributes,
3062 const uint8_t *key_buffer,
3063 size_t key_buffer_size,
3064 psa_algorithm_t alg,
3065 const uint8_t *input,
3066 size_t input_length,
3067 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003069{
3070 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003073 size_t hash_length;
3074 uint8_t hash[PSA_HASH_MAX_SIZE];
3075
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003076 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 PSA_ALG_SIGN_GET_HASH(alg),
3078 input, input_length,
3079 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003082 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003084
3085 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 attributes, key_buffer, key_buffer_size,
3087 alg, hash, hash_length,
3088 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003089 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003092}
3093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3095 psa_algorithm_t alg,
3096 const uint8_t *input,
3097 size_t input_length,
3098 const uint8_t *signature,
3099 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003100{
3101 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003102 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003104}
3105
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003106psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003107 const psa_key_attributes_t *attributes,
3108 const uint8_t *key_buffer, size_t key_buffer_size,
3109 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003111{
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3113 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3114 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003115#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3117 return mbedtls_psa_rsa_sign_hash(
3118 attributes,
3119 key_buffer, key_buffer_size,
3120 alg, hash, hash_length,
3121 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003122#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3123 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 } else {
3125 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003126 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3128 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003129#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3131 return mbedtls_psa_ecdsa_sign_hash(
3132 attributes,
3133 key_buffer, key_buffer_size,
3134 alg, hash, hash_length,
3135 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003136#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3137 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 } else {
3139 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003140 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003141 }
Ronald Cron4501c982021-03-19 20:09:32 +01003142
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 (void) key_buffer;
3144 (void) key_buffer_size;
3145 (void) hash;
3146 (void) hash_length;
3147 (void) signature;
3148 (void) signature_size;
3149 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003152}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003153
Gilles Peskine449bd832023-01-11 14:50:10 +01003154psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3155 psa_algorithm_t alg,
3156 const uint8_t *hash,
3157 size_t hash_length,
3158 uint8_t *signature,
3159 size_t signature_size,
3160 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003161{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003162 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003163 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003165}
3166
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003167psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003168 const psa_key_attributes_t *attributes,
3169 const uint8_t *key_buffer, size_t key_buffer_size,
3170 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003172{
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3174 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3175 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003176#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3178 return mbedtls_psa_rsa_verify_hash(
3179 attributes,
3180 key_buffer, key_buffer_size,
3181 alg, hash, hash_length,
3182 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003183#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3184 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 } else {
3186 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003187 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3189 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003190#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3192 return mbedtls_psa_ecdsa_verify_hash(
3193 attributes,
3194 key_buffer, key_buffer_size,
3195 alg, hash, hash_length,
3196 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003197#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3198 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 } else {
3200 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003201 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003202 }
Ronald Cron4501c982021-03-19 20:09:32 +01003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 (void) key_buffer;
3205 (void) key_buffer_size;
3206 (void) hash;
3207 (void) hash_length;
3208 (void) signature;
3209 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003210
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003212}
3213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3215 psa_algorithm_t alg,
3216 const uint8_t *hash,
3217 size_t hash_length,
3218 const uint8_t *signature,
3219 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003220{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003221 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003222 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003224}
3225
Gilles Peskine449bd832023-01-11 14:50:10 +01003226psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3227 psa_algorithm_t alg,
3228 const uint8_t *input,
3229 size_t input_length,
3230 const uint8_t *salt,
3231 size_t salt_length,
3232 uint8_t *output,
3233 size_t output_size,
3234 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003235{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003236 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003237 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003238 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003239 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003240
Darryl Green5cc689a2018-07-24 15:34:10 +01003241 (void) input;
3242 (void) input_length;
3243 (void) salt;
3244 (void) output;
3245 (void) output_size;
3246
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003247 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003248
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3250 return PSA_ERROR_INVALID_ARGUMENT;
3251 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003252
Ronald Cron5c522922020-11-14 16:35:34 +01003253 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3255 if (status != PSA_SUCCESS) {
3256 return status;
3257 }
3258 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3259 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003260 status = PSA_ERROR_INVALID_ARGUMENT;
3261 goto exit;
3262 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003263
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003264 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003266 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003267
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003268 status = psa_driver_wrapper_asymmetric_encrypt(
3269 &attributes, slot->key.data, slot->key.bytes,
3270 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003272exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003274
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003276}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3279 psa_algorithm_t alg,
3280 const uint8_t *input,
3281 size_t input_length,
3282 const uint8_t *salt,
3283 size_t salt_length,
3284 uint8_t *output,
3285 size_t output_size,
3286 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003287{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003288 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003289 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003290 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003291 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003292
Darryl Green5cc689a2018-07-24 15:34:10 +01003293 (void) input;
3294 (void) input_length;
3295 (void) salt;
3296 (void) output;
3297 (void) output_size;
3298
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003299 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003300
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3302 return PSA_ERROR_INVALID_ARGUMENT;
3303 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003304
Ronald Cron5c522922020-11-14 16:35:34 +01003305 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3307 if (status != PSA_SUCCESS) {
3308 return status;
3309 }
3310 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003311 status = PSA_ERROR_INVALID_ARGUMENT;
3312 goto exit;
3313 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003314
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003315 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003317 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003318
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003319 status = psa_driver_wrapper_asymmetric_decrypt(
3320 &attributes, slot->key.data, slot->key.bytes,
3321 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003323
3324exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003326
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003328}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003329
Paul Elliott9fe12f62022-11-30 19:16:02 +00003330/****************************************************************/
3331/* Asymmetric interruptible cryptography */
3332/****************************************************************/
3333
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003334static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3335
Paul Elliott9fe12f62022-11-30 19:16:02 +00003336void psa_interruptible_set_max_ops(uint32_t max_ops)
3337{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003338 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003339}
3340
3341uint32_t psa_interruptible_get_max_ops(void)
3342{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003343 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003344}
3345
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346uint32_t psa_sign_hash_get_num_ops(
3347 const psa_sign_hash_interruptible_operation_t *operation)
3348{
Paul Elliott296ede92022-12-15 17:00:30 +00003349 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003350}
3351
3352uint32_t psa_verify_hash_get_num_ops(
3353 const psa_verify_hash_interruptible_operation_t *operation)
3354{
Paul Elliott296ede92022-12-15 17:00:30 +00003355 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003356}
3357
Paul Elliott59ad9452022-12-18 15:09:02 +00003358static psa_status_t psa_sign_hash_abort_internal(
3359 psa_sign_hash_interruptible_operation_t *operation)
3360{
3361 if (operation->id == 0) {
3362 /* The object has (apparently) been initialized but it is not (yet)
3363 * in use. It's ok to call abort on such an object, and there's
3364 * nothing to do. */
3365 return PSA_SUCCESS;
3366 }
3367
3368 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3369
3370 status = psa_driver_wrapper_sign_hash_abort(operation);
3371
3372 operation->id = 0;
3373
Paul Elliotte6145dc2023-02-07 12:51:21 +00003374 /* Do not clear either the error_occurred or num_ops elements here as they
3375 * only want to be cleared by the application calling abort, not by abort
3376 * being called at completion of an operation. */
3377
Paul Elliott59ad9452022-12-18 15:09:02 +00003378 return status;
3379}
3380
Paul Elliott9fe12f62022-11-30 19:16:02 +00003381psa_status_t psa_sign_hash_start(
3382 psa_sign_hash_interruptible_operation_t *operation,
3383 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3384 const uint8_t *hash, size_t hash_length)
3385{
3386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3387 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3388 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003389 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003390
Paul Elliottc9774412023-02-06 15:14:07 +00003391 /* Check that start has not been previously called, or operation has not
3392 * previously errored. */
3393 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003394 return PSA_ERROR_BAD_STATE;
3395 }
3396
Paul Elliott9fe12f62022-11-30 19:16:02 +00003397 status = psa_sign_verify_check_alg(0, alg);
3398 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003399 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003400 return status;
3401 }
3402
3403 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3404 PSA_KEY_USAGE_SIGN_HASH,
3405 alg);
3406
3407 if (status != PSA_SUCCESS) {
3408 goto exit;
3409 }
3410
3411 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3412 status = PSA_ERROR_INVALID_ARGUMENT;
3413 goto exit;
3414 }
3415
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003416 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417 .core = slot->attr
3418 };
3419
Paul Elliott296ede92022-12-15 17:00:30 +00003420 /* Ensure ops count gets reset, in case of operation re-use. */
3421 operation->num_ops = 0;
3422
Paul Elliott9fe12f62022-11-30 19:16:02 +00003423 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3424 slot->key.data,
3425 slot->key.bytes, alg,
3426 hash, hash_length);
3427exit:
3428
3429 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003430 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003431 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003432 }
3433
3434 unlock_status = psa_unlock_key_slot(slot);
3435
Paul Elliottc9774412023-02-06 15:14:07 +00003436 if (unlock_status != PSA_SUCCESS) {
3437 operation->error_occurred = 1;
3438 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003439
Paul Elliottc9774412023-02-06 15:14:07 +00003440 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003441}
3442
3443
3444psa_status_t psa_sign_hash_complete(
3445 psa_sign_hash_interruptible_operation_t *operation,
3446 uint8_t *signature, size_t signature_size,
3447 size_t *signature_length)
3448{
3449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3450
3451 *signature_length = 0;
3452
Paul Elliottc9774412023-02-06 15:14:07 +00003453 /* Check that start has been called first, and that operation has not
3454 * previously errored. */
3455 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003456 status = PSA_ERROR_BAD_STATE;
3457 goto exit;
3458 }
3459
Paul Elliott096abc42023-02-03 18:33:23 +00003460 /* Immediately reject a zero-length signature buffer. This guarantees that
3461 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003462 if (signature_size == 0) {
3463 status = PSA_ERROR_BUFFER_TOO_SMALL;
3464 goto exit;
3465 }
3466
3467 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3468 signature_size,
3469 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003470
Paul Elliott296ede92022-12-15 17:00:30 +00003471 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003472 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003473
Paul Elliottebe225c2023-02-10 14:32:53 +00003474exit:
3475
Paul Elliott59200a22023-02-21 15:07:40 +00003476 psa_wipe_tag_output_buffer(signature, status, signature_size,
3477 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003478
Paul Elliott53bb3122023-02-10 14:22:22 +00003479 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003480 if (status != PSA_SUCCESS) {
3481 operation->error_occurred = 1;
3482 }
3483
Paul Elliott59ad9452022-12-18 15:09:02 +00003484 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003485 }
3486
3487 return status;
3488}
3489
3490psa_status_t psa_sign_hash_abort(
3491 psa_sign_hash_interruptible_operation_t *operation)
3492{
Paul Elliott59ad9452022-12-18 15:09:02 +00003493 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3494
3495 status = psa_sign_hash_abort_internal(operation);
3496
3497 /* We clear the number of ops done here, so that it is not cleared when
3498 * the operation fails or succeeds, only on manual abort. */
3499 operation->num_ops = 0;
3500
Paul Elliottc9774412023-02-06 15:14:07 +00003501 /* Likewise, failure state. */
3502 operation->error_occurred = 0;
3503
Paul Elliott59ad9452022-12-18 15:09:02 +00003504 return status;
3505}
3506
3507static psa_status_t psa_verify_hash_abort_internal(
3508 psa_verify_hash_interruptible_operation_t *operation)
3509{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003510 if (operation->id == 0) {
3511 /* The object has (apparently) been initialized but it is not (yet)
3512 * in use. It's ok to call abort on such an object, and there's
3513 * nothing to do. */
3514 return PSA_SUCCESS;
3515 }
3516
Paul Elliott59ad9452022-12-18 15:09:02 +00003517 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3518
3519 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003520
3521 operation->id = 0;
3522
Paul Elliotte6145dc2023-02-07 12:51:21 +00003523 /* Do not clear either the error_occurred or num_ops elements here as they
3524 * only want to be cleared by the application calling abort, not by abort
3525 * being called at completion of an operation. */
3526
Paul Elliott59ad9452022-12-18 15:09:02 +00003527 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003528}
3529
3530psa_status_t psa_verify_hash_start(
3531 psa_verify_hash_interruptible_operation_t *operation,
3532 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3533 const uint8_t *hash, size_t hash_length,
3534 const uint8_t *signature, size_t signature_length)
3535{
3536 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3537 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3538 psa_key_slot_t *slot;
3539
Paul Elliottc9774412023-02-06 15:14:07 +00003540 /* Check that start has not been previously called, or operation has not
3541 * previously errored. */
3542 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003543 return PSA_ERROR_BAD_STATE;
3544 }
3545
3546 status = psa_sign_verify_check_alg(0, alg);
3547 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003548 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003549 return status;
3550 }
3551
3552 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3553 PSA_KEY_USAGE_VERIFY_HASH,
3554 alg);
3555
3556 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003557 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003558 return status;
3559 }
3560
3561 psa_key_attributes_t attributes = {
3562 .core = slot->attr
3563 };
3564
Paul Elliott296ede92022-12-15 17:00:30 +00003565 /* Ensure ops count gets reset, in case of operation re-use. */
3566 operation->num_ops = 0;
3567
Paul Elliott9fe12f62022-11-30 19:16:02 +00003568 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3569 slot->key.data,
3570 slot->key.bytes,
3571 alg, hash, hash_length,
3572 signature, signature_length);
3573
3574 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003575 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003576 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003577 }
3578
3579 unlock_status = psa_unlock_key_slot(slot);
3580
Paul Elliottc9774412023-02-06 15:14:07 +00003581 if (unlock_status != PSA_SUCCESS) {
3582 operation->error_occurred = 1;
3583 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003584
Paul Elliottc9774412023-02-06 15:14:07 +00003585 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003586}
3587
3588psa_status_t psa_verify_hash_complete(
3589 psa_verify_hash_interruptible_operation_t *operation)
3590{
3591 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3592
Paul Elliottc9774412023-02-06 15:14:07 +00003593 /* Check that start has been called first, and that operation has not
3594 * previously errored. */
3595 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003596 status = PSA_ERROR_BAD_STATE;
3597 goto exit;
3598 }
3599
3600 status = psa_driver_wrapper_verify_hash_complete(operation);
3601
Paul Elliott296ede92022-12-15 17:00:30 +00003602 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003603 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003604 operation);
3605
Paul Elliottebe225c2023-02-10 14:32:53 +00003606exit:
3607
Paul Elliott9fe12f62022-11-30 19:16:02 +00003608 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003609 if (status != PSA_SUCCESS) {
3610 operation->error_occurred = 1;
3611 }
3612
Paul Elliott59ad9452022-12-18 15:09:02 +00003613 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003614 }
3615
3616 return status;
3617}
3618
3619psa_status_t psa_verify_hash_abort(
3620 psa_verify_hash_interruptible_operation_t *operation)
3621{
Paul Elliott59ad9452022-12-18 15:09:02 +00003622 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003623
Paul Elliott59ad9452022-12-18 15:09:02 +00003624 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003625
Paul Elliott59ad9452022-12-18 15:09:02 +00003626 /* We clear the number of ops done here, so that it is not cleared when
3627 * the operation fails or succeeds, only on manual abort. */
3628 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003629
Paul Elliottc9774412023-02-06 15:14:07 +00003630 /* Likewise, failure state. */
3631 operation->error_occurred = 0;
3632
Paul Elliott59ad9452022-12-18 15:09:02 +00003633 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003634}
3635
Paul Elliott588f8ed2022-12-02 18:10:26 +00003636/****************************************************************/
3637/* Asymmetric interruptible cryptography internal */
3638/* implementations */
3639/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003640
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3642{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003643
Paul Elliott588f8ed2022-12-02 18:10:26 +00003644#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3645 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3646 defined(MBEDTLS_ECP_RESTARTABLE)
3647
3648 /* Internal implementation uses zero to indicate infinite number max ops,
3649 * therefore avoid this value, and set to minimum possible. */
3650 if (max_ops == 0) {
3651 max_ops = 1;
3652 }
3653
Paul Elliott588f8ed2022-12-02 18:10:26 +00003654 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003655#else
3656 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003657#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3658 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3659 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3660}
3661
Paul Elliott588f8ed2022-12-02 18:10:26 +00003662uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003663 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003664{
3665#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3666 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3667 defined(MBEDTLS_ECP_RESTARTABLE)
3668
Paul Elliott93d9ca82023-02-15 18:14:21 +00003669 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003670#else
3671 (void) operation;
3672 return 0;
3673#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3674 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3675 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3676}
3677
3678uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003679 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003680{
3681 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3682 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3683 defined(MBEDTLS_ECP_RESTARTABLE)
3684
Paul Elliott93d9ca82023-02-15 18:14:21 +00003685 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686#else
3687 (void) operation;
3688 return 0;
3689#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3690 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3691 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3692}
3693
3694psa_status_t mbedtls_psa_sign_hash_start(
3695 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3696 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3697 size_t key_buffer_size, psa_algorithm_t alg,
3698 const uint8_t *hash, size_t hash_length)
3699{
3700 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003701 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003702
Paul Elliott068fe072023-01-16 13:59:15 +00003703 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3704 return PSA_ERROR_NOT_SUPPORTED;
3705 }
3706
3707 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003708 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003709 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003710
3711#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003712 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3713 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003714
Paul Elliotta1c94092023-02-15 16:38:04 +00003715 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3716
Paul Elliott93d9ca82023-02-15 18:14:21 +00003717 /* Ensure num_ops is zero'ed in case of context re-use. */
3718 operation->num_ops = 0;
3719
Paul Elliott068fe072023-01-16 13:59:15 +00003720 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3721 attributes->core.bits,
3722 key_buffer,
3723 key_buffer_size,
3724 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725
Paul Elliott068fe072023-01-16 13:59:15 +00003726 if (status != PSA_SUCCESS) {
3727 return status;
3728 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729
Paul Elliott1bc59df2023-02-05 13:41:57 +00003730 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003731 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003732
Paul Elliott068fe072023-01-16 13:59:15 +00003733 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003734 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003735 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003736
Paul Elliott0290a762023-02-09 14:30:24 +00003737 /* We only need to store the same length of hash as the private key size
3738 * here, it would be truncated by the internal implementation anyway. */
3739 required_hash_length = (hash_length < operation->coordinate_bytes ?
3740 hash_length : operation->coordinate_bytes);
3741
Paul Elliottba70ad42023-02-15 18:23:53 +00003742 if (required_hash_length > sizeof(operation->hash)) {
3743 /* Shouldn't happen, but better safe than sorry. */
3744 return PSA_ERROR_CORRUPTION_DETECTED;
3745 }
3746
Paul Elliott0290a762023-02-09 14:30:24 +00003747 memcpy(operation->hash, hash, required_hash_length);
3748 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003749
3750 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003751
3752#else
Paul Elliott068fe072023-01-16 13:59:15 +00003753 (void) operation;
3754 (void) key_buffer;
3755 (void) key_buffer_size;
3756 (void) alg;
3757 (void) hash;
3758 (void) hash_length;
3759 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003760 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003761
Paul Elliott068fe072023-01-16 13:59:15 +00003762 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003763#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3764 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3765 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003766}
3767
3768psa_status_t mbedtls_psa_sign_hash_complete(
3769 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3770 uint8_t *signature, size_t signature_size,
3771 size_t *signature_length)
3772{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3774 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3775 defined(MBEDTLS_ECP_RESTARTABLE)
3776
Paul Elliotta1c94092023-02-15 16:38:04 +00003777 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003778 mbedtls_mpi r;
3779 mbedtls_mpi s;
3780
Paul Elliott46845252023-02-03 14:59:11 +00003781 mbedtls_mpi_init(&r);
3782 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003784 /* Ensure max_ops is set to the current value (or default). */
3785 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3786
Paul Elliotta1c94092023-02-15 16:38:04 +00003787 if (signature_size < 2 * operation->coordinate_bytes) {
3788 status = PSA_ERROR_BUFFER_TOO_SMALL;
3789 goto exit;
3790 }
3791
Paul Elliott588f8ed2022-12-02 18:10:26 +00003792 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003793
Paul Elliott588f8ed2022-12-02 18:10:26 +00003794#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3795 status = mbedtls_to_psa_error(
3796 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003797 &r,
3798 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799 &operation->ctx->d,
3800 operation->hash,
3801 operation->hash_length,
3802 operation->md_alg,
3803 mbedtls_psa_get_random,
3804 MBEDTLS_PSA_RANDOM_STATE,
3805 &operation->restart_ctx));
3806#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003807 status = PSA_ERROR_NOT_SUPPORTED;
3808 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003809#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3810 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003811 status = mbedtls_to_psa_error(
3812 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003813 &r,
3814 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003815 &operation->ctx->d,
3816 operation->hash,
3817 operation->hash_length,
3818 mbedtls_psa_get_random,
3819 MBEDTLS_PSA_RANDOM_STATE,
3820 mbedtls_psa_get_random,
3821 MBEDTLS_PSA_RANDOM_STATE,
3822 &operation->restart_ctx));
3823 }
3824
Paul Elliottf8e5b562023-02-19 18:43:45 +00003825 /* Hide the fact that the restart context only holds a delta of number of
3826 * ops done during the last operation, not an absolute value. */
3827 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3828
Paul Elliott724bd252023-02-08 12:35:08 +00003829 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003830 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003831 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003832 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003833 operation->coordinate_bytes)
3834 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835
3836 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003837 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003838 }
3839
3840 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003841 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003842 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003843 operation->coordinate_bytes,
3844 operation->coordinate_bytes)
3845 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846
3847 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003848 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849 }
3850
Paul Elliott1bc59df2023-02-05 13:41:57 +00003851 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003852
Paul Elliott724bd252023-02-08 12:35:08 +00003853 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854 }
Paul Elliott724bd252023-02-08 12:35:08 +00003855
3856exit:
3857
3858 mbedtls_mpi_free(&r);
3859 mbedtls_mpi_free(&s);
3860 return status;
3861
Paul Elliott588f8ed2022-12-02 18:10:26 +00003862 #else
3863
3864 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003865 (void) signature;
3866 (void) signature_size;
3867 (void) signature_length;
3868
3869 return PSA_ERROR_NOT_SUPPORTED;
3870
3871#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3872 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3873 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3874}
3875
3876psa_status_t mbedtls_psa_sign_hash_abort(
3877 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3878{
3879
3880#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3881 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3882 defined(MBEDTLS_ECP_RESTARTABLE)
3883
3884 if (operation->ctx) {
3885 mbedtls_ecdsa_free(operation->ctx);
3886 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003887 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003888 }
3889
3890 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3891
Paul Elliott93d9ca82023-02-15 18:14:21 +00003892 operation->num_ops = 0;
3893
Paul Elliott588f8ed2022-12-02 18:10:26 +00003894 return PSA_SUCCESS;
3895
3896#else
3897
3898 (void) operation;
3899
3900 return PSA_ERROR_NOT_SUPPORTED;
3901
3902#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3903 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3904 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3905}
3906
3907psa_status_t mbedtls_psa_verify_hash_start(
3908 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3909 const psa_key_attributes_t *attributes,
3910 const uint8_t *key_buffer, size_t key_buffer_size,
3911 psa_algorithm_t alg,
3912 const uint8_t *hash, size_t hash_length,
3913 const uint8_t *signature, size_t signature_length)
3914{
3915 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003916 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003917 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003918
Paul Elliott068fe072023-01-16 13:59:15 +00003919 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3920 return PSA_ERROR_NOT_SUPPORTED;
3921 }
3922
3923 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003924 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003925 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003926
3927#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003928 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3929 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003930
Paul Elliotta1c94092023-02-15 16:38:04 +00003931 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3932 mbedtls_mpi_init(&operation->r);
3933 mbedtls_mpi_init(&operation->s);
3934
Paul Elliott93d9ca82023-02-15 18:14:21 +00003935 /* Ensure num_ops is zero'ed in case of context re-use. */
3936 operation->num_ops = 0;
3937
Paul Elliott068fe072023-01-16 13:59:15 +00003938 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3939 attributes->core.bits,
3940 key_buffer,
3941 key_buffer_size,
3942 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003943
Paul Elliott068fe072023-01-16 13:59:15 +00003944 if (status != PSA_SUCCESS) {
3945 return status;
3946 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003947
Paul Elliottc569fc22023-02-10 13:02:54 +00003948 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003949
Paul Elliott1bc59df2023-02-05 13:41:57 +00003950 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003951 return PSA_ERROR_INVALID_SIGNATURE;
3952 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003953
Paul Elliott068fe072023-01-16 13:59:15 +00003954 status = mbedtls_to_psa_error(
3955 mbedtls_mpi_read_binary(&operation->r,
3956 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003957 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003958
Paul Elliott068fe072023-01-16 13:59:15 +00003959 if (status != PSA_SUCCESS) {
3960 return status;
3961 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003962
Paul Elliott068fe072023-01-16 13:59:15 +00003963 status = mbedtls_to_psa_error(
3964 mbedtls_mpi_read_binary(&operation->s,
3965 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003966 coordinate_bytes,
3967 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003968
Paul Elliott068fe072023-01-16 13:59:15 +00003969 if (status != PSA_SUCCESS) {
3970 return status;
3971 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003972
Paul Elliott2c9843f2023-02-15 17:32:42 +00003973 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003974
Paul Elliott2c9843f2023-02-15 17:32:42 +00003975 if (status != PSA_SUCCESS) {
3976 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003977 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003978
Paul Elliott0290a762023-02-09 14:30:24 +00003979 /* We only need to store the same length of hash as the private key size
3980 * here, it would be truncated by the internal implementation anyway. */
3981 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3982 coordinate_bytes);
3983
Paul Elliottba70ad42023-02-15 18:23:53 +00003984 if (required_hash_length > sizeof(operation->hash)) {
3985 /* Shouldn't happen, but better safe than sorry. */
3986 return PSA_ERROR_CORRUPTION_DETECTED;
3987 }
3988
Paul Elliott0290a762023-02-09 14:30:24 +00003989 memcpy(operation->hash, hash, required_hash_length);
3990 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003991
3992 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003993#else
Paul Elliott068fe072023-01-16 13:59:15 +00003994 (void) operation;
3995 (void) key_buffer;
3996 (void) key_buffer_size;
3997 (void) alg;
3998 (void) hash;
3999 (void) hash_length;
4000 (void) signature;
4001 (void) signature_length;
4002 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004003 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004004 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004005
Paul Elliott068fe072023-01-16 13:59:15 +00004006 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004007#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4008 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4009 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004010}
4011
4012psa_status_t mbedtls_psa_verify_hash_complete(
4013 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4014{
4015
4016#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4017 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4018 defined(MBEDTLS_ECP_RESTARTABLE)
4019
Paul Elliottf8e5b562023-02-19 18:43:45 +00004020 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4021
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004022 /* Ensure max_ops is set to the current value (or default). */
4023 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4024
Paul Elliottf8e5b562023-02-19 18:43:45 +00004025 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004026 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4027 operation->hash,
4028 operation->hash_length,
4029 &operation->ctx->Q,
4030 &operation->r,
4031 &operation->s,
4032 &operation->restart_ctx));
4033
Paul Elliottf8e5b562023-02-19 18:43:45 +00004034 /* Hide the fact that the restart context only holds a delta of number of
4035 * ops done during the last operation, not an absolute value. */
4036 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4037
4038 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004039#else
4040 (void) operation;
4041
4042 return PSA_ERROR_NOT_SUPPORTED;
4043
4044#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4045 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4046 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4047}
4048
4049psa_status_t mbedtls_psa_verify_hash_abort(
4050 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4051{
4052
4053#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4054 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4055 defined(MBEDTLS_ECP_RESTARTABLE)
4056
4057 if (operation->ctx) {
4058 mbedtls_ecdsa_free(operation->ctx);
4059 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004060 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004061 }
4062
4063 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4064
Paul Elliott93d9ca82023-02-15 18:14:21 +00004065 operation->num_ops = 0;
4066
Paul Elliott588f8ed2022-12-02 18:10:26 +00004067 mbedtls_mpi_free(&operation->r);
4068 mbedtls_mpi_free(&operation->s);
4069
4070 return PSA_SUCCESS;
4071
4072#else
4073 (void) operation;
4074
4075 return PSA_ERROR_NOT_SUPPORTED;
4076
4077#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4078 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4079 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4080}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004081
mohammad1603503973b2018-03-12 15:59:30 +02004082/****************************************************************/
4083/* Symmetric cryptography */
4084/****************************************************************/
4085
Gilles Peskine449bd832023-01-11 14:50:10 +01004086static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4087 mbedtls_svc_key_id_t key,
4088 psa_algorithm_t alg,
4089 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004090{
4091 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4092 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004093 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4095 PSA_KEY_USAGE_ENCRYPT :
4096 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004097 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004098
4099 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004101 status = PSA_ERROR_BAD_STATE;
4102 goto exit;
4103 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004106 status = PSA_ERROR_INVALID_ARGUMENT;
4107 goto exit;
4108 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004109
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4111 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004114
Ronald Cron49fafa92021-03-10 08:34:23 +01004115 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004116 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004117 * so we only set it (in the driver wrapper) after resources have been
4118 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004119 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004121 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004123 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 }
4125 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004126
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004127 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004129 };
4130
Ronald Cronab99ac22020-10-01 16:38:28 +02004131 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 if (cipher_operation == MBEDTLS_ENCRYPT) {
4133 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4134 &attributes,
4135 slot->key.data,
4136 slot->key.bytes,
4137 alg);
4138 } else {
4139 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4140 &attributes,
4141 slot->key.data,
4142 slot->key.bytes,
4143 alg);
4144 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004145
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004146exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 if (status != PSA_SUCCESS) {
4148 psa_cipher_abort(operation);
4149 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004150
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004154}
4155
Gilles Peskine449bd832023-01-11 14:50:10 +01004156psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4157 mbedtls_svc_key_id_t key,
4158 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004159{
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004161}
4162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4164 mbedtls_svc_key_id_t key,
4165 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004166{
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004168}
4169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4171 uint8_t *iv,
4172 size_t iv_size,
4173 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004174{
Ronald Cron6d051732020-10-01 14:10:20 +02004175 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004176 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4177 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004178
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004180 status = PSA_ERROR_BAD_STATE;
4181 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004182 }
4183
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004185 status = PSA_ERROR_BAD_STATE;
4186 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004187 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004188
Ronald Cron2fb90522021-07-05 12:31:44 +02004189 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004191 status = PSA_ERROR_BUFFER_TOO_SMALL;
4192 goto exit;
4193 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004194
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004196 status = PSA_ERROR_GENERIC_ERROR;
4197 goto exit;
4198 }
4199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 status = psa_generate_random(local_iv, default_iv_length);
4201 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004202 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 }
Ronald Cron5618a392021-03-26 09:52:26 +01004204
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 status = psa_driver_wrapper_cipher_set_iv(operation,
4206 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004207
4208exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 if (status == PSA_SUCCESS) {
4210 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004211 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004212 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004214 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004216 }
Ronald Cron6d051732020-10-01 14:10:20 +02004217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004219}
4220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4222 const uint8_t *iv,
4223 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004224{
Ronald Cron6d051732020-10-01 14:10:20 +02004225 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004226
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004228 status = PSA_ERROR_BAD_STATE;
4229 goto exit;
4230 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004231
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004233 status = PSA_ERROR_BAD_STATE;
4234 goto exit;
4235 }
Ronald Crona0d68172021-03-26 10:15:08 +01004236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004238 status = PSA_ERROR_INVALID_ARGUMENT;
4239 goto exit;
4240 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 status = psa_driver_wrapper_cipher_set_iv(operation,
4243 iv,
4244 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004245
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004246exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004247 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004248 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 } else {
4250 psa_cipher_abort(operation);
4251 }
4252 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004253}
4254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4256 const uint8_t *input,
4257 size_t input_length,
4258 uint8_t *output,
4259 size_t output_size,
4260 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004261{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004262 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004265 status = PSA_ERROR_BAD_STATE;
4266 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004267 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004270 status = PSA_ERROR_BAD_STATE;
4271 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004272 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004273
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 status = psa_driver_wrapper_cipher_update(operation,
4275 input,
4276 input_length,
4277 output,
4278 output_size,
4279 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004280
4281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 if (status != PSA_SUCCESS) {
4283 psa_cipher_abort(operation);
4284 }
Ronald Cron6d051732020-10-01 14:10:20 +02004285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004287}
4288
Gilles Peskine449bd832023-01-11 14:50:10 +01004289psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4290 uint8_t *output,
4291 size_t output_size,
4292 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004293{
David Saadab4ecc272019-02-14 13:48:10 +02004294 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004297 status = PSA_ERROR_BAD_STATE;
4298 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004299 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004300
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004302 status = PSA_ERROR_BAD_STATE;
4303 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004304 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004305
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 status = psa_driver_wrapper_cipher_finish(operation,
4307 output,
4308 output_size,
4309 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004310
4311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 if (status == PSA_SUCCESS) {
4313 return psa_cipher_abort(operation);
4314 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004315 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004319 }
mohammad1603503973b2018-03-12 15:59:30 +02004320}
4321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004323{
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004325 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004326 * in use. It's ok to call abort on such an object, and there's
4327 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004329 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004330
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004332
Ronald Cron49fafa92021-03-10 08:34:23 +01004333 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004334 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004335 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004336
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004338}
4339
Gilles Peskine449bd832023-01-11 14:50:10 +01004340psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4341 psa_algorithm_t alg,
4342 const uint8_t *input,
4343 size_t input_length,
4344 uint8_t *output,
4345 size_t output_size,
4346 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004347{
4348 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004349 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004350 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004351 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4352 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004353 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004354
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004356 status = PSA_ERROR_INVALID_ARGUMENT;
4357 goto exit;
4358 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4361 PSA_KEY_USAGE_ENCRYPT,
4362 alg);
4363 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004364 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004366
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004367 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004369 };
4370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4372 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004373 status = PSA_ERROR_GENERIC_ERROR;
4374 goto exit;
4375 }
4376
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 if (default_iv_length > 0) {
4378 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004379 status = PSA_ERROR_BUFFER_TOO_SMALL;
4380 goto exit;
4381 }
4382
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 status = psa_generate_random(local_iv, default_iv_length);
4384 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004385 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004387 }
4388
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004389 status = psa_driver_wrapper_cipher_encrypt(
4390 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004391 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004392 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004394
4395exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 unlock_status = psa_unlock_key_slot(slot);
4397 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004398 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004399 }
Ronald Cron23919522021-07-08 19:00:07 +02004400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 if (status == PSA_SUCCESS) {
4402 if (default_iv_length > 0) {
4403 memcpy(output, local_iv, default_iv_length);
4404 }
4405 *output_length += default_iv_length;
4406 } else {
4407 *output_length = 0;
4408 }
4409
4410 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004411}
4412
Gilles Peskine449bd832023-01-11 14:50:10 +01004413psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4414 psa_algorithm_t alg,
4415 const uint8_t *input,
4416 size_t input_length,
4417 uint8_t *output,
4418 size_t output_size,
4419 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004420{
4421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004422 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004423 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004424 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004425
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004427 status = PSA_ERROR_INVALID_ARGUMENT;
4428 goto exit;
4429 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004430
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4432 PSA_KEY_USAGE_DECRYPT,
4433 alg);
4434 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004435 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004437
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004438 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004440 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4443 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004444 status = PSA_ERROR_INVALID_ARGUMENT;
4445 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004447 status = PSA_ERROR_INVALID_ARGUMENT;
4448 goto exit;
4449 }
4450
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004451 status = psa_driver_wrapper_cipher_decrypt(
4452 &attributes, slot->key.data, slot->key.bytes,
4453 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004455
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 unlock_status = psa_unlock_key_slot(slot);
4458 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004459 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004463 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 }
Ronald Cron23919522021-07-08 19:00:07 +02004465
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004467}
4468
4469
mohammad16035955c982018-04-26 00:53:03 +03004470/****************************************************************/
4471/* AEAD */
4472/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004473
Paul Elliottbaff51c2021-09-28 17:44:45 +01004474/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004475static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004476{
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004478}
4479
4480/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004481static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4482 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004483{
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004485
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004487#if defined(PSA_WANT_ALG_GCM)
4488 case PSA_ALG_GCM:
4489 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 * arbitrarily large nonces. Please note that we do not generally
4491 * recommend the usage of nonces of greater length than
4492 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4493 * size, which can then lead to collisions if you encrypt a very
4494 * large number of messages.*/
4495 if (nonce_length != 0) {
4496 return PSA_SUCCESS;
4497 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004498 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004499#endif /* PSA_WANT_ALG_GCM */
4500#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004501 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 if (nonce_length >= 7 && nonce_length <= 13) {
4503 return PSA_SUCCESS;
4504 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004505 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004506#endif /* PSA_WANT_ALG_CCM */
4507#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004508 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 if (nonce_length == 12) {
4510 return PSA_SUCCESS;
4511 } else if (nonce_length == 8) {
4512 return PSA_ERROR_NOT_SUPPORTED;
4513 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004514 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004515#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004516 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004517 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004519 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004522}
4523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004525{
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4527 return PSA_ERROR_INVALID_ARGUMENT;
4528 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004529
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004531}
4532
Gilles Peskine449bd832023-01-11 14:50:10 +01004533psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4534 psa_algorithm_t alg,
4535 const uint8_t *nonce,
4536 size_t nonce_length,
4537 const uint8_t *additional_data,
4538 size_t additional_data_length,
4539 const uint8_t *plaintext,
4540 size_t plaintext_length,
4541 uint8_t *ciphertext,
4542 size_t ciphertext_size,
4543 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004544{
Ronald Cron215633c2021-03-16 17:15:37 +01004545 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4546 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004547
mohammad1603f08a5502018-06-03 15:05:47 +03004548 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004549
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 status = psa_aead_check_algorithm(alg);
4551 if (status != PSA_SUCCESS) {
4552 return status;
4553 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004554
Ronald Cron9a986162021-03-26 12:40:07 +01004555 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4557 if (status != PSA_SUCCESS) {
4558 return status;
4559 }
mohammad16035955c982018-04-26 00:53:03 +03004560
Ronald Cron215633c2021-03-16 17:15:37 +01004561 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004563 };
mohammad16035955c982018-04-26 00:53:03 +03004564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 status = psa_aead_check_nonce_length(alg, nonce_length);
4566 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004567 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004569
Ronald Cronde822812021-03-17 16:08:20 +01004570 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004571 &attributes, slot->key.data, slot->key.bytes,
4572 alg,
4573 nonce, nonce_length,
4574 additional_data, additional_data_length,
4575 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004577
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4579 memset(ciphertext, 0, ciphertext_size);
4580 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004581
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004582exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004586}
4587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4589 psa_algorithm_t alg,
4590 const uint8_t *nonce,
4591 size_t nonce_length,
4592 const uint8_t *additional_data,
4593 size_t additional_data_length,
4594 const uint8_t *ciphertext,
4595 size_t ciphertext_length,
4596 uint8_t *plaintext,
4597 size_t plaintext_size,
4598 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004599{
Ronald Cron215633c2021-03-16 17:15:37 +01004600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4601 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004602
Gilles Peskineee652a32018-06-01 19:23:52 +02004603 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 status = psa_aead_check_algorithm(alg);
4606 if (status != PSA_SUCCESS) {
4607 return status;
4608 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004609
Ronald Cron9a986162021-03-26 12:40:07 +01004610 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4612 if (status != PSA_SUCCESS) {
4613 return status;
4614 }
mohammad16035955c982018-04-26 00:53:03 +03004615
Ronald Cron215633c2021-03-16 17:15:37 +01004616 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004618 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 status = psa_aead_check_nonce_length(alg, nonce_length);
4621 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004622 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004624
Ronald Cronde822812021-03-17 16:08:20 +01004625 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004626 &attributes, slot->key.data, slot->key.bytes,
4627 alg,
4628 nonce, nonce_length,
4629 additional_data, additional_data_length,
4630 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 if (status != PSA_SUCCESS && plaintext_size != 0) {
4634 memset(plaintext, 0, plaintext_size);
4635 }
mohammad160360a64d02018-06-03 17:20:42 +03004636
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004637exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004639
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 return status;
mohammad16035955c982018-04-26 00:53:03 +03004641}
4642
Gilles Peskine449bd832023-01-11 14:50:10 +01004643static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4644{
4645 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004648#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004650 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4652 return PSA_ERROR_INVALID_ARGUMENT;
4653 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004654 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004655#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004656
Przemek Stekiel86679c72022-10-06 17:06:56 +02004657#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004659 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4661 return PSA_ERROR_INVALID_ARGUMENT;
4662 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004663 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004664#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004665
Przemek Stekiel86679c72022-10-06 17:06:56 +02004666#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004668 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if (tag_len != 16) {
4670 return PSA_ERROR_INVALID_ARGUMENT;
4671 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004672 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004673#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004674
4675 default:
4676 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004678 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004680}
4681
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004682/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004683static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4684 int is_encrypt,
4685 mbedtls_svc_key_id_t key,
4686 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004687{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004688 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4689 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4690 psa_key_slot_t *slot = NULL;
4691 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004692 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004693
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 status = psa_aead_check_algorithm(alg);
4695 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004696 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004700 status = PSA_ERROR_BAD_STATE;
4701 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004702 }
4703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 if (operation->nonce_set || operation->lengths_set ||
4705 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004706 status = PSA_ERROR_BAD_STATE;
4707 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004708 }
4709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004711 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004713 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4717 alg);
4718 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004719 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004721
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004722 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004723 .core = slot->attr
4724 };
4725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004727 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 if (is_encrypt) {
4731 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4732 &attributes,
4733 slot->key.data,
4734 slot->key.bytes,
4735 alg);
4736 } else {
4737 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4738 &attributes,
4739 slot->key.data,
4740 slot->key.bytes,
4741 alg);
4742 }
4743 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004744 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004746
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004748
4749exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004753 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004755 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 } else {
4757 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004758 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004761}
4762
Paul Elliott302ff6b2021-04-20 18:10:30 +01004763/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004764psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4765 mbedtls_svc_key_id_t key,
4766 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004767{
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004769}
4770
4771/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004772psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4773 mbedtls_svc_key_id_t key,
4774 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004775{
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004777}
4778
4779/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004780psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4781 uint8_t *nonce,
4782 size_t nonce_size,
4783 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004784{
4785 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004786 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004787 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004788
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004789 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004792 status = PSA_ERROR_BAD_STATE;
4793 goto exit;
4794 }
4795
Gilles Peskine449bd832023-01-11 14:50:10 +01004796 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004797 status = PSA_ERROR_BAD_STATE;
4798 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004799 }
4800
Paul Elliotte193ea82021-10-01 13:00:16 +01004801 /* For CCM, this size may not be correct according to the PSA
4802 * specification. The PSA Crypto 1.0.1 specification states:
4803 *
4804 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4805 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4806 *
4807 * However this restriction that L has to be the smallest integer is not
4808 * applied in practice, and it is not implementable here since the
4809 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4811 operation->alg);
4812 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004813 status = PSA_ERROR_BUFFER_TOO_SMALL;
4814 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004815 }
4816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 status = psa_generate_random(local_nonce, required_nonce_size);
4818 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004819 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823
Paul Elliott39dc6b82021-05-11 19:16:09 +01004824exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 if (status == PSA_SUCCESS) {
4826 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004827 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 } else {
4829 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004830 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004831
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004833}
4834
4835/* Set the nonce for a multipart authenticated encryption or decryption
4836 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004837psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4838 const uint8_t *nonce,
4839 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004840{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004841 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004844 status = PSA_ERROR_BAD_STATE;
4845 goto exit;
4846 }
4847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004849 status = PSA_ERROR_BAD_STATE;
4850 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004851 }
4852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4854 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004855 status = PSA_ERROR_INVALID_ARGUMENT;
4856 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004857 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004858
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4860 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004861
Paul Elliott39dc6b82021-05-11 19:16:09 +01004862exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004864 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 } else {
4866 psa_aead_abort(operation);
4867 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004870}
4871
4872/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004873psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4874 size_t ad_length,
4875 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004876{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004877 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4878
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004880 status = PSA_ERROR_BAD_STATE;
4881 goto exit;
4882 }
4883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 if (operation->lengths_set || operation->ad_started ||
4885 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004886 status = PSA_ERROR_BAD_STATE;
4887 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004888 }
4889
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004891#if defined(PSA_WANT_ALG_GCM)
4892 case PSA_ALG_GCM:
4893 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 * bits. Without the guard this code will generate warnings on 32bit
4895 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004896#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 if (((uint64_t) ad_length) >> 61 != 0 ||
4898 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004899 status = PSA_ERROR_INVALID_ARGUMENT;
4900 goto exit;
4901 }
Paul Elliott325d3742021-09-27 17:56:28 +01004902#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004903 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004904#endif /* PSA_WANT_ALG_GCM */
4905#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004906 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004908 status = PSA_ERROR_INVALID_ARGUMENT;
4909 goto exit;
4910 }
4911 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004912#endif /* PSA_WANT_ALG_CCM */
4913#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004914 case PSA_ALG_CHACHA20_POLY1305:
4915 /* No length restrictions for ChaChaPoly. */
4916 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004917#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004918 default:
4919 break;
4920 }
Paul Elliott325d3742021-09-27 17:56:28 +01004921
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4923 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004924
Paul Elliott39dc6b82021-05-11 19:16:09 +01004925exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004927 operation->ad_remaining = ad_length;
4928 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004929 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 } else {
4931 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004932 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004935}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004936
4937/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004938psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4939 const uint8_t *input,
4940 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004941{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004942 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4943
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004945 status = PSA_ERROR_BAD_STATE;
4946 goto exit;
4947 }
4948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004950 status = PSA_ERROR_BAD_STATE;
4951 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004952 }
4953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 if (operation->lengths_set) {
4955 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004956 status = PSA_ERROR_INVALID_ARGUMENT;
4957 goto exit;
4958 }
4959
4960 operation->ad_remaining -= input_length;
4961 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004962#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004964 status = PSA_ERROR_BAD_STATE;
4965 goto exit;
4966 }
4967#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004968
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 status = psa_driver_wrapper_aead_update_ad(operation, input,
4970 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004971
Paul Elliott39dc6b82021-05-11 19:16:09 +01004972exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004974 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 } else {
4976 psa_aead_abort(operation);
4977 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004978
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004980}
4981
4982/* Encrypt or decrypt a message fragment in an active multipart AEAD
4983 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004984psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4985 const uint8_t *input,
4986 size_t input_length,
4987 uint8_t *output,
4988 size_t output_size,
4989 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004990{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004991 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004992
4993 *output_length = 0;
4994
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004996 status = PSA_ERROR_BAD_STATE;
4997 goto exit;
4998 }
4999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005001 status = PSA_ERROR_BAD_STATE;
5002 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005003 }
5004
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005006 /* Additional data length was supplied, but not all the additional
5007 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005009 status = PSA_ERROR_INVALID_ARGUMENT;
5010 goto exit;
5011 }
5012
5013 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005015 status = PSA_ERROR_INVALID_ARGUMENT;
5016 goto exit;
5017 }
5018
5019 operation->body_remaining -= input_length;
5020 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005021#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005023 status = PSA_ERROR_BAD_STATE;
5024 goto exit;
5025 }
5026#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005027
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5029 output, output_size,
5030 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005031
Paul Elliott39dc6b82021-05-11 19:16:09 +01005032exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005034 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 } else {
5036 psa_aead_abort(operation);
5037 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005038
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005040}
5041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005043{
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 if (operation->id == 0 || !operation->nonce_set) {
5045 return PSA_ERROR_BAD_STATE;
5046 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005047
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5049 operation->body_remaining != 0)) {
5050 return PSA_ERROR_INVALID_ARGUMENT;
5051 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005052
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005054}
5055
Paul Elliott302ff6b2021-04-20 18:10:30 +01005056/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005057psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5058 uint8_t *ciphertext,
5059 size_t ciphertext_size,
5060 size_t *ciphertext_length,
5061 uint8_t *tag,
5062 size_t tag_size,
5063 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005064{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005065 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5066
Paul Elliott302ff6b2021-04-20 18:10:30 +01005067 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005068 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005069
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 status = psa_aead_final_checks(operation);
5071 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005072 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005076 status = PSA_ERROR_BAD_STATE;
5077 goto exit;
5078 }
5079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5081 ciphertext_size,
5082 ciphertext_length,
5083 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005084
Paul Elliott39dc6b82021-05-11 19:16:09 +01005085exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005086
5087
Paul Elliottf47b0952021-05-21 18:02:33 +01005088 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005089 * the zero tag size, make sure the tag is set to something implausible.
5090 * Even if the operation succeeds, make sure we clear the rest of the
5091 * buffer to prevent potential leakage of anything previously placed in
5092 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005093 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005094
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005096
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005098}
5099
5100/* Finish authenticating and decrypting a message in a multipart AEAD
5101 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005102psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5103 uint8_t *plaintext,
5104 size_t plaintext_size,
5105 size_t *plaintext_length,
5106 const uint8_t *tag,
5107 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005108{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5110
Paul Elliott302ff6b2021-04-20 18:10:30 +01005111 *plaintext_length = 0;
5112
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 status = psa_aead_final_checks(operation);
5114 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005117
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005119 status = PSA_ERROR_BAD_STATE;
5120 goto exit;
5121 }
5122
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5124 plaintext_size,
5125 plaintext_length,
5126 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005127
5128exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005132}
5133
5134/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005135psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005136{
5137 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005140 /* The object has (apparently) been initialized but it is not (yet)
5141 * in use. It's ok to call abort on such an object, and there's
5142 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005144 }
5145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005151}
5152
Gilles Peskinee59236f2018-01-27 23:32:46 +01005153/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005154/* Generators */
5155/****************************************************************/
5156
Przemek Stekiel69c46792022-06-10 12:59:51 +02005157#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005158 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005159 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305160 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305161 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005162#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005163#endif /* At least one builtin KDF */
5164
Przemek Stekiel69c46792022-06-10 12:59:51 +02005165#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005166 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5167 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5168static psa_status_t psa_key_derivation_start_hmac(
5169 psa_mac_operation_t *operation,
5170 psa_algorithm_t hash_alg,
5171 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005173{
5174 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5177 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5178 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005179
Steven Cooreman72f736a2021-05-07 14:14:37 +02005180 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 status = psa_driver_wrapper_mac_sign_setup(operation,
5184 &attributes,
5185 hmac_key, hmac_key_length,
5186 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005187
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 psa_reset_key_attributes(&attributes);
5189 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005190}
5191#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005192
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005193#define HKDF_STATE_INIT 0 /* no input yet */
5194#define HKDF_STATE_STARTED 1 /* got salt */
5195#define HKDF_STATE_KEYED 2 /* got key */
5196#define HKDF_STATE_OUTPUT 3 /* output started */
5197
Gilles Peskinecbe66502019-05-16 16:59:18 +02005198static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005200{
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5202 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5203 } else {
5204 return operation->alg;
5205 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005206}
5207
Gilles Peskine449bd832023-01-11 14:50:10 +01005208psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005209{
5210 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5212 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005213 /* The object has (apparently) been initialized but it is not
5214 * in use. It's ok to call abort on such an object, and there's
5215 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005217#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5219 mbedtls_free(operation->ctx.hkdf.info);
5220 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5221 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005222#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005223#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5224 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5226 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5227 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5228 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005229 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005231 }
5232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005234 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005236 }
5237
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005239 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005241 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005242#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005244 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005246 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005247#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005248 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005249
5250 /* We leave the fields Ai and output_block to be erased safely by the
5251 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005253#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5254 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005255#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5257 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5258 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5259 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005260#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305261#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305262 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305263 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005264 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305265 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305266 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305267
5268 status = PSA_SUCCESS;
5269 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305270#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005271 {
5272 status = PSA_ERROR_BAD_STATE;
5273 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 mbedtls_platform_zeroize(operation, sizeof(*operation));
5275 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005276}
5277
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005278psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005280{
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005282 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005284 }
5285
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005288}
5289
Gilles Peskine449bd832023-01-11 14:50:10 +01005290psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5291 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005292{
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 if (operation->alg == 0) {
5294 return PSA_ERROR_BAD_STATE;
5295 }
5296 if (capacity > operation->capacity) {
5297 return PSA_ERROR_INVALID_ARGUMENT;
5298 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005299 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005301}
5302
Przemek Stekiel69c46792022-06-10 12:59:51 +02005303#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005304/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005305static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5306 psa_algorithm_t kdf_alg,
5307 uint8_t *output,
5308 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005309{
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5311 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005312 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005313 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005314#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005316#else
5317 const uint8_t last_block = 0xff;
5318#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005319
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 if (hkdf->state < HKDF_STATE_KEYED ||
5321 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005322#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005324#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 )) {
5326 return PSA_ERROR_BAD_STATE;
5327 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005328 hkdf->state = HKDF_STATE_OUTPUT;
5329
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005331 /* Copy what remains of the current block */
5332 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005334 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 }
5336 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005337 output += n;
5338 output_length -= n;
5339 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005341 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005343 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005344 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005345 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005346 * object was corrupted or if this function is called directly
5347 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 if (hkdf->block_number == last_block) {
5349 return PSA_ERROR_BAD_STATE;
5350 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005351
5352 /* We need a new block */
5353 ++hkdf->block_number;
5354 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005355
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5357 hash_alg,
5358 hkdf->prk,
5359 hash_length);
5360 if (status != PSA_SUCCESS) {
5361 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005362 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005363
5364 if (hkdf->block_number != 1) {
5365 status = psa_mac_update(&hkdf->hmac,
5366 hkdf->output_block,
5367 hash_length);
5368 if (status != PSA_SUCCESS) {
5369 return status;
5370 }
5371 }
5372 status = psa_mac_update(&hkdf->hmac,
5373 hkdf->info,
5374 hkdf->info_length);
5375 if (status != PSA_SUCCESS) {
5376 return status;
5377 }
5378 status = psa_mac_update(&hkdf->hmac,
5379 &hkdf->block_number, 1);
5380 if (status != PSA_SUCCESS) {
5381 return status;
5382 }
5383 status = psa_mac_sign_finish(&hkdf->hmac,
5384 hkdf->output_block,
5385 sizeof(hkdf->output_block),
5386 &hmac_output_length);
5387 if (status != PSA_SUCCESS) {
5388 return status;
5389 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005390 }
5391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005393}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005394#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005395
John Durkop07cc04a2020-11-16 22:08:34 -08005396#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5397 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005398static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5399 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005401{
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5403 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005404 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5405 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005406 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005407
Janos Follath7742fee2019-06-17 12:58:10 +01005408 /* We can't be wanting more output after block 0xff, otherwise
5409 * the capacity check in psa_key_derivation_output_bytes() would have
5410 * prevented this call. It could happen only if the operation
5411 * object was corrupted or if this function is called directly
5412 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 if (tls12_prf->block_number == 0xff) {
5414 return PSA_ERROR_CORRUPTION_DETECTED;
5415 }
Janos Follath7742fee2019-06-17 12:58:10 +01005416
5417 /* We need a new block */
5418 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005419 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005420
5421 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5422 *
5423 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5424 *
5425 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5426 * HMAC_hash(secret, A(2) + seed) +
5427 * HMAC_hash(secret, A(3) + seed) + ...
5428 *
5429 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005430 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005431 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005432 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005433 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005434 * is currently extracted as `output_block` and where i is
5435 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005436 */
5437
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 status = psa_key_derivation_start_hmac(&hmac,
5439 hash_alg,
5440 tls12_prf->secret,
5441 tls12_prf->secret_length);
5442 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005443 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005445
5446 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005448 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5449 * the variable seed and in this instance means it in the context of the
5450 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 status = psa_mac_update(&hmac,
5452 tls12_prf->label,
5453 tls12_prf->label_length);
5454 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005455 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 }
5457 status = psa_mac_update(&hmac,
5458 tls12_prf->seed,
5459 tls12_prf->seed_length);
5460 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005461 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 }
5463 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005464 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5466 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005467 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005469 }
5470
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 status = psa_mac_sign_finish(&hmac,
5472 tls12_prf->Ai, hash_length,
5473 &hmac_output_length);
5474 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005475 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 }
5477 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005478 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005480
5481 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 status = psa_key_derivation_start_hmac(&hmac,
5483 hash_alg,
5484 tls12_prf->secret,
5485 tls12_prf->secret_length);
5486 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005487 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 }
5489 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5490 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005491 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 }
5493 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5494 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005495 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 }
5497 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5498 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005499 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 }
5501 status = psa_mac_sign_finish(&hmac,
5502 tls12_prf->output_block, hash_length,
5503 &hmac_output_length);
5504 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005505 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005507
Janos Follath7742fee2019-06-17 12:58:10 +01005508
5509cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 cleanup_status = psa_mac_abort(&hmac);
5511 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005512 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005516}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005517
Janos Follath844eb0e2019-06-19 12:10:49 +01005518static psa_status_t psa_key_derivation_tls12_prf_read(
5519 psa_tls12_prf_key_derivation_t *tls12_prf,
5520 psa_algorithm_t alg,
5521 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005523{
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5525 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005526 psa_status_t status;
5527 uint8_t offset, length;
5528
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005530 case PSA_TLS12_PRF_STATE_LABEL_SET:
5531 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5532 break;
5533 case PSA_TLS12_PRF_STATE_OUTPUT:
5534 break;
5535 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005537 }
5538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005540 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 if (tls12_prf->left_in_block == 0) {
5542 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5543 alg);
5544 if (status != PSA_SUCCESS) {
5545 return status;
5546 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005547
5548 continue;
5549 }
5550
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005552 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005554 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005556
5557 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005559 output += length;
5560 output_length -= length;
5561 tls12_prf->left_in_block -= length;
5562 }
5563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005565}
John Durkop07cc04a2020-11-16 22:08:34 -08005566#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5567 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005568
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005569#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5570static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5571 psa_tls12_ecjpake_to_pms_t *ecjpake,
5572 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005574{
5575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005576 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 if (output_length != 32) {
5579 return PSA_ERROR_INVALID_ARGUMENT;
5580 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5583 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5584 &output_size);
5585 if (status != PSA_SUCCESS) {
5586 return status;
5587 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005588
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 if (output_size != output_length) {
5590 return PSA_ERROR_GENERIC_ERROR;
5591 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005592
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005594}
5595#endif
5596
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305597#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305598static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5599 psa_pbkdf2_key_derivation_t *pbkdf2,
5600 psa_algorithm_t prf_alg,
5601 uint8_t prf_output_length,
5602 psa_key_attributes_t *attributes)
5603{
5604 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305605 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305606 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305607 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305608 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305609 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305610 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305611
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305612 mac_operation.is_sign = 1;
5613 mac_operation.mac_size = prf_output_length;
5614 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305615
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305616 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5617 attributes,
5618 pbkdf2->password,
5619 pbkdf2->password_length,
5620 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305621 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305622 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305623 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305624 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5625 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305626 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305627 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305628 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305629 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305630 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305631 }
5632 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5633 &mac_output_length);
5634 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305635 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305636 }
5637
5638 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305639 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305640 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305641 }
5642
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305643 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305644
5645 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305646 /* We are passing prf_output_length as mac_size because the driver
5647 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305648 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305649 status = psa_driver_wrapper_mac_compute(attributes,
5650 pbkdf2->password,
5651 pbkdf2->password_length,
5652 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305653 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305654 &mac_output_length);
5655 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305656 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305657 }
5658
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305659 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305660 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305661
5662cleanup:
5663 /* Zeroise buffers to clear sensitive data from memory. */
5664 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5665 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305666}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305667
5668static psa_status_t psa_key_derivation_pbkdf2_read(
5669 psa_pbkdf2_key_derivation_t *pbkdf2,
5670 psa_algorithm_t kdf_alg,
5671 uint8_t *output,
5672 size_t output_length)
5673{
5674 psa_status_t status;
5675 psa_algorithm_t prf_alg;
5676 uint8_t prf_output_length;
5677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5678 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5679 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5680
5681 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5682 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5683 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5684 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305685 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5686 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305687 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305688 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305689 } else {
5690 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305691 }
5692
5693 switch (pbkdf2->state) {
5694 case PSA_PBKDF2_STATE_PASSWORD_SET:
5695 /* Initially we need a new block so bytes_used is equal to block size*/
5696 pbkdf2->bytes_used = prf_output_length;
5697 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5698 break;
5699 case PSA_PBKDF2_STATE_OUTPUT:
5700 break;
5701 default:
5702 return PSA_ERROR_BAD_STATE;
5703 }
5704
5705 while (output_length != 0) {
5706 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5707 if (n > output_length) {
5708 n = (uint8_t) output_length;
5709 }
5710 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5711 output += n;
5712 output_length -= n;
5713 pbkdf2->bytes_used += n;
5714
5715 if (output_length == 0) {
5716 break;
5717 }
5718
5719 /* We need a new block */
5720 pbkdf2->bytes_used = 0;
5721 pbkdf2->block_number++;
5722
5723 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5724 prf_output_length,
5725 &attributes);
5726 if (status != PSA_SUCCESS) {
5727 return status;
5728 }
5729 }
5730
5731 return PSA_SUCCESS;
5732}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305733#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305734
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005735psa_status_t psa_key_derivation_output_bytes(
5736 psa_key_derivation_operation_t *operation,
5737 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005739{
5740 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005746 }
5747
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005749 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005750 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005751 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005752 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005753 goto exit;
5754 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005756 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005757 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005758 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5759 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005760 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005761 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005763 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005764 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005765
Przemek Stekiel69c46792022-06-10 12:59:51 +02005766#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5768 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5769 output, output_length);
5770 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005771#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005772#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5773 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5775 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5776 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5777 kdf_alg, output,
5778 output_length);
5779 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005780#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5781 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005782#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005784 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5786 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005787#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305788#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305789 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305790 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5791 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305792 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305793#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005794
Gilles Peskineeab56e42018-07-12 17:12:33 +02005795 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005796 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005798 }
5799
5800exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005802 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005803 * This allows us to differentiate between exhausted operations and
5804 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5805 * operations. */
5806 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005808 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005810 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005812}
5813
David Brown7807bf72021-02-09 16:28:23 -07005814#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005815static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005816{
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 if (data_size >= 8) {
5818 mbedtls_des_key_set_parity(data);
5819 }
5820 if (data_size >= 16) {
5821 mbedtls_des_key_set_parity(data + 8);
5822 }
5823 if (data_size >= 24) {
5824 mbedtls_des_key_set_parity(data + 16);
5825 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005826}
David Brown7807bf72021-02-09 16:28:23 -07005827#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005828
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005829/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 * ECC keys on a Weierstrass elliptic curve require the generation
5831 * of a private key which is an integer
5832 * in the range [1, N - 1], where N is the boundary of the private key domain:
5833 * N is the prime p for Diffie-Hellman, or the order of the
5834 * curve’s base point for ECC.
5835 *
5836 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5837 * This function generates the private key using the following process:
5838 *
5839 * 1. Draw a byte string of length ceiling(m/8) bytes.
5840 * 2. If m is not a multiple of 8, set the most significant
5841 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5842 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5843 * 4. If k > N - 2, discard the result and return to step 1.
5844 * 5. Output k + 1 as the private key.
5845 *
5846 * This method allows compliance to NIST standards, specifically the methods titled
5847 * Key-Pair Generation by Testing Candidates in the following publications:
5848 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5849 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5850 * Diffie-Hellman keys.
5851 *
5852 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5853 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5854 *
5855 * Note: Function allocates memory for *data buffer, so given *data should be
5856 * always NULL.
5857 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005858#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5859#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005860static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005861 psa_key_slot_t *slot,
5862 size_t bits,
5863 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005864 uint8_t **data
5865 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005866{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005867 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005868 mbedtls_mpi k;
5869 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005871 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005872 size_t m;
5873 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 mbedtls_mpi_init(&k);
5876 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005877
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005878 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005880 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005884 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005885 goto cleanup;
5886 }
5887
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005888 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005892
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005893 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005894 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005895 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005896
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005897 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005898
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005899 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005901
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005902 /* Note: This function is always called with *data == NULL and it
5903 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 *data = mbedtls_calloc(1, m_bytes);
5905 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005906 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005907 goto cleanup;
5908 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005909
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005911 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005913 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005915
5916 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005918 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005919 * (8 * ceiling(m/8) - m) bits of the first byte in
5920 * the string to zero.
5921 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005923 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005924 }
5925
5926 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 * big-endian byte string.
5928 */
5929 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005930
5931 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 * Result of comparison is returned. When it indicates error
5933 * then this function is called again.
5934 */
5935 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005936 }
5937
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005938 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5940 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005941cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 if (ret != 0) {
5943 status = mbedtls_to_psa_error(ret);
5944 }
5945 if (status != PSA_SUCCESS) {
5946 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005947 *data = NULL;
5948 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 mbedtls_mpi_free(&k);
5950 mbedtls_mpi_free(&diff_N_2);
5951 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005952}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005953
5954/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5955 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5956 *
5957 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5958 * draw a 32-byte string and process it as specified in
5959 * Elliptic Curves for Security [RFC7748] §5.
5960 *
5961 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5962 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5963 *
5964 * Note: Function allocates memory for *data buffer, so given *data should be
5965 * always NULL.
5966 */
5967
5968static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5969 size_t bits,
5970 psa_key_derivation_operation_t *operation,
5971 uint8_t **data
5972 )
5973{
5974 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005975 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005978 case 255:
5979 output_length = 32;
5980 break;
5981 case 448:
5982 output_length = 56;
5983 break;
5984 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005986 break;
5987 }
5988
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 if (*data == NULL) {
5992 return PSA_ERROR_INSUFFICIENT_MEMORY;
5993 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005998 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006000
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006002 case 255:
6003 (*data)[0] &= 248;
6004 (*data)[31] &= 127;
6005 (*data)[31] |= 64;
6006 break;
6007 case 448:
6008 (*data)[0] &= 252;
6009 (*data)[55] |= 128;
6010 break;
6011 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006013 break;
6014 }
6015
6016 return status;
6017}
Valerio Setti86587ab2023-06-27 13:09:30 +02006018#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6019static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6020 psa_key_slot_t *slot, size_t bits,
6021 psa_key_derivation_operation_t *operation, uint8_t **data)
6022{
6023 (void) slot;
6024 (void) bits;
6025 (void) operation;
6026 (void) data;
6027 return PSA_ERROR_NOT_SUPPORTED;
6028}
6029
6030static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6031 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6032{
6033 (void) bits;
6034 (void) operation;
6035 (void) data;
6036 return PSA_ERROR_NOT_SUPPORTED;
6037}
6038#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6039#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006040
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006041static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006042 psa_key_slot_t *slot,
6043 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006045{
6046 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306048 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006050 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6053 return PSA_ERROR_INVALID_ARGUMENT;
6054 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006055
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006056#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006057 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6059 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6060 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006061 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6063 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006064 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 }
6066 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006067 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6069 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006070 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006072 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006073 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006074#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006075 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 if (key_type_is_raw_bytes(slot->attr.type)) {
6077 if (bits % 8 != 0) {
6078 return PSA_ERROR_INVALID_ARGUMENT;
6079 }
6080 data = mbedtls_calloc(1, bytes);
6081 if (data == NULL) {
6082 return PSA_ERROR_INSUFFICIENT_MEMORY;
6083 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 status = psa_key_derivation_output_bytes(operation, data, bytes);
6086 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006087 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 }
David Brown12ca5032021-02-11 11:02:00 -07006089#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6091 psa_des_set_key_parity(data, bytes);
6092 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006093#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 } else {
6095 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006096 }
Ronald Crondd04d422020-11-28 15:14:42 +01006097
Ronald Cronbf33c932020-11-28 18:06:53 +01006098 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006099 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006101 };
6102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6104 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6105 &storage_size);
6106 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 }
Archanad8a83dc2021-06-14 10:04:16 +05306109 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 status = psa_allocate_buffer_to_slot(slot, storage_size);
6111 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 }
Archanad8a83dc2021-06-14 10:04:16 +05306114
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 status = psa_driver_wrapper_import_key(&attributes,
6116 data, bytes,
6117 slot->key.data,
6118 slot->key.bytes,
6119 &slot->key.bytes, &bits);
6120 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006121 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006123
6124exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 mbedtls_free(data);
6126 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006127}
6128
Gilles Peskine449bd832023-01-11 14:50:10 +01006129psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6130 psa_key_derivation_operation_t *operation,
6131 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006132{
6133 psa_status_t status;
6134 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006135 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006136
Ronald Cron81709fc2020-11-14 12:10:32 +01006137 *key = MBEDTLS_SVC_KEY_ID_INIT;
6138
Gilles Peskine0f84d622019-09-12 19:03:13 +02006139 /* Reject any attempt to create a zero-length key so that we don't
6140 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 if (psa_get_key_bits(attributes) == 0) {
6142 return PSA_ERROR_INVALID_ARGUMENT;
6143 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 if (operation->alg == PSA_ALG_NONE) {
6146 return PSA_ERROR_BAD_STATE;
6147 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 if (!operation->can_output_key) {
6150 return PSA_ERROR_NOT_PERMITTED;
6151 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6154 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006155#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006157 /* Deriving a key in a secure element is not implemented yet. */
6158 status = PSA_ERROR_NOT_SUPPORTED;
6159 }
6160#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 if (status == PSA_SUCCESS) {
6162 status = psa_generate_derived_key_internal(slot,
6163 attributes->core.bits,
6164 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006165 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 if (status == PSA_SUCCESS) {
6167 status = psa_finish_key_creation(slot, driver, key);
6168 }
6169 if (status != PSA_SUCCESS) {
6170 psa_fail_key_creation(slot, driver);
6171 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006174}
6175
Gilles Peskineeab56e42018-07-12 17:12:33 +02006176
6177
6178/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006179/* Key derivation */
6180/****************************************************************/
6181
Steven Cooreman932ffb72021-02-15 12:14:32 +01006182#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006183static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006184{
6185#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6187 return 1;
6188 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006189#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006190#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6192 return 1;
6193 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006194#endif
6195#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6197 return 1;
6198 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006199#endif
6200#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6202 return 1;
6203 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006204#endif
6205#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6207 return 1;
6208 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006209#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006210#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6212 return 1;
6213 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006214#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306215#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6216 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6217 return 1;
6218 }
6219#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306220#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6221 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6222 return 1;
6223 }
6224#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006226}
6227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006229{
6230 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 psa_status_t status = psa_hash_setup(&operation, alg);
6232 psa_hash_abort(&operation);
6233 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006234}
6235
Gilles Peskine969c5d62019-01-16 15:53:06 +01006236static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006237 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006239{
Janos Follath5fe19732019-06-20 15:09:30 +01006240 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6241 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006243
Gilles Peskine969c5d62019-01-16 15:53:06 +01006244 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 if (!is_kdf_alg_supported(kdf_alg)) {
6246 return PSA_ERROR_NOT_SUPPORTED;
6247 }
John Durkop07cc04a2020-11-16 22:08:34 -08006248
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006249 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306250 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6252 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306253 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6254 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6255 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306256 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306257 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 if (hash_size == 0) {
6259 return PSA_ERROR_NOT_SUPPORTED;
6260 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006261
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006262 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6263 * we might fail later, which is somewhat unfriendly and potentially
6264 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 psa_status_t status = psa_hash_try_support(hash_alg);
6266 if (status != PSA_SUCCESS) {
6267 return status;
6268 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006269 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006270
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6272 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6273 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6274 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006275 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006276#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006277 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6279 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006280 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006282#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6283 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 operation->capacity = 255 * hash_size;
6285 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006286}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006289{
6290#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 if (alg == PSA_ALG_ECDH) {
6292 return PSA_SUCCESS;
6293 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006294#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006295#if defined(PSA_WANT_ALG_FFDH)
6296 if (alg == PSA_ALG_FFDH) {
6297 return PSA_SUCCESS;
6298 }
6299#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006300 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006302}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006303
6304static int psa_key_derivation_allows_free_form_secret_input(
6305 psa_algorithm_t kdf_alg)
6306{
6307#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6308 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6309 return 0;
6310 }
6311#endif
6312 (void) kdf_alg;
6313 return 1;
6314}
John Durkop07cc04a2020-11-16 22:08:34 -08006315#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6318 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006319{
6320 psa_status_t status;
6321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 if (operation->alg != 0) {
6323 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006324 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006325
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6327 return PSA_ERROR_INVALID_ARGUMENT;
6328 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6329#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6330 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6331 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6332 status = psa_key_agreement_try_support(ka_alg);
6333 if (status != PSA_SUCCESS) {
6334 return status;
6335 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006336 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6337 return PSA_ERROR_INVALID_ARGUMENT;
6338 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6340#else
6341 return PSA_ERROR_NOT_SUPPORTED;
6342#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6343 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6344#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6345 status = psa_key_derivation_setup_kdf(operation, alg);
6346#else
6347 return PSA_ERROR_NOT_SUPPORTED;
6348#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6349 } else {
6350 return PSA_ERROR_INVALID_ARGUMENT;
6351 }
6352
6353 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006354 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 }
6356 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006357}
6358
Przemek Stekiel69c46792022-06-10 12:59:51 +02006359#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006360static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6361 psa_algorithm_t kdf_alg,
6362 psa_key_derivation_step_t step,
6363 const uint8_t *data,
6364 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006365{
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006367 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006369 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006370#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6372 return PSA_ERROR_INVALID_ARGUMENT;
6373 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006374#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 if (hkdf->state != HKDF_STATE_INIT) {
6376 return PSA_ERROR_BAD_STATE;
6377 } else {
6378 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6379 hash_alg,
6380 data, data_length);
6381 if (status != PSA_SUCCESS) {
6382 return status;
6383 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006384 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006386 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006387 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006388#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006390 /* We shouldn't be in different state as HKDF_EXPAND only allows
6391 * two inputs: SECRET (this case) and INFO which does not modify
6392 * the state. It could happen only if the hkdf
6393 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 if (hkdf->state != HKDF_STATE_INIT) {
6395 return PSA_ERROR_BAD_STATE;
6396 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006397
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006398 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6400 return PSA_ERROR_INVALID_ARGUMENT;
6401 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006402
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 memcpy(hkdf->prk, data, data_length);
6404 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006405#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006406 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006407 /* HKDF: If no salt was provided, use an empty salt.
6408 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006410#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6412 return PSA_ERROR_BAD_STATE;
6413 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006414#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6416 hash_alg,
6417 NULL, 0);
6418 if (status != PSA_SUCCESS) {
6419 return status;
6420 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006421 hkdf->state = HKDF_STATE_STARTED;
6422 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 if (hkdf->state != HKDF_STATE_STARTED) {
6424 return PSA_ERROR_BAD_STATE;
6425 }
6426 status = psa_mac_update(&hkdf->hmac,
6427 data, data_length);
6428 if (status != PSA_SUCCESS) {
6429 return status;
6430 }
6431 status = psa_mac_sign_finish(&hkdf->hmac,
6432 hkdf->prk,
6433 sizeof(hkdf->prk),
6434 &data_length);
6435 if (status != PSA_SUCCESS) {
6436 return status;
6437 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006438 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006439
Gilles Peskine2b522db2019-04-12 15:11:49 +02006440 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006441 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006442#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006444 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006446 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006448#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006449 {
6450 /* Block 0 is empty, and the next block will be
6451 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006453 }
6454
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006456 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006457#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6459 return PSA_ERROR_INVALID_ARGUMENT;
6460 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006461#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006462#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6464 hkdf->state == HKDF_STATE_INIT) {
6465 return PSA_ERROR_BAD_STATE;
6466 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006467#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 if (hkdf->state == HKDF_STATE_OUTPUT) {
6469 return PSA_ERROR_BAD_STATE;
6470 }
6471 if (hkdf->info_set) {
6472 return PSA_ERROR_BAD_STATE;
6473 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006474 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 if (data_length != 0) {
6476 hkdf->info = mbedtls_calloc(1, data_length);
6477 if (hkdf->info == NULL) {
6478 return PSA_ERROR_INSUFFICIENT_MEMORY;
6479 }
6480 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006481 }
6482 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006484 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006486 }
6487}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006488#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006489
John Durkop07cc04a2020-11-16 22:08:34 -08006490#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6491 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006492static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6493 const uint8_t *data,
6494 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006495{
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6497 return PSA_ERROR_BAD_STATE;
6498 }
Janos Follathf08e2652019-06-13 09:05:41 +01006499
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 if (data_length != 0) {
6501 prf->seed = mbedtls_calloc(1, data_length);
6502 if (prf->seed == NULL) {
6503 return PSA_ERROR_INSUFFICIENT_MEMORY;
6504 }
Janos Follathf08e2652019-06-13 09:05:41 +01006505
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006507 prf->seed_length = data_length;
6508 }
Janos Follathf08e2652019-06-13 09:05:41 +01006509
Gilles Peskine43f958b2020-12-13 14:55:14 +01006510 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006511
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006513}
6514
Gilles Peskine449bd832023-01-11 14:50:10 +01006515static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6516 const uint8_t *data,
6517 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006518{
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6520 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6521 return PSA_ERROR_BAD_STATE;
6522 }
Janos Follath81550542019-06-13 14:26:34 +01006523
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 if (data_length != 0) {
6525 prf->secret = mbedtls_calloc(1, data_length);
6526 if (prf->secret == NULL) {
6527 return PSA_ERROR_INSUFFICIENT_MEMORY;
6528 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006529
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006531 prf->secret_length = data_length;
6532 }
Janos Follath81550542019-06-13 14:26:34 +01006533
Gilles Peskine43f958b2020-12-13 14:55:14 +01006534 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006535
Gilles Peskine449bd832023-01-11 14:50:10 +01006536 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006537}
6538
Gilles Peskine449bd832023-01-11 14:50:10 +01006539static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6540 const uint8_t *data,
6541 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006542{
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6544 return PSA_ERROR_BAD_STATE;
6545 }
Janos Follath63028dd2019-06-13 09:15:47 +01006546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 if (data_length != 0) {
6548 prf->label = mbedtls_calloc(1, data_length);
6549 if (prf->label == NULL) {
6550 return PSA_ERROR_INSUFFICIENT_MEMORY;
6551 }
Janos Follath63028dd2019-06-13 09:15:47 +01006552
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006554 prf->label_length = data_length;
6555 }
Janos Follath63028dd2019-06-13 09:15:47 +01006556
Gilles Peskine43f958b2020-12-13 14:55:14 +01006557 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006560}
6561
Gilles Peskine449bd832023-01-11 14:50:10 +01006562static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6563 psa_key_derivation_step_t step,
6564 const uint8_t *data,
6565 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006566{
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006568 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006570 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006572 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006574 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006576 }
6577}
John Durkop07cc04a2020-11-16 22:08:34 -08006578#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6579 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6580
6581#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6582static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6583 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006584 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006586{
6587 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6589 4 + data_length + prf->other_secret_length :
6590 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006591
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6593 return PSA_ERROR_INVALID_ARGUMENT;
6594 }
John Durkop07cc04a2020-11-16 22:08:34 -08006595
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 uint8_t *pms = mbedtls_calloc(1, pms_len);
6597 if (pms == NULL) {
6598 return PSA_ERROR_INSUFFICIENT_MEMORY;
6599 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006600 uint8_t *cur = pms;
6601
6602 /* pure-PSK:
6603 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006604 *
6605 * The premaster secret is formed as follows: if the PSK is N octets
6606 * long, concatenate a uint16 with the value N, N zero octets, a second
6607 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006608 *
6609 * mixed-PSK:
6610 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6611 * follows: concatenate a uint16 with the length of the other secret,
6612 * the other secret itself, uint16 with the length of PSK, and the
6613 * PSK itself.
6614 * For details please check:
6615 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6616 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6617 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006618 */
6619
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6621 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6622 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6623 if (prf->other_secret_length != 0) {
6624 memcpy(cur, prf->other_secret, prf->other_secret_length);
6625 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006626 cur += prf->other_secret_length;
6627 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 } else {
6629 *cur++ = MBEDTLS_BYTE_1(data_length);
6630 *cur++ = MBEDTLS_BYTE_0(data_length);
6631 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006632 cur += data_length;
6633 }
6634
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 *cur++ = MBEDTLS_BYTE_1(data_length);
6636 *cur++ = MBEDTLS_BYTE_0(data_length);
6637 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006638 cur += data_length;
6639
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006641
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006642 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006644}
Janos Follath6660f0e2019-06-17 08:44:03 +01006645
Przemek Stekiele47201b2022-04-19 13:53:28 +02006646static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6647 psa_tls12_prf_key_derivation_t *prf,
6648 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006649 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006650{
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6652 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006653 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006654
6655 if (data_length != 0) {
6656 prf->other_secret = mbedtls_calloc(1, data_length);
6657 if (prf->other_secret == NULL) {
6658 return PSA_ERROR_INSUFFICIENT_MEMORY;
6659 }
6660
6661 memcpy(prf->other_secret, data, data_length);
6662 prf->other_secret_length = data_length;
6663 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006664 prf->other_secret_length = 0;
6665 }
6666
6667 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6668
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006670}
6671
Janos Follath6660f0e2019-06-17 08:44:03 +01006672static psa_status_t psa_tls12_prf_psk_to_ms_input(
6673 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006674 psa_key_derivation_step_t step,
6675 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006677{
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006679 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 return psa_tls12_prf_psk_to_ms_set_key(prf,
6681 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006682 break;
6683 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6685 data,
6686 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006687 break;
6688 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006690 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006691
Przemek Stekiele47201b2022-04-19 13:53:28 +02006692 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006693}
John Durkop07cc04a2020-11-16 22:08:34 -08006694#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006695
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006696#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6697static psa_status_t psa_tls12_ecjpake_to_pms_input(
6698 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006699 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006700 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006702{
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6704 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6705 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006706 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006707
6708 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 if (data[0] != 0x04) {
6710 return PSA_ERROR_INVALID_ARGUMENT;
6711 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006712
6713 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006715
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006717}
6718#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306719
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306720#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306721static psa_status_t psa_pbkdf2_set_input_cost(
6722 psa_pbkdf2_key_derivation_t *pbkdf2,
6723 psa_key_derivation_step_t step,
6724 uint64_t data)
6725{
6726 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6727 return PSA_ERROR_INVALID_ARGUMENT;
6728 }
6729
6730 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6731 return PSA_ERROR_BAD_STATE;
6732 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306733
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306734 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306735 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306736 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306737
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306738 if (data == 0) {
6739 return PSA_ERROR_INVALID_ARGUMENT;
6740 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306741
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306742 pbkdf2->input_cost = data;
6743 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6744
6745 return PSA_SUCCESS;
6746}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306747
6748static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6749 const uint8_t *data,
6750 size_t data_length)
6751{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306752 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6753 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6754 return PSA_ERROR_BAD_STATE;
6755 }
6756
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306757 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6758 pbkdf2->salt = mbedtls_calloc(1, data_length);
6759 if (pbkdf2->salt == NULL) {
6760 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306761 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306762
6763 memcpy(pbkdf2->salt, data, data_length);
6764 pbkdf2->salt_length = data_length;
6765 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306766 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306767
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306768 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6769 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306770 return PSA_ERROR_INSUFFICIENT_MEMORY;
6771 }
6772
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306773 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6774 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306775 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306776 mbedtls_free(pbkdf2->salt);
6777 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306778 }
6779
6780 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6781
6782 return PSA_SUCCESS;
6783}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306784
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306785#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306786static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306787 const uint8_t *input,
6788 size_t input_len,
6789 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306790 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306791{
6792 psa_status_t status = PSA_SUCCESS;
6793 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6794 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306795 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306796 } else {
6797 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306798 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306799 }
6800 return status;
6801}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306802#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306803
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306804#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306805static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6806 size_t input_len,
6807 uint8_t *output,
6808 size_t *output_len)
6809{
6810 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306811 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306812 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306813 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306814 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6815 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6816 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306817 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306818 * mac_size as the driver function sets mac_output_length = mac_size
6819 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306820 status = psa_driver_wrapper_mac_compute(&attributes,
6821 zeros, sizeof(zeros),
6822 PSA_ALG_CMAC, input, input_len,
6823 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306824 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6825 128U,
6826 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306827 output_len);
6828 } else {
6829 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306830 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306831 }
6832 return status;
6833}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306834#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306835
6836static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306837 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306838 const uint8_t *data,
6839 size_t data_length)
6840{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306841 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306842 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6843 return PSA_ERROR_BAD_STATE;
6844 }
6845
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306846#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306847 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6848 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6849 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6850 pbkdf2->password,
6851 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306852 } else
6853#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6854#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6855 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306856 status = psa_pbkdf2_cmac_set_password(data, data_length,
6857 pbkdf2->password,
6858 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306859 } else
6860#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6861 {
6862 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306863 }
6864
6865 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6866
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306867 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306868}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306869
6870static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306871 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306872 psa_key_derivation_step_t step,
6873 const uint8_t *data,
6874 size_t data_length)
6875{
6876 switch (step) {
6877 case PSA_KEY_DERIVATION_INPUT_SALT:
6878 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6879 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306880 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306881 default:
6882 return PSA_ERROR_INVALID_ARGUMENT;
6883 }
6884}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306885#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306886
Gilles Peskineb8965192019-09-24 16:21:10 +02006887/** Check whether the given key type is acceptable for the given
6888 * input step of a key derivation.
6889 *
6890 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6891 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6892 * Both secret and non-secret inputs can alternatively have the type
6893 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6894 * that the input was passed as a buffer rather than via a key object.
6895 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006896static int psa_key_derivation_check_input_type(
6897 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006899{
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006901 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 if (key_type == PSA_KEY_TYPE_DERIVE) {
6903 return PSA_SUCCESS;
6904 }
6905 if (key_type == PSA_KEY_TYPE_NONE) {
6906 return PSA_SUCCESS;
6907 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006908 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006909 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 if (key_type == PSA_KEY_TYPE_DERIVE) {
6911 return PSA_SUCCESS;
6912 }
6913 if (key_type == PSA_KEY_TYPE_NONE) {
6914 return PSA_SUCCESS;
6915 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006916 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006917 case PSA_KEY_DERIVATION_INPUT_LABEL:
6918 case PSA_KEY_DERIVATION_INPUT_SALT:
6919 case PSA_KEY_DERIVATION_INPUT_INFO:
6920 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6922 return PSA_SUCCESS;
6923 }
6924 if (key_type == PSA_KEY_TYPE_NONE) {
6925 return PSA_SUCCESS;
6926 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006927 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306928 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6929 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6930 return PSA_SUCCESS;
6931 }
6932 if (key_type == PSA_KEY_TYPE_DERIVE) {
6933 return PSA_SUCCESS;
6934 }
6935 if (key_type == PSA_KEY_TYPE_NONE) {
6936 return PSA_SUCCESS;
6937 }
6938 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006939 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006941}
6942
Janos Follathb80a94e2019-06-12 15:54:46 +01006943static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006944 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006945 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006946 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006947 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006949{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006950 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006952
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 status = psa_key_derivation_check_input_type(step, key_type);
6954 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006955 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006957
Przemek Stekiel69c46792022-06-10 12:59:51 +02006958#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6960 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6961 step, data, data_length);
6962 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006963#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006964#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6966 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6967 step, data, data_length);
6968 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006969#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6970#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6972 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6973 step, data, data_length);
6974 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006975#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006976#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006978 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6980 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006981#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306982#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306983 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306984 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6985 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306986 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306987#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006988 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006989 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006990 (void) data;
6991 (void) data_length;
6992 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006994 }
6995
Gilles Peskine224b0d62019-09-23 18:13:17 +02006996exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 if (status != PSA_SUCCESS) {
6998 psa_key_derivation_abort(operation);
6999 }
7000 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007001}
7002
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307003static psa_status_t psa_key_derivation_input_integer_internal(
7004 psa_key_derivation_operation_t *operation,
7005 psa_key_derivation_step_t step,
7006 uint64_t value)
7007{
7008 psa_status_t status;
7009 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7010
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307011#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307012 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307013 status = psa_pbkdf2_set_input_cost(
7014 &operation->ctx.pbkdf2, step, value);
7015 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307016#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307017 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307018 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307019 (void) value;
7020 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307021 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307022 }
7023
7024 if (status != PSA_SUCCESS) {
7025 psa_key_derivation_abort(operation);
7026 }
7027 return status;
7028}
7029
Janos Follath51f4a0f2019-06-14 11:35:55 +01007030psa_status_t psa_key_derivation_input_bytes(
7031 psa_key_derivation_operation_t *operation,
7032 psa_key_derivation_step_t step,
7033 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007035{
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 return psa_key_derivation_input_internal(operation, step,
7037 PSA_KEY_TYPE_NONE,
7038 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007039}
7040
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307041psa_status_t psa_key_derivation_input_integer(
7042 psa_key_derivation_operation_t *operation,
7043 psa_key_derivation_step_t step,
7044 uint64_t value)
7045{
7046 return psa_key_derivation_input_integer_internal(operation, step, value);
7047}
7048
Janos Follath51f4a0f2019-06-14 11:35:55 +01007049psa_status_t psa_key_derivation_input_key(
7050 psa_key_derivation_operation_t *operation,
7051 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007053{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007055 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007056 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007057
Ronald Cron5c522922020-11-14 16:35:34 +01007058 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7060 if (status != PSA_SUCCESS) {
7061 psa_key_derivation_abort(operation);
7062 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007063 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007064
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307065 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7066 * permission to output to a key object. */
7067 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7068 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007069 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 status = psa_key_derivation_input_internal(operation,
7073 step, slot->attr.type,
7074 slot->key.data,
7075 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007076
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007078
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007080}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007081
7082
7083
7084/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007085/* Key agreement */
7086/****************************************************************/
7087
Gilles Peskine449bd832023-01-11 14:50:10 +01007088psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7089 const uint8_t *key_buffer,
7090 size_t key_buffer_size,
7091 psa_algorithm_t alg,
7092 const uint8_t *peer_key,
7093 size_t peer_key_length,
7094 uint8_t *shared_secret,
7095 size_t shared_secret_size,
7096 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007097{
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007099#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007100 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7102 key_buffer_size, alg,
7103 peer_key, peer_key_length,
7104 shared_secret,
7105 shared_secret_size,
7106 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007107#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007108
7109#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7110 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007111 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007112 peer_key,
7113 peer_key_length,
7114 key_buffer,
7115 key_buffer_size,
7116 shared_secret,
7117 shared_secret_size,
7118 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007119#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7120
Gilles Peskine01d718c2018-09-18 12:01:02 +02007121 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007122 (void) attributes;
7123 (void) key_buffer;
7124 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007125 (void) peer_key;
7126 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007127 (void) shared_secret;
7128 (void) shared_secret_size;
7129 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007131 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007132}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007133
Aditya Deshpande17845b82022-10-13 17:21:01 +01007134/** Internal function for raw key agreement
7135 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007136 * to the driver's implementation if a driver is present.
7137 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007138 * (psa_key_agreement_raw_builtin).
7139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007140static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7141 psa_key_slot_t *private_key,
7142 const uint8_t *peer_key,
7143 size_t peer_key_length,
7144 uint8_t *shared_secret,
7145 size_t shared_secret_size,
7146 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007147{
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7149 return PSA_ERROR_NOT_SUPPORTED;
7150 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007151
7152 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007154 };
7155
Gilles Peskine449bd832023-01-11 14:50:10 +01007156 return psa_driver_wrapper_key_agreement(&attributes,
7157 private_key->key.data,
7158 private_key->key.bytes, alg,
7159 peer_key, peer_key_length,
7160 shared_secret,
7161 shared_secret_size,
7162 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007163}
7164
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007165/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007166 * to potentially free embedded data structures and wipe confidential data.
7167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007168static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7169 psa_key_derivation_step_t step,
7170 psa_key_slot_t *private_key,
7171 const uint8_t *peer_key,
7172 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007173{
7174 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007175 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007176 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007178
7179 /* Step 1: run the secret agreement algorithm to generate the shared
7180 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 status = psa_key_agreement_raw_internal(ka_alg,
7182 private_key,
7183 peer_key, peer_key_length,
7184 shared_secret,
7185 sizeof(shared_secret),
7186 &shared_secret_length);
7187 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007188 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007190
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007191 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007192 * the shared secret. A shared secret is permitted wherever a key
7193 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 status = psa_key_derivation_input_internal(operation, step,
7195 PSA_KEY_TYPE_DERIVE,
7196 shared_secret,
7197 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007199 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7200 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007201}
7202
Gilles Peskine449bd832023-01-11 14:50:10 +01007203psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7204 psa_key_derivation_step_t step,
7205 mbedtls_svc_key_id_t private_key,
7206 const uint8_t *peer_key,
7207 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007208{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007209 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007210 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007211 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007212
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7214 return PSA_ERROR_INVALID_ARGUMENT;
7215 }
Ronald Cron5c522922020-11-14 16:35:34 +01007216 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7218 if (status != PSA_SUCCESS) {
7219 return status;
7220 }
7221 status = psa_key_agreement_internal(operation, step,
7222 slot,
7223 peer_key, peer_key_length);
7224 if (status != PSA_SUCCESS) {
7225 psa_key_derivation_abort(operation);
7226 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007227 /* If a private key has been added as SECRET, we allow the derived
7228 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007230 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007232 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007233
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007235
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007237}
7238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7240 mbedtls_svc_key_id_t private_key,
7241 const uint8_t *peer_key,
7242 size_t peer_key_length,
7243 uint8_t *output,
7244 size_t output_size,
7245 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007246{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007248 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007249 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007250 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007251
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007253 status = PSA_ERROR_INVALID_ARGUMENT;
7254 goto exit;
7255 }
Ronald Cron5c522922020-11-14 16:35:34 +01007256 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7258 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007259 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007261
Gilles Peskine3e561302022-04-14 00:17:15 +02007262 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7263 * for the output size. The PSA specification only guarantees that this
7264 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7265 * but it might be nice to allow smaller buffers if the output fits.
7266 * At the time of writing this comment, with only ECDH implemented,
7267 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7268 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7269 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007270 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7272 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007273 status = PSA_ERROR_BUFFER_TOO_SMALL;
7274 goto exit;
7275 }
7276
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 status = psa_key_agreement_raw_internal(alg, slot,
7278 peer_key, peer_key_length,
7279 output, output_size,
7280 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007281
7282exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007284 /* If an error happens and is not handled properly, the output
7285 * may be used as a key to protect sensitive data. Arrange for such
7286 * a key to be random, which is likely to result in decryption or
7287 * verification errors. This is better than filling the buffer with
7288 * some constant data such as zeros, which would result in the data
7289 * being protected with a reproducible, easily knowable key.
7290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007292 *output_length = output_size;
7293 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007294
Gilles Peskine449bd832023-01-11 14:50:10 +01007295 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007296
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007298}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007299
7300
Gilles Peskine30524eb2020-11-13 17:02:26 +01007301
Gilles Peskine86a440b2018-11-12 18:39:40 +01007302/****************************************************************/
7303/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007304/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007305
Gilles Peskine672a7712023-04-28 21:00:28 +02007306#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7307#include "entropy_poll.h"
7308#endif
7309
Gilles Peskine30524eb2020-11-13 17:02:26 +01007310/** Initialize the PSA random generator.
7311 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007312static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007313{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007314#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007315 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007316#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7317
Gilles Peskine30524eb2020-11-13 17:02:26 +01007318 /* Set default configuration if
7319 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007321 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 }
7323 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007324 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007326
Gilles Peskine449bd832023-01-11 14:50:10 +01007327 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007328#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7329 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7330 /* The PSA entropy injection feature depends on using NV seed as an entropy
7331 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 mbedtls_entropy_add_source(&rng->entropy,
7333 mbedtls_nv_seed_poll, NULL,
7334 MBEDTLS_ENTROPY_BLOCK_SIZE,
7335 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007336#endif
7337
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007339#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007340}
7341
7342/** Deinitialize the PSA random generator.
7343 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007344static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007345{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007346#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007348#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7350 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007351#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007352}
7353
7354/** Seed the PSA random generator.
7355 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007356static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007357{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007358#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7359 /* Do nothing: the external RNG seeds itself. */
7360 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007361 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007362#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007363 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7365 drbg_seed, sizeof(drbg_seed) - 1);
7366 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007367#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007368}
7369
Gilles Peskine449bd832023-01-11 14:50:10 +01007370psa_status_t psa_generate_random(uint8_t *output,
7371 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007372{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007373 GUARD_MODULE_INITIALIZED;
7374
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007375#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7376
7377 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7379 output, output_size,
7380 &output_length);
7381 if (status != PSA_SUCCESS) {
7382 return status;
7383 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007384 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007385 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 if (output_length != output_size) {
7387 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7388 }
7389 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007390
7391#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7392
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007394 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7396 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7397 output_size);
7398 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7399 output, request_size);
7400 if (ret != 0) {
7401 return mbedtls_to_psa_error(ret);
7402 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007403 output_size -= request_size;
7404 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007405 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007406 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007407#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007408}
7409
Gilles Peskine8814fc42020-12-14 15:33:44 +01007410/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007411 *
7412 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7413 * `psa_generate_random(...)`. The state parameter is ignored since the
7414 * PSA API doesn't support passing an explicit state.
7415 *
7416 * In the non-external case, psa_generate_random() calls an
7417 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7418 * and semantics as mbedtls_psa_get_random(). As an optimization,
7419 * instead of doing this back-and-forth between the PSA API and the
7420 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7421 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007422 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007423#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7424int mbedtls_psa_get_random(void *p_rng,
7425 unsigned char *output,
7426 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007427{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007428 /* This function takes a pointer to the RNG state because that's what
7429 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7430 * its own state internally and doesn't let the caller access that state.
7431 * So we just ignore the state parameter, and in practice we'll pass
7432 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007433 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 psa_status_t status = psa_generate_random(output, output_size);
7435 if (status == PSA_SUCCESS) {
7436 return 0;
7437 } else {
7438 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7439 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007440}
7441#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7442
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007443#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007444psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7445 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007446{
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 if (global_data.initialized) {
7448 return PSA_ERROR_NOT_PERMITTED;
7449 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007450
Gilles Peskine449bd832023-01-11 14:50:10 +01007451 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7452 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7453 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7454 return PSA_ERROR_INVALID_ARGUMENT;
7455 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007456
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007458}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007459#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007460
Ronald Cron3772afe2021-02-08 16:10:05 +01007461/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007462 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007463 * \param type The key type
7464 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007465 *
7466 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007467 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007468 * \retval #PSA_ERROR_INVALID_ARGUMENT
7469 * The size in bits of the key is not valid.
7470 * \retval #PSA_ERROR_NOT_SUPPORTED
7471 * The type and/or the size in bits of the key or the combination of
7472 * the two is not supported.
7473 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007474static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007476{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007478
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 if (key_type_is_raw_bytes(type)) {
7480 status = psa_validate_unstructured_key_bit_size(type, bits);
7481 if (status != PSA_SUCCESS) {
7482 return status;
7483 }
7484 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007485#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7487 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7488 return PSA_ERROR_NOT_SUPPORTED;
7489 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007490 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007491 return PSA_ERROR_NOT_SUPPORTED;
7492 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007493
Ronald Cron01b2aba2020-10-05 09:42:02 +02007494 /* Accept only byte-aligned keys, for the same reasons as
7495 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 if (bits % 8 != 0) {
7497 return PSA_ERROR_NOT_SUPPORTED;
7498 }
7499 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007500#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007501
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007502#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007504 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 return PSA_SUCCESS;
7506 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007507#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007508
Valerio Settia55f0422023-07-10 15:34:41 +02007509#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007510 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007511 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007512 return PSA_ERROR_NOT_SUPPORTED;
7513 }
7514 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007515#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007516 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007518 }
7519
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007521}
7522
Ronald Cron55ed0592020-10-05 10:30:40 +02007523psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007524 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007526{
7527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007528 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007529
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 if ((attributes->domain_parameters == NULL) &&
7531 (attributes->domain_parameters_size != 0)) {
7532 return PSA_ERROR_INVALID_ARGUMENT;
7533 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007534
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 if (key_type_is_raw_bytes(type)) {
7536 status = psa_generate_random(key_buffer, key_buffer_size);
7537 if (status != PSA_SUCCESS) {
7538 return status;
7539 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007540
David Brown12ca5032021-02-11 11:02:00 -07007541#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 if (type == PSA_KEY_TYPE_DES) {
7543 psa_des_set_key_parity(key_buffer, key_buffer_size);
7544 }
David Brown8107e312021-02-12 08:08:46 -07007545#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007547
Valerio Setti76df8c12023-07-11 14:11:28 +02007548#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7550 return mbedtls_psa_rsa_generate_key(attributes,
7551 key_buffer,
7552 key_buffer_size,
7553 key_buffer_length);
7554 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007555#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007556
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007557#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7559 return mbedtls_psa_ecp_generate_key(attributes,
7560 key_buffer,
7561 key_buffer_size,
7562 key_buffer_length);
7563 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007564#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007565
Valerio Settia55f0422023-07-10 15:34:41 +02007566#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007567 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7568 return mbedtls_psa_ffdh_generate_key(attributes,
7569 key_buffer,
7570 key_buffer_size,
7571 key_buffer_length);
7572 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007573#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007574 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007575 (void) key_buffer_length;
7576 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007577 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007578
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007580}
Darryl Green0c6575a2018-11-07 16:05:30 +00007581
Gilles Peskine449bd832023-01-11 14:50:10 +01007582psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7583 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007584{
7585 psa_status_t status;
7586 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007587 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007588 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007589
Ronald Cron81709fc2020-11-14 12:10:32 +01007590 *key = MBEDTLS_SVC_KEY_ID_INIT;
7591
Gilles Peskine0f84d622019-09-12 19:03:13 +02007592 /* Reject any attempt to create a zero-length key so that we don't
7593 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 if (psa_get_key_bits(attributes) == 0) {
7595 return PSA_ERROR_INVALID_ARGUMENT;
7596 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007597
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007598 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7600 return PSA_ERROR_INVALID_ARGUMENT;
7601 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007602
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7604 &slot, &driver);
7605 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007606 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 }
Gilles Peskine11792082019-08-06 18:36:36 +02007608
Ronald Cron977c2472020-10-13 08:32:21 +02007609 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307610 * storage ( thus not in the case of generating a key in a secure element
7611 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7612 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 if (slot->key.data == NULL) {
7614 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7615 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007616 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 attributes->core.type, attributes->core.bits);
7618 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007621
7622 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 attributes->core.type,
7624 attributes->core.bits);
7625 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007626 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 attributes, &key_buffer_size);
7628 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 }
Ronald Cron977c2472020-10-13 08:32:21 +02007631 }
Ronald Cron977c2472020-10-13 08:32:21 +02007632
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7634 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007635 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 }
Ronald Cron977c2472020-10-13 08:32:21 +02007637 }
7638
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 status = psa_driver_wrapper_generate_key(attributes,
7640 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007641
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 if (status != PSA_SUCCESS) {
7643 psa_remove_key_data_from_memory(slot);
7644 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007645
Gilles Peskine11792082019-08-06 18:36:36 +02007646exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 if (status == PSA_SUCCESS) {
7648 status = psa_finish_key_creation(slot, driver, key);
7649 }
7650 if (status != PSA_SUCCESS) {
7651 psa_fail_key_creation(slot, driver);
7652 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007653
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007655}
7656
Gilles Peskinee59236f2018-01-27 23:32:46 +01007657/****************************************************************/
7658/* Module setup */
7659/****************************************************************/
7660
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007661#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007662psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 void (* entropy_init)(mbedtls_entropy_context *ctx),
7664 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007665{
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7667 return PSA_ERROR_BAD_STATE;
7668 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007669 global_data.rng.entropy_init = entropy_init;
7670 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007671 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007672}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007673#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007674
Gilles Peskine449bd832023-01-11 14:50:10 +01007675void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007676{
Gilles Peskine449bd832023-01-11 14:50:10 +01007677 psa_wipe_all_key_slots();
7678 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7679 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007680 }
7681 /* Wipe all remaining data, including configuration.
7682 * In particular, this sets all state indicator to the value
7683 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007685
7686 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007688}
7689
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007690#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7691/** Recover a transaction that was interrupted by a power failure.
7692 *
7693 * This function is called during initialization, before psa_crypto_init()
7694 * returns. If this function returns a failure status, the initialization
7695 * fails.
7696 */
7697static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007699{
Gilles Peskine449bd832023-01-11 14:50:10 +01007700 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007701 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7702 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007703 /* TODO - fall through to the failure case until this
7704 * is implemented.
7705 * https://github.com/ARMmbed/mbed-crypto/issues/218
7706 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007707 default:
7708 /* We found an unsupported transaction in the storage.
7709 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007711 }
7712}
7713#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7714
Gilles Peskine449bd832023-01-11 14:50:10 +01007715psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007716{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007717 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007718
Gilles Peskinec6b69072018-11-20 21:42:52 +01007719 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 if (global_data.initialized != 0) {
7721 return PSA_SUCCESS;
7722 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007723
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007724 /* Init drivers */
7725 status = psa_driver_wrapper_init();
7726 if (status != PSA_SUCCESS) {
7727 goto exit;
7728 }
7729 global_data.drivers_initialized = 1;
7730
Gilles Peskine30524eb2020-11-13 17:02:26 +01007731 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007733 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007734 status = mbedtls_psa_random_seed(&global_data.rng);
7735 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007736 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007738 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007739
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 status = psa_initialize_key_slots();
7741 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007742 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007744
Gilles Peskine4b734222019-07-24 15:56:31 +02007745#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 status = psa_crypto_load_transaction();
7747 if (status == PSA_SUCCESS) {
7748 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7749 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007750 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 }
7752 status = psa_crypto_stop_transaction();
7753 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007754 /* There's no transaction to complete. It's all good. */
7755 status = PSA_SUCCESS;
7756 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007757#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007758
Gilles Peskinec6b69072018-11-20 21:42:52 +01007759 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007760 global_data.initialized = 1;
7761
7762exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007763 if (status != PSA_SUCCESS) {
7764 mbedtls_psa_crypto_free();
7765 }
7766 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007767}
7768
Yanray Wangffc3c482023-07-11 12:01:04 +08007769#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007770psa_status_t psa_crypto_driver_pake_get_password_len(
7771 const psa_crypto_driver_pake_inputs_t *inputs,
7772 size_t *password_len)
7773{
7774 if (inputs->password_len == 0) {
7775 return PSA_ERROR_BAD_STATE;
7776 }
7777
7778 *password_len = inputs->password_len;
7779
7780 return PSA_SUCCESS;
7781}
7782
7783psa_status_t psa_crypto_driver_pake_get_password(
7784 const psa_crypto_driver_pake_inputs_t *inputs,
7785 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7786{
7787 if (inputs->password_len == 0) {
7788 return PSA_ERROR_BAD_STATE;
7789 }
7790
7791 if (buffer_size < inputs->password_len) {
7792 return PSA_ERROR_BUFFER_TOO_SMALL;
7793 }
7794
7795 memcpy(buffer, inputs->password, inputs->password_len);
7796 *buffer_length = inputs->password_len;
7797
7798 return PSA_SUCCESS;
7799}
7800
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007801psa_status_t psa_crypto_driver_pake_get_user_len(
7802 const psa_crypto_driver_pake_inputs_t *inputs,
7803 size_t *user_len)
7804{
7805 if (inputs->user_len == 0) {
7806 return PSA_ERROR_BAD_STATE;
7807 }
7808
7809 *user_len = inputs->user_len;
7810
7811 return PSA_SUCCESS;
7812}
7813
7814psa_status_t psa_crypto_driver_pake_get_user(
7815 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007816 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007817{
7818 if (inputs->user_len == 0) {
7819 return PSA_ERROR_BAD_STATE;
7820 }
7821
Przemek Stekielc0e62502023-03-14 11:49:36 +01007822 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007823 return PSA_ERROR_BUFFER_TOO_SMALL;
7824 }
7825
Przemek Stekielc0e62502023-03-14 11:49:36 +01007826 memcpy(user_id, inputs->user, inputs->user_len);
7827 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007828
7829 return PSA_SUCCESS;
7830}
7831
7832psa_status_t psa_crypto_driver_pake_get_peer_len(
7833 const psa_crypto_driver_pake_inputs_t *inputs,
7834 size_t *peer_len)
7835{
7836 if (inputs->peer_len == 0) {
7837 return PSA_ERROR_BAD_STATE;
7838 }
7839
7840 *peer_len = inputs->peer_len;
7841
7842 return PSA_SUCCESS;
7843}
7844
7845psa_status_t psa_crypto_driver_pake_get_peer(
7846 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007847 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007848{
7849 if (inputs->peer_len == 0) {
7850 return PSA_ERROR_BAD_STATE;
7851 }
7852
Przemek Stekielc0e62502023-03-14 11:49:36 +01007853 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007854 return PSA_ERROR_BUFFER_TOO_SMALL;
7855 }
7856
Przemek Stekielc0e62502023-03-14 11:49:36 +01007857 memcpy(peer_id, inputs->peer, inputs->peer_len);
7858 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007859
7860 return PSA_SUCCESS;
7861}
7862
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007863psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7864 const psa_crypto_driver_pake_inputs_t *inputs,
7865 psa_pake_cipher_suite_t *cipher_suite)
7866{
7867 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7868 return PSA_ERROR_BAD_STATE;
7869 }
7870
7871 *cipher_suite = inputs->cipher_suite;
7872
7873 return PSA_SUCCESS;
7874}
7875
Neil Armstronga7d08c32022-06-01 18:21:20 +02007876psa_status_t psa_pake_setup(
7877 psa_pake_operation_t *operation,
7878 const psa_pake_cipher_suite_t *cipher_suite)
7879{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007881
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007882 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007883 status = PSA_ERROR_BAD_STATE;
7884 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007885 }
7886
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007887 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007888 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007889 status = PSA_ERROR_INVALID_ARGUMENT;
7890 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007891 }
7892
Przemek Stekiel51eac532022-12-07 11:04:51 +01007893 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7894
Przemek Stekiele12ed362022-12-21 12:54:46 +01007895 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007896 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7897 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007898 operation->data.inputs.cipher_suite = *cipher_suite;
7899
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007900#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007901 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007902 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007903 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007904
David Horstmann16f01512023-06-14 17:21:07 +01007905 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007906 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007907 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007908#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007909 {
7910 status = PSA_ERROR_NOT_SUPPORTED;
7911 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007912 }
7913
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007914 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7915
Przemek Stekiel51eac532022-12-07 11:04:51 +01007916 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007917exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007918 psa_pake_abort(operation);
7919 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007920}
7921
7922psa_status_t psa_pake_set_password_key(
7923 psa_pake_operation_t *operation,
7924 mbedtls_svc_key_id_t password)
7925{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007927 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7928 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007929 psa_key_attributes_t attributes;
7930 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007931
Przemek Stekiel51eac532022-12-07 11:04:51 +01007932 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007933 status = PSA_ERROR_BAD_STATE;
7934 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007935 }
7936
Przemek Stekiel6c764412022-11-22 14:05:12 +01007937 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7938 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007939 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007940 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007941 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007942 }
7943
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007944 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007945 .core = slot->attr
7946 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007947
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007948 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007949
Przemek Stekiel51eac532022-12-07 11:04:51 +01007950 if (type != PSA_KEY_TYPE_PASSWORD &&
7951 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7952 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007953 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007954 }
7955
Przemek Stekiel51eac532022-12-07 11:04:51 +01007956 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7957 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007958 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007959 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007960 }
7961
7962 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7963 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007964 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007965exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007966 if (status != PSA_SUCCESS) {
7967 psa_pake_abort(operation);
7968 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007969 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007970 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007971}
7972
7973psa_status_t psa_pake_set_user(
7974 psa_pake_operation_t *operation,
7975 const uint8_t *user_id,
7976 size_t user_id_len)
7977{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007978 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007979
7980 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007981 status = PSA_ERROR_BAD_STATE;
7982 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007983 }
7984
Przemek Stekiel51eac532022-12-07 11:04:51 +01007985 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007986 status = PSA_ERROR_INVALID_ARGUMENT;
7987 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007988 }
7989
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007990 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007991 status = PSA_ERROR_BAD_STATE;
7992 goto exit;
7993 }
7994
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007995 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7996 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007997 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7998 goto exit;
7999 }
8000
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008001 memcpy(operation->data.inputs.user, user_id, user_id_len);
8002 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008003
8004 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008005exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008006 psa_pake_abort(operation);
8007 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008008}
8009
8010psa_status_t psa_pake_set_peer(
8011 psa_pake_operation_t *operation,
8012 const uint8_t *peer_id,
8013 size_t peer_id_len)
8014{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008015 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008016
8017 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008018 status = PSA_ERROR_BAD_STATE;
8019 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008020 }
8021
Przemek Stekiel51eac532022-12-07 11:04:51 +01008022 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008023 status = PSA_ERROR_INVALID_ARGUMENT;
8024 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008025 }
8026
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008027 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008028 status = PSA_ERROR_BAD_STATE;
8029 goto exit;
8030 }
8031
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008032 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8033 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008034 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8035 goto exit;
8036 }
8037
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008038 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8039 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008040
8041 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008042exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008043 psa_pake_abort(operation);
8044 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008045}
8046
8047psa_status_t psa_pake_set_role(
8048 psa_pake_operation_t *operation,
8049 psa_pake_role_t role)
8050{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008052
Przemek Stekiel51eac532022-12-07 11:04:51 +01008053 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008054 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008055 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008056 }
8057
Przemek Stekiel09104b82023-03-06 14:11:51 +01008058 switch (operation->alg) {
8059#if defined(PSA_WANT_ALG_JPAKE)
8060 case PSA_ALG_JPAKE:
8061 if (role == PSA_PAKE_ROLE_NONE) {
8062 return PSA_SUCCESS;
8063 }
8064 status = PSA_ERROR_INVALID_ARGUMENT;
8065 break;
8066#endif
8067 default:
8068 (void) role;
8069 status = PSA_ERROR_NOT_SUPPORTED;
8070 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008071 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008072exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008073 psa_pake_abort(operation);
8074 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008075}
8076
David Horstmanne7f21e62023-05-12 18:17:21 +01008077/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008078#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008079static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008080 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008081{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008082 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008083 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008084 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008085
David Horstmann024e5c52023-06-14 15:48:21 +01008086 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008087 is_x1 = (stage->outputs < 1);
8088 } else {
8089 is_x1 = (stage->inputs < 1);
8090 }
8091
David Horstmann74a3d8c2023-06-14 18:28:19 +01008092 key_share_step = is_x1 ?
8093 PSA_JPAKE_X1_STEP_KEY_SHARE :
8094 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008095 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008096 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8097 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8098 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8099 } else {
8100 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008101 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008102 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008103}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008104#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008105
Przemek Stekiel2797d372022-12-22 11:19:22 +01008106static psa_status_t psa_pake_complete_inputs(
8107 psa_pake_operation_t *operation)
8108{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008110 /* Create copy of the inputs on stack as inputs share memory
8111 with the driver context which will be setup by the driver. */
8112 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008113
Przemek Stekielfde11282023-03-13 16:06:09 +01008114 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008115 return PSA_ERROR_BAD_STATE;
8116 }
8117
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008118 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008119 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8120 return PSA_ERROR_BAD_STATE;
8121 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008122 }
8123
Przemek Stekiel18620a32023-01-17 16:34:52 +01008124 /* Clear driver context */
8125 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8126
8127 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008128
8129 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008130 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008131
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008132 /* User and peer are translated to role. */
8133 mbedtls_free(inputs.user);
8134 mbedtls_free(inputs.peer);
8135
Przemek Stekiel2797d372022-12-22 11:19:22 +01008136 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008137#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008138 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008139 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008140 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008141#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008142 {
8143 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008144 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008145 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008146 return status;
8147}
8148
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008149#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008150static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008152 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008153 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008154{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8156 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8157 step != PSA_PAKE_STEP_ZK_PROOF) {
8158 return PSA_ERROR_INVALID_ARGUMENT;
8159 }
8160
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008161 psa_jpake_computation_stage_t *computation_stage =
8162 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008163
David Horstmann5da95602023-06-08 15:37:12 +01008164 if (computation_stage->round != PSA_JPAKE_FIRST &&
8165 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008166 return PSA_ERROR_BAD_STATE;
8167 }
8168
David Horstmanne7f21e62023-05-12 18:17:21 +01008169 /* Check that the step we are given is the one we were expecting */
8170 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008171 return PSA_ERROR_BAD_STATE;
8172 }
8173
David Horstmanne7f21e62023-05-12 18:17:21 +01008174 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8175 computation_stage->inputs == 0 &&
8176 computation_stage->outputs == 0) {
8177 /* Start of the round, so function decides whether we are inputting
8178 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008179 computation_stage->io_mode = io_mode;
8180 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008181 /* Middle of the round so the mode we are in must match the function
8182 * called by the user */
8183 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008184 }
8185
8186 return PSA_SUCCESS;
8187}
8188
David Horstmanne7f21e62023-05-12 18:17:21 +01008189static psa_status_t psa_jpake_epilogue(
8190 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008191 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008192{
David Horstmanne7f21e62023-05-12 18:17:21 +01008193 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008194 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008195
David Horstmanne7f21e62023-05-12 18:17:21 +01008196 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8197 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008198 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008199 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008200 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008201 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008202 }
8203 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008204 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008205 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008206 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008207 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008208 }
8209 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008210 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8211 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008212 /* End of a round, move to the next round */
8213 stage->inputs = 0;
8214 stage->outputs = 0;
8215 stage->round++;
8216 }
8217 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008218 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008219 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008220 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008221 return PSA_SUCCESS;
8222}
David Horstmanne7f21e62023-05-12 18:17:21 +01008223
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008224#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008225
Neil Armstronga7d08c32022-06-01 18:21:20 +02008226psa_status_t psa_pake_output(
8227 psa_pake_operation_t *operation,
8228 psa_pake_step_t step,
8229 uint8_t *output,
8230 size_t output_size,
8231 size_t *output_length)
8232{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008233 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008234 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008235 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008236
8237 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008238 status = psa_pake_complete_inputs(operation);
8239 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008240 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008241 }
8242 }
8243
8244 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008245 status = PSA_ERROR_BAD_STATE;
8246 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008247 }
8248
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008249 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008250 status = PSA_ERROR_INVALID_ARGUMENT;
8251 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008252 }
8253
Przemek Stekiele12ed362022-12-21 12:54:46 +01008254 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008255#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008256 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008257 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008259 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008260 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008261 driver_step = convert_jpake_computation_stage_to_driver_step(
8262 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008263 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008264#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008265 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008266 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008267 status = PSA_ERROR_NOT_SUPPORTED;
8268 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008269 }
8270
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008271 status = psa_driver_wrapper_pake_output(operation, driver_step,
8272 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008273
8274 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008275 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 }
8277
8278 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008279#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008280 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008281 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008282 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008283 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008284 }
8285 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008286#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008287 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008288 status = PSA_ERROR_NOT_SUPPORTED;
8289 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008290 }
8291
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008292 return PSA_SUCCESS;
8293exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008294 psa_pake_abort(operation);
8295 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008296}
8297
8298psa_status_t psa_pake_input(
8299 psa_pake_operation_t *operation,
8300 psa_pake_step_t step,
8301 const uint8_t *input,
8302 size_t input_length)
8303{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008305 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008306 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8307 operation->primitive,
8308 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008309
8310 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008311 status = psa_pake_complete_inputs(operation);
8312 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008313 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008314 }
8315 }
8316
8317 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008318 status = PSA_ERROR_BAD_STATE;
8319 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008320 }
8321
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008322 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008323 status = PSA_ERROR_INVALID_ARGUMENT;
8324 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008325 }
8326
Przemek Stekiele12ed362022-12-21 12:54:46 +01008327 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008328#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008329 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008330 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008331 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008332 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008333 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008334 driver_step = convert_jpake_computation_stage_to_driver_step(
8335 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008336 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008337#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008338 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008339 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008340 status = PSA_ERROR_NOT_SUPPORTED;
8341 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008342 }
8343
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008344 status = psa_driver_wrapper_pake_input(operation, driver_step,
8345 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008346
8347 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008348 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008349 }
8350
8351 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008352#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008353 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008354 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008355 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008356 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008357 }
8358 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008359#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008360 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008361 status = PSA_ERROR_NOT_SUPPORTED;
8362 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008363 }
8364
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008365 return PSA_SUCCESS;
8366exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008367 psa_pake_abort(operation);
8368 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008369}
8370
8371psa_status_t psa_pake_get_implicit_key(
8372 psa_pake_operation_t *operation,
8373 psa_key_derivation_operation_t *output)
8374{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008376 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008377 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008378 size_t shared_key_len = 0;
8379
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008380 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008381 status = PSA_ERROR_BAD_STATE;
8382 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008383 }
8384
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008385#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008386 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008387 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008388 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008389 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008390 status = PSA_ERROR_BAD_STATE;
8391 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008392 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008393 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008394#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008395 {
8396 status = PSA_ERROR_NOT_SUPPORTED;
8397 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008398 }
8399
Przemek Stekiel0c781802022-11-29 14:53:13 +01008400 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8401 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008402 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008403 &shared_key_len);
8404
8405 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008406 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008407 }
8408
8409 status = psa_key_derivation_input_bytes(output,
8410 PSA_KEY_DERIVATION_INPUT_SECRET,
8411 shared_key,
8412 shared_key_len);
8413
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008414 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008415exit:
8416 abort_status = psa_pake_abort(operation);
8417 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008418}
8419
8420psa_status_t psa_pake_abort(
8421 psa_pake_operation_t *operation)
8422{
Przemek Stekield69dca92023-01-31 19:59:20 +01008423 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008424
Przemek Stekield69dca92023-01-31 19:59:20 +01008425 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008426 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008427 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008428
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008429 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8430 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008431 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008432 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008433 }
8434 if (operation->data.inputs.user != NULL) {
8435 mbedtls_free(operation->data.inputs.user);
8436 }
8437 if (operation->data.inputs.peer != NULL) {
8438 mbedtls_free(operation->data.inputs.peer);
8439 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008440 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008441 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008442
Przemek Stekield69dca92023-01-31 19:59:20 +01008443 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008444}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008445#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008446
Gilles Peskinee59236f2018-01-27 23:32:46 +01008447#endif /* MBEDTLS_PSA_CRYPTO_C */