blob: 42a0fa7f0bfb98e41c0f4ee2cb0d08b8a8156231 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010038#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010039#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010040#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010042#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020044#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020045#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010046#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010047/* Include internal declarations that are useful for implementing persistently
48 * stored keys. */
49#include "psa_crypto_storage.h"
50
Gilles Peskineb2b64d32020-12-14 16:43:58 +010051#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010052
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020062#include "mbedtls/chacha20.h"
63#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020068#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/error.h"
72#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010074#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000075#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010076#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000077#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010078#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000079#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/sha1.h"
83#include "mbedtls/sha256.h"
84#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020085#include "md_psa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020090#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020091#endif
92
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010093/****************************************************************/
94/* Global data, support functions and library management */
95/****************************************************************/
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100}
101
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100102/* Values for psa_global_data_t::rng_state */
103#define RNG_NOT_INITIALIZED 0
104#define RNG_INITIALIZED 1
105#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100110 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100111 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112} psa_global_data_t;
113
114static psa_global_data_t global_data;
115
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100116#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
117mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
118 &global_data.rng.drbg;
119#endif
120
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (global_data.initialized == 0) \
123 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100125int psa_can_do_hash(psa_algorithm_t hash_alg)
126{
127 (void) hash_alg;
128 return global_data.drivers_initialized;
129}
Valerio Setti8bb57632023-05-26 13:48:07 +0200130#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200131 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Setti8bb57632023-05-26 13:48:07 +0200132 defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200133static int psa_is_dh_key_size_valid(size_t bits)
134{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200135 if (bits != 2048 && bits != 3072 && bits != 4096 &&
136 bits != 6144 && bits != 8192) {
137 return 0;
138 }
139
140 return 1;
141}
Valerio Setti8bb57632023-05-26 13:48:07 +0200142#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200143 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Setti8bb57632023-05-26 13:48:07 +0200144 MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100148 /* Mbed TLS error codes can combine a high-level error code and a
149 * low-level error code. The low-level error usually reflects the
150 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 int low_level_ret = -(-ret & 0x007f);
152 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100153 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100155
156 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
157 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200159 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
160 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
161 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
162 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
163 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200165 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200167 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100175
176 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180
Gilles Peskine26869f22019-05-06 15:25:00 +0200181 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200183
184 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200188
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
205 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100206 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
207 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
211 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100215#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100216
217 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100219
Gilles Peskinee59236f2018-01-27 23:32:46 +0100220 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
221 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
222 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
225 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200227 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100234 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
235 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100236 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100238 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
239 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100241 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243#endif
244
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200294 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
311 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200319
itayzafrir5c753392018-05-08 11:18:38 +0300320 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300321 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
328 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300330 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100332 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100334
Paul Elliott588f8ed2022-12-02 18:10:26 +0000335 case MBEDTLS_ERR_ECP_IN_PROGRESS:
336 return PSA_OPERATION_INCOMPLETE;
337
Janos Follatha13b9052019-11-22 12:48:59 +0000338 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300340
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100343 }
344}
345
Paul Elliottdc42ca82023-02-24 18:11:59 +0000346/**
347 * \brief For output buffers which contain "tags"
348 * (outputs that may be checked for validity like
349 * hashes, MACs and signatures), fill the unused
350 * part of the output buffer (the whole buffer on
351 * error, the trailing part on success) with
352 * something that isn't a valid tag (barring an
353 * attack on the tag and deliberately-crafted
354 * input), in case the caller doesn't check the
355 * return status properly.
356 *
357 * \param output_buffer Pointer to buffer to wipe. May not be NULL
358 * unless \p output_buffer_size is zero.
359 * \param status Status of function called to generate
360 * output_buffer originally
361 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
362 * could be NULL.
363 * \param output_buffer_length Length of data written to output_buffer, must be
364 * less than \p output_buffer_size
365 */
366static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000367 size_t output_buffer_size, size_t output_buffer_length)
368{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000369 size_t offset = 0;
370
371 if (output_buffer_size == 0) {
372 /* If output_buffer_size is 0 then we have nothing to do. We must not
373 call memset because output_buffer may be NULL in this case */
374 return;
375 }
376
377 if (status == PSA_SUCCESS) {
378 offset = output_buffer_length;
379 }
380
381 memset(output_buffer + offset, '!', output_buffer_size - offset);
382}
383
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200384
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200385
386
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100387/****************************************************************/
388/* Key management */
389/****************************************************************/
390
Valerio Settia9aab1a2023-06-19 13:39:54 +0200391#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200392psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
393 size_t *bits)
394{
395 switch (grpid) {
396 case MBEDTLS_ECP_DP_SECP192R1:
397 *bits = 192;
398 return PSA_ECC_FAMILY_SECP_R1;
399 case MBEDTLS_ECP_DP_SECP224R1:
400 *bits = 224;
401 return PSA_ECC_FAMILY_SECP_R1;
402 case MBEDTLS_ECP_DP_SECP256R1:
403 *bits = 256;
404 return PSA_ECC_FAMILY_SECP_R1;
405 case MBEDTLS_ECP_DP_SECP384R1:
406 *bits = 384;
407 return PSA_ECC_FAMILY_SECP_R1;
408 case MBEDTLS_ECP_DP_SECP521R1:
409 *bits = 521;
410 return PSA_ECC_FAMILY_SECP_R1;
411 case MBEDTLS_ECP_DP_BP256R1:
412 *bits = 256;
413 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
414 case MBEDTLS_ECP_DP_BP384R1:
415 *bits = 384;
416 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
417 case MBEDTLS_ECP_DP_BP512R1:
418 *bits = 512;
419 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
420 case MBEDTLS_ECP_DP_CURVE25519:
421 *bits = 255;
422 return PSA_ECC_FAMILY_MONTGOMERY;
423 case MBEDTLS_ECP_DP_SECP192K1:
424 *bits = 192;
425 return PSA_ECC_FAMILY_SECP_K1;
426 case MBEDTLS_ECP_DP_SECP224K1:
427 *bits = 224;
428 return PSA_ECC_FAMILY_SECP_K1;
429 case MBEDTLS_ECP_DP_SECP256K1:
430 *bits = 256;
431 return PSA_ECC_FAMILY_SECP_K1;
432 case MBEDTLS_ECP_DP_CURVE448:
433 *bits = 448;
434 return PSA_ECC_FAMILY_MONTGOMERY;
435 default:
436 *bits = 0;
437 return 0;
438 }
439}
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
442 size_t bits,
443 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444{
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100446 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100448#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100451#endif
452#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100453 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100455#endif
456#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100459#endif
460#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100461 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100463#endif
464#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100467 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (bits_is_sloppy) {
469 return MBEDTLS_ECP_DP_SECP521R1;
470 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100471 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100472#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100473 }
474 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100475
Paul Elliott8ff510a2020-06-02 17:19:28 +0100476 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100478#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100479 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100481#endif
482#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100483 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100485#endif
486#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100487 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100489#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100490 }
491 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100492
Paul Elliott8ff510a2020-06-02 17:19:28 +0100493 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100495#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100496 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100498 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (bits_is_sloppy) {
500 return MBEDTLS_ECP_DP_CURVE25519;
501 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100502 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100503#endif
504#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100505 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100507#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100508 }
509 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100510
Paul Elliott8ff510a2020-06-02 17:19:28 +0100511 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100513#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100514 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100516#endif
517#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100518 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100520#endif
521#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100522 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100524#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100525 }
526 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200527 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100528
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100529 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200531}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200532#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
535 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200536{
537 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200541 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200542 case PSA_KEY_TYPE_PASSWORD:
543 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200544 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100545#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200546 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (bits != 128 && bits != 192 && bits != 256) {
548 return PSA_ERROR_INVALID_ARGUMENT;
549 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200550 break;
551#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200552#if defined(PSA_WANT_KEY_TYPE_ARIA)
553 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (bits != 128 && bits != 192 && bits != 256) {
555 return PSA_ERROR_INVALID_ARGUMENT;
556 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200557 break;
558#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100559#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200560 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (bits != 128 && bits != 192 && bits != 256) {
562 return PSA_ERROR_INVALID_ARGUMENT;
563 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200564 break;
565#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100566#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200567 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (bits != 64 && bits != 128 && bits != 192) {
569 return PSA_ERROR_INVALID_ARGUMENT;
570 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200571 break;
572#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100573#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200574 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (bits != 256) {
576 return PSA_ERROR_INVALID_ARGUMENT;
577 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200578 break;
579#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200580 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200582 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (bits % 8 != 0) {
584 return PSA_ERROR_INVALID_ARGUMENT;
585 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200588}
589
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100590/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100591 *
Steven Cooremancd640932021-03-08 12:00:27 +0100592 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
593 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100594 *
595 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100596 * \param[in] key_type The key type of the key to be used with the
597 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100598 *
599 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100600 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100601 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100602 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100603 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100604MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100605 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100607{
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (PSA_ALG_IS_HMAC(algorithm)) {
609 if (key_type == PSA_KEY_TYPE_HMAC) {
610 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100611 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100612 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
615 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
616 * key. */
617 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
618 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
619 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
620 * the block length (larger than 1) for block ciphers. */
621 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
622 return PSA_SUCCESS;
623 }
624 }
625 }
626
627 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100628}
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
631 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200632{
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (slot->key.data != NULL) {
634 return PSA_ERROR_ALREADY_EXISTS;
635 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 slot->key.data = mbedtls_calloc(1, buffer_length);
638 if (slot->key.data == NULL) {
639 return PSA_ERROR_INSUFFICIENT_MEMORY;
640 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200641
Ronald Cronea0f8a62020-11-25 17:52:23 +0100642 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200644}
645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
647 const uint8_t *data,
648 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200649{
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 psa_status_t status = psa_allocate_buffer_to_slot(slot,
651 data_length);
652 if (status != PSA_SUCCESS) {
653 return status;
654 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 memcpy(slot->key.data, data, data_length);
657 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200658}
659
Ronald Crona0fe59f2020-11-29 11:14:53 +0100660psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100661 const psa_key_attributes_t *attributes,
662 const uint8_t *data, size_t data_length,
663 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
667 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200669 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (data_length == 0) {
671 return PSA_ERROR_NOT_SUPPORTED;
672 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (key_type_is_raw_bytes(type)) {
675 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
678 *bits);
679 if (status != PSA_SUCCESS) {
680 return status;
681 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200682
Ronald Crondd04d422020-11-28 15:14:42 +0100683 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100685 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return PSA_SUCCESS;
689 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +0200690#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200691 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100692 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200693 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100694 return PSA_ERROR_INVALID_ARGUMENT;
695 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200696 return mbedtls_psa_ffdh_import_key(attributes,
697 data, data_length,
698 key_buffer, key_buffer_size,
699 key_buffer_length,
700 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100701 }
Valerio Setti8bb57632023-05-26 13:48:07 +0200702#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200703 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200704#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
706 if (PSA_KEY_TYPE_IS_ECC(type)) {
707 return mbedtls_psa_ecp_import_key(attributes,
708 data, data_length,
709 key_buffer, key_buffer_size,
710 key_buffer_length,
711 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100712 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200713#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800714 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200715#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
716 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
718 if (PSA_KEY_TYPE_IS_RSA(type)) {
719 return mbedtls_psa_rsa_import_key(attributes,
720 data, data_length,
721 key_buffer, key_buffer_size,
722 key_buffer_length,
723 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200724 }
Valerio Settife478902023-07-25 12:27:19 +0200725#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
726 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800727 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100728 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100731}
732
Gilles Peskinef603c712019-01-19 13:40:11 +0100733/** Calculate the intersection of two algorithm usage policies.
734 *
735 * Return 0 (which allows no operation) on incompatibility.
736 */
737static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100738 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100739 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100741{
Gilles Peskine549ea862019-05-22 11:45:59 +0200742 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (alg1 == alg2) {
744 return alg1;
745 }
Steven Cooreman03488022021-02-18 12:07:20 +0100746 /* If the policies are from the same hash-and-sign family, check
747 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
749 PSA_ALG_IS_SIGN_HASH(alg2) &&
750 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
751 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
752 return alg2;
753 }
754 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
755 return alg1;
756 }
Steven Cooreman03488022021-02-18 12:07:20 +0100757 }
758 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100759 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100760 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
762 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
763 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
764 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
765 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100766 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100767
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100768 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
770 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
771 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
772 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100773 }
774 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
776 (alg1_len <= alg2_len)) {
777 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100778 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
780 (alg2_len <= alg1_len)) {
781 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100782 }
783 }
784 /* If the policies are from the same MAC family, check whether one
785 * of them is a minimum-MAC-length policy. Calculate the most
786 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
788 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
789 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100790 /* Validate the combination of key type and algorithm. Since the base
791 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
793 return 0;
794 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100795
Steven Cooreman31a876d2021-03-03 20:47:40 +0100796 /* Get the (exact or at-least) output lengths for both sides of the
797 * requested intersection. None of the currently supported algorithms
798 * have an output length dependent on the actual key size, so setting it
799 * to a bogus value of 0 is currently OK.
800 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100801 * Note that for at-least-this-length wildcard algorithms, the output
802 * length is set to the shortest allowed length, which allows us to
803 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
805 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100806 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100807
Steven Cooremana1d83222021-02-25 10:20:29 +0100808 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
810 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
811 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100812 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100813
814 /* If only one is an at-least-this-length policy, the intersection would
815 * be the other (fixed-length) policy as long as said fixed length is
816 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
818 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100819 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
821 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100822 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100823
Steven Cooremancd640932021-03-08 12:00:27 +0100824 /* If none of them are wildcards, check whether they define the same tag
825 * length. This is still possible here when one is default-length and
826 * the other specific-length. Ensure to always return the
827 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (alg1_len == alg2_len) {
829 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
830 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100831 }
832 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100834}
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836static int psa_key_algorithm_permits(psa_key_type_t key_type,
837 psa_algorithm_t policy_alg,
838 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200839{
Gilles Peskine549ea862019-05-22 11:45:59 +0200840 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (requested_alg == policy_alg) {
842 return 1;
843 }
Steven Cooreman03488022021-02-18 12:07:20 +0100844 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
845 * and requested_alg is the same hash-and-sign family with any hash,
846 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
848 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
849 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
850 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100851 }
852 /* If policy_alg is a wildcard AEAD algorithm of the same base as
853 * the requested algorithm, check the requested tag length to be
854 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (PSA_ALG_IS_AEAD(policy_alg) &&
856 PSA_ALG_IS_AEAD(requested_alg) &&
857 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
858 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
859 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
860 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
861 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100862 }
Steven Cooremancd640932021-03-08 12:00:27 +0100863 /* If policy_alg is a MAC algorithm of the same base as the requested
864 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (PSA_ALG_IS_MAC(policy_alg) &&
866 PSA_ALG_IS_MAC(requested_alg) &&
867 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
868 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100869 /* Validate the combination of key type and algorithm. Since the policy
870 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
872 return 0;
873 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100874
Steven Cooreman31a876d2021-03-03 20:47:40 +0100875 /* Get both the requested output length for the algorithm which is to be
876 * verified, and the default output length for the base algorithm.
877 * Note that none of the currently supported algorithms have an output
878 * length dependent on actual key size, so setting it to a bogus value
879 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100880 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100882 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 key_type, 0,
884 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100885
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100886 /* If the policy is default-length, only allow an algorithm with
887 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
889 return requested_output_length == default_output_length;
890 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100891
892 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100893 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
895 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
896 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100897 }
898
Steven Cooremancd640932021-03-08 12:00:27 +0100899 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
900 * check for the requested MAC length to be equal to or longer than the
901 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
903 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
904 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100905 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200906 }
Steven Cooremance48e852020-10-05 16:02:45 +0200907 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200908 * a key derivation with that key agreement should also be allowed. This
909 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
911 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
912 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
913 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200914 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100915 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200917}
918
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100919/** Test whether a policy permits an algorithm.
920 *
921 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100922 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100923 * \note This function requires providing the key type for which the policy is
924 * being validated, since some algorithm policy definitions (e.g. MAC)
925 * have different properties depending on what kind of cipher it is
926 * combined with.
927 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100928 * \retval PSA_SUCCESS When \p alg is a specific algorithm
929 * allowed by the \p policy.
930 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
931 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
932 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100933 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
935 psa_key_type_t key_type,
936 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100937{
Steven Cooremand788fab2021-02-25 11:29:17 +0100938 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (alg == 0) {
940 return PSA_ERROR_INVALID_ARGUMENT;
941 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100942
Steven Cooreman7e39f052021-02-23 14:18:32 +0100943 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (PSA_ALG_IS_WILDCARD(alg)) {
945 return PSA_ERROR_INVALID_ARGUMENT;
946 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
949 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
950 return PSA_SUCCESS;
951 } else {
952 return PSA_ERROR_NOT_PERMITTED;
953 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100954}
955
Gilles Peskinef603c712019-01-19 13:40:11 +0100956/** Restrict a key policy based on a constraint.
957 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100958 * \note This function requires providing the key type for which the policy is
959 * being restricted, since some algorithm policy definitions (e.g. MAC)
960 * have different properties depending on what kind of cipher it is
961 * combined with.
962 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100963 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100964 * \param[in,out] policy The policy to restrict.
965 * \param[in] constraint The policy constraint to apply.
966 *
967 * \retval #PSA_SUCCESS
968 * \c *policy contains the intersection of the original value of
969 * \c *policy and \c *constraint.
970 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100971 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100972 * \c *policy is unchanged.
973 */
974static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100975 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100976 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100978{
979 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 psa_key_policy_algorithm_intersection(key_type, policy->alg,
981 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200982 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
984 constraint->alg2);
985 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
986 return PSA_ERROR_INVALID_ARGUMENT;
987 }
988 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
989 return PSA_ERROR_INVALID_ARGUMENT;
990 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100991 policy->usage &= constraint->usage;
992 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200993 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100995}
996
Przemek Stekiel061f6942022-11-30 10:51:35 +0100997/** Get the description of a key given its identifier and policy constraints
998 * and lock it.
999 *
1000 * The key must have allow all the usage flags set in \p usage. If \p alg is
1001 * nonzero, the key must allow operations with this algorithm. If \p alg is
1002 * zero, the algorithm is not checked.
1003 *
1004 * In case of a persistent key, the function loads the description of the key
1005 * into a key slot if not already done.
1006 *
1007 * On success, the returned key slot is locked. It is the responsibility of
1008 * the caller to unlock the key slot when it does not access it anymore.
1009 */
1010static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001011 mbedtls_svc_key_id_t key,
1012 psa_key_slot_t **p_slot,
1013 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001015{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001017 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 status = psa_get_and_lock_key_slot(key, p_slot);
1020 if (status != PSA_SUCCESS) {
1021 return status;
1022 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001023 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001024
1025 /* Enforce that usage policy for the key slot contains all the flags
1026 * required by the usage parameter. There is one exception: public
1027 * keys can always be exported, so we treat public key objects as
1028 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001030 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001034 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001035 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001036 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001037
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001038 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (alg != 0) {
1040 status = psa_key_policy_permits(&slot->attr.policy,
1041 slot->attr.type,
1042 alg);
1043 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001044 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001046 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001049
1050error:
1051 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001055}
Darryl Green940d72c2018-07-13 13:18:51 +01001056
Ronald Cron5c522922020-11-14 16:35:34 +01001057/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001058 *
1059 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001060 * available, as opposed to a key in a secure element and/or to be used
1061 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001062 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001063 * This is a temporary function that may be used instead of
1064 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1065 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001066 *
Ronald Cron5c522922020-11-14 16:35:34 +01001067 * On success, the returned key slot is locked. It is the responsibility of the
1068 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001069 */
Ronald Cron5c522922020-11-14 16:35:34 +01001070static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1071 mbedtls_svc_key_id_t key,
1072 psa_key_slot_t **p_slot,
1073 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001075{
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1077 usage, alg);
1078 if (status != PSA_SUCCESS) {
1079 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001080 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1083 psa_unlock_key_slot(*p_slot);
1084 *p_slot = NULL;
1085 return PSA_ERROR_NOT_SUPPORTED;
1086 }
1087
1088 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001089}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001092{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001093 /* Data pointer will always be either a valid pointer or NULL in an
1094 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (slot->key.data != NULL) {
1096 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1097 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001100 slot->key.data = NULL;
1101 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001104}
1105
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001106/** Completely wipe a slot in memory, including its policy.
1107 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001109{
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 /*
1113 * As the return error code may not be handled in case of multiple errors,
1114 * do our best to report an unexpected lock counter. Assert with
1115 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1116 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1117 * function is called as part of the execution of a test suite, the
1118 * execution of the test suite is stopped in error if the assertion fails.
1119 */
1120 if (slot->lock_count != 1) {
1121 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001122 status = PSA_ERROR_CORRUPTION_DETECTED;
1123 }
1124
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001125 /* Multipart operations may still be using the key. This is safe
1126 * because all multipart operation objects are independent from
1127 * the key slot: if they need to access the key after the setup
1128 * phase, they have a copy of the key. Note that this means that
1129 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001130 /* At this point, key material and other type-specific content has
1131 * been wiped. Clear remaining metadata. We can call memset and not
1132 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 memset(slot, 0, sizeof(*slot));
1134 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001135}
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001138{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001139 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001140 psa_status_t status; /* status of the last operation */
1141 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001142#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1143 psa_se_drv_table_entry_t *driver;
1144#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (mbedtls_svc_key_id_is_null(key)) {
1147 return PSA_SUCCESS;
1148 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001149
Ronald Cronf2911112020-10-29 17:51:10 +01001150 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001151 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001152 * key, this will load the key description from persistent memory if not
1153 * done yet. We cannot avoid this loading as without it we don't know if
1154 * the key is operated by an SE or not and this information is needed by
1155 * the current implementation.
1156 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 status = psa_get_and_lock_key_slot(key, &slot);
1158 if (status != PSA_SUCCESS) {
1159 return status;
1160 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001161
Ronald Cronf2911112020-10-29 17:51:10 +01001162 /*
1163 * If the key slot containing the key description is under access by the
1164 * library (apart from the present access), the key cannot be destroyed
1165 * yet. For the time being, just return in error. Eventually (to be
1166 * implemented), the key should be destroyed when all accesses have
1167 * stopped.
1168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (slot->lock_count > 1) {
1170 psa_unlock_key_slot(slot);
1171 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001172 }
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001175 /* Refuse the destruction of a read-only key (which may or may not work
1176 * if we attempt it, depending on whether the key is merely read-only
1177 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001178 * Just do the best we can, which is to wipe the copy in memory
1179 * (done in this function's cleanup code). */
1180 overall_status = PSA_ERROR_NOT_PERMITTED;
1181 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001182 }
1183
Gilles Peskine354f7672019-07-12 23:46:38 +02001184#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1186 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001187 /* For a key in a secure element, we need to do three things:
1188 * remove the key file in internal storage, destroy the
1189 * key inside the secure element, and update the driver's
1190 * persistent data. Start a transaction that will encompass these
1191 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001193 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001195 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 status = psa_crypto_save_transaction();
1197 if (status != PSA_SUCCESS) {
1198 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001199 /* We should still try to destroy the key in the secure
1200 * element and the key metadata in storage. This is especially
1201 * important if the error is that the storage is full.
1202 * But how to do it exactly without risking an inconsistent
1203 * state after a reset?
1204 * https://github.com/ARMmbed/mbed-crypto/issues/215
1205 */
1206 overall_status = status;
1207 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001208 }
1209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 status = psa_destroy_se_key(driver,
1211 psa_key_slot_get_slot_number(slot));
1212 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001213 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001215 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1217
Darryl Greend49a4992018-06-18 17:27:26 +01001218#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1220 status = psa_destroy_persistent_key(slot->attr.id);
1221 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001222 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001224
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001225 /* TODO: other slots may have a copy of the same key. We should
1226 * invalidate them.
1227 * https://github.com/ARMmbed/mbed-crypto/issues/214
1228 */
Darryl Greend49a4992018-06-18 17:27:26 +01001229 }
1230#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001231
Gilles Peskinefc762652019-07-22 19:30:34 +02001232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 if (driver != NULL) {
1234 status = psa_save_se_persistent_data(driver);
1235 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001236 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 }
1238 status = psa_crypto_stop_transaction();
1239 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001240 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001242 }
1243#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1244
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001245exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001247 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001249 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 }
1251 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001252}
1253
Valerio Settib2bcedb2023-07-10 11:24:00 +02001254#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001255 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001256static psa_status_t psa_get_rsa_public_exponent(
1257 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001259{
1260 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001262 uint8_t *buffer = NULL;
1263 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1267 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001268 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 }
1270 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001271 /* It's the default value, which is reported as an empty string,
1272 * so there's nothing to do. */
1273 goto exit;
1274 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 buflen = mbedtls_mpi_size(&mpi);
1277 buffer = mbedtls_calloc(1, buflen);
1278 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001279 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1280 goto exit;
1281 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1283 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001284 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001286 attributes->domain_parameters = buffer;
1287 attributes->domain_parameters_size = buflen;
1288
1289exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 mbedtls_mpi_free(&mpi);
1291 if (ret != 0) {
1292 mbedtls_free(buffer);
1293 }
1294 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001295}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001296#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001297 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001298
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001299/** Retrieve all the publicly-accessible attributes of a key.
1300 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1302 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001303{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001305 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001306 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1311 if (status != PSA_SUCCESS) {
1312 return status;
1313 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001314
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001315 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1317 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318
1319#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1321 psa_set_key_slot_number(attributes,
1322 psa_key_slot_get_slot_number(slot));
1323 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001324#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001327#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1328 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001329 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001330 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001331 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001332 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001333 * is not yet implemented.
1334 * https://github.com/ARMmbed/mbed-crypto/issues/216
1335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001337 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001338
Ronald Cron90857082020-11-25 14:58:33 +01001339 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 slot->attr.type,
1341 slot->key.data,
1342 slot->key.bytes,
1343 &rsa);
1344 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001345 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 status = psa_get_rsa_public_exponent(rsa,
1349 attributes);
1350 mbedtls_rsa_free(rsa);
1351 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001352 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001353 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001354#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1355 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001356 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001357 default:
1358 /* Nothing else to do. */
1359 break;
1360 }
1361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (status != PSA_SUCCESS) {
1363 psa_reset_key_attributes(attributes);
1364 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369}
1370
Gilles Peskinec8000c02019-08-02 20:15:51 +02001371#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1372psa_status_t psa_get_key_slot_number(
1373 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001375{
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001377 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 return PSA_SUCCESS;
1379 } else {
1380 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001381 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001382}
1383#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1386 size_t key_buffer_size,
1387 uint8_t *data,
1388 size_t data_size,
1389 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001390{
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if (key_buffer_size > data_size) {
1392 return PSA_ERROR_BUFFER_TOO_SMALL;
1393 }
1394 memcpy(data, key_buffer, key_buffer_size);
1395 memset(data + key_buffer_size, 0,
1396 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001397 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001399}
1400
Ronald Cron7285cda2020-11-26 14:46:37 +01001401psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001402 const psa_key_attributes_t *attributes,
1403 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001405{
Ronald Crond18b5f82020-11-25 16:46:05 +01001406 psa_key_type_t type = attributes->core.type;
1407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (key_type_is_raw_bytes(type) ||
1409 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001410 PSA_KEY_TYPE_IS_ECC(type) ||
1411 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 return psa_export_key_buffer_internal(
1413 key_buffer, key_buffer_size,
1414 data, data_size, data_length);
1415 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001416 /* This shouldn't happen in the reference implementation, but
1417 it is valid for a special-purpose implementation to omit
1418 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001420 }
1421}
1422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1424 uint8_t *data,
1425 size_t data_size,
1426 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001427{
1428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1429 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1430 psa_key_slot_t *slot;
1431
Ronald Crone907e552021-01-18 13:22:38 +01001432 /* Reject a zero-length output buffer now, since this can never be a
1433 * valid key representation. This way we know that data must be a valid
1434 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (data_size == 0) {
1436 return PSA_ERROR_BUFFER_TOO_SMALL;
1437 }
Ronald Crone907e552021-01-18 13:22:38 +01001438
Ronald Cron9486f9d2020-11-26 13:36:23 +01001439 /* Set the key to empty now, so that even when there are errors, we always
1440 * set data_length to a value between 0 and data_size. On error, setting
1441 * the key to empty is a good choice because an empty key representation is
1442 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001443 *data_length = 0;
1444
Ronald Cron9486f9d2020-11-26 13:36:23 +01001445 /* Export requires the EXPORT flag. There is an exception for public keys,
1446 * which don't require any flag, but
1447 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1450 PSA_KEY_USAGE_EXPORT, 0);
1451 if (status != PSA_SUCCESS) {
1452 return status;
1453 }
mohammad160306e79202018-03-28 13:17:44 +03001454
Ronald Crond18b5f82020-11-25 16:46:05 +01001455 psa_key_attributes_t attributes = {
1456 .core = slot->attr
1457 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 status = psa_driver_wrapper_export_key(&attributes,
1459 slot->key.data, slot->key.bytes,
1460 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001465}
1466
Ronald Cron7285cda2020-11-26 14:46:37 +01001467psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001468 const psa_key_attributes_t *attributes,
1469 const uint8_t *key_buffer,
1470 size_t key_buffer_size,
1471 uint8_t *data,
1472 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001474{
Ronald Crond18b5f82020-11-25 16:46:05 +01001475 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001476
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001477 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001478 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1479 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001480 /* Exporting public -> public */
1481 return psa_export_key_buffer_internal(
1482 key_buffer, key_buffer_size,
1483 data, data_size, data_length);
1484 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001485#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001486 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1487 return mbedtls_psa_rsa_export_public_key(attributes,
1488 key_buffer,
1489 key_buffer_size,
1490 data,
1491 data_size,
1492 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001493#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001494 /* We don't know how to convert a private RSA key to public. */
1495 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001496#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001497 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001498 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001499#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001500 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1501 return mbedtls_psa_ecp_export_public_key(attributes,
1502 key_buffer,
1503 key_buffer_size,
1504 data,
1505 data_size,
1506 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001507#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001508 /* We don't know how to convert a private ECC key to public */
1509 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001510#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001511 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001512 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001513#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001514 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001515 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001516 key_buffer,
1517 key_buffer_size,
1518 data, data_size,
1519 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001520#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001521 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti8bb57632023-05-26 13:48:07 +02001522#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001523 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001525 (void) key_buffer;
1526 (void) key_buffer_size;
1527 (void) data;
1528 (void) data_size;
1529 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001531 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001532}
Ronald Crond18b5f82020-11-25 16:46:05 +01001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1535 uint8_t *data,
1536 size_t data_size,
1537 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001538{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001540 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001541 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001542
Ronald Crone907e552021-01-18 13:22:38 +01001543 /* Reject a zero-length output buffer now, since this can never be a
1544 * valid key representation. This way we know that data must be a valid
1545 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if (data_size == 0) {
1547 return PSA_ERROR_BUFFER_TOO_SMALL;
1548 }
Ronald Crone907e552021-01-18 13:22:38 +01001549
Darryl Greendd8fb772018-11-07 16:00:44 +00001550 /* Set the key to empty now, so that even when there are errors, we always
1551 * set data_length to a value between 0 and data_size. On error, setting
1552 * the key to empty is a good choice because an empty key representation is
1553 * unlikely to be accepted anywhere. */
1554 *data_length = 0;
1555
1556 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1558 if (status != PSA_SUCCESS) {
1559 return status;
1560 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1563 status = PSA_ERROR_INVALID_ARGUMENT;
1564 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001565 }
1566
Ronald Crond18b5f82020-11-25 16:46:05 +01001567 psa_key_attributes_t attributes = {
1568 .core = slot->attr
1569 };
Ronald Cron67227982020-11-26 15:16:05 +01001570 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001571 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001573
1574exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001578}
1579
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001580MBEDTLS_STATIC_ASSERT(
1581 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1582 "One or more key attribute flag is listed as both external-only and dual-use")
1583MBEDTLS_STATIC_ASSERT(
1584 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1585 "One or more key attribute flag is listed as both internal-only and dual-use")
1586MBEDTLS_STATIC_ASSERT(
1587 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1588 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001589
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001590/** Validate that a key policy is internally well-formed.
1591 *
1592 * This function only rejects invalid policies. It does not validate the
1593 * consistency of the policy with respect to other attributes of the key
1594 * such as the key type.
1595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001596static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001597{
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1599 PSA_KEY_USAGE_COPY |
1600 PSA_KEY_USAGE_ENCRYPT |
1601 PSA_KEY_USAGE_DECRYPT |
1602 PSA_KEY_USAGE_SIGN_MESSAGE |
1603 PSA_KEY_USAGE_VERIFY_MESSAGE |
1604 PSA_KEY_USAGE_SIGN_HASH |
1605 PSA_KEY_USAGE_VERIFY_HASH |
1606 PSA_KEY_USAGE_VERIFY_DERIVATION |
1607 PSA_KEY_USAGE_DERIVE)) != 0) {
1608 return PSA_ERROR_INVALID_ARGUMENT;
1609 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001612}
1613
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001614/** Validate the internal consistency of key attributes.
1615 *
1616 * This function only rejects invalid attribute values. If does not
1617 * validate the consistency of the attributes with any key data that may
1618 * be involved in the creation of the key.
1619 *
1620 * Call this function early in the key creation process.
1621 *
1622 * \param[in] attributes Key attributes for the new key.
1623 * \param[out] p_drv On any return, the driver for the key, if any.
1624 * NULL for a transparent key.
1625 *
1626 */
1627static psa_status_t psa_validate_key_attributes(
1628 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001630{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001631 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1633 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 status = psa_validate_key_location(lifetime, p_drv);
1636 if (status != PSA_SUCCESS) {
1637 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001638 }
1639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 status = psa_validate_key_persistence(lifetime);
1641 if (status != PSA_SUCCESS) {
1642 return status;
1643 }
1644
1645 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1646 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1647 return PSA_ERROR_INVALID_ARGUMENT;
1648 }
1649 } else {
1650 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1651 return PSA_ERROR_INVALID_ARGUMENT;
1652 }
1653 }
1654
1655 status = psa_validate_key_policy(&attributes->core.policy);
1656 if (status != PSA_SUCCESS) {
1657 return status;
1658 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001659
1660 /* Refuse to create overly large keys.
1661 * Note that this doesn't trigger on import if the attributes don't
1662 * explicitly specify a size (so psa_get_key_bits returns 0), so
1663 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1665 return PSA_ERROR_NOT_SUPPORTED;
1666 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001667
Gilles Peskine91e8c332019-08-02 19:19:39 +02001668 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1670 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1671 return PSA_ERROR_INVALID_ARGUMENT;
1672 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001675}
1676
Gilles Peskine4747d192019-04-17 15:05:45 +02001677/** Prepare a key slot to receive key material.
1678 *
1679 * This function allocates a key slot and sets its metadata.
1680 *
1681 * If this function fails, call psa_fail_key_creation().
1682 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001683 * This function is intended to be used as follows:
1684 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001685 * it with the specified attributes, and in case of a volatile key assign it
1686 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001687 * -# Populate the slot with the key material.
1688 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1689 * In case of failure at any step, stop the sequence and call
1690 * psa_fail_key_creation().
1691 *
Ronald Cron5c522922020-11-14 16:35:34 +01001692 * On success, the key slot is locked. It is the responsibility of the caller
1693 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001694 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001695 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001696 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001697 * \param[out] p_slot On success, a pointer to the prepared slot.
1698 * \param[out] p_drv On any return, the driver for the key, if any.
1699 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001700 *
1701 * \retval #PSA_SUCCESS
1702 * The key slot is ready to receive key material.
1703 * \return If this function fails, the key slot is an invalid state.
1704 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001705 */
1706static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001707 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001708 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001709 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001711{
1712 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001713 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001714 psa_key_slot_t *slot;
1715
Gilles Peskinedf179142019-07-15 22:02:14 +02001716 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001717 *p_drv = NULL;
1718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 status = psa_validate_key_attributes(attributes, p_drv);
1720 if (status != PSA_SUCCESS) {
1721 return status;
1722 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1725 if (status != PSA_SUCCESS) {
1726 return status;
1727 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 slot = *p_slot;
1729
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001730 /* We're storing the declared bit-size of the key. It's up to each
1731 * creation mechanism to verify that this information is correct.
1732 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001733 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001734 * is optional (import, copy). In case of a volatile key, assign it the
1735 * volatile key identifier associated to the slot returned to contain its
1736 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001737
1738 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001740#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1741 slot->attr.id = volatile_key_id;
1742#else
1743 slot->attr.id.key_id = volatile_key_id;
1744#endif
1745 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001746
Gilles Peskine91e8c332019-08-02 19:19:39 +02001747 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001748 * external-only flags, query `attributes`. Thanks to the check
1749 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001750 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001751 * may have set. */
1752 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001753
Gilles Peskinecbaff462019-07-12 23:46:04 +02001754#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001755 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001756 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001757 * create the key file in internal storage, create the
1758 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001759 * persistent data. This is done by starting a transaction that will
1760 * encompass these three actions.
1761 * For registering a volatile key, we just need to find an appropriate
1762 * slot number inside the SE. Since the key is designated volatile, creating
1763 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001764 /* The first thing to do is to find a slot number for the new key.
1765 * We save the slot number in persistent storage as part of the
1766 * transaction data. It will be needed to recover if the power
1767 * fails during the key creation process, to clean up on the secure
1768 * element side after restarting. Obtaining a slot number from the
1769 * secure element driver updates its persistent state, but we do not yet
1770 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001771 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001773 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1775 &slot_number);
1776 if (status != PSA_SUCCESS) {
1777 return status;
1778 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1781 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001782 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001783 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001784 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 status = psa_crypto_save_transaction();
1786 if (status != PSA_SUCCESS) {
1787 (void) psa_crypto_stop_transaction();
1788 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001789 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001790 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001791
1792 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001794 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001797 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001799 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001800#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001803}
Gilles Peskine4747d192019-04-17 15:05:45 +02001804
1805/** Finalize the creation of a key once its key material has been set.
1806 *
1807 * This entails writing the key to persistent storage.
1808 *
1809 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001810 * See the documentation of psa_start_key_creation() for the intended use
1811 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001812 *
Ronald Cron5c522922020-11-14 16:35:34 +01001813 * If the finalization succeeds, the function unlocks the key slot (it was
1814 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1815 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001816 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001817 * \param[in,out] slot Pointer to the slot with key material.
1818 * \param[in] driver The secure element driver for the key,
1819 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001820 * \param[out] key On success, identifier of the key. Note that the
1821 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001822 *
1823 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001824 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001825 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1826 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1827 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1828 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1829 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1830 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001831 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001832 * \return If this function fails, the key slot is an invalid state.
1833 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001834 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001835static psa_status_t psa_finish_key_creation(
1836 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001837 psa_se_drv_table_entry_t *driver,
1838 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001839{
1840 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001841 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001842 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001843
1844#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001846#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001848 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001849 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001851
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001852 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1853 sizeof(data.slot_number),
1854 "Slot number size does not match psa_se_key_data_storage_t");
1855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1857 status = psa_save_persistent_key(&slot->attr,
1858 (uint8_t *) &data,
1859 sizeof(data));
1860 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001861#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1862 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001863 /* Key material is saved in export representation in the slot, so
1864 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 status = psa_save_persistent_key(&slot->attr,
1866 slot->key.data,
1867 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001868 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001869 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001870#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1871
Gilles Peskinecbaff462019-07-12 23:46:04 +02001872#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001873 /* Finish the transaction for a key creation. This does not
1874 * happen when registering an existing key. Detect this case
1875 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001876 * creation of a persistent key in a secure element requires a transaction,
1877 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 if (driver != NULL &&
1879 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1880 status = psa_save_se_persistent_data(driver);
1881 if (status != PSA_SUCCESS) {
1882 psa_destroy_persistent_key(slot->attr.id);
1883 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001884 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001886 }
1887#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001890 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 status = psa_unlock_key_slot(slot);
1892 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001893 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001895 }
Ronald Cron50972942020-11-14 11:28:25 +01001896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001898}
1899
1900/** Abort the creation of a key.
1901 *
1902 * You may call this function after calling psa_start_key_creation(),
1903 * or after psa_finish_key_creation() fails. In other circumstances, this
1904 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001905 * See the documentation of psa_start_key_creation() for the intended use
1906 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001907 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001908 * \param[in,out] slot Pointer to the slot with key material.
1909 * \param[in] driver The secure element driver for the key,
1910 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001911 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001912static void psa_fail_key_creation(psa_key_slot_t *slot,
1913 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001914{
Gilles Peskine011e4282019-06-26 18:34:38 +02001915 (void) driver;
1916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001918 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001920
1921#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001922 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001923 * element, and the failure happened later (when saving metadata
1924 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001925 * element.
1926 * https://github.com/ARMmbed/mbed-crypto/issues/217
1927 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001928
Gilles Peskined7729582019-08-05 15:55:54 +02001929 /* Abort the ongoing transaction if any (there may not be one if
1930 * the creation process failed before starting one, or if the
1931 * key creation is a registration of a key in a secure element).
1932 * Earlier functions must already have done what it takes to undo any
1933 * partial creation. All that's left is to update the transaction data
1934 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001936#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001939}
1940
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001941/** Validate optional attributes during key creation.
1942 *
1943 * Some key attributes are optional during key creation. If they are
1944 * specified in the attributes structure, check that they are consistent
1945 * with the data in the slot.
1946 *
1947 * This function should be called near the end of key creation, after
1948 * the slot in memory is fully populated but before saving persistent data.
1949 */
1950static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953{
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 if (attributes->core.type != 0) {
1955 if (attributes->core.type != slot->attr.type) {
1956 return PSA_ERROR_INVALID_ARGUMENT;
1957 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001958 }
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001961#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1962 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1964 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001965 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001966 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001967 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001968
Ronald Cron90857082020-11-25 14:58:33 +01001969 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 slot->attr.type,
1971 slot->key.data,
1972 slot->key.bytes,
1973 &rsa);
1974 if (status != PSA_SUCCESS) {
1975 return status;
1976 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 mbedtls_mpi_init(&actual);
1979 mbedtls_mpi_init(&required);
1980 ret = mbedtls_rsa_export(rsa,
1981 NULL, NULL, NULL, NULL, &actual);
1982 mbedtls_rsa_free(rsa);
1983 mbedtls_free(rsa);
1984 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001985 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
1987 ret = mbedtls_mpi_read_binary(&required,
1988 attributes->domain_parameters,
1989 attributes->domain_parameters_size);
1990 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 }
1993 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 }
1996rsa_exit:
1997 mbedtls_mpi_free(&actual);
1998 mbedtls_mpi_free(&required);
1999 if (ret != 0) {
2000 return mbedtls_to_psa_error(ret);
2001 }
2002 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002003#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2004 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002005 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002006 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008 }
2009 }
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (attributes->core.bits != 0) {
2012 if (attributes->core.bits != slot->attr.bits) {
2013 return PSA_ERROR_INVALID_ARGUMENT;
2014 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 }
2016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018}
2019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2021 const uint8_t *data,
2022 size_t data_length,
2023 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002024{
2025 psa_status_t status;
2026 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002027 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002028 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302029 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002030
Ronald Cron81709fc2020-11-14 12:10:32 +01002031 *key = MBEDTLS_SVC_KEY_ID_INIT;
2032
Gilles Peskine0f84d622019-09-12 19:03:13 +02002033 /* Reject zero-length symmetric keys (including raw data key objects).
2034 * This also rejects any key which might be encoded as an empty string,
2035 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 if (data_length == 0) {
2037 return PSA_ERROR_INVALID_ARGUMENT;
2038 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002039
Archana449608b2021-09-08 15:36:05 +05302040 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 if (data_length > SIZE_MAX / 8) {
2042 return PSA_ERROR_NOT_SUPPORTED;
2043 }
Archana6ed4bda2021-08-04 10:47:15 +05302044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2046 &slot, &driver);
2047 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002048 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002050
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002051 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302052 * storage ( thus not in the case of importing a key in a secure element
2053 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2054 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if (slot->key.data == NULL) {
2056 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302057 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 attributes, data, data_length, &storage_size);
2059 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302060 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 }
Archanad8a83dc2021-06-14 10:04:16 +05302062 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 status = psa_allocate_buffer_to_slot(slot, storage_size);
2064 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002065 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002067 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002068
2069 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 status = psa_driver_wrapper_import_key(attributes,
2071 data, data_length,
2072 slot->key.data,
2073 slot->key.bytes,
2074 &slot->key.bytes, &bits);
2075 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002080 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002082 status = PSA_ERROR_INVALID_ARGUMENT;
2083 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002084 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002085
Archana6ed4bda2021-08-04 10:47:15 +05302086 /* Enforce a size limit, and in particular ensure that the bit
2087 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302089 status = PSA_ERROR_NOT_SUPPORTED;
2090 goto exit;
2091 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 status = psa_validate_optional_attributes(slot, attributes);
2093 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002094 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002098exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (status != PSA_SUCCESS) {
2100 psa_fail_key_creation(slot, driver);
2101 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002104}
2105
Gilles Peskined7729582019-08-05 15:55:54 +02002106#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2107psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002109{
2110 psa_status_t status;
2111 psa_key_slot_t *slot = NULL;
2112 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002114
2115 /* Leaving attributes unspecified is not currently supported.
2116 * It could make sense to query the key type and size from the
2117 * secure element, but not all secure elements support this
2118 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2120 return PSA_ERROR_NOT_SUPPORTED;
2121 }
2122 if (psa_get_key_bits(attributes) == 0) {
2123 return PSA_ERROR_NOT_SUPPORTED;
2124 }
Gilles Peskined7729582019-08-05 15:55:54 +02002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2127 &slot, &driver);
2128 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
Gilles Peskined7729582019-08-05 15:55:54 +02002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002133
2134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (status != PSA_SUCCESS) {
2136 psa_fail_key_creation(slot, driver);
2137 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002138
Gilles Peskined7729582019-08-05 15:55:54 +02002139 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 psa_close_key(key);
2141 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002142}
2143#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2146 const psa_key_attributes_t *specified_attributes,
2147 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002148{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002149 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002150 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002151 psa_key_slot_t *source_slot = NULL;
2152 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002153 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002154 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302155 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002156
Ronald Cron81709fc2020-11-14 12:10:32 +01002157 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2158
Archana8a180362021-07-05 02:18:48 +05302159 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2161 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 status = psa_validate_optional_attributes(source_slot,
2166 specified_attributes);
2167 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002168 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002170
Archana9d17bf42021-09-10 06:22:44 +05302171 /* The target key type and number of bits have been validated by
2172 * psa_validate_optional_attributes() to be either equal to zero or
2173 * equal to the ones of the source key. So it is safe to inherit
2174 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302175 * */
Archana9d17bf42021-09-10 06:22:44 +05302176 actual_attributes.core.bits = source_slot->attr.bits;
2177 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302178
2179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 status = psa_restrict_key_policy(source_slot->attr.type,
2181 &actual_attributes.core.policy,
2182 &source_slot->attr.policy);
2183 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002184 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2188 &target_slot, &driver);
2189 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 }
2192 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2193 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302194 /*
Archana9d17bf42021-09-10 06:22:44 +05302195 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302196 * the source key would need to be exported as plaintext and re-imported
2197 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302198 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302199 * appropriate API invocations from the application, if needed.
2200 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002201 status = PSA_ERROR_NOT_SUPPORTED;
2202 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002203 }
Archana8a180362021-07-05 02:18:48 +05302204 /*
2205 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302206 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302207 * - For opaque keys this translates to an invocation of the drivers'
2208 * copy_key entry point through the dispatch layer.
2209 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2211 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2212 &storage_size);
2213 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302214 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 }
Archana374fe5b2021-09-08 15:50:28 +05302216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2218 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302219 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 }
Archana374fe5b2021-09-08 15:50:28 +05302221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 status = psa_driver_wrapper_copy_key(&actual_attributes,
2223 source_slot->key.data,
2224 source_slot->key.bytes,
2225 target_slot->key.data,
2226 target_slot->key.bytes,
2227 &target_slot->key.bytes);
2228 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 }
2231 } else {
2232 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302233 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 source_slot->key.bytes);
2235 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 }
Archana8a180362021-07-05 02:18:48 +05302238 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002240exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if (status != PSA_SUCCESS) {
2242 psa_fail_key_creation(target_slot, driver);
2243 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002248}
2249
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002250
2251
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002252/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002253/* Message digests */
2254/****************************************************************/
2255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002258 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if (operation->id == 0) {
2260 return PSA_SUCCESS;
2261 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002264 operation->id = 0;
2265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267}
2268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2270 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002272 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002273
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002274 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002276 status = PSA_ERROR_BAD_STATE;
2277 goto exit;
2278 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002281 status = PSA_ERROR_INVALID_ARGUMENT;
2282 goto exit;
2283 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002284
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002285 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2286 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002290
2291exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (status != PSA_SUCCESS) {
2293 psa_hash_abort(operation);
2294 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002295
2296 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002297}
2298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2300 const uint8_t *input,
2301 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002302{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002303 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002306 status = PSA_ERROR_BAD_STATE;
2307 goto exit;
2308 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002309
Steven Cooremanc8288352021-03-04 14:02:19 +01002310 /* Don't require hash implementations to behave correctly on a
2311 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (input_length == 0) {
2313 return PSA_SUCCESS;
2314 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002317
2318exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (status != PSA_SUCCESS) {
2320 psa_hash_abort(operation);
2321 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324}
2325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2327 uint8_t *hash,
2328 size_t hash_size,
2329 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002331 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (operation->id == 0) {
2333 return PSA_ERROR_BAD_STATE;
2334 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335
Steven Cooreman1e582352021-02-18 17:24:37 +01002336 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 operation, hash, hash_size, hash_length);
2338 psa_hash_abort(operation);
2339 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340}
2341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2343 const uint8_t *hash,
2344 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345{
Ronald Cron69a63422021-10-18 09:47:58 +02002346 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002348 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 operation,
2350 actual_hash, sizeof(actual_hash),
2351 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002354 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002358 status = PSA_ERROR_INVALID_SIGNATURE;
2359 goto exit;
2360 }
2361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002363 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002365
2366exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2368 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002369 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002373}
2374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375psa_status_t psa_hash_compute(psa_algorithm_t alg,
2376 const uint8_t *input, size_t input_length,
2377 uint8_t *hash, size_t hash_size,
2378 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002379{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002380 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (!PSA_ALG_IS_HASH(alg)) {
2382 return PSA_ERROR_INVALID_ARGUMENT;
2383 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2386 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002387}
2388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389psa_status_t psa_hash_compare(psa_algorithm_t alg,
2390 const uint8_t *input, size_t input_length,
2391 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002392{
Ronald Cron69a63422021-10-18 09:47:58 +02002393 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002394 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (!PSA_ALG_IS_HASH(alg)) {
2397 return PSA_ERROR_INVALID_ARGUMENT;
2398 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002399
Steven Cooreman1e582352021-02-18 17:24:37 +01002400 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 alg, input, input_length,
2402 actual_hash, sizeof(actual_hash),
2403 &actual_hash_length);
2404 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002405 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 }
2407 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002408 status = PSA_ERROR_INVALID_SIGNATURE;
2409 goto exit;
2410 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002412 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002414
2415exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2417 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002418}
2419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2421 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002422{
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (source_operation->id == 0 ||
2424 target_operation->id != 0) {
2425 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002426 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2429 target_operation);
2430 if (status != PSA_SUCCESS) {
2431 psa_hash_abort(target_operation);
2432 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002435}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002436
2437
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002438/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002439/* MAC */
2440/****************************************************************/
2441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002443{
Steven Cooremane6804192021-03-19 18:28:56 +01002444 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (operation->id == 0) {
2446 return PSA_SUCCESS;
2447 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002450 operation->mac_size = 0;
2451 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002452 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002455}
2456
Ronald Cron2dff3b22021-06-17 16:33:22 +02002457static psa_status_t psa_mac_finalize_alg_and_key_validation(
2458 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002459 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 psa_key_type_t key_type = psa_get_key_type(attributes);
2464 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (!PSA_ALG_IS_MAC(alg)) {
2467 return PSA_ERROR_INVALID_ARGUMENT;
2468 }
Steven Cooremane6804192021-03-19 18:28:56 +01002469
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002470 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 status = psa_mac_key_can_do(alg, key_type);
2472 if (status != PSA_SUCCESS) {
2473 return status;
2474 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002475
Steven Cooremand1a68f12021-05-10 11:13:41 +02002476 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002480 /* A very short MAC is too short for security since it can be
2481 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2482 * so we make this our minimum, even though 32 bits is still
2483 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002485 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2488 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002489 /* It's impossible to "truncate" to a larger length than the full length
2490 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002492 }
2493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002495 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2496 * that is disabled in the compile-time configuration. The result can
2497 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2498 * configuration into account. In this case, force a return of
2499 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2500 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2501 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2502 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2503 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002505 }
2506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002508}
2509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2511 mbedtls_svc_key_id_t key,
2512 psa_algorithm_t alg,
2513 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002514{
2515 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2516 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002517 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518
2519 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002521 status = PSA_ERROR_BAD_STATE;
2522 goto exit;
2523 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524
2525 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 key,
2527 &slot,
2528 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2529 alg);
2530 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002531 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002533
2534 psa_key_attributes_t attributes = {
2535 .core = slot->attr
2536 };
2537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2539 &operation->mac_size);
2540 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002543
2544 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002545 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (is_sign) {
2547 status = psa_driver_wrapper_mac_sign_setup(operation,
2548 &attributes,
2549 slot->key.data,
2550 slot->key.bytes,
2551 alg);
2552 } else {
2553 status = psa_driver_wrapper_mac_verify_setup(operation,
2554 &attributes,
2555 slot->key.data,
2556 slot->key.bytes,
2557 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002558 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002559
Gilles Peskinefbfac682018-07-08 20:51:54 +02002560exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (status != PSA_SUCCESS) {
2562 psa_mac_abort(operation);
2563 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568}
2569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2571 mbedtls_svc_key_id_t key,
2572 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002573{
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002575}
2576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2578 mbedtls_svc_key_id_t key,
2579 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002580{
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002582}
2583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2585 const uint8_t *input,
2586 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587{
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (operation->id == 0) {
2589 return PSA_ERROR_BAD_STATE;
2590 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002591
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002592 /* Don't require hash implementations to behave correctly on a
2593 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (input_length == 0) {
2595 return PSA_SUCCESS;
2596 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2599 input, input_length);
2600 if (status != PSA_SUCCESS) {
2601 psa_mac_abort(operation);
2602 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605}
2606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2608 uint8_t *mac,
2609 size_t mac_size,
2610 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002611{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002612 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2613 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002616 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002617 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002618 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002621 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002622 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002623 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002624
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002625 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2626 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002628 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002629 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002630 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002633 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002634 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002635 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 status = psa_driver_wrapper_mac_sign_finish(operation,
2638 mac, operation->mac_size,
2639 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002640
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002641exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002642 /* In case of success, set the potential excess room in the output buffer
2643 * to an invalid value, to avoid potentially leaking a longer MAC.
2644 * In case of error, set the output length and content to a safe default,
2645 * such that in case the caller misses an error check, the output would be
2646 * an unachievable MAC.
2647 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002649 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002650 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002651 }
mohammad16036df908f2018-04-02 08:34:15 -07002652
Paul Elliottdc42ca82023-02-24 18:11:59 +00002653 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002658}
2659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2661 const uint8_t *mac,
2662 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002663{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2665 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002668 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002669 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002670 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002673 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002674 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002675 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002678 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002679 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002680 }
2681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 status = psa_driver_wrapper_mac_verify_finish(operation,
2683 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002684
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689}
2690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2692 psa_algorithm_t alg,
2693 const uint8_t *input,
2694 size_t input_length,
2695 uint8_t *mac,
2696 size_t mac_size,
2697 size_t *mac_length,
2698 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002699{
2700 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002701 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2702 psa_key_slot_t *slot;
2703 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704
Ronald Cron51131b52021-06-17 17:17:20 +02002705 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 key,
2707 &slot,
2708 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2709 alg);
2710 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002711 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002713
Ronald Cron51131b52021-06-17 17:17:20 +02002714 psa_key_attributes_t attributes = {
2715 .core = slot->attr
2716 };
2717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2719 &operation_mac_size);
2720 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002721 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002725 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002726 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002727 }
2728
2729 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 &attributes,
2731 slot->key.data, slot->key.bytes,
2732 alg,
2733 input, input_length,
2734 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002735
2736exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002737 /* In case of success, set the potential excess room in the output buffer
2738 * to an invalid value, to avoid potentially leaking a longer MAC.
2739 * In case of error, set the output length and content to a safe default,
2740 * such that in case the caller misses an error check, the output would be
2741 * an unachievable MAC.
2742 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002744 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002745 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002746 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002747
2748 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002753}
2754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002756 psa_algorithm_t alg,
2757 const uint8_t *input,
2758 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 uint8_t *mac,
2760 size_t mac_size,
2761 size_t *mac_length)
2762{
2763 return psa_mac_compute_internal(key, alg,
2764 input, input_length,
2765 mac, mac_size, mac_length, 1);
2766}
2767
2768psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2769 psa_algorithm_t alg,
2770 const uint8_t *input,
2771 size_t input_length,
2772 const uint8_t *mac,
2773 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002774{
2775 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002776 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2777 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 status = psa_mac_compute_internal(key, alg,
2780 input, input_length,
2781 actual_mac, sizeof(actual_mac),
2782 &actual_mac_length, 0);
2783 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002784 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002788 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002789 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002790 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002792 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002793 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002794 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002795
2796exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002800}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002801
Gilles Peskinee59236f2018-01-27 23:32:46 +01002802/****************************************************************/
2803/* Asymmetric cryptography */
2804/****************************************************************/
2805
Gilles Peskine449bd832023-01-11 14:50:10 +01002806static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2807 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002808{
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 if (input_is_message) {
2810 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2811 return PSA_ERROR_INVALID_ARGUMENT;
2812 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2815 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2816 return PSA_ERROR_INVALID_ARGUMENT;
2817 }
2818 }
2819 } else {
2820 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2821 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002822 }
2823 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002824
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002826}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002827
Gilles Peskine449bd832023-01-11 14:50:10 +01002828static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2829 int input_is_message,
2830 psa_algorithm_t alg,
2831 const uint8_t *input,
2832 size_t input_length,
2833 uint8_t *signature,
2834 size_t signature_size,
2835 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002836{
2837 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2838 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2839 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002840
2841 *signature_length = 0;
2842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 status = psa_sign_verify_check_alg(input_is_message, alg);
2844 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002845 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002847
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002848 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002849 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002850 * buffer can in principle be empty since it doesn't actually have
2851 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 if (signature_size == 0) {
2853 return PSA_ERROR_BUFFER_TOO_SMALL;
2854 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002855
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002856 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 key, &slot,
2858 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2859 PSA_KEY_USAGE_SIGN_HASH,
2860 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002863 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002867 status = PSA_ERROR_INVALID_ARGUMENT;
2868 goto exit;
2869 }
2870
2871 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002873 };
2874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002876 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002877 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002878 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 signature, signature_size, signature_length);
2880 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002881
2882 status = psa_driver_wrapper_sign_hash(
2883 &attributes, slot->key.data, slot->key.bytes,
2884 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002886 }
2887
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002888
2889exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002890 psa_wipe_tag_output_buffer(signature, status, signature_size,
2891 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002894
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002896}
2897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2899 int input_is_message,
2900 psa_algorithm_t alg,
2901 const uint8_t *input,
2902 size_t input_length,
2903 const uint8_t *signature,
2904 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002905{
2906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2907 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2908 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 status = psa_sign_verify_check_alg(input_is_message, alg);
2911 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002912 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002914
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002915 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 key, &slot,
2917 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2918 PSA_KEY_USAGE_VERIFY_HASH,
2919 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (status != PSA_SUCCESS) {
2922 return status;
2923 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002924
2925 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002927 };
2928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002930 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002931 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002932 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 signature, signature_length);
2934 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002935 status = psa_driver_wrapper_verify_hash(
2936 &attributes, slot->key.data, slot->key.bytes,
2937 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002939 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002944
2945}
2946
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002947psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002948 const psa_key_attributes_t *attributes,
2949 const uint8_t *key_buffer,
2950 size_t key_buffer_size,
2951 psa_algorithm_t alg,
2952 const uint8_t *input,
2953 size_t input_length,
2954 uint8_t *signature,
2955 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002957{
2958 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002961 size_t hash_length;
2962 uint8_t hash[PSA_HASH_MAX_SIZE];
2963
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002964 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 PSA_ALG_SIGN_GET_HASH(alg),
2966 input, input_length,
2967 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002970 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002972
2973 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 attributes, key_buffer, key_buffer_size,
2975 alg, hash, hash_length,
2976 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002977 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002980}
2981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2983 psa_algorithm_t alg,
2984 const uint8_t *input,
2985 size_t input_length,
2986 uint8_t *signature,
2987 size_t signature_size,
2988 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002989{
2990 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002991 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002993}
2994
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002995psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002996 const psa_key_attributes_t *attributes,
2997 const uint8_t *key_buffer,
2998 size_t key_buffer_size,
2999 psa_algorithm_t alg,
3000 const uint8_t *input,
3001 size_t input_length,
3002 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003004{
3005 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003008 size_t hash_length;
3009 uint8_t hash[PSA_HASH_MAX_SIZE];
3010
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003011 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 PSA_ALG_SIGN_GET_HASH(alg),
3013 input, input_length,
3014 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003017 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003019
3020 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 attributes, key_buffer, key_buffer_size,
3022 alg, hash, hash_length,
3023 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003024 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003025
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003027}
3028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3030 psa_algorithm_t alg,
3031 const uint8_t *input,
3032 size_t input_length,
3033 const uint8_t *signature,
3034 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003035{
3036 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003037 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003039}
3040
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003041psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003042 const psa_key_attributes_t *attributes,
3043 const uint8_t *key_buffer, size_t key_buffer_size,
3044 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003046{
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3048 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3049 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003050#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3052 return mbedtls_psa_rsa_sign_hash(
3053 attributes,
3054 key_buffer, key_buffer_size,
3055 alg, hash, hash_length,
3056 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003057#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3058 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 } else {
3060 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003061 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3063 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003064#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3066 return mbedtls_psa_ecdsa_sign_hash(
3067 attributes,
3068 key_buffer, key_buffer_size,
3069 alg, hash, hash_length,
3070 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003071#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3072 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 } else {
3074 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003075 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003076 }
Ronald Cron4501c982021-03-19 20:09:32 +01003077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 (void) key_buffer;
3079 (void) key_buffer_size;
3080 (void) hash;
3081 (void) hash_length;
3082 (void) signature;
3083 (void) signature_size;
3084 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003087}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3090 psa_algorithm_t alg,
3091 const uint8_t *hash,
3092 size_t hash_length,
3093 uint8_t *signature,
3094 size_t signature_size,
3095 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003096{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003097 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003098 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003100}
3101
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003102psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003103 const psa_key_attributes_t *attributes,
3104 const uint8_t *key_buffer, size_t key_buffer_size,
3105 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003107{
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3109 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3110 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003111#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3113 return mbedtls_psa_rsa_verify_hash(
3114 attributes,
3115 key_buffer, key_buffer_size,
3116 alg, hash, hash_length,
3117 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003118#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3119 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 } else {
3121 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003122 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3124 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003125#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3127 return mbedtls_psa_ecdsa_verify_hash(
3128 attributes,
3129 key_buffer, key_buffer_size,
3130 alg, hash, hash_length,
3131 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003132#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3133 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 } else {
3135 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003136 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003137 }
Ronald Cron4501c982021-03-19 20:09:32 +01003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 (void) key_buffer;
3140 (void) key_buffer_size;
3141 (void) hash;
3142 (void) hash_length;
3143 (void) signature;
3144 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003145
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003147}
3148
Gilles Peskine449bd832023-01-11 14:50:10 +01003149psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3150 psa_algorithm_t alg,
3151 const uint8_t *hash,
3152 size_t hash_length,
3153 const uint8_t *signature,
3154 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003155{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003156 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003157 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003159}
3160
Gilles Peskine449bd832023-01-11 14:50:10 +01003161psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3162 psa_algorithm_t alg,
3163 const uint8_t *input,
3164 size_t input_length,
3165 const uint8_t *salt,
3166 size_t salt_length,
3167 uint8_t *output,
3168 size_t output_size,
3169 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003170{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003172 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003173 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003174
Darryl Green5cc689a2018-07-24 15:34:10 +01003175 (void) input;
3176 (void) input_length;
3177 (void) salt;
3178 (void) output;
3179 (void) output_size;
3180
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003181 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003182
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3184 return PSA_ERROR_INVALID_ARGUMENT;
3185 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003186
Ronald Cron5c522922020-11-14 16:35:34 +01003187 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3189 if (status != PSA_SUCCESS) {
3190 return status;
3191 }
3192 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3193 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003194 status = PSA_ERROR_INVALID_ARGUMENT;
3195 goto exit;
3196 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003198 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003200 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003201
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003202 status = psa_driver_wrapper_asymmetric_encrypt(
3203 &attributes, slot->key.data, slot->key.bytes,
3204 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003206exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003208
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003210}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3213 psa_algorithm_t alg,
3214 const uint8_t *input,
3215 size_t input_length,
3216 const uint8_t *salt,
3217 size_t salt_length,
3218 uint8_t *output,
3219 size_t output_size,
3220 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003222 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003223 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003224 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003225
Darryl Green5cc689a2018-07-24 15:34:10 +01003226 (void) input;
3227 (void) input_length;
3228 (void) salt;
3229 (void) output;
3230 (void) output_size;
3231
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003232 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003233
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3235 return PSA_ERROR_INVALID_ARGUMENT;
3236 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003237
Ronald Cron5c522922020-11-14 16:35:34 +01003238 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3240 if (status != PSA_SUCCESS) {
3241 return status;
3242 }
3243 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003244 status = PSA_ERROR_INVALID_ARGUMENT;
3245 goto exit;
3246 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003248 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003250 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003251
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003252 status = psa_driver_wrapper_asymmetric_decrypt(
3253 &attributes, slot->key.data, slot->key.bytes,
3254 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003256
3257exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003261}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003262
Paul Elliott9fe12f62022-11-30 19:16:02 +00003263/****************************************************************/
3264/* Asymmetric interruptible cryptography */
3265/****************************************************************/
3266
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003267static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3268
Paul Elliott9fe12f62022-11-30 19:16:02 +00003269void psa_interruptible_set_max_ops(uint32_t max_ops)
3270{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003271 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003272}
3273
3274uint32_t psa_interruptible_get_max_ops(void)
3275{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003276 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003277}
3278
Paul Elliott9fe12f62022-11-30 19:16:02 +00003279uint32_t psa_sign_hash_get_num_ops(
3280 const psa_sign_hash_interruptible_operation_t *operation)
3281{
Paul Elliott296ede92022-12-15 17:00:30 +00003282 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003283}
3284
3285uint32_t psa_verify_hash_get_num_ops(
3286 const psa_verify_hash_interruptible_operation_t *operation)
3287{
Paul Elliott296ede92022-12-15 17:00:30 +00003288 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003289}
3290
Paul Elliott59ad9452022-12-18 15:09:02 +00003291static psa_status_t psa_sign_hash_abort_internal(
3292 psa_sign_hash_interruptible_operation_t *operation)
3293{
3294 if (operation->id == 0) {
3295 /* The object has (apparently) been initialized but it is not (yet)
3296 * in use. It's ok to call abort on such an object, and there's
3297 * nothing to do. */
3298 return PSA_SUCCESS;
3299 }
3300
3301 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3302
3303 status = psa_driver_wrapper_sign_hash_abort(operation);
3304
3305 operation->id = 0;
3306
Paul Elliotte6145dc2023-02-07 12:51:21 +00003307 /* Do not clear either the error_occurred or num_ops elements here as they
3308 * only want to be cleared by the application calling abort, not by abort
3309 * being called at completion of an operation. */
3310
Paul Elliott59ad9452022-12-18 15:09:02 +00003311 return status;
3312}
3313
Paul Elliott9fe12f62022-11-30 19:16:02 +00003314psa_status_t psa_sign_hash_start(
3315 psa_sign_hash_interruptible_operation_t *operation,
3316 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3317 const uint8_t *hash, size_t hash_length)
3318{
3319 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3320 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3321 psa_key_slot_t *slot;
3322
Paul Elliottc9774412023-02-06 15:14:07 +00003323 /* Check that start has not been previously called, or operation has not
3324 * previously errored. */
3325 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003326 return PSA_ERROR_BAD_STATE;
3327 }
3328
Paul Elliott9fe12f62022-11-30 19:16:02 +00003329 status = psa_sign_verify_check_alg(0, alg);
3330 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003331 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003332 return status;
3333 }
3334
3335 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3336 PSA_KEY_USAGE_SIGN_HASH,
3337 alg);
3338
3339 if (status != PSA_SUCCESS) {
3340 goto exit;
3341 }
3342
3343 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3344 status = PSA_ERROR_INVALID_ARGUMENT;
3345 goto exit;
3346 }
3347
3348 psa_key_attributes_t attributes = {
3349 .core = slot->attr
3350 };
3351
Paul Elliott296ede92022-12-15 17:00:30 +00003352 /* Ensure ops count gets reset, in case of operation re-use. */
3353 operation->num_ops = 0;
3354
Paul Elliott9fe12f62022-11-30 19:16:02 +00003355 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3356 slot->key.data,
3357 slot->key.bytes, alg,
3358 hash, hash_length);
3359exit:
3360
3361 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003362 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003363 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003364 }
3365
3366 unlock_status = psa_unlock_key_slot(slot);
3367
Paul Elliottc9774412023-02-06 15:14:07 +00003368 if (unlock_status != PSA_SUCCESS) {
3369 operation->error_occurred = 1;
3370 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003371
Paul Elliottc9774412023-02-06 15:14:07 +00003372 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003373}
3374
3375
3376psa_status_t psa_sign_hash_complete(
3377 psa_sign_hash_interruptible_operation_t *operation,
3378 uint8_t *signature, size_t signature_size,
3379 size_t *signature_length)
3380{
3381 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3382
3383 *signature_length = 0;
3384
Paul Elliottc9774412023-02-06 15:14:07 +00003385 /* Check that start has been called first, and that operation has not
3386 * previously errored. */
3387 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003388 status = PSA_ERROR_BAD_STATE;
3389 goto exit;
3390 }
3391
Paul Elliott096abc42023-02-03 18:33:23 +00003392 /* Immediately reject a zero-length signature buffer. This guarantees that
3393 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003394 if (signature_size == 0) {
3395 status = PSA_ERROR_BUFFER_TOO_SMALL;
3396 goto exit;
3397 }
3398
3399 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3400 signature_size,
3401 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003402
Paul Elliott296ede92022-12-15 17:00:30 +00003403 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003404 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003405
Paul Elliottebe225c2023-02-10 14:32:53 +00003406exit:
3407
Paul Elliott59200a22023-02-21 15:07:40 +00003408 psa_wipe_tag_output_buffer(signature, status, signature_size,
3409 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003410
Paul Elliott53bb3122023-02-10 14:22:22 +00003411 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003412 if (status != PSA_SUCCESS) {
3413 operation->error_occurred = 1;
3414 }
3415
Paul Elliott59ad9452022-12-18 15:09:02 +00003416 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417 }
3418
3419 return status;
3420}
3421
3422psa_status_t psa_sign_hash_abort(
3423 psa_sign_hash_interruptible_operation_t *operation)
3424{
Paul Elliott59ad9452022-12-18 15:09:02 +00003425 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3426
3427 status = psa_sign_hash_abort_internal(operation);
3428
3429 /* We clear the number of ops done here, so that it is not cleared when
3430 * the operation fails or succeeds, only on manual abort. */
3431 operation->num_ops = 0;
3432
Paul Elliottc9774412023-02-06 15:14:07 +00003433 /* Likewise, failure state. */
3434 operation->error_occurred = 0;
3435
Paul Elliott59ad9452022-12-18 15:09:02 +00003436 return status;
3437}
3438
3439static psa_status_t psa_verify_hash_abort_internal(
3440 psa_verify_hash_interruptible_operation_t *operation)
3441{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003442 if (operation->id == 0) {
3443 /* The object has (apparently) been initialized but it is not (yet)
3444 * in use. It's ok to call abort on such an object, and there's
3445 * nothing to do. */
3446 return PSA_SUCCESS;
3447 }
3448
Paul Elliott59ad9452022-12-18 15:09:02 +00003449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3450
3451 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003452
3453 operation->id = 0;
3454
Paul Elliotte6145dc2023-02-07 12:51:21 +00003455 /* Do not clear either the error_occurred or num_ops elements here as they
3456 * only want to be cleared by the application calling abort, not by abort
3457 * being called at completion of an operation. */
3458
Paul Elliott59ad9452022-12-18 15:09:02 +00003459 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003460}
3461
3462psa_status_t psa_verify_hash_start(
3463 psa_verify_hash_interruptible_operation_t *operation,
3464 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3465 const uint8_t *hash, size_t hash_length,
3466 const uint8_t *signature, size_t signature_length)
3467{
3468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3469 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3470 psa_key_slot_t *slot;
3471
Paul Elliottc9774412023-02-06 15:14:07 +00003472 /* Check that start has not been previously called, or operation has not
3473 * previously errored. */
3474 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475 return PSA_ERROR_BAD_STATE;
3476 }
3477
3478 status = psa_sign_verify_check_alg(0, alg);
3479 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003480 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003481 return status;
3482 }
3483
3484 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3485 PSA_KEY_USAGE_VERIFY_HASH,
3486 alg);
3487
3488 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003489 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003490 return status;
3491 }
3492
3493 psa_key_attributes_t attributes = {
3494 .core = slot->attr
3495 };
3496
Paul Elliott296ede92022-12-15 17:00:30 +00003497 /* Ensure ops count gets reset, in case of operation re-use. */
3498 operation->num_ops = 0;
3499
Paul Elliott9fe12f62022-11-30 19:16:02 +00003500 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3501 slot->key.data,
3502 slot->key.bytes,
3503 alg, hash, hash_length,
3504 signature, signature_length);
3505
3506 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003507 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003508 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003509 }
3510
3511 unlock_status = psa_unlock_key_slot(slot);
3512
Paul Elliottc9774412023-02-06 15:14:07 +00003513 if (unlock_status != PSA_SUCCESS) {
3514 operation->error_occurred = 1;
3515 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003516
Paul Elliottc9774412023-02-06 15:14:07 +00003517 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003518}
3519
3520psa_status_t psa_verify_hash_complete(
3521 psa_verify_hash_interruptible_operation_t *operation)
3522{
3523 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3524
Paul Elliottc9774412023-02-06 15:14:07 +00003525 /* Check that start has been called first, and that operation has not
3526 * previously errored. */
3527 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003528 status = PSA_ERROR_BAD_STATE;
3529 goto exit;
3530 }
3531
3532 status = psa_driver_wrapper_verify_hash_complete(operation);
3533
Paul Elliott296ede92022-12-15 17:00:30 +00003534 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003535 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003536 operation);
3537
Paul Elliottebe225c2023-02-10 14:32:53 +00003538exit:
3539
Paul Elliott9fe12f62022-11-30 19:16:02 +00003540 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003541 if (status != PSA_SUCCESS) {
3542 operation->error_occurred = 1;
3543 }
3544
Paul Elliott59ad9452022-12-18 15:09:02 +00003545 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003546 }
3547
3548 return status;
3549}
3550
3551psa_status_t psa_verify_hash_abort(
3552 psa_verify_hash_interruptible_operation_t *operation)
3553{
Paul Elliott59ad9452022-12-18 15:09:02 +00003554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003555
Paul Elliott59ad9452022-12-18 15:09:02 +00003556 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003557
Paul Elliott59ad9452022-12-18 15:09:02 +00003558 /* We clear the number of ops done here, so that it is not cleared when
3559 * the operation fails or succeeds, only on manual abort. */
3560 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003561
Paul Elliottc9774412023-02-06 15:14:07 +00003562 /* Likewise, failure state. */
3563 operation->error_occurred = 0;
3564
Paul Elliott59ad9452022-12-18 15:09:02 +00003565 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003566}
3567
Paul Elliott588f8ed2022-12-02 18:10:26 +00003568/****************************************************************/
3569/* Asymmetric interruptible cryptography internal */
3570/* implementations */
3571/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003572
Paul Elliott588f8ed2022-12-02 18:10:26 +00003573void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3574{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003575
Paul Elliott588f8ed2022-12-02 18:10:26 +00003576#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3577 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3578 defined(MBEDTLS_ECP_RESTARTABLE)
3579
3580 /* Internal implementation uses zero to indicate infinite number max ops,
3581 * therefore avoid this value, and set to minimum possible. */
3582 if (max_ops == 0) {
3583 max_ops = 1;
3584 }
3585
Paul Elliott588f8ed2022-12-02 18:10:26 +00003586 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003587#else
3588 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003589#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3590 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3591 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3592}
3593
Paul Elliott588f8ed2022-12-02 18:10:26 +00003594uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003595 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003596{
3597#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3598 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3599 defined(MBEDTLS_ECP_RESTARTABLE)
3600
Paul Elliott93d9ca82023-02-15 18:14:21 +00003601 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602#else
3603 (void) operation;
3604 return 0;
3605#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3606 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3607 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3608}
3609
3610uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003611 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003612{
3613 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3614 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3615 defined(MBEDTLS_ECP_RESTARTABLE)
3616
Paul Elliott93d9ca82023-02-15 18:14:21 +00003617 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003618#else
3619 (void) operation;
3620 return 0;
3621#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3622 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3623 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3624}
3625
3626psa_status_t mbedtls_psa_sign_hash_start(
3627 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3628 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3629 size_t key_buffer_size, psa_algorithm_t alg,
3630 const uint8_t *hash, size_t hash_length)
3631{
3632 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003633 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003634
Paul Elliott068fe072023-01-16 13:59:15 +00003635 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3636 return PSA_ERROR_NOT_SUPPORTED;
3637 }
3638
3639 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003640 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003641 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003642
3643#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003644 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3645 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646
Paul Elliotta1c94092023-02-15 16:38:04 +00003647 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3648
Paul Elliott93d9ca82023-02-15 18:14:21 +00003649 /* Ensure num_ops is zero'ed in case of context re-use. */
3650 operation->num_ops = 0;
3651
Paul Elliott068fe072023-01-16 13:59:15 +00003652 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3653 attributes->core.bits,
3654 key_buffer,
3655 key_buffer_size,
3656 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003657
Paul Elliott068fe072023-01-16 13:59:15 +00003658 if (status != PSA_SUCCESS) {
3659 return status;
3660 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003661
Paul Elliott1bc59df2023-02-05 13:41:57 +00003662 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003663 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003664
Paul Elliott068fe072023-01-16 13:59:15 +00003665 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003666 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003667 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003668
Paul Elliott0290a762023-02-09 14:30:24 +00003669 /* We only need to store the same length of hash as the private key size
3670 * here, it would be truncated by the internal implementation anyway. */
3671 required_hash_length = (hash_length < operation->coordinate_bytes ?
3672 hash_length : operation->coordinate_bytes);
3673
Paul Elliottba70ad42023-02-15 18:23:53 +00003674 if (required_hash_length > sizeof(operation->hash)) {
3675 /* Shouldn't happen, but better safe than sorry. */
3676 return PSA_ERROR_CORRUPTION_DETECTED;
3677 }
3678
Paul Elliott0290a762023-02-09 14:30:24 +00003679 memcpy(operation->hash, hash, required_hash_length);
3680 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003681
3682 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003683
3684#else
Paul Elliott068fe072023-01-16 13:59:15 +00003685 (void) operation;
3686 (void) key_buffer;
3687 (void) key_buffer_size;
3688 (void) alg;
3689 (void) hash;
3690 (void) hash_length;
3691 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003692 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693
Paul Elliott068fe072023-01-16 13:59:15 +00003694 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003695#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3696 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3697 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003698}
3699
3700psa_status_t mbedtls_psa_sign_hash_complete(
3701 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3702 uint8_t *signature, size_t signature_size,
3703 size_t *signature_length)
3704{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003705#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3706 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3707 defined(MBEDTLS_ECP_RESTARTABLE)
3708
Paul Elliotta1c94092023-02-15 16:38:04 +00003709 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003710 mbedtls_mpi r;
3711 mbedtls_mpi s;
3712
Paul Elliott46845252023-02-03 14:59:11 +00003713 mbedtls_mpi_init(&r);
3714 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003716 /* Ensure max_ops is set to the current value (or default). */
3717 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3718
Paul Elliotta1c94092023-02-15 16:38:04 +00003719 if (signature_size < 2 * operation->coordinate_bytes) {
3720 status = PSA_ERROR_BUFFER_TOO_SMALL;
3721 goto exit;
3722 }
3723
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003725
Paul Elliott588f8ed2022-12-02 18:10:26 +00003726#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3727 status = mbedtls_to_psa_error(
3728 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003729 &r,
3730 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731 &operation->ctx->d,
3732 operation->hash,
3733 operation->hash_length,
3734 operation->md_alg,
3735 mbedtls_psa_get_random,
3736 MBEDTLS_PSA_RANDOM_STATE,
3737 &operation->restart_ctx));
3738#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003739 status = PSA_ERROR_NOT_SUPPORTED;
3740 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3742 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003743 status = mbedtls_to_psa_error(
3744 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003745 &r,
3746 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003747 &operation->ctx->d,
3748 operation->hash,
3749 operation->hash_length,
3750 mbedtls_psa_get_random,
3751 MBEDTLS_PSA_RANDOM_STATE,
3752 mbedtls_psa_get_random,
3753 MBEDTLS_PSA_RANDOM_STATE,
3754 &operation->restart_ctx));
3755 }
3756
Paul Elliottf8e5b562023-02-19 18:43:45 +00003757 /* Hide the fact that the restart context only holds a delta of number of
3758 * ops done during the last operation, not an absolute value. */
3759 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3760
Paul Elliott724bd252023-02-08 12:35:08 +00003761 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003762 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003763 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003764 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003765 operation->coordinate_bytes)
3766 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767
3768 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003769 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770 }
3771
3772 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003773 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003774 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003775 operation->coordinate_bytes,
3776 operation->coordinate_bytes)
3777 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003778
3779 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003780 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003781 }
3782
Paul Elliott1bc59df2023-02-05 13:41:57 +00003783 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003784
Paul Elliott724bd252023-02-08 12:35:08 +00003785 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003786 }
Paul Elliott724bd252023-02-08 12:35:08 +00003787
3788exit:
3789
3790 mbedtls_mpi_free(&r);
3791 mbedtls_mpi_free(&s);
3792 return status;
3793
Paul Elliott588f8ed2022-12-02 18:10:26 +00003794 #else
3795
3796 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003797 (void) signature;
3798 (void) signature_size;
3799 (void) signature_length;
3800
3801 return PSA_ERROR_NOT_SUPPORTED;
3802
3803#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3804 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3805 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3806}
3807
3808psa_status_t mbedtls_psa_sign_hash_abort(
3809 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3810{
3811
3812#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3813 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3814 defined(MBEDTLS_ECP_RESTARTABLE)
3815
3816 if (operation->ctx) {
3817 mbedtls_ecdsa_free(operation->ctx);
3818 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003819 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820 }
3821
3822 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3823
Paul Elliott93d9ca82023-02-15 18:14:21 +00003824 operation->num_ops = 0;
3825
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826 return PSA_SUCCESS;
3827
3828#else
3829
3830 (void) operation;
3831
3832 return PSA_ERROR_NOT_SUPPORTED;
3833
3834#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3835 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3836 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3837}
3838
3839psa_status_t mbedtls_psa_verify_hash_start(
3840 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3841 const psa_key_attributes_t *attributes,
3842 const uint8_t *key_buffer, size_t key_buffer_size,
3843 psa_algorithm_t alg,
3844 const uint8_t *hash, size_t hash_length,
3845 const uint8_t *signature, size_t signature_length)
3846{
3847 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003848 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003849 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850
Paul Elliott068fe072023-01-16 13:59:15 +00003851 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3852 return PSA_ERROR_NOT_SUPPORTED;
3853 }
3854
3855 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003856 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003857 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003858
3859#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003860 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3861 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003862
Paul Elliotta1c94092023-02-15 16:38:04 +00003863 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3864 mbedtls_mpi_init(&operation->r);
3865 mbedtls_mpi_init(&operation->s);
3866
Paul Elliott93d9ca82023-02-15 18:14:21 +00003867 /* Ensure num_ops is zero'ed in case of context re-use. */
3868 operation->num_ops = 0;
3869
Paul Elliott068fe072023-01-16 13:59:15 +00003870 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3871 attributes->core.bits,
3872 key_buffer,
3873 key_buffer_size,
3874 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003875
Paul Elliott068fe072023-01-16 13:59:15 +00003876 if (status != PSA_SUCCESS) {
3877 return status;
3878 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003879
Paul Elliottc569fc22023-02-10 13:02:54 +00003880 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003881
Paul Elliott1bc59df2023-02-05 13:41:57 +00003882 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003883 return PSA_ERROR_INVALID_SIGNATURE;
3884 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885
Paul Elliott068fe072023-01-16 13:59:15 +00003886 status = mbedtls_to_psa_error(
3887 mbedtls_mpi_read_binary(&operation->r,
3888 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003889 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003890
Paul Elliott068fe072023-01-16 13:59:15 +00003891 if (status != PSA_SUCCESS) {
3892 return status;
3893 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003894
Paul Elliott068fe072023-01-16 13:59:15 +00003895 status = mbedtls_to_psa_error(
3896 mbedtls_mpi_read_binary(&operation->s,
3897 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003898 coordinate_bytes,
3899 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003900
Paul Elliott068fe072023-01-16 13:59:15 +00003901 if (status != PSA_SUCCESS) {
3902 return status;
3903 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003904
Paul Elliott2c9843f2023-02-15 17:32:42 +00003905 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003906
Paul Elliott2c9843f2023-02-15 17:32:42 +00003907 if (status != PSA_SUCCESS) {
3908 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003909 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003910
Paul Elliott0290a762023-02-09 14:30:24 +00003911 /* We only need to store the same length of hash as the private key size
3912 * here, it would be truncated by the internal implementation anyway. */
3913 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3914 coordinate_bytes);
3915
Paul Elliottba70ad42023-02-15 18:23:53 +00003916 if (required_hash_length > sizeof(operation->hash)) {
3917 /* Shouldn't happen, but better safe than sorry. */
3918 return PSA_ERROR_CORRUPTION_DETECTED;
3919 }
3920
Paul Elliott0290a762023-02-09 14:30:24 +00003921 memcpy(operation->hash, hash, required_hash_length);
3922 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003923
3924 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003925#else
Paul Elliott068fe072023-01-16 13:59:15 +00003926 (void) operation;
3927 (void) key_buffer;
3928 (void) key_buffer_size;
3929 (void) alg;
3930 (void) hash;
3931 (void) hash_length;
3932 (void) signature;
3933 (void) signature_length;
3934 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003935 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003936 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003937
Paul Elliott068fe072023-01-16 13:59:15 +00003938 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003939#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3940 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3941 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003942}
3943
3944psa_status_t mbedtls_psa_verify_hash_complete(
3945 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3946{
3947
3948#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3949 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3950 defined(MBEDTLS_ECP_RESTARTABLE)
3951
Paul Elliottf8e5b562023-02-19 18:43:45 +00003952 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3953
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003954 /* Ensure max_ops is set to the current value (or default). */
3955 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3956
Paul Elliottf8e5b562023-02-19 18:43:45 +00003957 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003958 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3959 operation->hash,
3960 operation->hash_length,
3961 &operation->ctx->Q,
3962 &operation->r,
3963 &operation->s,
3964 &operation->restart_ctx));
3965
Paul Elliottf8e5b562023-02-19 18:43:45 +00003966 /* Hide the fact that the restart context only holds a delta of number of
3967 * ops done during the last operation, not an absolute value. */
3968 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3969
3970 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003971#else
3972 (void) operation;
3973
3974 return PSA_ERROR_NOT_SUPPORTED;
3975
3976#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3977 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3978 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3979}
3980
3981psa_status_t mbedtls_psa_verify_hash_abort(
3982 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3983{
3984
3985#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3986 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3987 defined(MBEDTLS_ECP_RESTARTABLE)
3988
3989 if (operation->ctx) {
3990 mbedtls_ecdsa_free(operation->ctx);
3991 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003992 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003993 }
3994
3995 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3996
Paul Elliott93d9ca82023-02-15 18:14:21 +00003997 operation->num_ops = 0;
3998
Paul Elliott588f8ed2022-12-02 18:10:26 +00003999 mbedtls_mpi_free(&operation->r);
4000 mbedtls_mpi_free(&operation->s);
4001
4002 return PSA_SUCCESS;
4003
4004#else
4005 (void) operation;
4006
4007 return PSA_ERROR_NOT_SUPPORTED;
4008
4009#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4010 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4011 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4012}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004013
mohammad1603503973b2018-03-12 15:59:30 +02004014/****************************************************************/
4015/* Symmetric cryptography */
4016/****************************************************************/
4017
Gilles Peskine449bd832023-01-11 14:50:10 +01004018static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4019 mbedtls_svc_key_id_t key,
4020 psa_algorithm_t alg,
4021 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004022{
4023 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4024 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004025 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4027 PSA_KEY_USAGE_ENCRYPT :
4028 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004029
4030 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004032 status = PSA_ERROR_BAD_STATE;
4033 goto exit;
4034 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004035
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004037 status = PSA_ERROR_INVALID_ARGUMENT;
4038 goto exit;
4039 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004040
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4042 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004043 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004045
Ronald Cron49fafa92021-03-10 08:34:23 +01004046 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004047 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004048 * so we only set it (in the driver wrapper) after resources have been
4049 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004050 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004052 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004054 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 }
4056 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004057
Ronald Crona4af55f2020-12-14 14:36:06 +01004058 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004060 };
4061
Ronald Cronab99ac22020-10-01 16:38:28 +02004062 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 if (cipher_operation == MBEDTLS_ENCRYPT) {
4064 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4065 &attributes,
4066 slot->key.data,
4067 slot->key.bytes,
4068 alg);
4069 } else {
4070 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4071 &attributes,
4072 slot->key.data,
4073 slot->key.bytes,
4074 alg);
4075 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004076
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004077exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 if (status != PSA_SUCCESS) {
4079 psa_cipher_abort(operation);
4080 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004081
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004083
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004085}
4086
Gilles Peskine449bd832023-01-11 14:50:10 +01004087psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4088 mbedtls_svc_key_id_t key,
4089 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004090{
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004092}
4093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4095 mbedtls_svc_key_id_t key,
4096 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004097{
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004099}
4100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4102 uint8_t *iv,
4103 size_t iv_size,
4104 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004105{
Ronald Cron6d051732020-10-01 14:10:20 +02004106 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004107 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4108 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004109
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004111 status = PSA_ERROR_BAD_STATE;
4112 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004113 }
4114
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004116 status = PSA_ERROR_BAD_STATE;
4117 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004118 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004119
Ronald Cron2fb90522021-07-05 12:31:44 +02004120 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004122 status = PSA_ERROR_BUFFER_TOO_SMALL;
4123 goto exit;
4124 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004127 status = PSA_ERROR_GENERIC_ERROR;
4128 goto exit;
4129 }
4130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131 status = psa_generate_random(local_iv, default_iv_length);
4132 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 }
Ronald Cron5618a392021-03-26 09:52:26 +01004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 status = psa_driver_wrapper_cipher_set_iv(operation,
4137 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004138
4139exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 if (status == PSA_SUCCESS) {
4141 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004142 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004143 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004145 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004147 }
Ronald Cron6d051732020-10-01 14:10:20 +02004148
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004150}
4151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4153 const uint8_t *iv,
4154 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004155{
Ronald Cron6d051732020-10-01 14:10:20 +02004156 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004159 status = PSA_ERROR_BAD_STATE;
4160 goto exit;
4161 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004164 status = PSA_ERROR_BAD_STATE;
4165 goto exit;
4166 }
Ronald Crona0d68172021-03-26 10:15:08 +01004167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004169 status = PSA_ERROR_INVALID_ARGUMENT;
4170 goto exit;
4171 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 status = psa_driver_wrapper_cipher_set_iv(operation,
4174 iv,
4175 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004176
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004179 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 } else {
4181 psa_cipher_abort(operation);
4182 }
4183 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004184}
4185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4187 const uint8_t *input,
4188 size_t input_length,
4189 uint8_t *output,
4190 size_t output_size,
4191 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004192{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004193 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004194
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004196 status = PSA_ERROR_BAD_STATE;
4197 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004198 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004201 status = PSA_ERROR_BAD_STATE;
4202 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004203 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004204
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 status = psa_driver_wrapper_cipher_update(operation,
4206 input,
4207 input_length,
4208 output,
4209 output_size,
4210 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004211
4212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 if (status != PSA_SUCCESS) {
4214 psa_cipher_abort(operation);
4215 }
Ronald Cron6d051732020-10-01 14:10:20 +02004216
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004218}
4219
Gilles Peskine449bd832023-01-11 14:50:10 +01004220psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4221 uint8_t *output,
4222 size_t output_size,
4223 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004224{
David Saadab4ecc272019-02-14 13:48:10 +02004225 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +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;
Steven Cooreman7df02922020-09-09 15:28:49 +02004230 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004231
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004233 status = PSA_ERROR_BAD_STATE;
4234 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004235 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 status = psa_driver_wrapper_cipher_finish(operation,
4238 output,
4239 output_size,
4240 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004241
4242exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 if (status == PSA_SUCCESS) {
4244 return psa_cipher_abort(operation);
4245 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004246 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004247 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004250 }
mohammad1603503973b2018-03-12 15:59:30 +02004251}
4252
Gilles Peskine449bd832023-01-11 14:50:10 +01004253psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004254{
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004256 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004257 * in use. It's ok to call abort on such an object, and there's
4258 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004260 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004263
Ronald Cron49fafa92021-03-10 08:34:23 +01004264 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004265 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004266 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004269}
4270
Gilles Peskine449bd832023-01-11 14:50:10 +01004271psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4272 psa_algorithm_t alg,
4273 const uint8_t *input,
4274 size_t input_length,
4275 uint8_t *output,
4276 size_t output_size,
4277 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004278{
4279 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004280 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004281 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004282 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4283 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004284
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004286 status = PSA_ERROR_INVALID_ARGUMENT;
4287 goto exit;
4288 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4291 PSA_KEY_USAGE_ENCRYPT,
4292 alg);
4293 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004294 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004296
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004297 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004299 };
4300
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4302 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004303 status = PSA_ERROR_GENERIC_ERROR;
4304 goto exit;
4305 }
4306
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 if (default_iv_length > 0) {
4308 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004309 status = PSA_ERROR_BUFFER_TOO_SMALL;
4310 goto exit;
4311 }
4312
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 status = psa_generate_random(local_iv, default_iv_length);
4314 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004315 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004317 }
4318
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004319 status = psa_driver_wrapper_cipher_encrypt(
4320 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004321 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004322 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004324
4325exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 unlock_status = psa_unlock_key_slot(slot);
4327 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004328 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004329 }
Ronald Cron23919522021-07-08 19:00:07 +02004330
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 if (status == PSA_SUCCESS) {
4332 if (default_iv_length > 0) {
4333 memcpy(output, local_iv, default_iv_length);
4334 }
4335 *output_length += default_iv_length;
4336 } else {
4337 *output_length = 0;
4338 }
4339
4340 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004341}
4342
Gilles Peskine449bd832023-01-11 14:50:10 +01004343psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4344 psa_algorithm_t alg,
4345 const uint8_t *input,
4346 size_t input_length,
4347 uint8_t *output,
4348 size_t output_size,
4349 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004350{
4351 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004352 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004353 psa_key_slot_t *slot = NULL;
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_DECRYPT,
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
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004367 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004369 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4372 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004373 status = PSA_ERROR_INVALID_ARGUMENT;
4374 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004376 status = PSA_ERROR_INVALID_ARGUMENT;
4377 goto exit;
4378 }
4379
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004380 status = psa_driver_wrapper_cipher_decrypt(
4381 &attributes, slot->key.data, slot->key.bytes,
4382 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004384
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004385exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 unlock_status = psa_unlock_key_slot(slot);
4387 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004388 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004390
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004392 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 }
Ronald Cron23919522021-07-08 19:00:07 +02004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004396}
4397
4398
mohammad16035955c982018-04-26 00:53:03 +03004399/****************************************************************/
4400/* AEAD */
4401/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004402
Paul Elliottbaff51c2021-09-28 17:44:45 +01004403/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004404static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004405{
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004407}
4408
4409/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004410static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4411 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004412{
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004416#if defined(PSA_WANT_ALG_GCM)
4417 case PSA_ALG_GCM:
4418 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 * arbitrarily large nonces. Please note that we do not generally
4420 * recommend the usage of nonces of greater length than
4421 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4422 * size, which can then lead to collisions if you encrypt a very
4423 * large number of messages.*/
4424 if (nonce_length != 0) {
4425 return PSA_SUCCESS;
4426 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004427 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004428#endif /* PSA_WANT_ALG_GCM */
4429#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004430 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 if (nonce_length >= 7 && nonce_length <= 13) {
4432 return PSA_SUCCESS;
4433 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004434 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004435#endif /* PSA_WANT_ALG_CCM */
4436#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004437 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 if (nonce_length == 12) {
4439 return PSA_SUCCESS;
4440 } else if (nonce_length == 8) {
4441 return PSA_ERROR_NOT_SUPPORTED;
4442 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004443 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004444#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004445 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004446 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004448 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004451}
4452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004454{
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4456 return PSA_ERROR_INVALID_ARGUMENT;
4457 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004460}
4461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4463 psa_algorithm_t alg,
4464 const uint8_t *nonce,
4465 size_t nonce_length,
4466 const uint8_t *additional_data,
4467 size_t additional_data_length,
4468 const uint8_t *plaintext,
4469 size_t plaintext_length,
4470 uint8_t *ciphertext,
4471 size_t ciphertext_size,
4472 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004473{
Ronald Cron215633c2021-03-16 17:15:37 +01004474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4475 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004476
mohammad1603f08a5502018-06-03 15:05:47 +03004477 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004478
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 status = psa_aead_check_algorithm(alg);
4480 if (status != PSA_SUCCESS) {
4481 return status;
4482 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004483
Ronald Cron9a986162021-03-26 12:40:07 +01004484 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4486 if (status != PSA_SUCCESS) {
4487 return status;
4488 }
mohammad16035955c982018-04-26 00:53:03 +03004489
Ronald Cron215633c2021-03-16 17:15:37 +01004490 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004492 };
mohammad16035955c982018-04-26 00:53:03 +03004493
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 status = psa_aead_check_nonce_length(alg, nonce_length);
4495 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004496 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004498
Ronald Cronde822812021-03-17 16:08:20 +01004499 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004500 &attributes, slot->key.data, slot->key.bytes,
4501 alg,
4502 nonce, nonce_length,
4503 additional_data, additional_data_length,
4504 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004506
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4508 memset(ciphertext, 0, ciphertext_size);
4509 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004510
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004511exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004515}
4516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4518 psa_algorithm_t alg,
4519 const uint8_t *nonce,
4520 size_t nonce_length,
4521 const uint8_t *additional_data,
4522 size_t additional_data_length,
4523 const uint8_t *ciphertext,
4524 size_t ciphertext_length,
4525 uint8_t *plaintext,
4526 size_t plaintext_size,
4527 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004528{
Ronald Cron215633c2021-03-16 17:15:37 +01004529 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4530 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004531
Gilles Peskineee652a32018-06-01 19:23:52 +02004532 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 status = psa_aead_check_algorithm(alg);
4535 if (status != PSA_SUCCESS) {
4536 return status;
4537 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004538
Ronald Cron9a986162021-03-26 12:40:07 +01004539 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4541 if (status != PSA_SUCCESS) {
4542 return status;
4543 }
mohammad16035955c982018-04-26 00:53:03 +03004544
Ronald Cron215633c2021-03-16 17:15:37 +01004545 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004547 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004548
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 status = psa_aead_check_nonce_length(alg, nonce_length);
4550 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004551 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004553
Ronald Cronde822812021-03-17 16:08:20 +01004554 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004555 &attributes, slot->key.data, slot->key.bytes,
4556 alg,
4557 nonce, nonce_length,
4558 additional_data, additional_data_length,
4559 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004561
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 if (status != PSA_SUCCESS && plaintext_size != 0) {
4563 memset(plaintext, 0, plaintext_size);
4564 }
mohammad160360a64d02018-06-03 17:20:42 +03004565
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004566exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 return status;
mohammad16035955c982018-04-26 00:53:03 +03004570}
4571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4573{
4574 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004577#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004579 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4581 return PSA_ERROR_INVALID_ARGUMENT;
4582 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004583 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004584#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004585
Przemek Stekiel86679c72022-10-06 17:06:56 +02004586#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004588 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4590 return PSA_ERROR_INVALID_ARGUMENT;
4591 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004592 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004593#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004594
Przemek Stekiel86679c72022-10-06 17:06:56 +02004595#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004597 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 if (tag_len != 16) {
4599 return PSA_ERROR_INVALID_ARGUMENT;
4600 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004601 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004602#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004603
4604 default:
4605 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004607 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004609}
4610
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004611/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004612static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4613 int is_encrypt,
4614 mbedtls_svc_key_id_t key,
4615 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004616{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4618 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4619 psa_key_slot_t *slot = NULL;
4620 psa_key_usage_t key_usage = 0;
4621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 status = psa_aead_check_algorithm(alg);
4623 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004624 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004628 status = PSA_ERROR_BAD_STATE;
4629 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004630 }
4631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 if (operation->nonce_set || operation->lengths_set ||
4633 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004634 status = PSA_ERROR_BAD_STATE;
4635 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004636 }
4637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004639 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004641 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4645 alg);
4646 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004647 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004649
4650 psa_key_attributes_t attributes = {
4651 .core = slot->attr
4652 };
4653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004655 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004657
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 if (is_encrypt) {
4659 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4660 &attributes,
4661 slot->key.data,
4662 slot->key.bytes,
4663 alg);
4664 } else {
4665 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4666 &attributes,
4667 slot->key.data,
4668 slot->key.bytes,
4669 alg);
4670 }
4671 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004672 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004676
4677exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004679
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004681 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004683 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 } else {
4685 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004686 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004689}
4690
Paul Elliott302ff6b2021-04-20 18:10:30 +01004691/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004692psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4693 mbedtls_svc_key_id_t key,
4694 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004695{
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004697}
4698
4699/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004700psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4701 mbedtls_svc_key_id_t key,
4702 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004703{
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004705}
4706
4707/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004708psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4709 uint8_t *nonce,
4710 size_t nonce_size,
4711 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004712{
4713 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004714 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004715 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004716
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004717 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004718
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004720 status = PSA_ERROR_BAD_STATE;
4721 goto exit;
4722 }
4723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004725 status = PSA_ERROR_BAD_STATE;
4726 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004727 }
4728
Paul Elliotte193ea82021-10-01 13:00:16 +01004729 /* For CCM, this size may not be correct according to the PSA
4730 * specification. The PSA Crypto 1.0.1 specification states:
4731 *
4732 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4733 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4734 *
4735 * However this restriction that L has to be the smallest integer is not
4736 * applied in practice, and it is not implementable here since the
4737 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4739 operation->alg);
4740 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004741 status = PSA_ERROR_BUFFER_TOO_SMALL;
4742 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004743 }
4744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 status = psa_generate_random(local_nonce, required_nonce_size);
4746 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004747 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004749
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004751
Paul Elliott39dc6b82021-05-11 19:16:09 +01004752exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if (status == PSA_SUCCESS) {
4754 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004755 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 } else {
4757 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004758 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004761}
4762
4763/* Set the nonce for a multipart authenticated encryption or decryption
4764 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004765psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4766 const uint8_t *nonce,
4767 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004768{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004769 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004772 status = PSA_ERROR_BAD_STATE;
4773 goto exit;
4774 }
4775
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004777 status = PSA_ERROR_BAD_STATE;
4778 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004779 }
4780
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4782 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004783 status = PSA_ERROR_INVALID_ARGUMENT;
4784 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004785 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4788 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004789
Paul Elliott39dc6b82021-05-11 19:16:09 +01004790exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004792 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 } else {
4794 psa_aead_abort(operation);
4795 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004798}
4799
4800/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004801psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4802 size_t ad_length,
4803 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004804{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004805 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004808 status = PSA_ERROR_BAD_STATE;
4809 goto exit;
4810 }
4811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 if (operation->lengths_set || operation->ad_started ||
4813 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004814 status = PSA_ERROR_BAD_STATE;
4815 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004816 }
4817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004819#if defined(PSA_WANT_ALG_GCM)
4820 case PSA_ALG_GCM:
4821 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 * bits. Without the guard this code will generate warnings on 32bit
4823 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004824#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 if (((uint64_t) ad_length) >> 61 != 0 ||
4826 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004827 status = PSA_ERROR_INVALID_ARGUMENT;
4828 goto exit;
4829 }
Paul Elliott325d3742021-09-27 17:56:28 +01004830#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004831 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004832#endif /* PSA_WANT_ALG_GCM */
4833#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004834 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004836 status = PSA_ERROR_INVALID_ARGUMENT;
4837 goto exit;
4838 }
4839 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004840#endif /* PSA_WANT_ALG_CCM */
4841#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004842 case PSA_ALG_CHACHA20_POLY1305:
4843 /* No length restrictions for ChaChaPoly. */
4844 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004845#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004846 default:
4847 break;
4848 }
Paul Elliott325d3742021-09-27 17:56:28 +01004849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4851 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004852
Paul Elliott39dc6b82021-05-11 19:16:09 +01004853exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004855 operation->ad_remaining = ad_length;
4856 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004857 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 } else {
4859 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004860 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004861
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004863}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004864
4865/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004866psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4867 const uint8_t *input,
4868 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004869{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004870 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004873 status = PSA_ERROR_BAD_STATE;
4874 goto exit;
4875 }
4876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004878 status = PSA_ERROR_BAD_STATE;
4879 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004880 }
4881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 if (operation->lengths_set) {
4883 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004884 status = PSA_ERROR_INVALID_ARGUMENT;
4885 goto exit;
4886 }
4887
4888 operation->ad_remaining -= input_length;
4889 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004890#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004892 status = PSA_ERROR_BAD_STATE;
4893 goto exit;
4894 }
4895#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004896
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 status = psa_driver_wrapper_aead_update_ad(operation, input,
4898 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004899
Paul Elliott39dc6b82021-05-11 19:16:09 +01004900exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004902 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 } else {
4904 psa_aead_abort(operation);
4905 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004906
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004908}
4909
4910/* Encrypt or decrypt a message fragment in an active multipart AEAD
4911 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004912psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4913 const uint8_t *input,
4914 size_t input_length,
4915 uint8_t *output,
4916 size_t output_size,
4917 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004918{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004919 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004920
4921 *output_length = 0;
4922
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004924 status = PSA_ERROR_BAD_STATE;
4925 goto exit;
4926 }
4927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004929 status = PSA_ERROR_BAD_STATE;
4930 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004931 }
4932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004934 /* Additional data length was supplied, but not all the additional
4935 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004937 status = PSA_ERROR_INVALID_ARGUMENT;
4938 goto exit;
4939 }
4940
4941 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004943 status = PSA_ERROR_INVALID_ARGUMENT;
4944 goto exit;
4945 }
4946
4947 operation->body_remaining -= input_length;
4948 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004949#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004951 status = PSA_ERROR_BAD_STATE;
4952 goto exit;
4953 }
4954#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4957 output, output_size,
4958 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004959
Paul Elliott39dc6b82021-05-11 19:16:09 +01004960exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004962 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 } else {
4964 psa_aead_abort(operation);
4965 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004966
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004968}
4969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004971{
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 if (operation->id == 0 || !operation->nonce_set) {
4973 return PSA_ERROR_BAD_STATE;
4974 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004975
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4977 operation->body_remaining != 0)) {
4978 return PSA_ERROR_INVALID_ARGUMENT;
4979 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004980
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004982}
4983
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004985psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4986 uint8_t *ciphertext,
4987 size_t ciphertext_size,
4988 size_t *ciphertext_length,
4989 uint8_t *tag,
4990 size_t tag_size,
4991 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004992{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004993 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4994
Paul Elliott302ff6b2021-04-20 18:10:30 +01004995 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004996 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004997
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 status = psa_aead_final_checks(operation);
4999 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005000 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005004 status = PSA_ERROR_BAD_STATE;
5005 goto exit;
5006 }
5007
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5009 ciphertext_size,
5010 ciphertext_length,
5011 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005012
Paul Elliott39dc6b82021-05-11 19:16:09 +01005013exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005014
5015
Paul Elliottf47b0952021-05-21 18:02:33 +01005016 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005017 * the zero tag size, make sure the tag is set to something implausible.
5018 * Even if the operation succeeds, make sure we clear the rest of the
5019 * buffer to prevent potential leakage of anything previously placed in
5020 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005021 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005022
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005024
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005026}
5027
5028/* Finish authenticating and decrypting a message in a multipart AEAD
5029 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005030psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5031 uint8_t *plaintext,
5032 size_t plaintext_size,
5033 size_t *plaintext_length,
5034 const uint8_t *tag,
5035 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005036{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005037 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5038
Paul Elliott302ff6b2021-04-20 18:10:30 +01005039 *plaintext_length = 0;
5040
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 status = psa_aead_final_checks(operation);
5042 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005043 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005047 status = PSA_ERROR_BAD_STATE;
5048 goto exit;
5049 }
5050
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5052 plaintext_size,
5053 plaintext_length,
5054 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005055
5056exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005058
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005060}
5061
5062/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005063psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005064{
5065 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5066
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005068 /* The object has (apparently) been initialized but it is not (yet)
5069 * in use. It's ok to call abort on such an object, and there's
5070 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005072 }
5073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005075
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005079}
5080
Gilles Peskinee59236f2018-01-27 23:32:46 +01005081/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005082/* Generators */
5083/****************************************************************/
5084
Przemek Stekiel69c46792022-06-10 12:59:51 +02005085#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005086 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005087 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305088 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
5089 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
John Durkop07cc04a2020-11-16 22:08:34 -08005090#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005091#endif /* At least one builtin KDF */
5092
Przemek Stekiel69c46792022-06-10 12:59:51 +02005093#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005094 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5095 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5096static psa_status_t psa_key_derivation_start_hmac(
5097 psa_mac_operation_t *operation,
5098 psa_algorithm_t hash_alg,
5099 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005101{
5102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5103 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5105 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5106 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005107
Steven Cooreman72f736a2021-05-07 14:14:37 +02005108 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 status = psa_driver_wrapper_mac_sign_setup(operation,
5112 &attributes,
5113 hmac_key, hmac_key_length,
5114 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005115
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 psa_reset_key_attributes(&attributes);
5117 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005118}
5119#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005120
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005121#define HKDF_STATE_INIT 0 /* no input yet */
5122#define HKDF_STATE_STARTED 1 /* got salt */
5123#define HKDF_STATE_KEYED 2 /* got key */
5124#define HKDF_STATE_OUTPUT 3 /* output started */
5125
Gilles Peskinecbe66502019-05-16 16:59:18 +02005126static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005128{
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5130 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5131 } else {
5132 return operation->alg;
5133 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005134}
5135
Gilles Peskine449bd832023-01-11 14:50:10 +01005136psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005137{
5138 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5140 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005141 /* The object has (apparently) been initialized but it is not
5142 * in use. It's ok to call abort on such an object, and there's
5143 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005145#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5147 mbedtls_free(operation->ctx.hkdf.info);
5148 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5149 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005150#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005151#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5152 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5154 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5155 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5156 if (operation->ctx.tls12_prf.secret != NULL) {
5157 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5158 operation->ctx.tls12_prf.secret_length);
5159 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005160 }
5161
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 if (operation->ctx.tls12_prf.seed != NULL) {
5163 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5164 operation->ctx.tls12_prf.seed_length);
5165 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005166 }
5167
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 if (operation->ctx.tls12_prf.label != NULL) {
5169 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5170 operation->ctx.tls12_prf.label_length);
5171 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005172 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005173#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 if (operation->ctx.tls12_prf.other_secret != NULL) {
5175 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5176 operation->ctx.tls12_prf.other_secret_length);
5177 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005178 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005179#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005180 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005181
5182 /* We leave the fields Ai and output_block to be erased safely by the
5183 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005185#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5186 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005187#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5189 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5190 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5191 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005192#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305193#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5194 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305195 if (operation->ctx.pbkdf2.salt != NULL) {
5196 mbedtls_platform_zeroize(operation->ctx.pbkdf2.salt,
5197 operation->ctx.pbkdf2.salt_length);
5198 mbedtls_free(operation->ctx.pbkdf2.salt);
5199 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305200
5201 status = PSA_SUCCESS;
5202 } else
5203#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005204 {
5205 status = PSA_ERROR_BAD_STATE;
5206 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 mbedtls_platform_zeroize(operation, sizeof(*operation));
5208 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005209}
5210
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005213{
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005217 }
5218
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005221}
5222
Gilles Peskine449bd832023-01-11 14:50:10 +01005223psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5224 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005225{
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 if (operation->alg == 0) {
5227 return PSA_ERROR_BAD_STATE;
5228 }
5229 if (capacity > operation->capacity) {
5230 return PSA_ERROR_INVALID_ARGUMENT;
5231 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005232 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005234}
5235
Przemek Stekiel69c46792022-06-10 12:59:51 +02005236#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005237/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005238static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5239 psa_algorithm_t kdf_alg,
5240 uint8_t *output,
5241 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005242{
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5244 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005245 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005246 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005247#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005249#else
5250 const uint8_t last_block = 0xff;
5251#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005252
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 if (hkdf->state < HKDF_STATE_KEYED ||
5254 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005255#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005257#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 )) {
5259 return PSA_ERROR_BAD_STATE;
5260 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005261 hkdf->state = HKDF_STATE_OUTPUT;
5262
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005264 /* Copy what remains of the current block */
5265 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005267 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 }
5269 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005270 output += n;
5271 output_length -= n;
5272 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005274 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005276 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005277 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005278 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005279 * object was corrupted or if this function is called directly
5280 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 if (hkdf->block_number == last_block) {
5282 return PSA_ERROR_BAD_STATE;
5283 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005284
5285 /* We need a new block */
5286 ++hkdf->block_number;
5287 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005288
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5290 hash_alg,
5291 hkdf->prk,
5292 hash_length);
5293 if (status != PSA_SUCCESS) {
5294 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005295 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005296
5297 if (hkdf->block_number != 1) {
5298 status = psa_mac_update(&hkdf->hmac,
5299 hkdf->output_block,
5300 hash_length);
5301 if (status != PSA_SUCCESS) {
5302 return status;
5303 }
5304 }
5305 status = psa_mac_update(&hkdf->hmac,
5306 hkdf->info,
5307 hkdf->info_length);
5308 if (status != PSA_SUCCESS) {
5309 return status;
5310 }
5311 status = psa_mac_update(&hkdf->hmac,
5312 &hkdf->block_number, 1);
5313 if (status != PSA_SUCCESS) {
5314 return status;
5315 }
5316 status = psa_mac_sign_finish(&hkdf->hmac,
5317 hkdf->output_block,
5318 sizeof(hkdf->output_block),
5319 &hmac_output_length);
5320 if (status != PSA_SUCCESS) {
5321 return status;
5322 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005323 }
5324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005326}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005327#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005328
John Durkop07cc04a2020-11-16 22:08:34 -08005329#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5330 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005331static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5332 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005334{
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5336 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005337 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5338 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005339 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005340
Janos Follath7742fee2019-06-17 12:58:10 +01005341 /* We can't be wanting more output after block 0xff, otherwise
5342 * the capacity check in psa_key_derivation_output_bytes() would have
5343 * prevented this call. It could happen only if the operation
5344 * object was corrupted or if this function is called directly
5345 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 if (tls12_prf->block_number == 0xff) {
5347 return PSA_ERROR_CORRUPTION_DETECTED;
5348 }
Janos Follath7742fee2019-06-17 12:58:10 +01005349
5350 /* We need a new block */
5351 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005352 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005353
5354 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5355 *
5356 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5357 *
5358 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5359 * HMAC_hash(secret, A(2) + seed) +
5360 * HMAC_hash(secret, A(3) + seed) + ...
5361 *
5362 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005363 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005364 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005365 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005366 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005367 * is currently extracted as `output_block` and where i is
5368 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005369 */
5370
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 status = psa_key_derivation_start_hmac(&hmac,
5372 hash_alg,
5373 tls12_prf->secret,
5374 tls12_prf->secret_length);
5375 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005376 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005378
5379 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005381 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5382 * the variable seed and in this instance means it in the context of the
5383 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 status = psa_mac_update(&hmac,
5385 tls12_prf->label,
5386 tls12_prf->label_length);
5387 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005388 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 }
5390 status = psa_mac_update(&hmac,
5391 tls12_prf->seed,
5392 tls12_prf->seed_length);
5393 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005394 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 }
5396 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005397 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5399 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005400 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005402 }
5403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 status = psa_mac_sign_finish(&hmac,
5405 tls12_prf->Ai, hash_length,
5406 &hmac_output_length);
5407 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005408 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 }
5410 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005411 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005413
5414 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 status = psa_key_derivation_start_hmac(&hmac,
5416 hash_alg,
5417 tls12_prf->secret,
5418 tls12_prf->secret_length);
5419 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005420 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 }
5422 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5423 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005424 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 }
5426 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5427 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005428 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 }
5430 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5431 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005432 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 }
5434 status = psa_mac_sign_finish(&hmac,
5435 tls12_prf->output_block, hash_length,
5436 &hmac_output_length);
5437 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005438 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005440
Janos Follath7742fee2019-06-17 12:58:10 +01005441
5442cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 cleanup_status = psa_mac_abort(&hmac);
5444 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005445 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005449}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005450
Janos Follath844eb0e2019-06-19 12:10:49 +01005451static psa_status_t psa_key_derivation_tls12_prf_read(
5452 psa_tls12_prf_key_derivation_t *tls12_prf,
5453 psa_algorithm_t alg,
5454 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005456{
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5458 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005459 psa_status_t status;
5460 uint8_t offset, length;
5461
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005463 case PSA_TLS12_PRF_STATE_LABEL_SET:
5464 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5465 break;
5466 case PSA_TLS12_PRF_STATE_OUTPUT:
5467 break;
5468 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005470 }
5471
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005473 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 if (tls12_prf->left_in_block == 0) {
5475 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5476 alg);
5477 if (status != PSA_SUCCESS) {
5478 return status;
5479 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005480
5481 continue;
5482 }
5483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005485 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005487 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005489
5490 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005492 output += length;
5493 output_length -= length;
5494 tls12_prf->left_in_block -= length;
5495 }
5496
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005498}
John Durkop07cc04a2020-11-16 22:08:34 -08005499#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5500 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005501
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005502#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5503static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5504 psa_tls12_ecjpake_to_pms_t *ecjpake,
5505 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005507{
5508 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005509 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005510
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 if (output_length != 32) {
5512 return PSA_ERROR_INVALID_ARGUMENT;
5513 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5516 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5517 &output_size);
5518 if (status != PSA_SUCCESS) {
5519 return status;
5520 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005521
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 if (output_size != output_length) {
5523 return PSA_ERROR_GENERIC_ERROR;
5524 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005525
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005527}
5528#endif
5529
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305530#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5531static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5532 psa_pbkdf2_key_derivation_t *pbkdf2,
5533 psa_algorithm_t prf_alg,
5534 uint8_t prf_output_length,
5535 psa_key_attributes_t *attributes)
5536{
5537 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305538 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305539 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305540 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305541 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305542 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305543 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305544
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305545 mac_operation.is_sign = 1;
5546 mac_operation.mac_size = prf_output_length;
5547 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305548
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305549 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5550 attributes,
5551 pbkdf2->password,
5552 pbkdf2->password_length,
5553 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305554 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305555 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305556 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305557 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5558 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305559 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305560 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305561 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305562 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305563 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305564 }
5565 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5566 &mac_output_length);
5567 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305568 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305569 }
5570
5571 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305572 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305573 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305574 }
5575
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305576 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305577
5578 for (i = 1; i < pbkdf2->input_cost; i++) {
5579 status = psa_driver_wrapper_mac_compute(attributes,
5580 pbkdf2->password,
5581 pbkdf2->password_length,
5582 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305583 U_i, sizeof(U_i),
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305584 &mac_output_length);
5585 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305586 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305587 }
5588
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305589 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305590 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305591
5592cleanup:
5593 /* Zeroise buffers to clear sensitive data from memory. */
5594 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5595 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305596}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305597
5598static psa_status_t psa_key_derivation_pbkdf2_read(
5599 psa_pbkdf2_key_derivation_t *pbkdf2,
5600 psa_algorithm_t kdf_alg,
5601 uint8_t *output,
5602 size_t output_length)
5603{
5604 psa_status_t status;
5605 psa_algorithm_t prf_alg;
5606 uint8_t prf_output_length;
5607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5608 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5609 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5610
5611 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5612 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5613 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5614 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305615 } else {
5616 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305617 }
5618
5619 switch (pbkdf2->state) {
5620 case PSA_PBKDF2_STATE_PASSWORD_SET:
5621 /* Initially we need a new block so bytes_used is equal to block size*/
5622 pbkdf2->bytes_used = prf_output_length;
5623 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5624 break;
5625 case PSA_PBKDF2_STATE_OUTPUT:
5626 break;
5627 default:
5628 return PSA_ERROR_BAD_STATE;
5629 }
5630
5631 while (output_length != 0) {
5632 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5633 if (n > output_length) {
5634 n = (uint8_t) output_length;
5635 }
5636 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5637 output += n;
5638 output_length -= n;
5639 pbkdf2->bytes_used += n;
5640
5641 if (output_length == 0) {
5642 break;
5643 }
5644
5645 /* We need a new block */
5646 pbkdf2->bytes_used = 0;
5647 pbkdf2->block_number++;
5648
5649 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5650 prf_output_length,
5651 &attributes);
5652 if (status != PSA_SUCCESS) {
5653 return status;
5654 }
5655 }
5656
5657 return PSA_SUCCESS;
5658}
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305659#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
5660
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005661psa_status_t psa_key_derivation_output_bytes(
5662 psa_key_derivation_operation_t *operation,
5663 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005665{
5666 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005668
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005670 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005672 }
5673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005675 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005676 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005677 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005678 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005679 goto exit;
5680 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005683 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005684 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5685 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005687 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005689 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005690 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005691
Przemek Stekiel69c46792022-06-10 12:59:51 +02005692#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5694 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5695 output, output_length);
5696 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005697#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005698#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5699 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5701 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5702 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5703 kdf_alg, output,
5704 output_length);
5705 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005706#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5707 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005708#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005710 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5712 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005713#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305714#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5715 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305716 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5717 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305718 } else
5719#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005720
Gilles Peskineeab56e42018-07-12 17:12:33 +02005721 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005722 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005724 }
5725
5726exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005728 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005729 * This allows us to differentiate between exhausted operations and
5730 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5731 * operations. */
5732 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005734 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005736 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005738}
5739
David Brown7807bf72021-02-09 16:28:23 -07005740#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005741static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005742{
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 if (data_size >= 8) {
5744 mbedtls_des_key_set_parity(data);
5745 }
5746 if (data_size >= 16) {
5747 mbedtls_des_key_set_parity(data + 8);
5748 }
5749 if (data_size >= 24) {
5750 mbedtls_des_key_set_parity(data + 16);
5751 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005752}
David Brown7807bf72021-02-09 16:28:23 -07005753#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005754
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005755/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 * ECC keys on a Weierstrass elliptic curve require the generation
5757 * of a private key which is an integer
5758 * in the range [1, N - 1], where N is the boundary of the private key domain:
5759 * N is the prime p for Diffie-Hellman, or the order of the
5760 * curve’s base point for ECC.
5761 *
5762 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5763 * This function generates the private key using the following process:
5764 *
5765 * 1. Draw a byte string of length ceiling(m/8) bytes.
5766 * 2. If m is not a multiple of 8, set the most significant
5767 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5768 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5769 * 4. If k > N - 2, discard the result and return to step 1.
5770 * 5. Output k + 1 as the private key.
5771 *
5772 * This method allows compliance to NIST standards, specifically the methods titled
5773 * Key-Pair Generation by Testing Candidates in the following publications:
5774 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5775 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5776 * Diffie-Hellman keys.
5777 *
5778 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5779 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5780 *
5781 * Note: Function allocates memory for *data buffer, so given *data should be
5782 * always NULL.
5783 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005784#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5785#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005786static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005787 psa_key_slot_t *slot,
5788 size_t bits,
5789 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005790 uint8_t **data
5791 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005792{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005793 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005794 mbedtls_mpi k;
5795 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005796 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005797 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 mbedtls_mpi_init(&k);
5800 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005801
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005802 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005804 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005808 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005809 goto cleanup;
5810 }
5811
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005812 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005816
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005817 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005818 /* Let m be the bit size of N. */
5819 size_t m = ecp_group.nbits;
5820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005822
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005823 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005825
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005826 /* Note: This function is always called with *data == NULL and it
5827 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 *data = mbedtls_calloc(1, m_bytes);
5829 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005830 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005831 goto cleanup;
5832 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005835 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005837 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005839
5840 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005842 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005843 * (8 * ceiling(m/8) - m) bits of the first byte in
5844 * the string to zero.
5845 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005847 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005848 }
5849
5850 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 * big-endian byte string.
5852 */
5853 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005854
5855 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 * Result of comparison is returned. When it indicates error
5857 * then this function is called again.
5858 */
5859 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005860 }
5861
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005862 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5864 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005865cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 if (ret != 0) {
5867 status = mbedtls_to_psa_error(ret);
5868 }
5869 if (status != PSA_SUCCESS) {
5870 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005871 *data = NULL;
5872 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 mbedtls_mpi_free(&k);
5874 mbedtls_mpi_free(&diff_N_2);
5875 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005876}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005877
5878/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5879 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5880 *
5881 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5882 * draw a 32-byte string and process it as specified in
5883 * Elliptic Curves for Security [RFC7748] §5.
5884 *
5885 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5886 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5887 *
5888 * Note: Function allocates memory for *data buffer, so given *data should be
5889 * always NULL.
5890 */
5891
5892static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5893 size_t bits,
5894 psa_key_derivation_operation_t *operation,
5895 uint8_t **data
5896 )
5897{
5898 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005900
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005902 case 255:
5903 output_length = 32;
5904 break;
5905 case 448:
5906 output_length = 56;
5907 break;
5908 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005910 break;
5911 }
5912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005914
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 if (*data == NULL) {
5916 return PSA_ERROR_INSUFFICIENT_MEMORY;
5917 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005918
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005920
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005922 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005924
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005926 case 255:
5927 (*data)[0] &= 248;
5928 (*data)[31] &= 127;
5929 (*data)[31] |= 64;
5930 break;
5931 case 448:
5932 (*data)[0] &= 252;
5933 (*data)[55] |= 128;
5934 break;
5935 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005937 break;
5938 }
5939
5940 return status;
5941}
Valerio Setti86587ab2023-06-27 13:09:30 +02005942#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5943static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5944 psa_key_slot_t *slot, size_t bits,
5945 psa_key_derivation_operation_t *operation, uint8_t **data)
5946{
5947 (void) slot;
5948 (void) bits;
5949 (void) operation;
5950 (void) data;
5951 return PSA_ERROR_NOT_SUPPORTED;
5952}
5953
5954static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5955 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5956{
5957 (void) bits;
5958 (void) operation;
5959 (void) data;
5960 return PSA_ERROR_NOT_SUPPORTED;
5961}
5962#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5963#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005964
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005965static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005966 psa_key_slot_t *slot,
5967 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005969{
5970 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305972 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5976 return PSA_ERROR_INVALID_ARGUMENT;
5977 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005978
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005979#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005980 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5982 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5983 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005984 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5986 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005987 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 }
5989 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005990 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5992 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005993 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005995 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005996 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005997#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005998 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 if (key_type_is_raw_bytes(slot->attr.type)) {
6000 if (bits % 8 != 0) {
6001 return PSA_ERROR_INVALID_ARGUMENT;
6002 }
6003 data = mbedtls_calloc(1, bytes);
6004 if (data == NULL) {
6005 return PSA_ERROR_INSUFFICIENT_MEMORY;
6006 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 status = psa_key_derivation_output_bytes(operation, data, bytes);
6009 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006010 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 }
David Brown12ca5032021-02-11 11:02:00 -07006012#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6014 psa_des_set_key_parity(data, bytes);
6015 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006016#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 } else {
6018 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006019 }
Ronald Crondd04d422020-11-28 15:14:42 +01006020
Ronald Cronbf33c932020-11-28 18:06:53 +01006021 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006022 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006024 };
6025
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6027 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6028 &storage_size);
6029 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306030 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 }
Archanad8a83dc2021-06-14 10:04:16 +05306032 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 status = psa_allocate_buffer_to_slot(slot, storage_size);
6034 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306035 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 }
Archanad8a83dc2021-06-14 10:04:16 +05306037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 status = psa_driver_wrapper_import_key(&attributes,
6039 data, bytes,
6040 slot->key.data,
6041 slot->key.bytes,
6042 &slot->key.bytes, &bits);
6043 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006044 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006046
6047exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 mbedtls_free(data);
6049 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006050}
6051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6053 psa_key_derivation_operation_t *operation,
6054 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006055{
6056 psa_status_t status;
6057 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006058 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006059
Ronald Cron81709fc2020-11-14 12:10:32 +01006060 *key = MBEDTLS_SVC_KEY_ID_INIT;
6061
Gilles Peskine0f84d622019-09-12 19:03:13 +02006062 /* Reject any attempt to create a zero-length key so that we don't
6063 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 if (psa_get_key_bits(attributes) == 0) {
6065 return PSA_ERROR_INVALID_ARGUMENT;
6066 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006067
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 if (operation->alg == PSA_ALG_NONE) {
6069 return PSA_ERROR_BAD_STATE;
6070 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 if (!operation->can_output_key) {
6073 return PSA_ERROR_NOT_PERMITTED;
6074 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6077 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006078#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006080 /* Deriving a key in a secure element is not implemented yet. */
6081 status = PSA_ERROR_NOT_SUPPORTED;
6082 }
6083#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 if (status == PSA_SUCCESS) {
6085 status = psa_generate_derived_key_internal(slot,
6086 attributes->core.bits,
6087 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006088 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 if (status == PSA_SUCCESS) {
6090 status = psa_finish_key_creation(slot, driver, key);
6091 }
6092 if (status != PSA_SUCCESS) {
6093 psa_fail_key_creation(slot, driver);
6094 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006097}
6098
Gilles Peskineeab56e42018-07-12 17:12:33 +02006099
6100
6101/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006102/* Key derivation */
6103/****************************************************************/
6104
Steven Cooreman932ffb72021-02-15 12:14:32 +01006105#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006106static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006107{
6108#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6110 return 1;
6111 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006112#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006113#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6115 return 1;
6116 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006117#endif
6118#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6120 return 1;
6121 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006122#endif
6123#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6125 return 1;
6126 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006127#endif
6128#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6130 return 1;
6131 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006132#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006133#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6135 return 1;
6136 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006137#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306138#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6139 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6140 return 1;
6141 }
6142#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006144}
6145
Gilles Peskine449bd832023-01-11 14:50:10 +01006146static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006147{
6148 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 psa_status_t status = psa_hash_setup(&operation, alg);
6150 psa_hash_abort(&operation);
6151 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006152}
6153
Gilles Peskine969c5d62019-01-16 15:53:06 +01006154static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006155 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006157{
Janos Follath5fe19732019-06-20 15:09:30 +01006158 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6159 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006161
Gilles Peskine969c5d62019-01-16 15:53:06 +01006162 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 if (!is_kdf_alg_supported(kdf_alg)) {
6164 return PSA_ERROR_NOT_SUPPORTED;
6165 }
John Durkop07cc04a2020-11-16 22:08:34 -08006166
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006167 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006168 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6170 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6171 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6172 if (hash_size == 0) {
6173 return PSA_ERROR_NOT_SUPPORTED;
6174 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006175
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006176 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6177 * we might fail later, which is somewhat unfriendly and potentially
6178 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 psa_status_t status = psa_hash_try_support(hash_alg);
6180 if (status != PSA_SUCCESS) {
6181 return status;
6182 }
6183 } else {
6184 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006185 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006186
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6188 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6189 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6190 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006191 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006192#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006193 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6195 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006196 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006198#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6199 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 operation->capacity = 255 * hash_size;
6201 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006202}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006205{
6206#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 if (alg == PSA_ALG_ECDH) {
6208 return PSA_SUCCESS;
6209 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006210#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006211#if defined(PSA_WANT_ALG_FFDH)
6212 if (alg == PSA_ALG_FFDH) {
6213 return PSA_SUCCESS;
6214 }
6215#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006216 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006218}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006219
6220static int psa_key_derivation_allows_free_form_secret_input(
6221 psa_algorithm_t kdf_alg)
6222{
6223#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6224 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6225 return 0;
6226 }
6227#endif
6228 (void) kdf_alg;
6229 return 1;
6230}
John Durkop07cc04a2020-11-16 22:08:34 -08006231#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006232
Gilles Peskine449bd832023-01-11 14:50:10 +01006233psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6234 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006235{
6236 psa_status_t status;
6237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 if (operation->alg != 0) {
6239 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006240 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6243 return PSA_ERROR_INVALID_ARGUMENT;
6244 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6245#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6246 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6247 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6248 status = psa_key_agreement_try_support(ka_alg);
6249 if (status != PSA_SUCCESS) {
6250 return status;
6251 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006252 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6253 return PSA_ERROR_INVALID_ARGUMENT;
6254 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6256#else
6257 return PSA_ERROR_NOT_SUPPORTED;
6258#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6259 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6260#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6261 status = psa_key_derivation_setup_kdf(operation, alg);
6262#else
6263 return PSA_ERROR_NOT_SUPPORTED;
6264#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6265 } else {
6266 return PSA_ERROR_INVALID_ARGUMENT;
6267 }
6268
6269 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006270 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 }
6272 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006273}
6274
Przemek Stekiel69c46792022-06-10 12:59:51 +02006275#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006276static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6277 psa_algorithm_t kdf_alg,
6278 psa_key_derivation_step_t step,
6279 const uint8_t *data,
6280 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006281{
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006283 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006285 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006286#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6288 return PSA_ERROR_INVALID_ARGUMENT;
6289 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006290#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 if (hkdf->state != HKDF_STATE_INIT) {
6292 return PSA_ERROR_BAD_STATE;
6293 } else {
6294 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6295 hash_alg,
6296 data, data_length);
6297 if (status != PSA_SUCCESS) {
6298 return status;
6299 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006300 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006302 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006303 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006304#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006306 /* We shouldn't be in different state as HKDF_EXPAND only allows
6307 * two inputs: SECRET (this case) and INFO which does not modify
6308 * the state. It could happen only if the hkdf
6309 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 if (hkdf->state != HKDF_STATE_INIT) {
6311 return PSA_ERROR_BAD_STATE;
6312 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006313
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006314 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6316 return PSA_ERROR_INVALID_ARGUMENT;
6317 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 memcpy(hkdf->prk, data, data_length);
6320 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006321#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006322 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006323 /* HKDF: If no salt was provided, use an empty salt.
6324 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006326#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6328 return PSA_ERROR_BAD_STATE;
6329 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006330#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6332 hash_alg,
6333 NULL, 0);
6334 if (status != PSA_SUCCESS) {
6335 return status;
6336 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006337 hkdf->state = HKDF_STATE_STARTED;
6338 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 if (hkdf->state != HKDF_STATE_STARTED) {
6340 return PSA_ERROR_BAD_STATE;
6341 }
6342 status = psa_mac_update(&hkdf->hmac,
6343 data, data_length);
6344 if (status != PSA_SUCCESS) {
6345 return status;
6346 }
6347 status = psa_mac_sign_finish(&hkdf->hmac,
6348 hkdf->prk,
6349 sizeof(hkdf->prk),
6350 &data_length);
6351 if (status != PSA_SUCCESS) {
6352 return status;
6353 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006354 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006355
Gilles Peskine2b522db2019-04-12 15:11:49 +02006356 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006357 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006358#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006360 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006362 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006364#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006365 {
6366 /* Block 0 is empty, and the next block will be
6367 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006369 }
6370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006372 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006373#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6375 return PSA_ERROR_INVALID_ARGUMENT;
6376 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006377#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006378#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6380 hkdf->state == HKDF_STATE_INIT) {
6381 return PSA_ERROR_BAD_STATE;
6382 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006383#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 if (hkdf->state == HKDF_STATE_OUTPUT) {
6385 return PSA_ERROR_BAD_STATE;
6386 }
6387 if (hkdf->info_set) {
6388 return PSA_ERROR_BAD_STATE;
6389 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006390 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 if (data_length != 0) {
6392 hkdf->info = mbedtls_calloc(1, data_length);
6393 if (hkdf->info == NULL) {
6394 return PSA_ERROR_INSUFFICIENT_MEMORY;
6395 }
6396 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006397 }
6398 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006400 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006402 }
6403}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006404#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006405
John Durkop07cc04a2020-11-16 22:08:34 -08006406#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6407 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006408static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6409 const uint8_t *data,
6410 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006411{
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6413 return PSA_ERROR_BAD_STATE;
6414 }
Janos Follathf08e2652019-06-13 09:05:41 +01006415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 if (data_length != 0) {
6417 prf->seed = mbedtls_calloc(1, data_length);
6418 if (prf->seed == NULL) {
6419 return PSA_ERROR_INSUFFICIENT_MEMORY;
6420 }
Janos Follathf08e2652019-06-13 09:05:41 +01006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006423 prf->seed_length = data_length;
6424 }
Janos Follathf08e2652019-06-13 09:05:41 +01006425
Gilles Peskine43f958b2020-12-13 14:55:14 +01006426 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006427
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006429}
6430
Gilles Peskine449bd832023-01-11 14:50:10 +01006431static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6432 const uint8_t *data,
6433 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006434{
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6436 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6437 return PSA_ERROR_BAD_STATE;
6438 }
Janos Follath81550542019-06-13 14:26:34 +01006439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 if (data_length != 0) {
6441 prf->secret = mbedtls_calloc(1, data_length);
6442 if (prf->secret == NULL) {
6443 return PSA_ERROR_INSUFFICIENT_MEMORY;
6444 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006445
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006447 prf->secret_length = data_length;
6448 }
Janos Follath81550542019-06-13 14:26:34 +01006449
Gilles Peskine43f958b2020-12-13 14:55:14 +01006450 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006451
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006453}
6454
Gilles Peskine449bd832023-01-11 14:50:10 +01006455static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6456 const uint8_t *data,
6457 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006458{
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6460 return PSA_ERROR_BAD_STATE;
6461 }
Janos Follath63028dd2019-06-13 09:15:47 +01006462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 if (data_length != 0) {
6464 prf->label = mbedtls_calloc(1, data_length);
6465 if (prf->label == NULL) {
6466 return PSA_ERROR_INSUFFICIENT_MEMORY;
6467 }
Janos Follath63028dd2019-06-13 09:15:47 +01006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006470 prf->label_length = data_length;
6471 }
Janos Follath63028dd2019-06-13 09:15:47 +01006472
Gilles Peskine43f958b2020-12-13 14:55:14 +01006473 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006474
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006476}
6477
Gilles Peskine449bd832023-01-11 14:50:10 +01006478static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6479 psa_key_derivation_step_t step,
6480 const uint8_t *data,
6481 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006482{
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006484 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006486 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006488 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006490 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006492 }
6493}
John Durkop07cc04a2020-11-16 22:08:34 -08006494#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6495 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6496
6497#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6498static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6499 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006500 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006502{
6503 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6505 4 + data_length + prf->other_secret_length :
6506 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6509 return PSA_ERROR_INVALID_ARGUMENT;
6510 }
John Durkop07cc04a2020-11-16 22:08:34 -08006511
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 uint8_t *pms = mbedtls_calloc(1, pms_len);
6513 if (pms == NULL) {
6514 return PSA_ERROR_INSUFFICIENT_MEMORY;
6515 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006516 uint8_t *cur = pms;
6517
6518 /* pure-PSK:
6519 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006520 *
6521 * The premaster secret is formed as follows: if the PSK is N octets
6522 * long, concatenate a uint16 with the value N, N zero octets, a second
6523 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006524 *
6525 * mixed-PSK:
6526 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6527 * follows: concatenate a uint16 with the length of the other secret,
6528 * the other secret itself, uint16 with the length of PSK, and the
6529 * PSK itself.
6530 * For details please check:
6531 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6532 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6533 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006534 */
6535
Gilles Peskine449bd832023-01-11 14:50:10 +01006536 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6537 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6538 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6539 if (prf->other_secret_length != 0) {
6540 memcpy(cur, prf->other_secret, prf->other_secret_length);
6541 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006542 cur += prf->other_secret_length;
6543 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 } else {
6545 *cur++ = MBEDTLS_BYTE_1(data_length);
6546 *cur++ = MBEDTLS_BYTE_0(data_length);
6547 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006548 cur += data_length;
6549 }
6550
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 *cur++ = MBEDTLS_BYTE_1(data_length);
6552 *cur++ = MBEDTLS_BYTE_0(data_length);
6553 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006554 cur += data_length;
6555
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006557
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 mbedtls_platform_zeroize(pms, pms_len);
6559 mbedtls_free(pms);
6560 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006561}
Janos Follath6660f0e2019-06-17 08:44:03 +01006562
Przemek Stekiele47201b2022-04-19 13:53:28 +02006563static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6564 psa_tls12_prf_key_derivation_t *prf,
6565 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006567{
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6569 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006570 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006571
6572 if (data_length != 0) {
6573 prf->other_secret = mbedtls_calloc(1, data_length);
6574 if (prf->other_secret == NULL) {
6575 return PSA_ERROR_INSUFFICIENT_MEMORY;
6576 }
6577
6578 memcpy(prf->other_secret, data, data_length);
6579 prf->other_secret_length = data_length;
6580 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006581 prf->other_secret_length = 0;
6582 }
6583
6584 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6585
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006587}
6588
Janos Follath6660f0e2019-06-17 08:44:03 +01006589static psa_status_t psa_tls12_prf_psk_to_ms_input(
6590 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006591 psa_key_derivation_step_t step,
6592 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006594{
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006596 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 return psa_tls12_prf_psk_to_ms_set_key(prf,
6598 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006599 break;
6600 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6602 data,
6603 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006604 break;
6605 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006607 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006608
Przemek Stekiele47201b2022-04-19 13:53:28 +02006609 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006610}
John Durkop07cc04a2020-11-16 22:08:34 -08006611#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006612
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006613#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6614static psa_status_t psa_tls12_ecjpake_to_pms_input(
6615 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006616 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006617 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006619{
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6621 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6622 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006623 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006624
6625 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006626 if (data[0] != 0x04) {
6627 return PSA_ERROR_INVALID_ARGUMENT;
6628 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006629
6630 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006632
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006634}
6635#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306636
6637#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6638static psa_status_t psa_pbkdf2_set_input_cost(
6639 psa_pbkdf2_key_derivation_t *pbkdf2,
6640 psa_key_derivation_step_t step,
6641 uint64_t data)
6642{
6643 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6644 return PSA_ERROR_INVALID_ARGUMENT;
6645 }
6646
6647 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6648 return PSA_ERROR_BAD_STATE;
6649 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306650
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306651 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306652 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306653 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306654
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306655 if (data == 0) {
6656 return PSA_ERROR_INVALID_ARGUMENT;
6657 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306658
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306659 pbkdf2->input_cost = data;
6660 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6661
6662 return PSA_SUCCESS;
6663}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306664
6665static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6666 const uint8_t *data,
6667 size_t data_length)
6668{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306669 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6670 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6671 return PSA_ERROR_BAD_STATE;
6672 }
6673
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306674 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6675 pbkdf2->salt = mbedtls_calloc(1, data_length);
6676 if (pbkdf2->salt == NULL) {
6677 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306678 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306679
6680 memcpy(pbkdf2->salt, data, data_length);
6681 pbkdf2->salt_length = data_length;
6682 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306683 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306684
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306685 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6686 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306687 return PSA_ERROR_INSUFFICIENT_MEMORY;
6688 }
6689
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306690 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6691 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306692 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306693 mbedtls_free(pbkdf2->salt);
6694 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306695 }
6696
6697 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6698
6699 return PSA_SUCCESS;
6700}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306701
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306702static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306703 const uint8_t *input,
6704 size_t input_len,
6705 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306706 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306707{
6708 psa_status_t status = PSA_SUCCESS;
6709 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6710 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306711 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306712 } else {
6713 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306714 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306715 }
6716 return status;
6717}
6718
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306719static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306720 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306721 const uint8_t *data,
6722 size_t data_length)
6723{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306724 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306725 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6726 return PSA_ERROR_BAD_STATE;
6727 }
6728
6729 if (data_length != 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306730 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6731 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6732 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6733 pbkdf2->password,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306734 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306735 }
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306736 }
6737
6738 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6739
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306740 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306741}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306742
6743static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306744 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306745 psa_key_derivation_step_t step,
6746 const uint8_t *data,
6747 size_t data_length)
6748{
6749 switch (step) {
6750 case PSA_KEY_DERIVATION_INPUT_SALT:
6751 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6752 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306753 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306754 default:
6755 return PSA_ERROR_INVALID_ARGUMENT;
6756 }
6757}
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306758#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6759
Gilles Peskineb8965192019-09-24 16:21:10 +02006760/** Check whether the given key type is acceptable for the given
6761 * input step of a key derivation.
6762 *
6763 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6764 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6765 * Both secret and non-secret inputs can alternatively have the type
6766 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6767 * that the input was passed as a buffer rather than via a key object.
6768 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006769static int psa_key_derivation_check_input_type(
6770 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006772{
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006774 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 if (key_type == PSA_KEY_TYPE_DERIVE) {
6776 return PSA_SUCCESS;
6777 }
6778 if (key_type == PSA_KEY_TYPE_NONE) {
6779 return PSA_SUCCESS;
6780 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006781 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006782 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 if (key_type == PSA_KEY_TYPE_DERIVE) {
6784 return PSA_SUCCESS;
6785 }
6786 if (key_type == PSA_KEY_TYPE_NONE) {
6787 return PSA_SUCCESS;
6788 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006789 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006790 case PSA_KEY_DERIVATION_INPUT_LABEL:
6791 case PSA_KEY_DERIVATION_INPUT_SALT:
6792 case PSA_KEY_DERIVATION_INPUT_INFO:
6793 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6795 return PSA_SUCCESS;
6796 }
6797 if (key_type == PSA_KEY_TYPE_NONE) {
6798 return PSA_SUCCESS;
6799 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006800 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306801 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6802 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6803 return PSA_SUCCESS;
6804 }
6805 if (key_type == PSA_KEY_TYPE_DERIVE) {
6806 return PSA_SUCCESS;
6807 }
6808 if (key_type == PSA_KEY_TYPE_NONE) {
6809 return PSA_SUCCESS;
6810 }
6811 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006812 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006814}
6815
Janos Follathb80a94e2019-06-12 15:54:46 +01006816static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006817 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006818 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006819 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006820 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006822{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006823 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006825
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 status = psa_key_derivation_check_input_type(step, key_type);
6827 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006828 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006830
Przemek Stekiel69c46792022-06-10 12:59:51 +02006831#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6833 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6834 step, data, data_length);
6835 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006836#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006837#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6839 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6840 step, data, data_length);
6841 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006842#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6843#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6845 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6846 step, data, data_length);
6847 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006848#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006849#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006850 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006851 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6853 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006854#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306855#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6856 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306857 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6858 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306859 } else
6860#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006861 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006862 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006863 (void) data;
6864 (void) data_length;
6865 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006867 }
6868
Gilles Peskine224b0d62019-09-23 18:13:17 +02006869exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 if (status != PSA_SUCCESS) {
6871 psa_key_derivation_abort(operation);
6872 }
6873 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006874}
6875
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306876static psa_status_t psa_key_derivation_input_integer_internal(
6877 psa_key_derivation_operation_t *operation,
6878 psa_key_derivation_step_t step,
6879 uint64_t value)
6880{
6881 psa_status_t status;
6882 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6883
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306884#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6885 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6886 status = psa_pbkdf2_set_input_cost(
6887 &operation->ctx.pbkdf2, step, value);
6888 } else
6889#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306890 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306891 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306892 (void) value;
6893 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306894 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306895 }
6896
6897 if (status != PSA_SUCCESS) {
6898 psa_key_derivation_abort(operation);
6899 }
6900 return status;
6901}
6902
Janos Follath51f4a0f2019-06-14 11:35:55 +01006903psa_status_t psa_key_derivation_input_bytes(
6904 psa_key_derivation_operation_t *operation,
6905 psa_key_derivation_step_t step,
6906 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006908{
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 return psa_key_derivation_input_internal(operation, step,
6910 PSA_KEY_TYPE_NONE,
6911 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006912}
6913
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306914psa_status_t psa_key_derivation_input_integer(
6915 psa_key_derivation_operation_t *operation,
6916 psa_key_derivation_step_t step,
6917 uint64_t value)
6918{
6919 return psa_key_derivation_input_integer_internal(operation, step, value);
6920}
6921
Janos Follath51f4a0f2019-06-14 11:35:55 +01006922psa_status_t psa_key_derivation_input_key(
6923 psa_key_derivation_operation_t *operation,
6924 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006926{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006927 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006928 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006929 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006930
Ronald Cron5c522922020-11-14 16:35:34 +01006931 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6933 if (status != PSA_SUCCESS) {
6934 psa_key_derivation_abort(operation);
6935 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006936 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006937
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306938 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6939 * permission to output to a key object. */
6940 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6941 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006942 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 status = psa_key_derivation_input_internal(operation,
6946 step, slot->attr.type,
6947 slot->key.data,
6948 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006949
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006951
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006953}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006954
6955
6956
6957/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006958/* Key agreement */
6959/****************************************************************/
6960
Gilles Peskine449bd832023-01-11 14:50:10 +01006961psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6962 const uint8_t *key_buffer,
6963 size_t key_buffer_size,
6964 psa_algorithm_t alg,
6965 const uint8_t *peer_key,
6966 size_t peer_key_length,
6967 uint8_t *shared_secret,
6968 size_t shared_secret_size,
6969 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006970{
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006972#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006973 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006974 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6975 key_buffer_size, alg,
6976 peer_key, peer_key_length,
6977 shared_secret,
6978 shared_secret_size,
6979 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006980#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006981
6982#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6983 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006984 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006985 peer_key,
6986 peer_key_length,
6987 key_buffer,
6988 key_buffer_size,
6989 shared_secret,
6990 shared_secret_size,
6991 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006992#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6993
Gilles Peskine01d718c2018-09-18 12:01:02 +02006994 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006995 (void) attributes;
6996 (void) key_buffer;
6997 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006998 (void) peer_key;
6999 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007000 (void) shared_secret;
7001 (void) shared_secret_size;
7002 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007004 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007005}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007006
Aditya Deshpande17845b82022-10-13 17:21:01 +01007007/** Internal function for raw key agreement
7008 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007009 * to the driver's implementation if a driver is present.
7010 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007011 * (psa_key_agreement_raw_builtin).
7012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007013static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7014 psa_key_slot_t *private_key,
7015 const uint8_t *peer_key,
7016 size_t peer_key_length,
7017 uint8_t *shared_secret,
7018 size_t shared_secret_size,
7019 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007020{
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7022 return PSA_ERROR_NOT_SUPPORTED;
7023 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007024
7025 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007027 };
7028
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 return psa_driver_wrapper_key_agreement(&attributes,
7030 private_key->key.data,
7031 private_key->key.bytes, alg,
7032 peer_key, peer_key_length,
7033 shared_secret,
7034 shared_secret_size,
7035 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007036}
7037
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007038/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007039 * to potentially free embedded data structures and wipe confidential data.
7040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007041static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7042 psa_key_derivation_step_t step,
7043 psa_key_slot_t *private_key,
7044 const uint8_t *peer_key,
7045 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007046{
7047 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007048 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007049 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007051
7052 /* Step 1: run the secret agreement algorithm to generate the shared
7053 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 status = psa_key_agreement_raw_internal(ka_alg,
7055 private_key,
7056 peer_key, peer_key_length,
7057 shared_secret,
7058 sizeof(shared_secret),
7059 &shared_secret_length);
7060 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007061 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007063
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007064 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007065 * the shared secret. A shared secret is permitted wherever a key
7066 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 status = psa_key_derivation_input_internal(operation, step,
7068 PSA_KEY_TYPE_DERIVE,
7069 shared_secret,
7070 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007071exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7073 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007074}
7075
Gilles Peskine449bd832023-01-11 14:50:10 +01007076psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7077 psa_key_derivation_step_t step,
7078 mbedtls_svc_key_id_t private_key,
7079 const uint8_t *peer_key,
7080 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007081{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007082 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007083 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007084 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007085
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7087 return PSA_ERROR_INVALID_ARGUMENT;
7088 }
Ronald Cron5c522922020-11-14 16:35:34 +01007089 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7091 if (status != PSA_SUCCESS) {
7092 return status;
7093 }
7094 status = psa_key_agreement_internal(operation, step,
7095 slot,
7096 peer_key, peer_key_length);
7097 if (status != PSA_SUCCESS) {
7098 psa_key_derivation_abort(operation);
7099 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007100 /* If a private key has been added as SECRET, we allow the derived
7101 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007103 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007105 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007106
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007108
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007110}
7111
Gilles Peskine449bd832023-01-11 14:50:10 +01007112psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7113 mbedtls_svc_key_id_t private_key,
7114 const uint8_t *peer_key,
7115 size_t peer_key_length,
7116 uint8_t *output,
7117 size_t output_size,
7118 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007119{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007120 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007121 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007122 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007123
Gilles Peskine449bd832023-01-11 14:50:10 +01007124 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007125 status = PSA_ERROR_INVALID_ARGUMENT;
7126 goto exit;
7127 }
Ronald Cron5c522922020-11-14 16:35:34 +01007128 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7130 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007131 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007133
Gilles Peskine3e561302022-04-14 00:17:15 +02007134 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7135 * for the output size. The PSA specification only guarantees that this
7136 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7137 * but it might be nice to allow smaller buffers if the output fits.
7138 * At the time of writing this comment, with only ECDH implemented,
7139 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7140 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7141 * be exact for it as well. */
7142 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7144 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007145 status = PSA_ERROR_BUFFER_TOO_SMALL;
7146 goto exit;
7147 }
7148
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 status = psa_key_agreement_raw_internal(alg, slot,
7150 peer_key, peer_key_length,
7151 output, output_size,
7152 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007153
7154exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007156 /* If an error happens and is not handled properly, the output
7157 * may be used as a key to protect sensitive data. Arrange for such
7158 * a key to be random, which is likely to result in decryption or
7159 * verification errors. This is better than filling the buffer with
7160 * some constant data such as zeros, which would result in the data
7161 * being protected with a reproducible, easily knowable key.
7162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007164 *output_length = output_size;
7165 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007166
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007168
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007170}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007171
7172
Gilles Peskine30524eb2020-11-13 17:02:26 +01007173
Gilles Peskine86a440b2018-11-12 18:39:40 +01007174/****************************************************************/
7175/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007176/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007177
Gilles Peskine30524eb2020-11-13 17:02:26 +01007178/** Initialize the PSA random generator.
7179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007180static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007181{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007182#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007184#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7185
Gilles Peskine30524eb2020-11-13 17:02:26 +01007186 /* Set default configuration if
7187 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007189 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007190 }
7191 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007192 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007194
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007196#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7197 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7198 /* The PSA entropy injection feature depends on using NV seed as an entropy
7199 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 mbedtls_entropy_add_source(&rng->entropy,
7201 mbedtls_nv_seed_poll, NULL,
7202 MBEDTLS_ENTROPY_BLOCK_SIZE,
7203 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007204#endif
7205
Gilles Peskine449bd832023-01-11 14:50:10 +01007206 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007207#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007208}
7209
7210/** Deinitialize the PSA random generator.
7211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007212static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007213{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007214#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007216#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7218 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007219#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007220}
7221
7222/** Seed the PSA random generator.
7223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007224static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007225{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007226#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7227 /* Do nothing: the external RNG seeds itself. */
7228 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007230#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007231 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7233 drbg_seed, sizeof(drbg_seed) - 1);
7234 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007235#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007236}
7237
Gilles Peskine449bd832023-01-11 14:50:10 +01007238psa_status_t psa_generate_random(uint8_t *output,
7239 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007240{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007241 GUARD_MODULE_INITIALIZED;
7242
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007243#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7244
7245 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7247 output, output_size,
7248 &output_length);
7249 if (status != PSA_SUCCESS) {
7250 return status;
7251 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007252 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007253 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 if (output_length != output_size) {
7255 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7256 }
7257 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007258
7259#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7260
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007262 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7264 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7265 output_size);
7266 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7267 output, request_size);
7268 if (ret != 0) {
7269 return mbedtls_to_psa_error(ret);
7270 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007271 output_size -= request_size;
7272 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007273 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007275#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007276}
7277
Gilles Peskine8814fc42020-12-14 15:33:44 +01007278/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007279 *
7280 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7281 * `psa_generate_random(...)`. The state parameter is ignored since the
7282 * PSA API doesn't support passing an explicit state.
7283 *
7284 * In the non-external case, psa_generate_random() calls an
7285 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7286 * and semantics as mbedtls_psa_get_random(). As an optimization,
7287 * instead of doing this back-and-forth between the PSA API and the
7288 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7289 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007291#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7292int mbedtls_psa_get_random(void *p_rng,
7293 unsigned char *output,
7294 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007295{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007296 /* This function takes a pointer to the RNG state because that's what
7297 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7298 * its own state internally and doesn't let the caller access that state.
7299 * So we just ignore the state parameter, and in practice we'll pass
7300 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007301 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007302 psa_status_t status = psa_generate_random(output, output_size);
7303 if (status == PSA_SUCCESS) {
7304 return 0;
7305 } else {
7306 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7307 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007308}
7309#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7310
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007311#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00007312#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007313
Gilles Peskine449bd832023-01-11 14:50:10 +01007314psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7315 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007316{
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 if (global_data.initialized) {
7318 return PSA_ERROR_NOT_PERMITTED;
7319 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007320
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7322 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7323 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7324 return PSA_ERROR_INVALID_ARGUMENT;
7325 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007326
Gilles Peskine449bd832023-01-11 14:50:10 +01007327 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007328}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007329#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007330
Ronald Cron3772afe2021-02-08 16:10:05 +01007331/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007332 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007333 * \param type The key type
7334 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007335 *
7336 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007337 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007338 * \retval #PSA_ERROR_INVALID_ARGUMENT
7339 * The size in bits of the key is not valid.
7340 * \retval #PSA_ERROR_NOT_SUPPORTED
7341 * The type and/or the size in bits of the key or the combination of
7342 * the two is not supported.
7343 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007344static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007346{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007348
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 if (key_type_is_raw_bytes(type)) {
7350 status = psa_validate_unstructured_key_bit_size(type, bits);
7351 if (status != PSA_SUCCESS) {
7352 return status;
7353 }
7354 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007355#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7357 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7358 return PSA_ERROR_NOT_SUPPORTED;
7359 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007360
Ronald Cron01b2aba2020-10-05 09:42:02 +02007361 /* Accept only byte-aligned keys, for the same reasons as
7362 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 if (bits % 8 != 0) {
7364 return PSA_ERROR_NOT_SUPPORTED;
7365 }
7366 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007367#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007368
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007369#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007370 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007371 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007372 return PSA_SUCCESS;
7373 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007374#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007375
Valerio Setti8bb57632023-05-26 13:48:07 +02007376#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007377 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007378 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007379 return PSA_ERROR_NOT_SUPPORTED;
7380 }
7381 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007382#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007383 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007385 }
7386
Gilles Peskine449bd832023-01-11 14:50:10 +01007387 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007388}
7389
Ronald Cron55ed0592020-10-05 10:30:40 +02007390psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007391 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007393{
7394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007395 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007396
Gilles Peskine449bd832023-01-11 14:50:10 +01007397 if ((attributes->domain_parameters == NULL) &&
7398 (attributes->domain_parameters_size != 0)) {
7399 return PSA_ERROR_INVALID_ARGUMENT;
7400 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007401
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 if (key_type_is_raw_bytes(type)) {
7403 status = psa_generate_random(key_buffer, key_buffer_size);
7404 if (status != PSA_SUCCESS) {
7405 return status;
7406 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007407
David Brown12ca5032021-02-11 11:02:00 -07007408#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 if (type == PSA_KEY_TYPE_DES) {
7410 psa_des_set_key_parity(key_buffer, key_buffer_size);
7411 }
David Brown8107e312021-02-12 08:08:46 -07007412#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007414
Valerio Setti76df8c12023-07-11 14:11:28 +02007415#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7417 return mbedtls_psa_rsa_generate_key(attributes,
7418 key_buffer,
7419 key_buffer_size,
7420 key_buffer_length);
7421 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007422#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007423
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007424#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007425 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7426 return mbedtls_psa_ecp_generate_key(attributes,
7427 key_buffer,
7428 key_buffer_size,
7429 key_buffer_length);
7430 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007431#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007432
Valerio Setti8bb57632023-05-26 13:48:07 +02007433#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007434 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7435 return mbedtls_psa_ffdh_generate_key(attributes,
7436 key_buffer,
7437 key_buffer_size,
7438 key_buffer_length);
7439 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007440#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Steven Cooreman29149862020-08-05 15:43:42 +02007441 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 (void) key_buffer_length;
7443 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007444 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007445
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007447}
Darryl Green0c6575a2018-11-07 16:05:30 +00007448
Gilles Peskine449bd832023-01-11 14:50:10 +01007449psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7450 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007451{
7452 psa_status_t status;
7453 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007454 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007455 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007456
Ronald Cron81709fc2020-11-14 12:10:32 +01007457 *key = MBEDTLS_SVC_KEY_ID_INIT;
7458
Gilles Peskine0f84d622019-09-12 19:03:13 +02007459 /* Reject any attempt to create a zero-length key so that we don't
7460 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 if (psa_get_key_bits(attributes) == 0) {
7462 return PSA_ERROR_INVALID_ARGUMENT;
7463 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007464
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007465 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7467 return PSA_ERROR_INVALID_ARGUMENT;
7468 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007469
Gilles Peskine449bd832023-01-11 14:50:10 +01007470 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7471 &slot, &driver);
7472 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007473 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 }
Gilles Peskine11792082019-08-06 18:36:36 +02007475
Ronald Cron977c2472020-10-13 08:32:21 +02007476 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307477 * storage ( thus not in the case of generating a key in a secure element
7478 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7479 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 if (slot->key.data == NULL) {
7481 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7482 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007483 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 attributes->core.type, attributes->core.bits);
7485 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007486 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007487 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007488
7489 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 attributes->core.type,
7491 attributes->core.bits);
7492 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007493 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007494 attributes, &key_buffer_size);
7495 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007496 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007497 }
Ronald Cron977c2472020-10-13 08:32:21 +02007498 }
Ronald Cron977c2472020-10-13 08:32:21 +02007499
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7501 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007502 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 }
Ronald Cron977c2472020-10-13 08:32:21 +02007504 }
7505
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 status = psa_driver_wrapper_generate_key(attributes,
7507 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007508
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 if (status != PSA_SUCCESS) {
7510 psa_remove_key_data_from_memory(slot);
7511 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007512
Gilles Peskine11792082019-08-06 18:36:36 +02007513exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 if (status == PSA_SUCCESS) {
7515 status = psa_finish_key_creation(slot, driver, key);
7516 }
7517 if (status != PSA_SUCCESS) {
7518 psa_fail_key_creation(slot, driver);
7519 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007520
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007522}
7523
Gilles Peskinee59236f2018-01-27 23:32:46 +01007524/****************************************************************/
7525/* Module setup */
7526/****************************************************************/
7527
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007528#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007529psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 void (* entropy_init)(mbedtls_entropy_context *ctx),
7531 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007532{
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7534 return PSA_ERROR_BAD_STATE;
7535 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007536 global_data.rng.entropy_init = entropy_init;
7537 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007539}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007540#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007541
Gilles Peskine449bd832023-01-11 14:50:10 +01007542void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007543{
Gilles Peskine449bd832023-01-11 14:50:10 +01007544 psa_wipe_all_key_slots();
7545 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7546 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007547 }
7548 /* Wipe all remaining data, including configuration.
7549 * In particular, this sets all state indicator to the value
7550 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007552
7553 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007555}
7556
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007557#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7558/** Recover a transaction that was interrupted by a power failure.
7559 *
7560 * This function is called during initialization, before psa_crypto_init()
7561 * returns. If this function returns a failure status, the initialization
7562 * fails.
7563 */
7564static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007566{
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007568 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7569 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007570 /* TODO - fall through to the failure case until this
7571 * is implemented.
7572 * https://github.com/ARMmbed/mbed-crypto/issues/218
7573 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007574 default:
7575 /* We found an unsupported transaction in the storage.
7576 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007578 }
7579}
7580#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7581
Gilles Peskine449bd832023-01-11 14:50:10 +01007582psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007583{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007584 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007585
Gilles Peskinec6b69072018-11-20 21:42:52 +01007586 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 if (global_data.initialized != 0) {
7588 return PSA_SUCCESS;
7589 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007590
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007591 /* Init drivers */
7592 status = psa_driver_wrapper_init();
7593 if (status != PSA_SUCCESS) {
7594 goto exit;
7595 }
7596 global_data.drivers_initialized = 1;
7597
Gilles Peskine30524eb2020-11-13 17:02:26 +01007598 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007600 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 status = mbedtls_psa_random_seed(&global_data.rng);
7602 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007604 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007605 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007606
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 status = psa_initialize_key_slots();
7608 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007609 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007611
Gilles Peskine4b734222019-07-24 15:56:31 +02007612#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 status = psa_crypto_load_transaction();
7614 if (status == PSA_SUCCESS) {
7615 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7616 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007617 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 }
7619 status = psa_crypto_stop_transaction();
7620 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007621 /* There's no transaction to complete. It's all good. */
7622 status = PSA_SUCCESS;
7623 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007624#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007625
Gilles Peskinec6b69072018-11-20 21:42:52 +01007626 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007627 global_data.initialized = 1;
7628
7629exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 if (status != PSA_SUCCESS) {
7631 mbedtls_psa_crypto_free();
7632 }
7633 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007634}
7635
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007636psa_status_t psa_crypto_driver_pake_get_password_len(
7637 const psa_crypto_driver_pake_inputs_t *inputs,
7638 size_t *password_len)
7639{
7640 if (inputs->password_len == 0) {
7641 return PSA_ERROR_BAD_STATE;
7642 }
7643
7644 *password_len = inputs->password_len;
7645
7646 return PSA_SUCCESS;
7647}
7648
7649psa_status_t psa_crypto_driver_pake_get_password(
7650 const psa_crypto_driver_pake_inputs_t *inputs,
7651 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7652{
7653 if (inputs->password_len == 0) {
7654 return PSA_ERROR_BAD_STATE;
7655 }
7656
7657 if (buffer_size < inputs->password_len) {
7658 return PSA_ERROR_BUFFER_TOO_SMALL;
7659 }
7660
7661 memcpy(buffer, inputs->password, inputs->password_len);
7662 *buffer_length = inputs->password_len;
7663
7664 return PSA_SUCCESS;
7665}
7666
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007667psa_status_t psa_crypto_driver_pake_get_user_len(
7668 const psa_crypto_driver_pake_inputs_t *inputs,
7669 size_t *user_len)
7670{
7671 if (inputs->user_len == 0) {
7672 return PSA_ERROR_BAD_STATE;
7673 }
7674
7675 *user_len = inputs->user_len;
7676
7677 return PSA_SUCCESS;
7678}
7679
7680psa_status_t psa_crypto_driver_pake_get_user(
7681 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007682 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007683{
7684 if (inputs->user_len == 0) {
7685 return PSA_ERROR_BAD_STATE;
7686 }
7687
Przemek Stekielc0e62502023-03-14 11:49:36 +01007688 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007689 return PSA_ERROR_BUFFER_TOO_SMALL;
7690 }
7691
Przemek Stekielc0e62502023-03-14 11:49:36 +01007692 memcpy(user_id, inputs->user, inputs->user_len);
7693 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007694
7695 return PSA_SUCCESS;
7696}
7697
7698psa_status_t psa_crypto_driver_pake_get_peer_len(
7699 const psa_crypto_driver_pake_inputs_t *inputs,
7700 size_t *peer_len)
7701{
7702 if (inputs->peer_len == 0) {
7703 return PSA_ERROR_BAD_STATE;
7704 }
7705
7706 *peer_len = inputs->peer_len;
7707
7708 return PSA_SUCCESS;
7709}
7710
7711psa_status_t psa_crypto_driver_pake_get_peer(
7712 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007713 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007714{
7715 if (inputs->peer_len == 0) {
7716 return PSA_ERROR_BAD_STATE;
7717 }
7718
Przemek Stekielc0e62502023-03-14 11:49:36 +01007719 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007720 return PSA_ERROR_BUFFER_TOO_SMALL;
7721 }
7722
Przemek Stekielc0e62502023-03-14 11:49:36 +01007723 memcpy(peer_id, inputs->peer, inputs->peer_len);
7724 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007725
7726 return PSA_SUCCESS;
7727}
7728
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007729psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7730 const psa_crypto_driver_pake_inputs_t *inputs,
7731 psa_pake_cipher_suite_t *cipher_suite)
7732{
7733 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7734 return PSA_ERROR_BAD_STATE;
7735 }
7736
7737 *cipher_suite = inputs->cipher_suite;
7738
7739 return PSA_SUCCESS;
7740}
7741
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01007742#if defined(PSA_WANT_ALG_SOME_PAKE)
Neil Armstronga7d08c32022-06-01 18:21:20 +02007743psa_status_t psa_pake_setup(
7744 psa_pake_operation_t *operation,
7745 const psa_pake_cipher_suite_t *cipher_suite)
7746{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007747 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007748
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007749 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007750 status = PSA_ERROR_BAD_STATE;
7751 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007752 }
7753
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007754 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007755 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007756 status = PSA_ERROR_INVALID_ARGUMENT;
7757 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007758 }
7759
Przemek Stekiel51eac532022-12-07 11:04:51 +01007760 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7761
Przemek Stekiele12ed362022-12-21 12:54:46 +01007762 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007763 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7764 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007765 operation->data.inputs.cipher_suite = *cipher_suite;
7766
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007767#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007768 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007769 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007770 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007771
David Horstmann16f01512023-06-14 17:21:07 +01007772 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007773 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007774 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007775#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007776 {
7777 status = PSA_ERROR_NOT_SUPPORTED;
7778 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007779 }
7780
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007781 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7782
Przemek Stekiel51eac532022-12-07 11:04:51 +01007783 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007784exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007785 psa_pake_abort(operation);
7786 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007787}
7788
7789psa_status_t psa_pake_set_password_key(
7790 psa_pake_operation_t *operation,
7791 mbedtls_svc_key_id_t password)
7792{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007793 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007794 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7795 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007796
Przemek Stekiel51eac532022-12-07 11:04:51 +01007797 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007798 status = PSA_ERROR_BAD_STATE;
7799 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007800 }
7801
Przemek Stekiel6c764412022-11-22 14:05:12 +01007802 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7803 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007804 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007805 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007806 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007807 }
7808
Przemek Stekiel6c764412022-11-22 14:05:12 +01007809 psa_key_attributes_t attributes = {
7810 .core = slot->attr
7811 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007812
Przemek Stekiel51eac532022-12-07 11:04:51 +01007813 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007814
Przemek Stekiel51eac532022-12-07 11:04:51 +01007815 if (type != PSA_KEY_TYPE_PASSWORD &&
7816 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7817 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007818 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007819 }
7820
Przemek Stekiel51eac532022-12-07 11:04:51 +01007821 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7822 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007823 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007824 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007825 }
7826
7827 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7828 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007829 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007830exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007831 if (status != PSA_SUCCESS) {
7832 psa_pake_abort(operation);
7833 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007834 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007835 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007836}
7837
7838psa_status_t psa_pake_set_user(
7839 psa_pake_operation_t *operation,
7840 const uint8_t *user_id,
7841 size_t user_id_len)
7842{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007843 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007844
7845 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007846 status = PSA_ERROR_BAD_STATE;
7847 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007848 }
7849
Przemek Stekiel51eac532022-12-07 11:04:51 +01007850 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007851 status = PSA_ERROR_INVALID_ARGUMENT;
7852 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007853 }
7854
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007855 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007856 status = PSA_ERROR_BAD_STATE;
7857 goto exit;
7858 }
7859
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007860 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7861 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007862 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7863 goto exit;
7864 }
7865
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007866 memcpy(operation->data.inputs.user, user_id, user_id_len);
7867 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007868
7869 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007870exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007871 psa_pake_abort(operation);
7872 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007873}
7874
7875psa_status_t psa_pake_set_peer(
7876 psa_pake_operation_t *operation,
7877 const uint8_t *peer_id,
7878 size_t peer_id_len)
7879{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007881
7882 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007883 status = PSA_ERROR_BAD_STATE;
7884 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007885 }
7886
Przemek Stekiel51eac532022-12-07 11:04:51 +01007887 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007888 status = PSA_ERROR_INVALID_ARGUMENT;
7889 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007890 }
7891
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007892 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007893 status = PSA_ERROR_BAD_STATE;
7894 goto exit;
7895 }
7896
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007897 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7898 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007899 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7900 goto exit;
7901 }
7902
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007903 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7904 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007905
7906 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007907exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007908 psa_pake_abort(operation);
7909 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007910}
7911
7912psa_status_t psa_pake_set_role(
7913 psa_pake_operation_t *operation,
7914 psa_pake_role_t role)
7915{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007916 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007917
Przemek Stekiel51eac532022-12-07 11:04:51 +01007918 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007919 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007920 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007921 }
7922
Przemek Stekiel09104b82023-03-06 14:11:51 +01007923 switch (operation->alg) {
7924#if defined(PSA_WANT_ALG_JPAKE)
7925 case PSA_ALG_JPAKE:
7926 if (role == PSA_PAKE_ROLE_NONE) {
7927 return PSA_SUCCESS;
7928 }
7929 status = PSA_ERROR_INVALID_ARGUMENT;
7930 break;
7931#endif
7932 default:
7933 (void) role;
7934 status = PSA_ERROR_NOT_SUPPORTED;
7935 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007936 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007937exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007938 psa_pake_abort(operation);
7939 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007940}
7941
David Horstmanne7f21e62023-05-12 18:17:21 +01007942/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007943#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007944static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007945 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007946{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007947 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007948 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007949 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007950
David Horstmann024e5c52023-06-14 15:48:21 +01007951 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007952 is_x1 = (stage->outputs < 1);
7953 } else {
7954 is_x1 = (stage->inputs < 1);
7955 }
7956
David Horstmann74a3d8c2023-06-14 18:28:19 +01007957 key_share_step = is_x1 ?
7958 PSA_JPAKE_X1_STEP_KEY_SHARE :
7959 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007960 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007961 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7962 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7963 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7964 } else {
7965 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007966 }
David Horstmann74a3d8c2023-06-14 18:28:19 +01007967 return key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007968}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007969#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007970
Przemek Stekiel2797d372022-12-22 11:19:22 +01007971static psa_status_t psa_pake_complete_inputs(
7972 psa_pake_operation_t *operation)
7973{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007974 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007975 /* Create copy of the inputs on stack as inputs share memory
7976 with the driver context which will be setup by the driver. */
7977 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007978
Przemek Stekielfde11282023-03-13 16:06:09 +01007979 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007980 return PSA_ERROR_BAD_STATE;
7981 }
7982
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007983 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007984 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7985 return PSA_ERROR_BAD_STATE;
7986 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007987 }
7988
Przemek Stekiel18620a32023-01-17 16:34:52 +01007989 /* Clear driver context */
7990 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7991
7992 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007993
7994 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007995 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7996 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007997
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007998 /* User and peer are translated to role. */
7999 mbedtls_free(inputs.user);
8000 mbedtls_free(inputs.peer);
8001
Przemek Stekiel2797d372022-12-22 11:19:22 +01008002 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008003#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008004 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008005 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008006 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008007#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008008 {
8009 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008010 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008011 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008012 return status;
8013}
8014
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008015#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008016static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008017 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008018 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008019 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008020{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008021 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8022 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8023 step != PSA_PAKE_STEP_ZK_PROOF) {
8024 return PSA_ERROR_INVALID_ARGUMENT;
8025 }
8026
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008027 psa_jpake_computation_stage_t *computation_stage =
8028 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008029
David Horstmann5da95602023-06-08 15:37:12 +01008030 if (computation_stage->round != PSA_JPAKE_FIRST &&
8031 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008032 return PSA_ERROR_BAD_STATE;
8033 }
8034
David Horstmanne7f21e62023-05-12 18:17:21 +01008035 /* Check that the step we are given is the one we were expecting */
8036 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008037 return PSA_ERROR_BAD_STATE;
8038 }
8039
David Horstmanne7f21e62023-05-12 18:17:21 +01008040 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8041 computation_stage->inputs == 0 &&
8042 computation_stage->outputs == 0) {
8043 /* Start of the round, so function decides whether we are inputting
8044 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008045 computation_stage->io_mode = io_mode;
8046 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008047 /* Middle of the round so the mode we are in must match the function
8048 * called by the user */
8049 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008050 }
8051
8052 return PSA_SUCCESS;
8053}
8054
David Horstmanne7f21e62023-05-12 18:17:21 +01008055static psa_status_t psa_jpake_epilogue(
8056 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008057 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008058{
David Horstmanne7f21e62023-05-12 18:17:21 +01008059 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008060 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008061
David Horstmanne7f21e62023-05-12 18:17:21 +01008062 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8063 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008064 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008065 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008066 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008067 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008068 }
8069 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008070 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008071 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008072 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008073 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008074 }
8075 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008076 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8077 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008078 /* End of a round, move to the next round */
8079 stage->inputs = 0;
8080 stage->outputs = 0;
8081 stage->round++;
8082 }
8083 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008084 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008085 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008086 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008087 return PSA_SUCCESS;
8088}
David Horstmanne7f21e62023-05-12 18:17:21 +01008089
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008090#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008091
Neil Armstronga7d08c32022-06-01 18:21:20 +02008092psa_status_t psa_pake_output(
8093 psa_pake_operation_t *operation,
8094 psa_pake_step_t step,
8095 uint8_t *output,
8096 size_t output_size,
8097 size_t *output_length)
8098{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008100 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008101 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008102
8103 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008104 status = psa_pake_complete_inputs(operation);
8105 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008106 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008107 }
8108 }
8109
8110 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008111 status = PSA_ERROR_BAD_STATE;
8112 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008113 }
8114
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008115 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008116 status = PSA_ERROR_INVALID_ARGUMENT;
8117 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008118 }
8119
Przemek Stekiele12ed362022-12-21 12:54:46 +01008120 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008121#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008122 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008123 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008124 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008125 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008126 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008127 driver_step = convert_jpake_computation_stage_to_driver_step(
8128 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008129 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008130#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008131 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008132 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008133 status = PSA_ERROR_NOT_SUPPORTED;
8134 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008135 }
8136
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008137 status = psa_driver_wrapper_pake_output(operation, driver_step,
8138 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008139
8140 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008141 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008142 }
8143
8144 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008145#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008146 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008147 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008148 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008149 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008150 }
8151 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008152#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008153 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008154 status = PSA_ERROR_NOT_SUPPORTED;
8155 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008156 }
8157
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008158 return PSA_SUCCESS;
8159exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008160 psa_pake_abort(operation);
8161 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008162}
8163
8164psa_status_t psa_pake_input(
8165 psa_pake_operation_t *operation,
8166 psa_pake_step_t step,
8167 const uint8_t *input,
8168 size_t input_length)
8169{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008171 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008172 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8173 operation->primitive,
8174 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008175
8176 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008177 status = psa_pake_complete_inputs(operation);
8178 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008179 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008180 }
8181 }
8182
8183 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008184 status = PSA_ERROR_BAD_STATE;
8185 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008186 }
8187
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008188 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008189 status = PSA_ERROR_INVALID_ARGUMENT;
8190 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008191 }
8192
Przemek Stekiele12ed362022-12-21 12:54:46 +01008193 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008194#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008195 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008196 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008197 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008198 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008199 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008200 driver_step = convert_jpake_computation_stage_to_driver_step(
8201 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008202 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008203#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008204 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008205 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008206 status = PSA_ERROR_NOT_SUPPORTED;
8207 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008208 }
8209
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008210 status = psa_driver_wrapper_pake_input(operation, driver_step,
8211 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008212
8213 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008214 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008215 }
8216
8217 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008218#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008219 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008220 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008221 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008222 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008223 }
8224 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008225#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008226 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008227 status = PSA_ERROR_NOT_SUPPORTED;
8228 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008229 }
8230
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008231 return PSA_SUCCESS;
8232exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008233 psa_pake_abort(operation);
8234 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008235}
8236
8237psa_status_t psa_pake_get_implicit_key(
8238 psa_pake_operation_t *operation,
8239 psa_key_derivation_operation_t *output)
8240{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008241 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008242 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008243 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008244 size_t shared_key_len = 0;
8245
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008246 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008247 status = PSA_ERROR_BAD_STATE;
8248 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008249 }
8250
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008251#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008252 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008253 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008254 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008255 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008256 status = PSA_ERROR_BAD_STATE;
8257 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008259 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008260#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008261 {
8262 status = PSA_ERROR_NOT_SUPPORTED;
8263 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008264 }
8265
Przemek Stekiel0c781802022-11-29 14:53:13 +01008266 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8267 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008268 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008269 &shared_key_len);
8270
8271 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008272 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008273 }
8274
8275 status = psa_key_derivation_input_bytes(output,
8276 PSA_KEY_DERIVATION_INPUT_SECRET,
8277 shared_key,
8278 shared_key_len);
8279
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008280 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008281exit:
8282 abort_status = psa_pake_abort(operation);
8283 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008284}
8285
8286psa_status_t psa_pake_abort(
8287 psa_pake_operation_t *operation)
8288{
Przemek Stekield69dca92023-01-31 19:59:20 +01008289 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008290
Przemek Stekield69dca92023-01-31 19:59:20 +01008291 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008292 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008293 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008294
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008295 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8296 if (operation->data.inputs.password != NULL) {
8297 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008298 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008299 mbedtls_free(operation->data.inputs.password);
8300 }
8301 if (operation->data.inputs.user != NULL) {
8302 mbedtls_free(operation->data.inputs.user);
8303 }
8304 if (operation->data.inputs.peer != NULL) {
8305 mbedtls_free(operation->data.inputs.peer);
8306 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008307 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008308 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008309
Przemek Stekield69dca92023-01-31 19:59:20 +01008310 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008311}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008312#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008313
Gilles Peskinee59236f2018-01-27 23:32:46 +01008314#endif /* MBEDTLS_PSA_CRYPTO_C */