blob: f735d88aa31268c2fd7c81e316234a5b4bed7da4 [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}
Przemek Stekiel53410502023-04-28 13:18:43 +0200130#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
131 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
132 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
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}
Przemek Stekiel53410502023-04-28 13:18:43 +0200142#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR ||
143 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
144 PSA_WANT_KEY_TYPE_DH_KEY_PAIR */
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 Settid4a5d462023-04-05 18:19:01 +0200391#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100392mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
393 size_t bits,
394 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200395{
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100397 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100399#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100400 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100402#endif
403#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100404 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100406#endif
407#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100408 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100410#endif
411#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100412 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100414#endif
415#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100416 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100418 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (bits_is_sloppy) {
420 return MBEDTLS_ECP_DP_SECP521R1;
421 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100422 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100423#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 }
425 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100426
Paul Elliott8ff510a2020-06-02 17:19:28 +0100427 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100429#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100430 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100432#endif
433#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100434 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100436#endif
437#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100438 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100440#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100441 }
442 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100443
Paul Elliott8ff510a2020-06-02 17:19:28 +0100444 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100446#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (bits_is_sloppy) {
451 return MBEDTLS_ECP_DP_CURVE25519;
452 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100453 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100454#endif
455#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100456 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100458#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100459 }
460 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100461
Paul Elliott8ff510a2020-06-02 17:19:28 +0100462 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100464#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100467#endif
468#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100471#endif
472#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100475#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100476 }
477 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200478 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100479
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100480 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200482}
Valerio Settid4a5d462023-04-05 18:19:01 +0200483#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
486 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487{
488 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200492 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200493 case PSA_KEY_TYPE_PASSWORD:
494 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200495 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100496#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (bits != 128 && bits != 192 && bits != 256) {
499 return PSA_ERROR_INVALID_ARGUMENT;
500 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501 break;
502#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200503#if defined(PSA_WANT_KEY_TYPE_ARIA)
504 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (bits != 128 && bits != 192 && bits != 256) {
506 return PSA_ERROR_INVALID_ARGUMENT;
507 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200508 break;
509#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100510#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (bits != 128 && bits != 192 && bits != 256) {
513 return PSA_ERROR_INVALID_ARGUMENT;
514 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200515 break;
516#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100517#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200518 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (bits != 64 && bits != 128 && bits != 192) {
520 return PSA_ERROR_INVALID_ARGUMENT;
521 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 break;
523#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100524#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200525 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (bits != 256) {
527 return PSA_ERROR_INVALID_ARGUMENT;
528 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200529 break;
530#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200531 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (bits % 8 != 0) {
535 return PSA_ERROR_INVALID_ARGUMENT;
536 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539}
540
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100541/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100542 *
Steven Cooremancd640932021-03-08 12:00:27 +0100543 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
544 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100545 *
546 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100547 * \param[in] key_type The key type of the key to be used with the
548 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 *
550 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100551 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100552 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100553 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100555MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100556 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100558{
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (PSA_ALG_IS_HMAC(algorithm)) {
560 if (key_type == PSA_KEY_TYPE_HMAC) {
561 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100562 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100563 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
566 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
567 * key. */
568 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
569 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
570 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
571 * the block length (larger than 1) for block ciphers. */
572 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
573 return PSA_SUCCESS;
574 }
575 }
576 }
577
578 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100579}
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
582 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200583{
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (slot->key.data != NULL) {
585 return PSA_ERROR_ALREADY_EXISTS;
586 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 slot->key.data = mbedtls_calloc(1, buffer_length);
589 if (slot->key.data == NULL) {
590 return PSA_ERROR_INSUFFICIENT_MEMORY;
591 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200592
Ronald Cronea0f8a62020-11-25 17:52:23 +0100593 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200595}
596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
598 const uint8_t *data,
599 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200600{
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 psa_status_t status = psa_allocate_buffer_to_slot(slot,
602 data_length);
603 if (status != PSA_SUCCESS) {
604 return status;
605 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 memcpy(slot->key.data, data, data_length);
608 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200609}
610
Ronald Crona0fe59f2020-11-29 11:14:53 +0100611psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100612 const psa_key_attributes_t *attributes,
613 const uint8_t *data, size_t data_length,
614 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100616{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
618 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200620 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (data_length == 0) {
622 return PSA_ERROR_NOT_SUPPORTED;
623 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 if (key_type_is_raw_bytes(type)) {
626 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
629 *bits);
630 if (status != PSA_SUCCESS) {
631 return status;
632 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200633
Ronald Crondd04d422020-11-28 15:14:42 +0100634 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100636 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return PSA_SUCCESS;
640 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200641#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
642 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100643 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200644 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100645 return PSA_ERROR_INVALID_ARGUMENT;
646 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200647 return mbedtls_psa_ffdh_import_key(attributes,
648 data, data_length,
649 key_buffer, key_buffer_size,
650 key_buffer_length,
651 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100652 }
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200653#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
654 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
John Durkop9814fa22020-11-04 12:28:15 -0800655#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
657 if (PSA_KEY_TYPE_IS_ECC(type)) {
658 return mbedtls_psa_ecp_import_key(attributes,
659 data, data_length,
660 key_buffer, key_buffer_size,
661 key_buffer_length,
662 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100663 }
John Durkop9814fa22020-11-04 12:28:15 -0800664#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
665 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700666#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
668 if (PSA_KEY_TYPE_IS_RSA(type)) {
669 return mbedtls_psa_rsa_import_key(attributes,
670 data, data_length,
671 key_buffer, key_buffer_size,
672 key_buffer_length,
673 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200674 }
John Durkop9814fa22020-11-04 12:28:15 -0800675#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
676 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100677 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100680}
681
Gilles Peskinef603c712019-01-19 13:40:11 +0100682/** Calculate the intersection of two algorithm usage policies.
683 *
684 * Return 0 (which allows no operation) on incompatibility.
685 */
686static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100687 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100688 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100690{
Gilles Peskine549ea862019-05-22 11:45:59 +0200691 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (alg1 == alg2) {
693 return alg1;
694 }
Steven Cooreman03488022021-02-18 12:07:20 +0100695 /* If the policies are from the same hash-and-sign family, check
696 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
698 PSA_ALG_IS_SIGN_HASH(alg2) &&
699 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
700 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
701 return alg2;
702 }
703 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
704 return alg1;
705 }
Steven Cooreman03488022021-02-18 12:07:20 +0100706 }
707 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100708 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100709 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
711 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
712 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
713 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
714 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100715 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100716
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100717 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
719 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
720 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
721 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100722 }
723 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
725 (alg1_len <= alg2_len)) {
726 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100727 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
729 (alg2_len <= alg1_len)) {
730 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100731 }
732 }
733 /* If the policies are from the same MAC family, check whether one
734 * of them is a minimum-MAC-length policy. Calculate the most
735 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
737 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
738 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100739 /* Validate the combination of key type and algorithm. Since the base
740 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
742 return 0;
743 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100744
Steven Cooreman31a876d2021-03-03 20:47:40 +0100745 /* Get the (exact or at-least) output lengths for both sides of the
746 * requested intersection. None of the currently supported algorithms
747 * have an output length dependent on the actual key size, so setting it
748 * to a bogus value of 0 is currently OK.
749 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100750 * Note that for at-least-this-length wildcard algorithms, the output
751 * length is set to the shortest allowed length, which allows us to
752 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
754 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100755 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100756
Steven Cooremana1d83222021-02-25 10:20:29 +0100757 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
759 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
760 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100761 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100762
763 /* If only one is an at-least-this-length policy, the intersection would
764 * be the other (fixed-length) policy as long as said fixed length is
765 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
767 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100768 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
770 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100771 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100772
Steven Cooremancd640932021-03-08 12:00:27 +0100773 /* If none of them are wildcards, check whether they define the same tag
774 * length. This is still possible here when one is default-length and
775 * the other specific-length. Ensure to always return the
776 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (alg1_len == alg2_len) {
778 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
779 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100780 }
781 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100783}
784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785static int psa_key_algorithm_permits(psa_key_type_t key_type,
786 psa_algorithm_t policy_alg,
787 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200788{
Gilles Peskine549ea862019-05-22 11:45:59 +0200789 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (requested_alg == policy_alg) {
791 return 1;
792 }
Steven Cooreman03488022021-02-18 12:07:20 +0100793 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
794 * and requested_alg is the same hash-and-sign family with any hash,
795 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
797 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
798 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
799 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100800 }
801 /* If policy_alg is a wildcard AEAD algorithm of the same base as
802 * the requested algorithm, check the requested tag length to be
803 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (PSA_ALG_IS_AEAD(policy_alg) &&
805 PSA_ALG_IS_AEAD(requested_alg) &&
806 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
807 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
808 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
809 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
810 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100811 }
Steven Cooremancd640932021-03-08 12:00:27 +0100812 /* If policy_alg is a MAC algorithm of the same base as the requested
813 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (PSA_ALG_IS_MAC(policy_alg) &&
815 PSA_ALG_IS_MAC(requested_alg) &&
816 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
817 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100818 /* Validate the combination of key type and algorithm. Since the policy
819 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
821 return 0;
822 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100823
Steven Cooreman31a876d2021-03-03 20:47:40 +0100824 /* Get both the requested output length for the algorithm which is to be
825 * verified, and the default output length for the base algorithm.
826 * Note that none of the currently supported algorithms have an output
827 * length dependent on actual key size, so setting it to a bogus value
828 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100829 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100831 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 key_type, 0,
833 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100834
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100835 /* If the policy is default-length, only allow an algorithm with
836 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
838 return requested_output_length == default_output_length;
839 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100840
841 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100842 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
844 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
845 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100846 }
847
Steven Cooremancd640932021-03-08 12:00:27 +0100848 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
849 * check for the requested MAC length to be equal to or longer than the
850 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
852 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
853 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100854 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200855 }
Steven Cooremance48e852020-10-05 16:02:45 +0200856 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200857 * a key derivation with that key agreement should also be allowed. This
858 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
860 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
861 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
862 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200863 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100864 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200866}
867
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100868/** Test whether a policy permits an algorithm.
869 *
870 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100871 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100872 * \note This function requires providing the key type for which the policy is
873 * being validated, since some algorithm policy definitions (e.g. MAC)
874 * have different properties depending on what kind of cipher it is
875 * combined with.
876 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100877 * \retval PSA_SUCCESS When \p alg is a specific algorithm
878 * allowed by the \p policy.
879 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
880 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
881 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100882 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
884 psa_key_type_t key_type,
885 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100886{
Steven Cooremand788fab2021-02-25 11:29:17 +0100887 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (alg == 0) {
889 return PSA_ERROR_INVALID_ARGUMENT;
890 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100891
Steven Cooreman7e39f052021-02-23 14:18:32 +0100892 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (PSA_ALG_IS_WILDCARD(alg)) {
894 return PSA_ERROR_INVALID_ARGUMENT;
895 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
898 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
899 return PSA_SUCCESS;
900 } else {
901 return PSA_ERROR_NOT_PERMITTED;
902 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100903}
904
Gilles Peskinef603c712019-01-19 13:40:11 +0100905/** Restrict a key policy based on a constraint.
906 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100907 * \note This function requires providing the key type for which the policy is
908 * being restricted, since some algorithm policy definitions (e.g. MAC)
909 * have different properties depending on what kind of cipher it is
910 * combined with.
911 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100912 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100913 * \param[in,out] policy The policy to restrict.
914 * \param[in] constraint The policy constraint to apply.
915 *
916 * \retval #PSA_SUCCESS
917 * \c *policy contains the intersection of the original value of
918 * \c *policy and \c *constraint.
919 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100920 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100921 * \c *policy is unchanged.
922 */
923static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100924 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100925 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100927{
928 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 psa_key_policy_algorithm_intersection(key_type, policy->alg,
930 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200931 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
933 constraint->alg2);
934 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
935 return PSA_ERROR_INVALID_ARGUMENT;
936 }
937 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
938 return PSA_ERROR_INVALID_ARGUMENT;
939 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100940 policy->usage &= constraint->usage;
941 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200942 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100944}
945
Przemek Stekiel061f6942022-11-30 10:51:35 +0100946/** Get the description of a key given its identifier and policy constraints
947 * and lock it.
948 *
949 * The key must have allow all the usage flags set in \p usage. If \p alg is
950 * nonzero, the key must allow operations with this algorithm. If \p alg is
951 * zero, the algorithm is not checked.
952 *
953 * In case of a persistent key, the function loads the description of the key
954 * into a key slot if not already done.
955 *
956 * On success, the returned key slot is locked. It is the responsibility of
957 * the caller to unlock the key slot when it does not access it anymore.
958 */
959static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100960 mbedtls_svc_key_id_t key,
961 psa_key_slot_t **p_slot,
962 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100964{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200965 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100966 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 status = psa_get_and_lock_key_slot(key, p_slot);
969 if (status != PSA_SUCCESS) {
970 return status;
971 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200972 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100973
974 /* Enforce that usage policy for the key slot contains all the flags
975 * required by the usage parameter. There is one exception: public
976 * keys can always be exported, so we treat public key objects as
977 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100979 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100983 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200984 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100985 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100986
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800987 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 if (alg != 0) {
989 status = psa_key_policy_permits(&slot->attr.policy,
990 slot->attr.type,
991 alg);
992 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100993 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100995 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200998
999error:
1000 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001004}
Darryl Green940d72c2018-07-13 13:18:51 +01001005
Ronald Cron5c522922020-11-14 16:35:34 +01001006/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001007 *
1008 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001009 * available, as opposed to a key in a secure element and/or to be used
1010 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001011 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001012 * This is a temporary function that may be used instead of
1013 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1014 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001015 *
Ronald Cron5c522922020-11-14 16:35:34 +01001016 * On success, the returned key slot is locked. It is the responsibility of the
1017 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001018 */
Ronald Cron5c522922020-11-14 16:35:34 +01001019static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1020 mbedtls_svc_key_id_t key,
1021 psa_key_slot_t **p_slot,
1022 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001024{
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1026 usage, alg);
1027 if (status != PSA_SUCCESS) {
1028 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001029 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1032 psa_unlock_key_slot(*p_slot);
1033 *p_slot = NULL;
1034 return PSA_ERROR_NOT_SUPPORTED;
1035 }
1036
1037 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001038}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001041{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001042 /* Data pointer will always be either a valid pointer or NULL in an
1043 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if (slot->key.data != NULL) {
1045 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1046 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001049 slot->key.data = NULL;
1050 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001053}
1054
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001055/** Completely wipe a slot in memory, including its policy.
1056 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001058{
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 /*
1062 * As the return error code may not be handled in case of multiple errors,
1063 * do our best to report an unexpected lock counter. Assert with
1064 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1065 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1066 * function is called as part of the execution of a test suite, the
1067 * execution of the test suite is stopped in error if the assertion fails.
1068 */
1069 if (slot->lock_count != 1) {
1070 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001071 status = PSA_ERROR_CORRUPTION_DETECTED;
1072 }
1073
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001074 /* Multipart operations may still be using the key. This is safe
1075 * because all multipart operation objects are independent from
1076 * the key slot: if they need to access the key after the setup
1077 * phase, they have a copy of the key. Note that this means that
1078 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001079 /* At this point, key material and other type-specific content has
1080 * been wiped. Clear remaining metadata. We can call memset and not
1081 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 memset(slot, 0, sizeof(*slot));
1083 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001084}
1085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001088 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001089 psa_status_t status; /* status of the last operation */
1090 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001091#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1092 psa_se_drv_table_entry_t *driver;
1093#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (mbedtls_svc_key_id_is_null(key)) {
1096 return PSA_SUCCESS;
1097 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001098
Ronald Cronf2911112020-10-29 17:51:10 +01001099 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001100 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001101 * key, this will load the key description from persistent memory if not
1102 * done yet. We cannot avoid this loading as without it we don't know if
1103 * the key is operated by an SE or not and this information is needed by
1104 * the current implementation.
1105 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 status = psa_get_and_lock_key_slot(key, &slot);
1107 if (status != PSA_SUCCESS) {
1108 return status;
1109 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001110
Ronald Cronf2911112020-10-29 17:51:10 +01001111 /*
1112 * If the key slot containing the key description is under access by the
1113 * library (apart from the present access), the key cannot be destroyed
1114 * yet. For the time being, just return in error. Eventually (to be
1115 * implemented), the key should be destroyed when all accesses have
1116 * stopped.
1117 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (slot->lock_count > 1) {
1119 psa_unlock_key_slot(slot);
1120 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001121 }
1122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001124 /* Refuse the destruction of a read-only key (which may or may not work
1125 * if we attempt it, depending on whether the key is merely read-only
1126 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001127 * Just do the best we can, which is to wipe the copy in memory
1128 * (done in this function's cleanup code). */
1129 overall_status = PSA_ERROR_NOT_PERMITTED;
1130 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001131 }
1132
Gilles Peskine354f7672019-07-12 23:46:38 +02001133#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1135 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001136 /* For a key in a secure element, we need to do three things:
1137 * remove the key file in internal storage, destroy the
1138 * key inside the secure element, and update the driver's
1139 * persistent data. Start a transaction that will encompass these
1140 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001142 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001144 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 status = psa_crypto_save_transaction();
1146 if (status != PSA_SUCCESS) {
1147 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001148 /* We should still try to destroy the key in the secure
1149 * element and the key metadata in storage. This is especially
1150 * important if the error is that the storage is full.
1151 * But how to do it exactly without risking an inconsistent
1152 * state after a reset?
1153 * https://github.com/ARMmbed/mbed-crypto/issues/215
1154 */
1155 overall_status = status;
1156 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001157 }
1158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 status = psa_destroy_se_key(driver,
1160 psa_key_slot_get_slot_number(slot));
1161 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001162 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001164 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001165#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1166
Darryl Greend49a4992018-06-18 17:27:26 +01001167#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1169 status = psa_destroy_persistent_key(slot->attr.id);
1170 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001171 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001173
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001174 /* TODO: other slots may have a copy of the same key. We should
1175 * invalidate them.
1176 * https://github.com/ARMmbed/mbed-crypto/issues/214
1177 */
Darryl Greend49a4992018-06-18 17:27:26 +01001178 }
1179#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001180
Gilles Peskinefc762652019-07-22 19:30:34 +02001181#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (driver != NULL) {
1183 status = psa_save_se_persistent_data(driver);
1184 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001185 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 }
1187 status = psa_crypto_stop_transaction();
1188 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001189 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001191 }
1192#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1193
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001194exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001196 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001198 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 }
1200 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001201}
1202
John Durkop07cc04a2020-11-16 22:08:34 -08001203#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001204 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001205static psa_status_t psa_get_rsa_public_exponent(
1206 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001208{
1209 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001211 uint8_t *buffer = NULL;
1212 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1216 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 }
1219 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001220 /* It's the default value, which is reported as an empty string,
1221 * so there's nothing to do. */
1222 goto exit;
1223 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 buflen = mbedtls_mpi_size(&mpi);
1226 buffer = mbedtls_calloc(1, buflen);
1227 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001228 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1229 goto exit;
1230 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1232 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001233 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001235 attributes->domain_parameters = buffer;
1236 attributes->domain_parameters_size = buflen;
1237
1238exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_mpi_free(&mpi);
1240 if (ret != 0) {
1241 mbedtls_free(buffer);
1242 }
1243 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001244}
John Durkop07cc04a2020-11-16 22:08:34 -08001245#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001246 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001247
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001248/** Retrieve all the publicly-accessible attributes of a key.
1249 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1251 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001252{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001253 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001254 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001255 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1260 if (status != PSA_SUCCESS) {
1261 return status;
1262 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001263
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001264 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1266 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001267
1268#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1270 psa_set_key_slot_number(attributes,
1271 psa_key_slot_get_slot_number(slot));
1272 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001273#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001276#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001277 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001278 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001279 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001280 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001281 * is not yet implemented.
1282 * https://github.com/ARMmbed/mbed-crypto/issues/216
1283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001285 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001286
Ronald Cron90857082020-11-25 14:58:33 +01001287 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 slot->attr.type,
1289 slot->key.data,
1290 slot->key.bytes,
1291 &rsa);
1292 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001293 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 status = psa_get_rsa_public_exponent(rsa,
1297 attributes);
1298 mbedtls_rsa_free(rsa);
1299 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001300 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001301 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001302#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001303 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001304 default:
1305 /* Nothing else to do. */
1306 break;
1307 }
1308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (status != PSA_SUCCESS) {
1310 psa_reset_key_attributes(attributes);
1311 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001316}
1317
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1319psa_status_t psa_get_key_slot_number(
1320 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001322{
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001324 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 return PSA_SUCCESS;
1326 } else {
1327 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001328 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001329}
1330#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1333 size_t key_buffer_size,
1334 uint8_t *data,
1335 size_t data_size,
1336 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001337{
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (key_buffer_size > data_size) {
1339 return PSA_ERROR_BUFFER_TOO_SMALL;
1340 }
1341 memcpy(data, key_buffer, key_buffer_size);
1342 memset(data + key_buffer_size, 0,
1343 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001344 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001346}
1347
Ronald Cron7285cda2020-11-26 14:46:37 +01001348psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 const psa_key_attributes_t *attributes,
1350 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352{
Ronald Crond18b5f82020-11-25 16:46:05 +01001353 psa_key_type_t type = attributes->core.type;
1354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 if (key_type_is_raw_bytes(type) ||
1356 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001357 PSA_KEY_TYPE_IS_ECC(type) ||
1358 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 return psa_export_key_buffer_internal(
1360 key_buffer, key_buffer_size,
1361 data, data_size, data_length);
1362 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001363 /* This shouldn't happen in the reference implementation, but
1364 it is valid for a special-purpose implementation to omit
1365 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001367 }
1368}
1369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1371 uint8_t *data,
1372 size_t data_size,
1373 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001374{
1375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1376 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1377 psa_key_slot_t *slot;
1378
Ronald Crone907e552021-01-18 13:22:38 +01001379 /* Reject a zero-length output buffer now, since this can never be a
1380 * valid key representation. This way we know that data must be a valid
1381 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if (data_size == 0) {
1383 return PSA_ERROR_BUFFER_TOO_SMALL;
1384 }
Ronald Crone907e552021-01-18 13:22:38 +01001385
Ronald Cron9486f9d2020-11-26 13:36:23 +01001386 /* Set the key to empty now, so that even when there are errors, we always
1387 * set data_length to a value between 0 and data_size. On error, setting
1388 * the key to empty is a good choice because an empty key representation is
1389 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001390 *data_length = 0;
1391
Ronald Cron9486f9d2020-11-26 13:36:23 +01001392 /* Export requires the EXPORT flag. There is an exception for public keys,
1393 * which don't require any flag, but
1394 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1397 PSA_KEY_USAGE_EXPORT, 0);
1398 if (status != PSA_SUCCESS) {
1399 return status;
1400 }
mohammad160306e79202018-03-28 13:17:44 +03001401
Ronald Crond18b5f82020-11-25 16:46:05 +01001402 psa_key_attributes_t attributes = {
1403 .core = slot->attr
1404 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 status = psa_driver_wrapper_export_key(&attributes,
1406 slot->key.data, slot->key.bytes,
1407 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001412}
1413
Ronald Cron7285cda2020-11-26 14:46:37 +01001414psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001415 const psa_key_attributes_t *attributes,
1416 const uint8_t *key_buffer,
1417 size_t key_buffer_size,
1418 uint8_t *data,
1419 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001421{
Ronald Crond18b5f82020-11-25 16:46:05 +01001422 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001423
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001424 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001425 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1426 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001427 /* Exporting public -> public */
1428 return psa_export_key_buffer_internal(
1429 key_buffer, key_buffer_size,
1430 data, data_size, data_length);
1431 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001432#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001433 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1434 return mbedtls_psa_rsa_export_public_key(attributes,
1435 key_buffer,
1436 key_buffer_size,
1437 data,
1438 data_size,
1439 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001440#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001441 /* We don't know how to convert a private RSA key to public. */
1442 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001443#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1444 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001445 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001446#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001447 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1448 return mbedtls_psa_ecp_export_public_key(attributes,
1449 key_buffer,
1450 key_buffer_size,
1451 data,
1452 data_size,
1453 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001454#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001455 /* We don't know how to convert a private ECC key to public */
1456 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001457#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1458 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001459 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001460#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
1461 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001462 return mbedtls_psa_export_ffdh_public_key(attributes,
1463 key_buffer,
1464 key_buffer_size,
1465 data, data_size,
1466 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001467#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001468 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001469#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
1470 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001472 (void) key_buffer;
1473 (void) key_buffer_size;
1474 (void) data;
1475 (void) data_size;
1476 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001478 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001479}
Ronald Crond18b5f82020-11-25 16:46:05 +01001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1482 uint8_t *data,
1483 size_t data_size,
1484 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001485{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001486 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001487 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001488 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001489
Ronald Crone907e552021-01-18 13:22:38 +01001490 /* Reject a zero-length output buffer now, since this can never be a
1491 * valid key representation. This way we know that data must be a valid
1492 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (data_size == 0) {
1494 return PSA_ERROR_BUFFER_TOO_SMALL;
1495 }
Ronald Crone907e552021-01-18 13:22:38 +01001496
Darryl Greendd8fb772018-11-07 16:00:44 +00001497 /* Set the key to empty now, so that even when there are errors, we always
1498 * set data_length to a value between 0 and data_size. On error, setting
1499 * the key to empty is a good choice because an empty key representation is
1500 * unlikely to be accepted anywhere. */
1501 *data_length = 0;
1502
1503 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1505 if (status != PSA_SUCCESS) {
1506 return status;
1507 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1510 status = PSA_ERROR_INVALID_ARGUMENT;
1511 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001512 }
1513
Ronald Crond18b5f82020-11-25 16:46:05 +01001514 psa_key_attributes_t attributes = {
1515 .core = slot->attr
1516 };
Ronald Cron67227982020-11-26 15:16:05 +01001517 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001518 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001520
1521exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001525}
1526
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001527MBEDTLS_STATIC_ASSERT(
1528 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1529 "One or more key attribute flag is listed as both external-only and dual-use")
1530MBEDTLS_STATIC_ASSERT(
1531 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1532 "One or more key attribute flag is listed as both internal-only and dual-use")
1533MBEDTLS_STATIC_ASSERT(
1534 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1535 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001536
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001537/** Validate that a key policy is internally well-formed.
1538 *
1539 * This function only rejects invalid policies. It does not validate the
1540 * consistency of the policy with respect to other attributes of the key
1541 * such as the key type.
1542 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001543static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001544{
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1546 PSA_KEY_USAGE_COPY |
1547 PSA_KEY_USAGE_ENCRYPT |
1548 PSA_KEY_USAGE_DECRYPT |
1549 PSA_KEY_USAGE_SIGN_MESSAGE |
1550 PSA_KEY_USAGE_VERIFY_MESSAGE |
1551 PSA_KEY_USAGE_SIGN_HASH |
1552 PSA_KEY_USAGE_VERIFY_HASH |
1553 PSA_KEY_USAGE_VERIFY_DERIVATION |
1554 PSA_KEY_USAGE_DERIVE)) != 0) {
1555 return PSA_ERROR_INVALID_ARGUMENT;
1556 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001559}
1560
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001561/** Validate the internal consistency of key attributes.
1562 *
1563 * This function only rejects invalid attribute values. If does not
1564 * validate the consistency of the attributes with any key data that may
1565 * be involved in the creation of the key.
1566 *
1567 * Call this function early in the key creation process.
1568 *
1569 * \param[in] attributes Key attributes for the new key.
1570 * \param[out] p_drv On any return, the driver for the key, if any.
1571 * NULL for a transparent key.
1572 *
1573 */
1574static psa_status_t psa_validate_key_attributes(
1575 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001577{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001578 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1580 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 status = psa_validate_key_location(lifetime, p_drv);
1583 if (status != PSA_SUCCESS) {
1584 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001585 }
1586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 status = psa_validate_key_persistence(lifetime);
1588 if (status != PSA_SUCCESS) {
1589 return status;
1590 }
1591
1592 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1593 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1594 return PSA_ERROR_INVALID_ARGUMENT;
1595 }
1596 } else {
1597 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1598 return PSA_ERROR_INVALID_ARGUMENT;
1599 }
1600 }
1601
1602 status = psa_validate_key_policy(&attributes->core.policy);
1603 if (status != PSA_SUCCESS) {
1604 return status;
1605 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001606
1607 /* Refuse to create overly large keys.
1608 * Note that this doesn't trigger on import if the attributes don't
1609 * explicitly specify a size (so psa_get_key_bits returns 0), so
1610 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1612 return PSA_ERROR_NOT_SUPPORTED;
1613 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001614
Gilles Peskine91e8c332019-08-02 19:19:39 +02001615 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1617 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1618 return PSA_ERROR_INVALID_ARGUMENT;
1619 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001622}
1623
Gilles Peskine4747d192019-04-17 15:05:45 +02001624/** Prepare a key slot to receive key material.
1625 *
1626 * This function allocates a key slot and sets its metadata.
1627 *
1628 * If this function fails, call psa_fail_key_creation().
1629 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001630 * This function is intended to be used as follows:
1631 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001632 * it with the specified attributes, and in case of a volatile key assign it
1633 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001634 * -# Populate the slot with the key material.
1635 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1636 * In case of failure at any step, stop the sequence and call
1637 * psa_fail_key_creation().
1638 *
Ronald Cron5c522922020-11-14 16:35:34 +01001639 * On success, the key slot is locked. It is the responsibility of the caller
1640 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001641 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001642 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001643 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001644 * \param[out] p_slot On success, a pointer to the prepared slot.
1645 * \param[out] p_drv On any return, the driver for the key, if any.
1646 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001647 *
1648 * \retval #PSA_SUCCESS
1649 * The key slot is ready to receive key material.
1650 * \return If this function fails, the key slot is an invalid state.
1651 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 */
1653static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001654 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001656 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001658{
1659 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001660 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001661 psa_key_slot_t *slot;
1662
Gilles Peskinedf179142019-07-15 22:02:14 +02001663 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001664 *p_drv = NULL;
1665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 status = psa_validate_key_attributes(attributes, p_drv);
1667 if (status != PSA_SUCCESS) {
1668 return status;
1669 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1672 if (status != PSA_SUCCESS) {
1673 return status;
1674 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001675 slot = *p_slot;
1676
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001677 /* We're storing the declared bit-size of the key. It's up to each
1678 * creation mechanism to verify that this information is correct.
1679 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001680 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001681 * is optional (import, copy). In case of a volatile key, assign it the
1682 * volatile key identifier associated to the slot returned to contain its
1683 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001684
1685 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001687#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1688 slot->attr.id = volatile_key_id;
1689#else
1690 slot->attr.id.key_id = volatile_key_id;
1691#endif
1692 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001693
Gilles Peskine91e8c332019-08-02 19:19:39 +02001694 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001695 * external-only flags, query `attributes`. Thanks to the check
1696 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001697 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001698 * may have set. */
1699 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001700
Gilles Peskinecbaff462019-07-12 23:46:04 +02001701#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001702 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001703 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001704 * create the key file in internal storage, create the
1705 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001706 * persistent data. This is done by starting a transaction that will
1707 * encompass these three actions.
1708 * For registering a volatile key, we just need to find an appropriate
1709 * slot number inside the SE. Since the key is designated volatile, creating
1710 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001711 /* The first thing to do is to find a slot number for the new key.
1712 * We save the slot number in persistent storage as part of the
1713 * transaction data. It will be needed to recover if the power
1714 * fails during the key creation process, to clean up on the secure
1715 * element side after restarting. Obtaining a slot number from the
1716 * secure element driver updates its persistent state, but we do not yet
1717 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001718 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001720 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1722 &slot_number);
1723 if (status != PSA_SUCCESS) {
1724 return status;
1725 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1728 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001729 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001730 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001731 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 status = psa_crypto_save_transaction();
1733 if (status != PSA_SUCCESS) {
1734 (void) psa_crypto_stop_transaction();
1735 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001736 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001737 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001738
1739 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001741 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001744 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001746 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001747#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001750}
Gilles Peskine4747d192019-04-17 15:05:45 +02001751
1752/** Finalize the creation of a key once its key material has been set.
1753 *
1754 * This entails writing the key to persistent storage.
1755 *
1756 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001757 * See the documentation of psa_start_key_creation() for the intended use
1758 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 *
Ronald Cron5c522922020-11-14 16:35:34 +01001760 * If the finalization succeeds, the function unlocks the key slot (it was
1761 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1762 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001763 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001764 * \param[in,out] slot Pointer to the slot with key material.
1765 * \param[in] driver The secure element driver for the key,
1766 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001767 * \param[out] key On success, identifier of the key. Note that the
1768 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001769 *
1770 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001771 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001772 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1773 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1774 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1775 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1776 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1777 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001778 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001779 * \return If this function fails, the key slot is an invalid state.
1780 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001781 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001782static psa_status_t psa_finish_key_creation(
1783 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001784 psa_se_drv_table_entry_t *driver,
1785 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001786{
1787 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001788 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001789 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001790
1791#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001793#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001795 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001796 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001798
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001799 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1800 sizeof(data.slot_number),
1801 "Slot number size does not match psa_se_key_data_storage_t");
1802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1804 status = psa_save_persistent_key(&slot->attr,
1805 (uint8_t *) &data,
1806 sizeof(data));
1807 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001808#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1809 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001810 /* Key material is saved in export representation in the slot, so
1811 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 status = psa_save_persistent_key(&slot->attr,
1813 slot->key.data,
1814 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001815 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001816 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001817#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1818
Gilles Peskinecbaff462019-07-12 23:46:04 +02001819#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001820 /* Finish the transaction for a key creation. This does not
1821 * happen when registering an existing key. Detect this case
1822 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001823 * creation of a persistent key in a secure element requires a transaction,
1824 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 if (driver != NULL &&
1826 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1827 status = psa_save_se_persistent_data(driver);
1828 if (status != PSA_SUCCESS) {
1829 psa_destroy_persistent_key(slot->attr.id);
1830 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001831 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001833 }
1834#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001837 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 status = psa_unlock_key_slot(slot);
1839 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001840 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001842 }
Ronald Cron50972942020-11-14 11:28:25 +01001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001845}
1846
1847/** Abort the creation of a key.
1848 *
1849 * You may call this function after calling psa_start_key_creation(),
1850 * or after psa_finish_key_creation() fails. In other circumstances, this
1851 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001852 * See the documentation of psa_start_key_creation() for the intended use
1853 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001854 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001855 * \param[in,out] slot Pointer to the slot with key material.
1856 * \param[in] driver The secure element driver for the key,
1857 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001858 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001859static void psa_fail_key_creation(psa_key_slot_t *slot,
1860 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001861{
Gilles Peskine011e4282019-06-26 18:34:38 +02001862 (void) driver;
1863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001865 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001867
1868#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001869 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001870 * element, and the failure happened later (when saving metadata
1871 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001872 * element.
1873 * https://github.com/ARMmbed/mbed-crypto/issues/217
1874 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001875
Gilles Peskined7729582019-08-05 15:55:54 +02001876 /* Abort the ongoing transaction if any (there may not be one if
1877 * the creation process failed before starting one, or if the
1878 * key creation is a registration of a key in a secure element).
1879 * Earlier functions must already have done what it takes to undo any
1880 * partial creation. All that's left is to update the transaction data
1881 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001883#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001886}
1887
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001888/** Validate optional attributes during key creation.
1889 *
1890 * Some key attributes are optional during key creation. If they are
1891 * specified in the attributes structure, check that they are consistent
1892 * with the data in the slot.
1893 *
1894 * This function should be called near the end of key creation, after
1895 * the slot in memory is fully populated but before saving persistent data.
1896 */
1897static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900{
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (attributes->core.type != 0) {
1902 if (attributes->core.type != slot->attr.type) {
1903 return PSA_ERROR_INVALID_ARGUMENT;
1904 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001905 }
1906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001908#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1910 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001911 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001912 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001914
Ronald Cron90857082020-11-25 14:58:33 +01001915 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 slot->attr.type,
1917 slot->key.data,
1918 slot->key.bytes,
1919 &rsa);
1920 if (status != PSA_SUCCESS) {
1921 return status;
1922 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 mbedtls_mpi_init(&actual);
1925 mbedtls_mpi_init(&required);
1926 ret = mbedtls_rsa_export(rsa,
1927 NULL, NULL, NULL, NULL, &actual);
1928 mbedtls_rsa_free(rsa);
1929 mbedtls_free(rsa);
1930 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001931 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 }
1933 ret = mbedtls_mpi_read_binary(&required,
1934 attributes->domain_parameters,
1935 attributes->domain_parameters_size);
1936 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001937 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 }
1939 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 }
1942rsa_exit:
1943 mbedtls_mpi_free(&actual);
1944 mbedtls_mpi_free(&required);
1945 if (ret != 0) {
1946 return mbedtls_to_psa_error(ret);
1947 }
1948 } else
John Durkop9814fa22020-11-04 12:28:15 -08001949#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1950 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953 }
1954 }
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (attributes->core.bits != 0) {
1957 if (attributes->core.bits != slot->attr.bits) {
1958 return PSA_ERROR_INVALID_ARGUMENT;
1959 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001960 }
1961
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963}
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1966 const uint8_t *data,
1967 size_t data_length,
1968 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001969{
1970 psa_status_t status;
1971 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001972 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001973 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301974 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001975
Ronald Cron81709fc2020-11-14 12:10:32 +01001976 *key = MBEDTLS_SVC_KEY_ID_INIT;
1977
Gilles Peskine0f84d622019-09-12 19:03:13 +02001978 /* Reject zero-length symmetric keys (including raw data key objects).
1979 * This also rejects any key which might be encoded as an empty string,
1980 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 if (data_length == 0) {
1982 return PSA_ERROR_INVALID_ARGUMENT;
1983 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001984
Archana449608b2021-09-08 15:36:05 +05301985 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 if (data_length > SIZE_MAX / 8) {
1987 return PSA_ERROR_NOT_SUPPORTED;
1988 }
Archana6ed4bda2021-08-04 10:47:15 +05301989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1991 &slot, &driver);
1992 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001993 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001995
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001996 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301997 * storage ( thus not in the case of importing a key in a secure element
1998 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1999 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 if (slot->key.data == NULL) {
2001 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302002 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 attributes, data, data_length, &storage_size);
2004 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302005 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 }
Archanad8a83dc2021-06-14 10:04:16 +05302007 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 status = psa_allocate_buffer_to_slot(slot, storage_size);
2009 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002010 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002012 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002013
2014 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 status = psa_driver_wrapper_import_key(attributes,
2016 data, data_length,
2017 slot->key.data,
2018 slot->key.bytes,
2019 &slot->key.bytes, &bits);
2020 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002021 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002025 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002027 status = PSA_ERROR_INVALID_ARGUMENT;
2028 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002029 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002030
Archana6ed4bda2021-08-04 10:47:15 +05302031 /* Enforce a size limit, and in particular ensure that the bit
2032 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302034 status = PSA_ERROR_NOT_SUPPORTED;
2035 goto exit;
2036 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 status = psa_validate_optional_attributes(slot, attributes);
2038 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (status != PSA_SUCCESS) {
2045 psa_fail_key_creation(slot, driver);
2046 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002049}
2050
Gilles Peskined7729582019-08-05 15:55:54 +02002051#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2052psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002054{
2055 psa_status_t status;
2056 psa_key_slot_t *slot = NULL;
2057 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002058 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002059
2060 /* Leaving attributes unspecified is not currently supported.
2061 * It could make sense to query the key type and size from the
2062 * secure element, but not all secure elements support this
2063 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2065 return PSA_ERROR_NOT_SUPPORTED;
2066 }
2067 if (psa_get_key_bits(attributes) == 0) {
2068 return PSA_ERROR_NOT_SUPPORTED;
2069 }
Gilles Peskined7729582019-08-05 15:55:54 +02002070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2072 &slot, &driver);
2073 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002074 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 }
Gilles Peskined7729582019-08-05 15:55:54 +02002076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002078
2079exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (status != PSA_SUCCESS) {
2081 psa_fail_key_creation(slot, driver);
2082 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002083
Gilles Peskined7729582019-08-05 15:55:54 +02002084 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 psa_close_key(key);
2086 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002087}
2088#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2091 const psa_key_attributes_t *specified_attributes,
2092 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002093{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002095 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002096 psa_key_slot_t *source_slot = NULL;
2097 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002098 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002099 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302100 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002101
Ronald Cron81709fc2020-11-14 12:10:32 +01002102 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2103
Archana8a180362021-07-05 02:18:48 +05302104 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2106 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 status = psa_validate_optional_attributes(source_slot,
2111 specified_attributes);
2112 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002115
Archana9d17bf42021-09-10 06:22:44 +05302116 /* The target key type and number of bits have been validated by
2117 * psa_validate_optional_attributes() to be either equal to zero or
2118 * equal to the ones of the source key. So it is safe to inherit
2119 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302120 * */
Archana9d17bf42021-09-10 06:22:44 +05302121 actual_attributes.core.bits = source_slot->attr.bits;
2122 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302123
2124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 status = psa_restrict_key_policy(source_slot->attr.type,
2126 &actual_attributes.core.policy,
2127 &source_slot->attr.policy);
2128 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2133 &target_slot, &driver);
2134 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 }
2137 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2138 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302139 /*
Archana9d17bf42021-09-10 06:22:44 +05302140 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302141 * the source key would need to be exported as plaintext and re-imported
2142 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302143 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302144 * appropriate API invocations from the application, if needed.
2145 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002146 status = PSA_ERROR_NOT_SUPPORTED;
2147 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002148 }
Archana8a180362021-07-05 02:18:48 +05302149 /*
2150 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302151 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302152 * - For opaque keys this translates to an invocation of the drivers'
2153 * copy_key entry point through the dispatch layer.
2154 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2156 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2157 &storage_size);
2158 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302159 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 }
Archana374fe5b2021-09-08 15:50:28 +05302161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2163 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 }
Archana374fe5b2021-09-08 15:50:28 +05302166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 status = psa_driver_wrapper_copy_key(&actual_attributes,
2168 source_slot->key.data,
2169 source_slot->key.bytes,
2170 target_slot->key.data,
2171 target_slot->key.bytes,
2172 &target_slot->key.bytes);
2173 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 }
2176 } else {
2177 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302178 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 source_slot->key.bytes);
2180 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 }
Archana8a180362021-07-05 02:18:48 +05302183 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (status != PSA_SUCCESS) {
2187 psa_fail_key_creation(target_slot, driver);
2188 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002193}
2194
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002195
2196
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002197/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002198/* Message digests */
2199/****************************************************************/
2200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002203 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (operation->id == 0) {
2205 return PSA_SUCCESS;
2206 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002209 operation->id = 0;
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212}
2213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2215 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002218
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002219 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002221 status = PSA_ERROR_BAD_STATE;
2222 goto exit;
2223 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002226 status = PSA_ERROR_INVALID_ARGUMENT;
2227 goto exit;
2228 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002229
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002230 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2231 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002235
2236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (status != PSA_SUCCESS) {
2238 psa_hash_abort(operation);
2239 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002240
2241 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242}
2243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2245 const uint8_t *input,
2246 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002248 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002251 status = PSA_ERROR_BAD_STATE;
2252 goto exit;
2253 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002254
Steven Cooremanc8288352021-03-04 14:02:19 +01002255 /* Don't require hash implementations to behave correctly on a
2256 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if (input_length == 0) {
2258 return PSA_SUCCESS;
2259 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002262
2263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (status != PSA_SUCCESS) {
2265 psa_hash_abort(operation);
2266 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269}
2270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2272 uint8_t *hash,
2273 size_t hash_size,
2274 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002276 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 if (operation->id == 0) {
2278 return PSA_ERROR_BAD_STATE;
2279 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280
Steven Cooreman1e582352021-02-18 17:24:37 +01002281 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 operation, hash, hash_size, hash_length);
2283 psa_hash_abort(operation);
2284 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285}
2286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2288 const uint8_t *hash,
2289 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290{
Ronald Cron69a63422021-10-18 09:47:58 +02002291 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002293 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 operation,
2295 actual_hash, sizeof(actual_hash),
2296 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002299 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002303 status = PSA_ERROR_INVALID_SIGNATURE;
2304 goto exit;
2305 }
2306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002308 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002310
2311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2313 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002314 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318}
2319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320psa_status_t psa_hash_compute(psa_algorithm_t alg,
2321 const uint8_t *input, size_t input_length,
2322 uint8_t *hash, size_t hash_size,
2323 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002324{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002325 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (!PSA_ALG_IS_HASH(alg)) {
2327 return PSA_ERROR_INVALID_ARGUMENT;
2328 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2331 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002332}
2333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334psa_status_t psa_hash_compare(psa_algorithm_t alg,
2335 const uint8_t *input, size_t input_length,
2336 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002337{
Ronald Cron69a63422021-10-18 09:47:58 +02002338 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002339 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (!PSA_ALG_IS_HASH(alg)) {
2342 return PSA_ERROR_INVALID_ARGUMENT;
2343 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002344
Steven Cooreman1e582352021-02-18 17:24:37 +01002345 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 alg, input, input_length,
2347 actual_hash, sizeof(actual_hash),
2348 &actual_hash_length);
2349 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002350 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 }
2352 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002353 status = PSA_ERROR_INVALID_SIGNATURE;
2354 goto exit;
2355 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002357 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002359
2360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2362 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002363}
2364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2366 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002367{
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 if (source_operation->id == 0 ||
2369 target_operation->id != 0) {
2370 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002371 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2374 target_operation);
2375 if (status != PSA_SUCCESS) {
2376 psa_hash_abort(target_operation);
2377 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002380}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381
2382
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002383/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002384/* MAC */
2385/****************************************************************/
2386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002388{
Steven Cooremane6804192021-03-19 18:28:56 +01002389 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (operation->id == 0) {
2391 return PSA_SUCCESS;
2392 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002395 operation->mac_size = 0;
2396 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002397 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002400}
2401
Ronald Cron2dff3b22021-06-17 16:33:22 +02002402static psa_status_t psa_mac_finalize_alg_and_key_validation(
2403 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002404 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002406{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 psa_key_type_t key_type = psa_get_key_type(attributes);
2409 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (!PSA_ALG_IS_MAC(alg)) {
2412 return PSA_ERROR_INVALID_ARGUMENT;
2413 }
Steven Cooremane6804192021-03-19 18:28:56 +01002414
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002415 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 status = psa_mac_key_can_do(alg, key_type);
2417 if (status != PSA_SUCCESS) {
2418 return status;
2419 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002420
Steven Cooremand1a68f12021-05-10 11:13:41 +02002421 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002425 /* A very short MAC is too short for security since it can be
2426 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2427 * so we make this our minimum, even though 32 bits is still
2428 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002430 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2433 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002434 /* It's impossible to "truncate" to a larger length than the full length
2435 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002437 }
2438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002440 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2441 * that is disabled in the compile-time configuration. The result can
2442 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2443 * configuration into account. In this case, force a return of
2444 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2445 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2446 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2447 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2448 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002450 }
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002453}
2454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2456 mbedtls_svc_key_id_t key,
2457 psa_algorithm_t alg,
2458 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002459{
2460 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2461 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002462 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002463
2464 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002466 status = PSA_ERROR_BAD_STATE;
2467 goto exit;
2468 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002469
2470 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 key,
2472 &slot,
2473 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2474 alg);
2475 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002476 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478
2479 psa_key_attributes_t attributes = {
2480 .core = slot->attr
2481 };
2482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2484 &operation->mac_size);
2485 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488
2489 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002490 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (is_sign) {
2492 status = psa_driver_wrapper_mac_sign_setup(operation,
2493 &attributes,
2494 slot->key.data,
2495 slot->key.bytes,
2496 alg);
2497 } else {
2498 status = psa_driver_wrapper_mac_verify_setup(operation,
2499 &attributes,
2500 slot->key.data,
2501 slot->key.bytes,
2502 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002504
Gilles Peskinefbfac682018-07-08 20:51:54 +02002505exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (status != PSA_SUCCESS) {
2507 psa_mac_abort(operation);
2508 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513}
2514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2516 mbedtls_svc_key_id_t key,
2517 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002518{
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002520}
2521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2523 mbedtls_svc_key_id_t key,
2524 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002525{
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002527}
2528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2530 const uint8_t *input,
2531 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532{
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 if (operation->id == 0) {
2534 return PSA_ERROR_BAD_STATE;
2535 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002537 /* Don't require hash implementations to behave correctly on a
2538 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (input_length == 0) {
2540 return PSA_SUCCESS;
2541 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2544 input, input_length);
2545 if (status != PSA_SUCCESS) {
2546 psa_mac_abort(operation);
2547 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550}
2551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2553 uint8_t *mac,
2554 size_t mac_size,
2555 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002556{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2558 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002561 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002562 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002563 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002566 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002567 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002568 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002569
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002570 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2571 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002573 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002574 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002575 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002578 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002579 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002580 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 status = psa_driver_wrapper_mac_sign_finish(operation,
2583 mac, operation->mac_size,
2584 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002585
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002586exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002587 /* In case of success, set the potential excess room in the output buffer
2588 * to an invalid value, to avoid potentially leaking a longer MAC.
2589 * In case of error, set the output length and content to a safe default,
2590 * such that in case the caller misses an error check, the output would be
2591 * an unachievable MAC.
2592 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002594 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002595 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002596 }
mohammad16036df908f2018-04-02 08:34:15 -07002597
Paul Elliottdc42ca82023-02-24 18:11:59 +00002598 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002603}
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2606 const uint8_t *mac,
2607 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002609 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2610 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002613 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002614 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002615 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002618 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002619 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002620 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002623 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002624 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002625 }
2626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 status = psa_driver_wrapper_mac_verify_finish(operation,
2628 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002629
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002630exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002634}
2635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2637 psa_algorithm_t alg,
2638 const uint8_t *input,
2639 size_t input_length,
2640 uint8_t *mac,
2641 size_t mac_size,
2642 size_t *mac_length,
2643 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002644{
2645 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002646 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2647 psa_key_slot_t *slot;
2648 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002649
Ronald Cron51131b52021-06-17 17:17:20 +02002650 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 key,
2652 &slot,
2653 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2654 alg);
2655 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002656 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002658
Ronald Cron51131b52021-06-17 17:17:20 +02002659 psa_key_attributes_t attributes = {
2660 .core = slot->attr
2661 };
2662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2664 &operation_mac_size);
2665 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002670 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002671 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002672 }
2673
2674 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 &attributes,
2676 slot->key.data, slot->key.bytes,
2677 alg,
2678 input, input_length,
2679 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002680
2681exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002682 /* In case of success, set the potential excess room in the output buffer
2683 * to an invalid value, to avoid potentially leaking a longer MAC.
2684 * In case of error, set the output length and content to a safe default,
2685 * such that in case the caller misses an error check, the output would be
2686 * an unachievable MAC.
2687 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002689 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002690 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002691 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002692
2693 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002698}
2699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002701 psa_algorithm_t alg,
2702 const uint8_t *input,
2703 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 uint8_t *mac,
2705 size_t mac_size,
2706 size_t *mac_length)
2707{
2708 return psa_mac_compute_internal(key, alg,
2709 input, input_length,
2710 mac, mac_size, mac_length, 1);
2711}
2712
2713psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2714 psa_algorithm_t alg,
2715 const uint8_t *input,
2716 size_t input_length,
2717 const uint8_t *mac,
2718 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002719{
2720 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002721 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2722 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 status = psa_mac_compute_internal(key, alg,
2725 input, input_length,
2726 actual_mac, sizeof(actual_mac),
2727 &actual_mac_length, 0);
2728 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002729 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002733 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002734 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002735 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002737 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002738 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002739 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002740
2741exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002745}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002746
Gilles Peskinee59236f2018-01-27 23:32:46 +01002747/****************************************************************/
2748/* Asymmetric cryptography */
2749/****************************************************************/
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2752 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002753{
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 if (input_is_message) {
2755 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2756 return PSA_ERROR_INVALID_ARGUMENT;
2757 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2760 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2761 return PSA_ERROR_INVALID_ARGUMENT;
2762 }
2763 }
2764 } else {
2765 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2766 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002767 }
2768 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002771}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2774 int input_is_message,
2775 psa_algorithm_t alg,
2776 const uint8_t *input,
2777 size_t input_length,
2778 uint8_t *signature,
2779 size_t signature_size,
2780 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002781{
2782 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2783 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2784 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002785
2786 *signature_length = 0;
2787
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 status = psa_sign_verify_check_alg(input_is_message, alg);
2789 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002790 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002792
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002793 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002794 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002795 * buffer can in principle be empty since it doesn't actually have
2796 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 if (signature_size == 0) {
2798 return PSA_ERROR_BUFFER_TOO_SMALL;
2799 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002801 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 key, &slot,
2803 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2804 PSA_KEY_USAGE_SIGN_HASH,
2805 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002808 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002812 status = PSA_ERROR_INVALID_ARGUMENT;
2813 goto exit;
2814 }
2815
2816 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002818 };
2819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002821 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002822 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002823 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 signature, signature_size, signature_length);
2825 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002826
2827 status = psa_driver_wrapper_sign_hash(
2828 &attributes, slot->key.data, slot->key.bytes,
2829 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002831 }
2832
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002833
2834exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002835 psa_wipe_tag_output_buffer(signature, status, signature_size,
2836 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002841}
2842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2844 int input_is_message,
2845 psa_algorithm_t alg,
2846 const uint8_t *input,
2847 size_t input_length,
2848 const uint8_t *signature,
2849 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002850{
2851 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2852 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2853 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 status = psa_sign_verify_check_alg(input_is_message, alg);
2856 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002857 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002859
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002860 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 key, &slot,
2862 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2863 PSA_KEY_USAGE_VERIFY_HASH,
2864 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (status != PSA_SUCCESS) {
2867 return status;
2868 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002869
2870 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002872 };
2873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002875 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002876 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002877 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 signature, signature_length);
2879 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002880 status = psa_driver_wrapper_verify_hash(
2881 &attributes, slot->key.data, slot->key.bytes,
2882 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002884 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002889
2890}
2891
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002892psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002893 const psa_key_attributes_t *attributes,
2894 const uint8_t *key_buffer,
2895 size_t key_buffer_size,
2896 psa_algorithm_t alg,
2897 const uint8_t *input,
2898 size_t input_length,
2899 uint8_t *signature,
2900 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002902{
2903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002906 size_t hash_length;
2907 uint8_t hash[PSA_HASH_MAX_SIZE];
2908
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002909 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 PSA_ALG_SIGN_GET_HASH(alg),
2911 input, input_length,
2912 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002915 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002917
2918 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 attributes, key_buffer, key_buffer_size,
2920 alg, hash, hash_length,
2921 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002922 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002925}
2926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2928 psa_algorithm_t alg,
2929 const uint8_t *input,
2930 size_t input_length,
2931 uint8_t *signature,
2932 size_t signature_size,
2933 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002934{
2935 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002936 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002938}
2939
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002940psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002941 const psa_key_attributes_t *attributes,
2942 const uint8_t *key_buffer,
2943 size_t key_buffer_size,
2944 psa_algorithm_t alg,
2945 const uint8_t *input,
2946 size_t input_length,
2947 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002949{
2950 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002951
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002953 size_t hash_length;
2954 uint8_t hash[PSA_HASH_MAX_SIZE];
2955
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002956 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 PSA_ALG_SIGN_GET_HASH(alg),
2958 input, input_length,
2959 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002962 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002964
2965 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 attributes, key_buffer, key_buffer_size,
2967 alg, hash, hash_length,
2968 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002969 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002972}
2973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2975 psa_algorithm_t alg,
2976 const uint8_t *input,
2977 size_t input_length,
2978 const uint8_t *signature,
2979 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002980{
2981 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002982 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002984}
2985
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002986psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002987 const psa_key_attributes_t *attributes,
2988 const uint8_t *key_buffer, size_t key_buffer_size,
2989 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002991{
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2993 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2994 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002995#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2997 return mbedtls_psa_rsa_sign_hash(
2998 attributes,
2999 key_buffer, key_buffer_size,
3000 alg, hash, hash_length,
3001 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003002#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3003 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 } else {
3005 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003006 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3008 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003009#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3011 return mbedtls_psa_ecdsa_sign_hash(
3012 attributes,
3013 key_buffer, key_buffer_size,
3014 alg, hash, hash_length,
3015 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003016#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3017 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 } else {
3019 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003020 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003021 }
Ronald Cron4501c982021-03-19 20:09:32 +01003022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 (void) key_buffer;
3024 (void) key_buffer_size;
3025 (void) hash;
3026 (void) hash_length;
3027 (void) signature;
3028 (void) signature_size;
3029 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003032}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3035 psa_algorithm_t alg,
3036 const uint8_t *hash,
3037 size_t hash_length,
3038 uint8_t *signature,
3039 size_t signature_size,
3040 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003041{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003042 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003043 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003045}
3046
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003047psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003048 const psa_key_attributes_t *attributes,
3049 const uint8_t *key_buffer, size_t key_buffer_size,
3050 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003052{
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3054 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3055 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003056#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3058 return mbedtls_psa_rsa_verify_hash(
3059 attributes,
3060 key_buffer, key_buffer_size,
3061 alg, hash, hash_length,
3062 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003063#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3064 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 } else {
3066 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003067 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3069 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003070#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3072 return mbedtls_psa_ecdsa_verify_hash(
3073 attributes,
3074 key_buffer, key_buffer_size,
3075 alg, hash, hash_length,
3076 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003077#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3078 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 } else {
3080 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003081 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003082 }
Ronald Cron4501c982021-03-19 20:09:32 +01003083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 (void) key_buffer;
3085 (void) key_buffer_size;
3086 (void) hash;
3087 (void) hash_length;
3088 (void) signature;
3089 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003092}
3093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3095 psa_algorithm_t alg,
3096 const uint8_t *hash,
3097 size_t hash_length,
3098 const uint8_t *signature,
3099 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003100{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003101 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003102 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003104}
3105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3107 psa_algorithm_t alg,
3108 const uint8_t *input,
3109 size_t input_length,
3110 const uint8_t *salt,
3111 size_t salt_length,
3112 uint8_t *output,
3113 size_t output_size,
3114 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003116 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003117 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003118 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003119
Darryl Green5cc689a2018-07-24 15:34:10 +01003120 (void) input;
3121 (void) input_length;
3122 (void) salt;
3123 (void) output;
3124 (void) output_size;
3125
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003126 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003127
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3129 return PSA_ERROR_INVALID_ARGUMENT;
3130 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003131
Ronald Cron5c522922020-11-14 16:35:34 +01003132 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3134 if (status != PSA_SUCCESS) {
3135 return status;
3136 }
3137 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3138 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003139 status = PSA_ERROR_INVALID_ARGUMENT;
3140 goto exit;
3141 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003143 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003145 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003146
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003147 status = psa_driver_wrapper_asymmetric_encrypt(
3148 &attributes, slot->key.data, slot->key.bytes,
3149 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003153
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003155}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3158 psa_algorithm_t alg,
3159 const uint8_t *input,
3160 size_t input_length,
3161 const uint8_t *salt,
3162 size_t salt_length,
3163 uint8_t *output,
3164 size_t output_size,
3165 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003168 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003169 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003170
Darryl Green5cc689a2018-07-24 15:34:10 +01003171 (void) input;
3172 (void) input_length;
3173 (void) salt;
3174 (void) output;
3175 (void) output_size;
3176
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003177 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3180 return PSA_ERROR_INVALID_ARGUMENT;
3181 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003182
Ronald Cron5c522922020-11-14 16:35:34 +01003183 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3185 if (status != PSA_SUCCESS) {
3186 return status;
3187 }
3188 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003189 status = PSA_ERROR_INVALID_ARGUMENT;
3190 goto exit;
3191 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003193 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003195 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003197 status = psa_driver_wrapper_asymmetric_decrypt(
3198 &attributes, slot->key.data, slot->key.bytes,
3199 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003201
3202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003206}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003207
Paul Elliott9fe12f62022-11-30 19:16:02 +00003208/****************************************************************/
3209/* Asymmetric interruptible cryptography */
3210/****************************************************************/
3211
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003212static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3213
Paul Elliott9fe12f62022-11-30 19:16:02 +00003214void psa_interruptible_set_max_ops(uint32_t max_ops)
3215{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003216 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003217}
3218
3219uint32_t psa_interruptible_get_max_ops(void)
3220{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003221 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003222}
3223
Paul Elliott9fe12f62022-11-30 19:16:02 +00003224uint32_t psa_sign_hash_get_num_ops(
3225 const psa_sign_hash_interruptible_operation_t *operation)
3226{
Paul Elliott296ede92022-12-15 17:00:30 +00003227 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003228}
3229
3230uint32_t psa_verify_hash_get_num_ops(
3231 const psa_verify_hash_interruptible_operation_t *operation)
3232{
Paul Elliott296ede92022-12-15 17:00:30 +00003233 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003234}
3235
Paul Elliott59ad9452022-12-18 15:09:02 +00003236static psa_status_t psa_sign_hash_abort_internal(
3237 psa_sign_hash_interruptible_operation_t *operation)
3238{
3239 if (operation->id == 0) {
3240 /* The object has (apparently) been initialized but it is not (yet)
3241 * in use. It's ok to call abort on such an object, and there's
3242 * nothing to do. */
3243 return PSA_SUCCESS;
3244 }
3245
3246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3247
3248 status = psa_driver_wrapper_sign_hash_abort(operation);
3249
3250 operation->id = 0;
3251
Paul Elliotte6145dc2023-02-07 12:51:21 +00003252 /* Do not clear either the error_occurred or num_ops elements here as they
3253 * only want to be cleared by the application calling abort, not by abort
3254 * being called at completion of an operation. */
3255
Paul Elliott59ad9452022-12-18 15:09:02 +00003256 return status;
3257}
3258
Paul Elliott9fe12f62022-11-30 19:16:02 +00003259psa_status_t psa_sign_hash_start(
3260 psa_sign_hash_interruptible_operation_t *operation,
3261 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3262 const uint8_t *hash, size_t hash_length)
3263{
3264 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3265 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3266 psa_key_slot_t *slot;
3267
Paul Elliottc9774412023-02-06 15:14:07 +00003268 /* Check that start has not been previously called, or operation has not
3269 * previously errored. */
3270 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003271 return PSA_ERROR_BAD_STATE;
3272 }
3273
Paul Elliott9fe12f62022-11-30 19:16:02 +00003274 status = psa_sign_verify_check_alg(0, alg);
3275 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003276 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003277 return status;
3278 }
3279
3280 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3281 PSA_KEY_USAGE_SIGN_HASH,
3282 alg);
3283
3284 if (status != PSA_SUCCESS) {
3285 goto exit;
3286 }
3287
3288 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3289 status = PSA_ERROR_INVALID_ARGUMENT;
3290 goto exit;
3291 }
3292
3293 psa_key_attributes_t attributes = {
3294 .core = slot->attr
3295 };
3296
Paul Elliott296ede92022-12-15 17:00:30 +00003297 /* Ensure ops count gets reset, in case of operation re-use. */
3298 operation->num_ops = 0;
3299
Paul Elliott9fe12f62022-11-30 19:16:02 +00003300 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3301 slot->key.data,
3302 slot->key.bytes, alg,
3303 hash, hash_length);
3304exit:
3305
3306 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003307 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003308 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003309 }
3310
3311 unlock_status = psa_unlock_key_slot(slot);
3312
Paul Elliottc9774412023-02-06 15:14:07 +00003313 if (unlock_status != PSA_SUCCESS) {
3314 operation->error_occurred = 1;
3315 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003316
Paul Elliottc9774412023-02-06 15:14:07 +00003317 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003318}
3319
3320
3321psa_status_t psa_sign_hash_complete(
3322 psa_sign_hash_interruptible_operation_t *operation,
3323 uint8_t *signature, size_t signature_size,
3324 size_t *signature_length)
3325{
3326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3327
3328 *signature_length = 0;
3329
Paul Elliottc9774412023-02-06 15:14:07 +00003330 /* Check that start has been called first, and that operation has not
3331 * previously errored. */
3332 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003333 status = PSA_ERROR_BAD_STATE;
3334 goto exit;
3335 }
3336
Paul Elliott096abc42023-02-03 18:33:23 +00003337 /* Immediately reject a zero-length signature buffer. This guarantees that
3338 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003339 if (signature_size == 0) {
3340 status = PSA_ERROR_BUFFER_TOO_SMALL;
3341 goto exit;
3342 }
3343
3344 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3345 signature_size,
3346 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003347
Paul Elliott296ede92022-12-15 17:00:30 +00003348 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003349 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003350
Paul Elliottebe225c2023-02-10 14:32:53 +00003351exit:
3352
Paul Elliott59200a22023-02-21 15:07:40 +00003353 psa_wipe_tag_output_buffer(signature, status, signature_size,
3354 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003355
Paul Elliott53bb3122023-02-10 14:22:22 +00003356 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003357 if (status != PSA_SUCCESS) {
3358 operation->error_occurred = 1;
3359 }
3360
Paul Elliott59ad9452022-12-18 15:09:02 +00003361 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003362 }
3363
3364 return status;
3365}
3366
3367psa_status_t psa_sign_hash_abort(
3368 psa_sign_hash_interruptible_operation_t *operation)
3369{
Paul Elliott59ad9452022-12-18 15:09:02 +00003370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3371
3372 status = psa_sign_hash_abort_internal(operation);
3373
3374 /* We clear the number of ops done here, so that it is not cleared when
3375 * the operation fails or succeeds, only on manual abort. */
3376 operation->num_ops = 0;
3377
Paul Elliottc9774412023-02-06 15:14:07 +00003378 /* Likewise, failure state. */
3379 operation->error_occurred = 0;
3380
Paul Elliott59ad9452022-12-18 15:09:02 +00003381 return status;
3382}
3383
3384static psa_status_t psa_verify_hash_abort_internal(
3385 psa_verify_hash_interruptible_operation_t *operation)
3386{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003387 if (operation->id == 0) {
3388 /* The object has (apparently) been initialized but it is not (yet)
3389 * in use. It's ok to call abort on such an object, and there's
3390 * nothing to do. */
3391 return PSA_SUCCESS;
3392 }
3393
Paul Elliott59ad9452022-12-18 15:09:02 +00003394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3395
3396 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003397
3398 operation->id = 0;
3399
Paul Elliotte6145dc2023-02-07 12:51:21 +00003400 /* Do not clear either the error_occurred or num_ops elements here as they
3401 * only want to be cleared by the application calling abort, not by abort
3402 * being called at completion of an operation. */
3403
Paul Elliott59ad9452022-12-18 15:09:02 +00003404 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003405}
3406
3407psa_status_t psa_verify_hash_start(
3408 psa_verify_hash_interruptible_operation_t *operation,
3409 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3410 const uint8_t *hash, size_t hash_length,
3411 const uint8_t *signature, size_t signature_length)
3412{
3413 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3414 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3415 psa_key_slot_t *slot;
3416
Paul Elliottc9774412023-02-06 15:14:07 +00003417 /* Check that start has not been previously called, or operation has not
3418 * previously errored. */
3419 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420 return PSA_ERROR_BAD_STATE;
3421 }
3422
3423 status = psa_sign_verify_check_alg(0, alg);
3424 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003425 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003426 return status;
3427 }
3428
3429 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3430 PSA_KEY_USAGE_VERIFY_HASH,
3431 alg);
3432
3433 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003434 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003435 return status;
3436 }
3437
3438 psa_key_attributes_t attributes = {
3439 .core = slot->attr
3440 };
3441
Paul Elliott296ede92022-12-15 17:00:30 +00003442 /* Ensure ops count gets reset, in case of operation re-use. */
3443 operation->num_ops = 0;
3444
Paul Elliott9fe12f62022-11-30 19:16:02 +00003445 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3446 slot->key.data,
3447 slot->key.bytes,
3448 alg, hash, hash_length,
3449 signature, signature_length);
3450
3451 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003452 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003453 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003454 }
3455
3456 unlock_status = psa_unlock_key_slot(slot);
3457
Paul Elliottc9774412023-02-06 15:14:07 +00003458 if (unlock_status != PSA_SUCCESS) {
3459 operation->error_occurred = 1;
3460 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461
Paul Elliottc9774412023-02-06 15:14:07 +00003462 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003463}
3464
3465psa_status_t psa_verify_hash_complete(
3466 psa_verify_hash_interruptible_operation_t *operation)
3467{
3468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3469
Paul Elliottc9774412023-02-06 15:14:07 +00003470 /* Check that start has been called first, and that operation has not
3471 * previously errored. */
3472 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003473 status = PSA_ERROR_BAD_STATE;
3474 goto exit;
3475 }
3476
3477 status = psa_driver_wrapper_verify_hash_complete(operation);
3478
Paul Elliott296ede92022-12-15 17:00:30 +00003479 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003480 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003481 operation);
3482
Paul Elliottebe225c2023-02-10 14:32:53 +00003483exit:
3484
Paul Elliott9fe12f62022-11-30 19:16:02 +00003485 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003486 if (status != PSA_SUCCESS) {
3487 operation->error_occurred = 1;
3488 }
3489
Paul Elliott59ad9452022-12-18 15:09:02 +00003490 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003491 }
3492
3493 return status;
3494}
3495
3496psa_status_t psa_verify_hash_abort(
3497 psa_verify_hash_interruptible_operation_t *operation)
3498{
Paul Elliott59ad9452022-12-18 15:09:02 +00003499 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003500
Paul Elliott59ad9452022-12-18 15:09:02 +00003501 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003502
Paul Elliott59ad9452022-12-18 15:09:02 +00003503 /* We clear the number of ops done here, so that it is not cleared when
3504 * the operation fails or succeeds, only on manual abort. */
3505 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003506
Paul Elliottc9774412023-02-06 15:14:07 +00003507 /* Likewise, failure state. */
3508 operation->error_occurred = 0;
3509
Paul Elliott59ad9452022-12-18 15:09:02 +00003510 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003511}
3512
Paul Elliott588f8ed2022-12-02 18:10:26 +00003513/****************************************************************/
3514/* Asymmetric interruptible cryptography internal */
3515/* implementations */
3516/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003517
Paul Elliott588f8ed2022-12-02 18:10:26 +00003518void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3519{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003520
Paul Elliott588f8ed2022-12-02 18:10:26 +00003521#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3522 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3523 defined(MBEDTLS_ECP_RESTARTABLE)
3524
3525 /* Internal implementation uses zero to indicate infinite number max ops,
3526 * therefore avoid this value, and set to minimum possible. */
3527 if (max_ops == 0) {
3528 max_ops = 1;
3529 }
3530
Paul Elliott588f8ed2022-12-02 18:10:26 +00003531 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003532#else
3533 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003534#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3535 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3536 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3537}
3538
Paul Elliott588f8ed2022-12-02 18:10:26 +00003539uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003540 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003541{
3542#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3543 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3544 defined(MBEDTLS_ECP_RESTARTABLE)
3545
Paul Elliott93d9ca82023-02-15 18:14:21 +00003546 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003547#else
3548 (void) operation;
3549 return 0;
3550#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3551 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3552 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3553}
3554
3555uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003556 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003557{
3558 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3559 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3560 defined(MBEDTLS_ECP_RESTARTABLE)
3561
Paul Elliott93d9ca82023-02-15 18:14:21 +00003562 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003563#else
3564 (void) operation;
3565 return 0;
3566#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3567 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3568 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3569}
3570
3571psa_status_t mbedtls_psa_sign_hash_start(
3572 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3573 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3574 size_t key_buffer_size, psa_algorithm_t alg,
3575 const uint8_t *hash, size_t hash_length)
3576{
3577 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003578 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003579
Paul Elliott068fe072023-01-16 13:59:15 +00003580 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3581 return PSA_ERROR_NOT_SUPPORTED;
3582 }
3583
3584 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003585 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003586 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587
3588#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003589 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3590 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003591
Paul Elliotta1c94092023-02-15 16:38:04 +00003592 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3593
Paul Elliott93d9ca82023-02-15 18:14:21 +00003594 /* Ensure num_ops is zero'ed in case of context re-use. */
3595 operation->num_ops = 0;
3596
Paul Elliott068fe072023-01-16 13:59:15 +00003597 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3598 attributes->core.bits,
3599 key_buffer,
3600 key_buffer_size,
3601 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602
Paul Elliott068fe072023-01-16 13:59:15 +00003603 if (status != PSA_SUCCESS) {
3604 return status;
3605 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003606
Paul Elliott1bc59df2023-02-05 13:41:57 +00003607 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003608 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609
Paul Elliott068fe072023-01-16 13:59:15 +00003610 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003611 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003612 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003613
Paul Elliott0290a762023-02-09 14:30:24 +00003614 /* We only need to store the same length of hash as the private key size
3615 * here, it would be truncated by the internal implementation anyway. */
3616 required_hash_length = (hash_length < operation->coordinate_bytes ?
3617 hash_length : operation->coordinate_bytes);
3618
Paul Elliottba70ad42023-02-15 18:23:53 +00003619 if (required_hash_length > sizeof(operation->hash)) {
3620 /* Shouldn't happen, but better safe than sorry. */
3621 return PSA_ERROR_CORRUPTION_DETECTED;
3622 }
3623
Paul Elliott0290a762023-02-09 14:30:24 +00003624 memcpy(operation->hash, hash, required_hash_length);
3625 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003626
3627 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003628
3629#else
Paul Elliott068fe072023-01-16 13:59:15 +00003630 (void) operation;
3631 (void) key_buffer;
3632 (void) key_buffer_size;
3633 (void) alg;
3634 (void) hash;
3635 (void) hash_length;
3636 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003637 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638
Paul Elliott068fe072023-01-16 13:59:15 +00003639 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3641 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3642 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643}
3644
3645psa_status_t mbedtls_psa_sign_hash_complete(
3646 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3647 uint8_t *signature, size_t signature_size,
3648 size_t *signature_length)
3649{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003650#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3651 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3652 defined(MBEDTLS_ECP_RESTARTABLE)
3653
Paul Elliotta1c94092023-02-15 16:38:04 +00003654 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003655 mbedtls_mpi r;
3656 mbedtls_mpi s;
3657
Paul Elliott46845252023-02-03 14:59:11 +00003658 mbedtls_mpi_init(&r);
3659 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003661 /* Ensure max_ops is set to the current value (or default). */
3662 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3663
Paul Elliotta1c94092023-02-15 16:38:04 +00003664 if (signature_size < 2 * operation->coordinate_bytes) {
3665 status = PSA_ERROR_BUFFER_TOO_SMALL;
3666 goto exit;
3667 }
3668
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003670
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3672 status = mbedtls_to_psa_error(
3673 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003674 &r,
3675 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676 &operation->ctx->d,
3677 operation->hash,
3678 operation->hash_length,
3679 operation->md_alg,
3680 mbedtls_psa_get_random,
3681 MBEDTLS_PSA_RANDOM_STATE,
3682 &operation->restart_ctx));
3683#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003684 status = PSA_ERROR_NOT_SUPPORTED;
3685 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3687 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688 status = mbedtls_to_psa_error(
3689 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003690 &r,
3691 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692 &operation->ctx->d,
3693 operation->hash,
3694 operation->hash_length,
3695 mbedtls_psa_get_random,
3696 MBEDTLS_PSA_RANDOM_STATE,
3697 mbedtls_psa_get_random,
3698 MBEDTLS_PSA_RANDOM_STATE,
3699 &operation->restart_ctx));
3700 }
3701
Paul Elliottf8e5b562023-02-19 18:43:45 +00003702 /* Hide the fact that the restart context only holds a delta of number of
3703 * ops done during the last operation, not an absolute value. */
3704 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3705
Paul Elliott724bd252023-02-08 12:35:08 +00003706 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003707 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003708 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003710 operation->coordinate_bytes)
3711 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712
3713 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003714 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715 }
3716
3717 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003718 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003720 operation->coordinate_bytes,
3721 operation->coordinate_bytes)
3722 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003723
3724 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003725 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003726 }
3727
Paul Elliott1bc59df2023-02-05 13:41:57 +00003728 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729
Paul Elliott724bd252023-02-08 12:35:08 +00003730 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731 }
Paul Elliott724bd252023-02-08 12:35:08 +00003732
3733exit:
3734
3735 mbedtls_mpi_free(&r);
3736 mbedtls_mpi_free(&s);
3737 return status;
3738
Paul Elliott588f8ed2022-12-02 18:10:26 +00003739 #else
3740
3741 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003742 (void) signature;
3743 (void) signature_size;
3744 (void) signature_length;
3745
3746 return PSA_ERROR_NOT_SUPPORTED;
3747
3748#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3749 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3750 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3751}
3752
3753psa_status_t mbedtls_psa_sign_hash_abort(
3754 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3755{
3756
3757#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3758 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3759 defined(MBEDTLS_ECP_RESTARTABLE)
3760
3761 if (operation->ctx) {
3762 mbedtls_ecdsa_free(operation->ctx);
3763 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003764 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765 }
3766
3767 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3768
Paul Elliott93d9ca82023-02-15 18:14:21 +00003769 operation->num_ops = 0;
3770
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771 return PSA_SUCCESS;
3772
3773#else
3774
3775 (void) operation;
3776
3777 return PSA_ERROR_NOT_SUPPORTED;
3778
3779#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3780 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3781 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3782}
3783
3784psa_status_t mbedtls_psa_verify_hash_start(
3785 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3786 const psa_key_attributes_t *attributes,
3787 const uint8_t *key_buffer, size_t key_buffer_size,
3788 psa_algorithm_t alg,
3789 const uint8_t *hash, size_t hash_length,
3790 const uint8_t *signature, size_t signature_length)
3791{
3792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003793 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003794 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003795
Paul Elliott068fe072023-01-16 13:59:15 +00003796 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3797 return PSA_ERROR_NOT_SUPPORTED;
3798 }
3799
3800 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003801 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003802 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803
3804#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003805 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3806 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003807
Paul Elliotta1c94092023-02-15 16:38:04 +00003808 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3809 mbedtls_mpi_init(&operation->r);
3810 mbedtls_mpi_init(&operation->s);
3811
Paul Elliott93d9ca82023-02-15 18:14:21 +00003812 /* Ensure num_ops is zero'ed in case of context re-use. */
3813 operation->num_ops = 0;
3814
Paul Elliott068fe072023-01-16 13:59:15 +00003815 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3816 attributes->core.bits,
3817 key_buffer,
3818 key_buffer_size,
3819 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820
Paul Elliott068fe072023-01-16 13:59:15 +00003821 if (status != PSA_SUCCESS) {
3822 return status;
3823 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003824
Paul Elliottc569fc22023-02-10 13:02:54 +00003825 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826
Paul Elliott1bc59df2023-02-05 13:41:57 +00003827 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003828 return PSA_ERROR_INVALID_SIGNATURE;
3829 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003830
Paul Elliott068fe072023-01-16 13:59:15 +00003831 status = mbedtls_to_psa_error(
3832 mbedtls_mpi_read_binary(&operation->r,
3833 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003834 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835
Paul Elliott068fe072023-01-16 13:59:15 +00003836 if (status != PSA_SUCCESS) {
3837 return status;
3838 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839
Paul Elliott068fe072023-01-16 13:59:15 +00003840 status = mbedtls_to_psa_error(
3841 mbedtls_mpi_read_binary(&operation->s,
3842 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003843 coordinate_bytes,
3844 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845
Paul Elliott068fe072023-01-16 13:59:15 +00003846 if (status != PSA_SUCCESS) {
3847 return status;
3848 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849
Paul Elliott2c9843f2023-02-15 17:32:42 +00003850 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851
Paul Elliott2c9843f2023-02-15 17:32:42 +00003852 if (status != PSA_SUCCESS) {
3853 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003854 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003855
Paul Elliott0290a762023-02-09 14:30:24 +00003856 /* We only need to store the same length of hash as the private key size
3857 * here, it would be truncated by the internal implementation anyway. */
3858 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3859 coordinate_bytes);
3860
Paul Elliottba70ad42023-02-15 18:23:53 +00003861 if (required_hash_length > sizeof(operation->hash)) {
3862 /* Shouldn't happen, but better safe than sorry. */
3863 return PSA_ERROR_CORRUPTION_DETECTED;
3864 }
3865
Paul Elliott0290a762023-02-09 14:30:24 +00003866 memcpy(operation->hash, hash, required_hash_length);
3867 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003868
3869 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003870#else
Paul Elliott068fe072023-01-16 13:59:15 +00003871 (void) operation;
3872 (void) key_buffer;
3873 (void) key_buffer_size;
3874 (void) alg;
3875 (void) hash;
3876 (void) hash_length;
3877 (void) signature;
3878 (void) signature_length;
3879 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003880 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003881 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882
Paul Elliott068fe072023-01-16 13:59:15 +00003883 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3885 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3886 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003887}
3888
3889psa_status_t mbedtls_psa_verify_hash_complete(
3890 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3891{
3892
3893#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3894 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3895 defined(MBEDTLS_ECP_RESTARTABLE)
3896
Paul Elliottf8e5b562023-02-19 18:43:45 +00003897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3898
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003899 /* Ensure max_ops is set to the current value (or default). */
3900 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3901
Paul Elliottf8e5b562023-02-19 18:43:45 +00003902 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003903 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3904 operation->hash,
3905 operation->hash_length,
3906 &operation->ctx->Q,
3907 &operation->r,
3908 &operation->s,
3909 &operation->restart_ctx));
3910
Paul Elliottf8e5b562023-02-19 18:43:45 +00003911 /* Hide the fact that the restart context only holds a delta of number of
3912 * ops done during the last operation, not an absolute value. */
3913 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3914
3915 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003916#else
3917 (void) operation;
3918
3919 return PSA_ERROR_NOT_SUPPORTED;
3920
3921#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3922 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3923 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3924}
3925
3926psa_status_t mbedtls_psa_verify_hash_abort(
3927 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3928{
3929
3930#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3931 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3932 defined(MBEDTLS_ECP_RESTARTABLE)
3933
3934 if (operation->ctx) {
3935 mbedtls_ecdsa_free(operation->ctx);
3936 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003937 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003938 }
3939
3940 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3941
Paul Elliott93d9ca82023-02-15 18:14:21 +00003942 operation->num_ops = 0;
3943
Paul Elliott588f8ed2022-12-02 18:10:26 +00003944 mbedtls_mpi_free(&operation->r);
3945 mbedtls_mpi_free(&operation->s);
3946
3947 return PSA_SUCCESS;
3948
3949#else
3950 (void) operation;
3951
3952 return PSA_ERROR_NOT_SUPPORTED;
3953
3954#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3955 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3956 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3957}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003958
mohammad1603503973b2018-03-12 15:59:30 +02003959/****************************************************************/
3960/* Symmetric cryptography */
3961/****************************************************************/
3962
Gilles Peskine449bd832023-01-11 14:50:10 +01003963static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3964 mbedtls_svc_key_id_t key,
3965 psa_algorithm_t alg,
3966 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003967{
3968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3969 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003970 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3972 PSA_KEY_USAGE_ENCRYPT :
3973 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003974
3975 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003977 status = PSA_ERROR_BAD_STATE;
3978 goto exit;
3979 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003980
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003982 status = PSA_ERROR_INVALID_ARGUMENT;
3983 goto exit;
3984 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3987 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003988 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003990
Ronald Cron49fafa92021-03-10 08:34:23 +01003991 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003992 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003993 * so we only set it (in the driver wrapper) after resources have been
3994 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003995 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003997 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003999 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 }
4001 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004002
Ronald Crona4af55f2020-12-14 14:36:06 +01004003 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004005 };
4006
Ronald Cronab99ac22020-10-01 16:38:28 +02004007 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 if (cipher_operation == MBEDTLS_ENCRYPT) {
4009 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4010 &attributes,
4011 slot->key.data,
4012 slot->key.bytes,
4013 alg);
4014 } else {
4015 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4016 &attributes,
4017 slot->key.data,
4018 slot->key.bytes,
4019 alg);
4020 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004021
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004022exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 if (status != PSA_SUCCESS) {
4024 psa_cipher_abort(operation);
4025 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004028
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004030}
4031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4033 mbedtls_svc_key_id_t key,
4034 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004035{
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004037}
4038
Gilles Peskine449bd832023-01-11 14:50:10 +01004039psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4040 mbedtls_svc_key_id_t key,
4041 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004042{
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004044}
4045
Gilles Peskine449bd832023-01-11 14:50:10 +01004046psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4047 uint8_t *iv,
4048 size_t iv_size,
4049 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004050{
Ronald Cron6d051732020-10-01 14:10:20 +02004051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004052 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4053 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004054
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004056 status = PSA_ERROR_BAD_STATE;
4057 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004058 }
4059
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004061 status = PSA_ERROR_BAD_STATE;
4062 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004063 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004064
Ronald Cron2fb90522021-07-05 12:31:44 +02004065 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004067 status = PSA_ERROR_BUFFER_TOO_SMALL;
4068 goto exit;
4069 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004070
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004072 status = PSA_ERROR_GENERIC_ERROR;
4073 goto exit;
4074 }
4075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 status = psa_generate_random(local_iv, default_iv_length);
4077 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004078 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 }
Ronald Cron5618a392021-03-26 09:52:26 +01004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 status = psa_driver_wrapper_cipher_set_iv(operation,
4082 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004083
4084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 if (status == PSA_SUCCESS) {
4086 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004087 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004088 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004090 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004092 }
Ronald Cron6d051732020-10-01 14:10:20 +02004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004095}
4096
Gilles Peskine449bd832023-01-11 14:50:10 +01004097psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4098 const uint8_t *iv,
4099 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004100{
Ronald Cron6d051732020-10-01 14:10:20 +02004101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004102
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004104 status = PSA_ERROR_BAD_STATE;
4105 goto exit;
4106 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004107
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004109 status = PSA_ERROR_BAD_STATE;
4110 goto exit;
4111 }
Ronald Crona0d68172021-03-26 10:15:08 +01004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004114 status = PSA_ERROR_INVALID_ARGUMENT;
4115 goto exit;
4116 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 status = psa_driver_wrapper_cipher_set_iv(operation,
4119 iv,
4120 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004121
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004122exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004124 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 } else {
4126 psa_cipher_abort(operation);
4127 }
4128 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004129}
4130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4132 const uint8_t *input,
4133 size_t input_length,
4134 uint8_t *output,
4135 size_t output_size,
4136 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004137{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004141 status = PSA_ERROR_BAD_STATE;
4142 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004143 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004146 status = PSA_ERROR_BAD_STATE;
4147 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004148 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 status = psa_driver_wrapper_cipher_update(operation,
4151 input,
4152 input_length,
4153 output,
4154 output_size,
4155 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004156
4157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 if (status != PSA_SUCCESS) {
4159 psa_cipher_abort(operation);
4160 }
Ronald Cron6d051732020-10-01 14:10:20 +02004161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004163}
4164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4166 uint8_t *output,
4167 size_t output_size,
4168 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004169{
David Saadab4ecc272019-02-14 13:48:10 +02004170 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004173 status = PSA_ERROR_BAD_STATE;
4174 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004175 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004178 status = PSA_ERROR_BAD_STATE;
4179 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004180 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 status = psa_driver_wrapper_cipher_finish(operation,
4183 output,
4184 output_size,
4185 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004186
4187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 if (status == PSA_SUCCESS) {
4189 return psa_cipher_abort(operation);
4190 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004191 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004195 }
mohammad1603503973b2018-03-12 15:59:30 +02004196}
4197
Gilles Peskine449bd832023-01-11 14:50:10 +01004198psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004199{
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004201 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004202 * in use. It's ok to call abort on such an object, and there's
4203 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004205 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004208
Ronald Cron49fafa92021-03-10 08:34:23 +01004209 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004210 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004211 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004214}
4215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4217 psa_algorithm_t alg,
4218 const uint8_t *input,
4219 size_t input_length,
4220 uint8_t *output,
4221 size_t output_size,
4222 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004223{
4224 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004225 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004226 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004227 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4228 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004229
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004231 status = PSA_ERROR_INVALID_ARGUMENT;
4232 goto exit;
4233 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4236 PSA_KEY_USAGE_ENCRYPT,
4237 alg);
4238 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004239 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004241
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004242 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004244 };
4245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4247 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004248 status = PSA_ERROR_GENERIC_ERROR;
4249 goto exit;
4250 }
4251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 if (default_iv_length > 0) {
4253 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004254 status = PSA_ERROR_BUFFER_TOO_SMALL;
4255 goto exit;
4256 }
4257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 status = psa_generate_random(local_iv, default_iv_length);
4259 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004260 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004262 }
4263
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004264 status = psa_driver_wrapper_cipher_encrypt(
4265 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004266 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004267 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004269
4270exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 unlock_status = psa_unlock_key_slot(slot);
4272 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004273 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004274 }
Ronald Cron23919522021-07-08 19:00:07 +02004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 if (status == PSA_SUCCESS) {
4277 if (default_iv_length > 0) {
4278 memcpy(output, local_iv, default_iv_length);
4279 }
4280 *output_length += default_iv_length;
4281 } else {
4282 *output_length = 0;
4283 }
4284
4285 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004286}
4287
Gilles Peskine449bd832023-01-11 14:50:10 +01004288psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4289 psa_algorithm_t alg,
4290 const uint8_t *input,
4291 size_t input_length,
4292 uint8_t *output,
4293 size_t output_size,
4294 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004295{
4296 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004297 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004298 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004301 status = PSA_ERROR_INVALID_ARGUMENT;
4302 goto exit;
4303 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004304
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4306 PSA_KEY_USAGE_DECRYPT,
4307 alg);
4308 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004309 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004311
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004312 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004314 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4317 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004318 status = PSA_ERROR_INVALID_ARGUMENT;
4319 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004321 status = PSA_ERROR_INVALID_ARGUMENT;
4322 goto exit;
4323 }
4324
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004325 status = psa_driver_wrapper_cipher_decrypt(
4326 &attributes, slot->key.data, slot->key.bytes,
4327 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004329
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004330exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 unlock_status = psa_unlock_key_slot(slot);
4332 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004333 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004337 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 }
Ronald Cron23919522021-07-08 19:00:07 +02004339
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004341}
4342
4343
mohammad16035955c982018-04-26 00:53:03 +03004344/****************************************************************/
4345/* AEAD */
4346/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004347
Paul Elliottbaff51c2021-09-28 17:44:45 +01004348/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004349static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004350{
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004352}
4353
4354/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004355static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4356 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004357{
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004361#if defined(PSA_WANT_ALG_GCM)
4362 case PSA_ALG_GCM:
4363 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 * arbitrarily large nonces. Please note that we do not generally
4365 * recommend the usage of nonces of greater length than
4366 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4367 * size, which can then lead to collisions if you encrypt a very
4368 * large number of messages.*/
4369 if (nonce_length != 0) {
4370 return PSA_SUCCESS;
4371 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004372 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004373#endif /* PSA_WANT_ALG_GCM */
4374#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004375 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 if (nonce_length >= 7 && nonce_length <= 13) {
4377 return PSA_SUCCESS;
4378 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004379 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004380#endif /* PSA_WANT_ALG_CCM */
4381#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004382 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 if (nonce_length == 12) {
4384 return PSA_SUCCESS;
4385 } else if (nonce_length == 8) {
4386 return PSA_ERROR_NOT_SUPPORTED;
4387 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004388 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004389#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004390 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004391 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004393 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004396}
4397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004399{
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4401 return PSA_ERROR_INVALID_ARGUMENT;
4402 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004403
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004405}
4406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4408 psa_algorithm_t alg,
4409 const uint8_t *nonce,
4410 size_t nonce_length,
4411 const uint8_t *additional_data,
4412 size_t additional_data_length,
4413 const uint8_t *plaintext,
4414 size_t plaintext_length,
4415 uint8_t *ciphertext,
4416 size_t ciphertext_size,
4417 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004418{
Ronald Cron215633c2021-03-16 17:15:37 +01004419 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4420 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004421
mohammad1603f08a5502018-06-03 15:05:47 +03004422 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 status = psa_aead_check_algorithm(alg);
4425 if (status != PSA_SUCCESS) {
4426 return status;
4427 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004428
Ronald Cron9a986162021-03-26 12:40:07 +01004429 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4431 if (status != PSA_SUCCESS) {
4432 return status;
4433 }
mohammad16035955c982018-04-26 00:53:03 +03004434
Ronald Cron215633c2021-03-16 17:15:37 +01004435 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004437 };
mohammad16035955c982018-04-26 00:53:03 +03004438
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 status = psa_aead_check_nonce_length(alg, nonce_length);
4440 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004441 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004443
Ronald Cronde822812021-03-17 16:08:20 +01004444 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004445 &attributes, slot->key.data, slot->key.bytes,
4446 alg,
4447 nonce, nonce_length,
4448 additional_data, additional_data_length,
4449 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004451
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4453 memset(ciphertext, 0, ciphertext_size);
4454 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004455
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004460}
4461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462psa_status_t psa_aead_decrypt(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 *ciphertext,
4469 size_t ciphertext_length,
4470 uint8_t *plaintext,
4471 size_t plaintext_size,
4472 size_t *plaintext_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
Gilles Peskineee652a32018-06-01 19:23:52 +02004477 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004478
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_DECRYPT, 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 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004493
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_decrypt(
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 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004506
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 if (status != PSA_SUCCESS && plaintext_size != 0) {
4508 memset(plaintext, 0, plaintext_size);
4509 }
mohammad160360a64d02018-06-03 17:20:42 +03004510
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004511exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 return status;
mohammad16035955c982018-04-26 00:53:03 +03004515}
4516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4518{
4519 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004522#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004524 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4526 return PSA_ERROR_INVALID_ARGUMENT;
4527 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004528 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004529#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004530
Przemek Stekiel86679c72022-10-06 17:06:56 +02004531#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004533 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4535 return PSA_ERROR_INVALID_ARGUMENT;
4536 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004537 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004538#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004539
Przemek Stekiel86679c72022-10-06 17:06:56 +02004540#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004542 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 if (tag_len != 16) {
4544 return PSA_ERROR_INVALID_ARGUMENT;
4545 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004546 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004547#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004548
4549 default:
4550 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004552 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004554}
4555
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004556/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004557static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4558 int is_encrypt,
4559 mbedtls_svc_key_id_t key,
4560 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004561{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4563 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4564 psa_key_slot_t *slot = NULL;
4565 psa_key_usage_t key_usage = 0;
4566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 status = psa_aead_check_algorithm(alg);
4568 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004569 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004573 status = PSA_ERROR_BAD_STATE;
4574 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004575 }
4576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 if (operation->nonce_set || operation->lengths_set ||
4578 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004579 status = PSA_ERROR_BAD_STATE;
4580 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004581 }
4582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004584 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004586 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4590 alg);
4591 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004592 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004594
4595 psa_key_attributes_t attributes = {
4596 .core = slot->attr
4597 };
4598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004600 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 if (is_encrypt) {
4604 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4605 &attributes,
4606 slot->key.data,
4607 slot->key.bytes,
4608 alg);
4609 } else {
4610 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4611 &attributes,
4612 slot->key.data,
4613 slot->key.bytes,
4614 alg);
4615 }
4616 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004617 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004621
4622exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004626 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004628 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 } else {
4630 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004631 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004634}
4635
Paul Elliott302ff6b2021-04-20 18:10:30 +01004636/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004637psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4638 mbedtls_svc_key_id_t key,
4639 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004640{
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004642}
4643
4644/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004645psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4646 mbedtls_svc_key_id_t key,
4647 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004648{
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004650}
4651
4652/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004653psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4654 uint8_t *nonce,
4655 size_t nonce_size,
4656 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004657{
4658 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004659 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004660 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004661
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004662 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004665 status = PSA_ERROR_BAD_STATE;
4666 goto exit;
4667 }
4668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004670 status = PSA_ERROR_BAD_STATE;
4671 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004672 }
4673
Paul Elliotte193ea82021-10-01 13:00:16 +01004674 /* For CCM, this size may not be correct according to the PSA
4675 * specification. The PSA Crypto 1.0.1 specification states:
4676 *
4677 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4678 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4679 *
4680 * However this restriction that L has to be the smallest integer is not
4681 * applied in practice, and it is not implementable here since the
4682 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4684 operation->alg);
4685 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004686 status = PSA_ERROR_BUFFER_TOO_SMALL;
4687 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004688 }
4689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 status = psa_generate_random(local_nonce, required_nonce_size);
4691 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004692 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004696
Paul Elliott39dc6b82021-05-11 19:16:09 +01004697exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (status == PSA_SUCCESS) {
4699 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004700 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 } else {
4702 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004703 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004706}
4707
4708/* Set the nonce for a multipart authenticated encryption or decryption
4709 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004710psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4711 const uint8_t *nonce,
4712 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004713{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004714 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004717 status = PSA_ERROR_BAD_STATE;
4718 goto exit;
4719 }
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004722 status = PSA_ERROR_BAD_STATE;
4723 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004724 }
4725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4727 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004728 status = PSA_ERROR_INVALID_ARGUMENT;
4729 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004730 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4733 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004734
Paul Elliott39dc6b82021-05-11 19:16:09 +01004735exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004737 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 } else {
4739 psa_aead_abort(operation);
4740 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004743}
4744
4745/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004746psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4747 size_t ad_length,
4748 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004749{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004750 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004753 status = PSA_ERROR_BAD_STATE;
4754 goto exit;
4755 }
4756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 if (operation->lengths_set || operation->ad_started ||
4758 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004759 status = PSA_ERROR_BAD_STATE;
4760 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004761 }
4762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004764#if defined(PSA_WANT_ALG_GCM)
4765 case PSA_ALG_GCM:
4766 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 * bits. Without the guard this code will generate warnings on 32bit
4768 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004769#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 if (((uint64_t) ad_length) >> 61 != 0 ||
4771 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004772 status = PSA_ERROR_INVALID_ARGUMENT;
4773 goto exit;
4774 }
Paul Elliott325d3742021-09-27 17:56:28 +01004775#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004776 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004777#endif /* PSA_WANT_ALG_GCM */
4778#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004779 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004781 status = PSA_ERROR_INVALID_ARGUMENT;
4782 goto exit;
4783 }
4784 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004785#endif /* PSA_WANT_ALG_CCM */
4786#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004787 case PSA_ALG_CHACHA20_POLY1305:
4788 /* No length restrictions for ChaChaPoly. */
4789 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004790#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004791 default:
4792 break;
4793 }
Paul Elliott325d3742021-09-27 17:56:28 +01004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4796 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004797
Paul Elliott39dc6b82021-05-11 19:16:09 +01004798exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004800 operation->ad_remaining = ad_length;
4801 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004802 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 } else {
4804 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004805 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004808}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004809
4810/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004811psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4812 const uint8_t *input,
4813 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004814{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004818 status = PSA_ERROR_BAD_STATE;
4819 goto exit;
4820 }
4821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004823 status = PSA_ERROR_BAD_STATE;
4824 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004825 }
4826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (operation->lengths_set) {
4828 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004829 status = PSA_ERROR_INVALID_ARGUMENT;
4830 goto exit;
4831 }
4832
4833 operation->ad_remaining -= input_length;
4834 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004835#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004837 status = PSA_ERROR_BAD_STATE;
4838 goto exit;
4839 }
4840#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 status = psa_driver_wrapper_aead_update_ad(operation, input,
4843 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004844
Paul Elliott39dc6b82021-05-11 19:16:09 +01004845exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004847 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 } else {
4849 psa_aead_abort(operation);
4850 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004853}
4854
4855/* Encrypt or decrypt a message fragment in an active multipart AEAD
4856 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004857psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4858 const uint8_t *input,
4859 size_t input_length,
4860 uint8_t *output,
4861 size_t output_size,
4862 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004863{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004864 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004865
4866 *output_length = 0;
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004869 status = PSA_ERROR_BAD_STATE;
4870 goto exit;
4871 }
4872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004874 status = PSA_ERROR_BAD_STATE;
4875 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004876 }
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004879 /* Additional data length was supplied, but not all the additional
4880 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004882 status = PSA_ERROR_INVALID_ARGUMENT;
4883 goto exit;
4884 }
4885
4886 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004888 status = PSA_ERROR_INVALID_ARGUMENT;
4889 goto exit;
4890 }
4891
4892 operation->body_remaining -= input_length;
4893 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004894#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004896 status = PSA_ERROR_BAD_STATE;
4897 goto exit;
4898 }
4899#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004900
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4902 output, output_size,
4903 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004904
Paul Elliott39dc6b82021-05-11 19:16:09 +01004905exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004907 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 } else {
4909 psa_aead_abort(operation);
4910 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004913}
4914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004916{
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 if (operation->id == 0 || !operation->nonce_set) {
4918 return PSA_ERROR_BAD_STATE;
4919 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004920
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4922 operation->body_remaining != 0)) {
4923 return PSA_ERROR_INVALID_ARGUMENT;
4924 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004927}
4928
Paul Elliott302ff6b2021-04-20 18:10:30 +01004929/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004930psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4931 uint8_t *ciphertext,
4932 size_t ciphertext_size,
4933 size_t *ciphertext_length,
4934 uint8_t *tag,
4935 size_t tag_size,
4936 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004937{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004938 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4939
Paul Elliott302ff6b2021-04-20 18:10:30 +01004940 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004941 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 status = psa_aead_final_checks(operation);
4944 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004945 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004949 status = PSA_ERROR_BAD_STATE;
4950 goto exit;
4951 }
4952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4954 ciphertext_size,
4955 ciphertext_length,
4956 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004957
Paul Elliott39dc6b82021-05-11 19:16:09 +01004958exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004959
4960
Paul Elliottf47b0952021-05-21 18:02:33 +01004961 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004962 * the zero tag size, make sure the tag is set to something implausible.
4963 * Even if the operation succeeds, make sure we clear the rest of the
4964 * buffer to prevent potential leakage of anything previously placed in
4965 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004966 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004971}
4972
4973/* Finish authenticating and decrypting a message in a multipart AEAD
4974 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004975psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4976 uint8_t *plaintext,
4977 size_t plaintext_size,
4978 size_t *plaintext_length,
4979 const uint8_t *tag,
4980 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004982 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4983
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984 *plaintext_length = 0;
4985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 status = psa_aead_final_checks(operation);
4987 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004988 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004992 status = PSA_ERROR_BAD_STATE;
4993 goto exit;
4994 }
4995
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4997 plaintext_size,
4998 plaintext_length,
4999 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005000
5001exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005005}
5006
5007/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005008psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005009{
5010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005013 /* The object has (apparently) been initialized but it is not (yet)
5014 * in use. It's ok to call abort on such an object, and there's
5015 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005017 }
5018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005022
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005024}
5025
Gilles Peskinee59236f2018-01-27 23:32:46 +01005026/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005027/* Generators */
5028/****************************************************************/
5029
Przemek Stekiel69c46792022-06-10 12:59:51 +02005030#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005031 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005032 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305033 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
5034 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
John Durkop07cc04a2020-11-16 22:08:34 -08005035#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005036#endif /* At least one builtin KDF */
5037
Przemek Stekiel69c46792022-06-10 12:59:51 +02005038#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005039 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5040 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5041static psa_status_t psa_key_derivation_start_hmac(
5042 psa_mac_operation_t *operation,
5043 psa_algorithm_t hash_alg,
5044 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005046{
5047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5048 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5050 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5051 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005052
Steven Cooreman72f736a2021-05-07 14:14:37 +02005053 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 status = psa_driver_wrapper_mac_sign_setup(operation,
5057 &attributes,
5058 hmac_key, hmac_key_length,
5059 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005060
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 psa_reset_key_attributes(&attributes);
5062 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005063}
5064#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005065
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005066#define HKDF_STATE_INIT 0 /* no input yet */
5067#define HKDF_STATE_STARTED 1 /* got salt */
5068#define HKDF_STATE_KEYED 2 /* got key */
5069#define HKDF_STATE_OUTPUT 3 /* output started */
5070
Gilles Peskinecbe66502019-05-16 16:59:18 +02005071static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005073{
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5075 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5076 } else {
5077 return operation->alg;
5078 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005079}
5080
Gilles Peskine449bd832023-01-11 14:50:10 +01005081psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005082{
5083 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5085 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005086 /* The object has (apparently) been initialized but it is not
5087 * in use. It's ok to call abort on such an object, and there's
5088 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005090#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5092 mbedtls_free(operation->ctx.hkdf.info);
5093 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5094 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005095#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005096#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5097 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5099 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5100 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5101 if (operation->ctx.tls12_prf.secret != NULL) {
5102 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5103 operation->ctx.tls12_prf.secret_length);
5104 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005105 }
5106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 if (operation->ctx.tls12_prf.seed != NULL) {
5108 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5109 operation->ctx.tls12_prf.seed_length);
5110 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005111 }
5112
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 if (operation->ctx.tls12_prf.label != NULL) {
5114 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5115 operation->ctx.tls12_prf.label_length);
5116 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005117 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005118#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 if (operation->ctx.tls12_prf.other_secret != NULL) {
5120 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5121 operation->ctx.tls12_prf.other_secret_length);
5122 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005123 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005124#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005125 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005126
5127 /* We leave the fields Ai and output_block to be erased safely by the
5128 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005130#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5131 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005132#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5134 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5135 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5136 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005137#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305138#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5139 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305140 if (operation->ctx.pbkdf2.salt != NULL) {
5141 mbedtls_platform_zeroize(operation->ctx.pbkdf2.salt,
5142 operation->ctx.pbkdf2.salt_length);
5143 mbedtls_free(operation->ctx.pbkdf2.salt);
5144 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305145
5146 status = PSA_SUCCESS;
5147 } else
5148#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005149 {
5150 status = PSA_ERROR_BAD_STATE;
5151 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 mbedtls_platform_zeroize(operation, sizeof(*operation));
5153 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005154}
5155
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005156psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005158{
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005160 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005162 }
5163
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005166}
5167
Gilles Peskine449bd832023-01-11 14:50:10 +01005168psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5169 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005170{
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 if (operation->alg == 0) {
5172 return PSA_ERROR_BAD_STATE;
5173 }
5174 if (capacity > operation->capacity) {
5175 return PSA_ERROR_INVALID_ARGUMENT;
5176 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005177 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005179}
5180
Przemek Stekiel69c46792022-06-10 12:59:51 +02005181#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005182/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005183static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5184 psa_algorithm_t kdf_alg,
5185 uint8_t *output,
5186 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005187{
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5189 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005190 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005191 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005192#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005194#else
5195 const uint8_t last_block = 0xff;
5196#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005197
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 if (hkdf->state < HKDF_STATE_KEYED ||
5199 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005200#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005202#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 )) {
5204 return PSA_ERROR_BAD_STATE;
5205 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005206 hkdf->state = HKDF_STATE_OUTPUT;
5207
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005209 /* Copy what remains of the current block */
5210 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005212 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 }
5214 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005215 output += n;
5216 output_length -= n;
5217 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005219 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005221 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005222 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005223 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005224 * object was corrupted or if this function is called directly
5225 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 if (hkdf->block_number == last_block) {
5227 return PSA_ERROR_BAD_STATE;
5228 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005229
5230 /* We need a new block */
5231 ++hkdf->block_number;
5232 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5235 hash_alg,
5236 hkdf->prk,
5237 hash_length);
5238 if (status != PSA_SUCCESS) {
5239 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005240 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005241
5242 if (hkdf->block_number != 1) {
5243 status = psa_mac_update(&hkdf->hmac,
5244 hkdf->output_block,
5245 hash_length);
5246 if (status != PSA_SUCCESS) {
5247 return status;
5248 }
5249 }
5250 status = psa_mac_update(&hkdf->hmac,
5251 hkdf->info,
5252 hkdf->info_length);
5253 if (status != PSA_SUCCESS) {
5254 return status;
5255 }
5256 status = psa_mac_update(&hkdf->hmac,
5257 &hkdf->block_number, 1);
5258 if (status != PSA_SUCCESS) {
5259 return status;
5260 }
5261 status = psa_mac_sign_finish(&hkdf->hmac,
5262 hkdf->output_block,
5263 sizeof(hkdf->output_block),
5264 &hmac_output_length);
5265 if (status != PSA_SUCCESS) {
5266 return status;
5267 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005268 }
5269
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005271}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005272#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005273
John Durkop07cc04a2020-11-16 22:08:34 -08005274#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5275 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005276static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5277 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005279{
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5281 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005282 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5283 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005284 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005285
Janos Follath7742fee2019-06-17 12:58:10 +01005286 /* We can't be wanting more output after block 0xff, otherwise
5287 * the capacity check in psa_key_derivation_output_bytes() would have
5288 * prevented this call. It could happen only if the operation
5289 * object was corrupted or if this function is called directly
5290 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 if (tls12_prf->block_number == 0xff) {
5292 return PSA_ERROR_CORRUPTION_DETECTED;
5293 }
Janos Follath7742fee2019-06-17 12:58:10 +01005294
5295 /* We need a new block */
5296 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005297 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005298
5299 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5300 *
5301 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5302 *
5303 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5304 * HMAC_hash(secret, A(2) + seed) +
5305 * HMAC_hash(secret, A(3) + seed) + ...
5306 *
5307 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005308 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005309 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005310 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005311 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005312 * is currently extracted as `output_block` and where i is
5313 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005314 */
5315
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 status = psa_key_derivation_start_hmac(&hmac,
5317 hash_alg,
5318 tls12_prf->secret,
5319 tls12_prf->secret_length);
5320 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005321 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005323
5324 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005326 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5327 * the variable seed and in this instance means it in the context of the
5328 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 status = psa_mac_update(&hmac,
5330 tls12_prf->label,
5331 tls12_prf->label_length);
5332 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005333 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 }
5335 status = psa_mac_update(&hmac,
5336 tls12_prf->seed,
5337 tls12_prf->seed_length);
5338 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005339 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 }
5341 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005342 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5344 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005345 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005347 }
5348
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 status = psa_mac_sign_finish(&hmac,
5350 tls12_prf->Ai, hash_length,
5351 &hmac_output_length);
5352 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005353 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 }
5355 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005356 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005358
5359 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 status = psa_key_derivation_start_hmac(&hmac,
5361 hash_alg,
5362 tls12_prf->secret,
5363 tls12_prf->secret_length);
5364 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005365 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 }
5367 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5368 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005369 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 }
5371 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5372 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005373 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 }
5375 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5376 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005377 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 }
5379 status = psa_mac_sign_finish(&hmac,
5380 tls12_prf->output_block, hash_length,
5381 &hmac_output_length);
5382 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005383 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005385
Janos Follath7742fee2019-06-17 12:58:10 +01005386
5387cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 cleanup_status = psa_mac_abort(&hmac);
5389 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005390 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005394}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005395
Janos Follath844eb0e2019-06-19 12:10:49 +01005396static psa_status_t psa_key_derivation_tls12_prf_read(
5397 psa_tls12_prf_key_derivation_t *tls12_prf,
5398 psa_algorithm_t alg,
5399 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005401{
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5403 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005404 psa_status_t status;
5405 uint8_t offset, length;
5406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005408 case PSA_TLS12_PRF_STATE_LABEL_SET:
5409 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5410 break;
5411 case PSA_TLS12_PRF_STATE_OUTPUT:
5412 break;
5413 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005415 }
5416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005418 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 if (tls12_prf->left_in_block == 0) {
5420 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5421 alg);
5422 if (status != PSA_SUCCESS) {
5423 return status;
5424 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005425
5426 continue;
5427 }
5428
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005430 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005432 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005434
5435 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005437 output += length;
5438 output_length -= length;
5439 tls12_prf->left_in_block -= length;
5440 }
5441
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005443}
John Durkop07cc04a2020-11-16 22:08:34 -08005444#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5445 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005446
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005447#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5448static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5449 psa_tls12_ecjpake_to_pms_t *ecjpake,
5450 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005452{
5453 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005454 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 if (output_length != 32) {
5457 return PSA_ERROR_INVALID_ARGUMENT;
5458 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005459
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5461 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5462 &output_size);
5463 if (status != PSA_SUCCESS) {
5464 return status;
5465 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 if (output_size != output_length) {
5468 return PSA_ERROR_GENERIC_ERROR;
5469 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005470
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005472}
5473#endif
5474
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005475psa_status_t psa_key_derivation_output_bytes(
5476 psa_key_derivation_operation_t *operation,
5477 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005479{
5480 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005482
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005484 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005486 }
5487
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005489 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005490 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005491 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005492 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005493 goto exit;
5494 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005496 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005497 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005498 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5499 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005500 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005501 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005503 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005504 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005505
Przemek Stekiel69c46792022-06-10 12:59:51 +02005506#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5508 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5509 output, output_length);
5510 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005511#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005512#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5513 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5515 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5516 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5517 kdf_alg, output,
5518 output_length);
5519 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005520#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5521 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005522#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005524 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5526 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005527#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305528#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5529 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5530 /* As output functionality is not added yet return
5531 * PSA_ERROR_NOT_SUPPORTED for now if inputs are passed correctly.
5532 * If input validation fails operation is aborted and output_bytes
5533 * will return PSA_ERROR_BAD_STATE */
5534 status = PSA_ERROR_NOT_SUPPORTED;
5535 } else
5536#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005537
Gilles Peskineeab56e42018-07-12 17:12:33 +02005538 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005539 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005541 }
5542
5543exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005545 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005546 * This allows us to differentiate between exhausted operations and
5547 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5548 * operations. */
5549 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005551 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005553 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005555}
5556
David Brown7807bf72021-02-09 16:28:23 -07005557#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005558static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005559{
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 if (data_size >= 8) {
5561 mbedtls_des_key_set_parity(data);
5562 }
5563 if (data_size >= 16) {
5564 mbedtls_des_key_set_parity(data + 8);
5565 }
5566 if (data_size >= 24) {
5567 mbedtls_des_key_set_parity(data + 16);
5568 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005569}
David Brown7807bf72021-02-09 16:28:23 -07005570#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005571
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005572/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 * ECC keys on a Weierstrass elliptic curve require the generation
5574 * of a private key which is an integer
5575 * in the range [1, N - 1], where N is the boundary of the private key domain:
5576 * N is the prime p for Diffie-Hellman, or the order of the
5577 * curve’s base point for ECC.
5578 *
5579 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5580 * This function generates the private key using the following process:
5581 *
5582 * 1. Draw a byte string of length ceiling(m/8) bytes.
5583 * 2. If m is not a multiple of 8, set the most significant
5584 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5585 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5586 * 4. If k > N - 2, discard the result and return to step 1.
5587 * 5. Output k + 1 as the private key.
5588 *
5589 * This method allows compliance to NIST standards, specifically the methods titled
5590 * Key-Pair Generation by Testing Candidates in the following publications:
5591 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5592 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5593 * Diffie-Hellman keys.
5594 *
5595 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5596 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5597 *
5598 * Note: Function allocates memory for *data buffer, so given *data should be
5599 * always NULL.
5600 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005601#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5602 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005603 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5604 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5605 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005606static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005607 psa_key_slot_t *slot,
5608 size_t bits,
5609 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005610 uint8_t **data
5611 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005612{
Valerio Setti52789862023-04-07 12:13:11 +02005613#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005614 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005615 mbedtls_mpi k;
5616 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005617 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005618 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 mbedtls_mpi_init(&k);
5621 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005622
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005623 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005625 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005627
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005629 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005630 goto cleanup;
5631 }
5632
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005633 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005635
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005637
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005638 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005639 /* Let m be the bit size of N. */
5640 size_t m = ecp_group.nbits;
5641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005643
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005644 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005646
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005647 /* Note: This function is always called with *data == NULL and it
5648 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 *data = mbedtls_calloc(1, m_bytes);
5650 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005651 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005652 goto cleanup;
5653 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005656 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005658 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005660
5661 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005663 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005664 * (8 * ceiling(m/8) - m) bits of the first byte in
5665 * the string to zero.
5666 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005668 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005669 }
5670
5671 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 * big-endian byte string.
5673 */
5674 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005675
5676 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 * Result of comparison is returned. When it indicates error
5678 * then this function is called again.
5679 */
5680 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005681 }
5682
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005683 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5685 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005686cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 if (ret != 0) {
5688 status = mbedtls_to_psa_error(ret);
5689 }
5690 if (status != PSA_SUCCESS) {
5691 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005692 *data = NULL;
5693 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 mbedtls_mpi_free(&k);
5695 mbedtls_mpi_free(&diff_N_2);
5696 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005697#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005698 (void) slot;
5699 (void) bits;
5700 (void) operation;
5701 (void) data;
5702 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005703#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005704}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005705
5706/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5707 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5708 *
5709 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5710 * draw a 32-byte string and process it as specified in
5711 * Elliptic Curves for Security [RFC7748] §5.
5712 *
5713 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5714 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5715 *
5716 * Note: Function allocates memory for *data buffer, so given *data should be
5717 * always NULL.
5718 */
5719
5720static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5721 size_t bits,
5722 psa_key_derivation_operation_t *operation,
5723 uint8_t **data
5724 )
5725{
5726 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005727 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005730 case 255:
5731 output_length = 32;
5732 break;
5733 case 448:
5734 output_length = 56;
5735 break;
5736 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005738 break;
5739 }
5740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 if (*data == NULL) {
5744 return PSA_ERROR_INSUFFICIENT_MEMORY;
5745 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005750 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005754 case 255:
5755 (*data)[0] &= 248;
5756 (*data)[31] &= 127;
5757 (*data)[31] |= 64;
5758 break;
5759 case 448:
5760 (*data)[0] &= 252;
5761 (*data)[55] |= 128;
5762 break;
5763 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005765 break;
5766 }
5767
5768 return status;
5769}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005770#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5771 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005772 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5773 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5774 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005775
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005776static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005777 psa_key_slot_t *slot,
5778 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005780{
5781 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305783 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005784 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5787 return PSA_ERROR_INVALID_ARGUMENT;
5788 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005789
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005790#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5791 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005792 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5793 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5794 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5796 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5797 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005798 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5800 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005801 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 }
5803 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005804 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5806 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005807 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005809 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005810 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005811#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5812 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005813 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5814 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5815 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 if (key_type_is_raw_bytes(slot->attr.type)) {
5817 if (bits % 8 != 0) {
5818 return PSA_ERROR_INVALID_ARGUMENT;
5819 }
5820 data = mbedtls_calloc(1, bytes);
5821 if (data == NULL) {
5822 return PSA_ERROR_INSUFFICIENT_MEMORY;
5823 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 status = psa_key_derivation_output_bytes(operation, data, bytes);
5826 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005827 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 }
David Brown12ca5032021-02-11 11:02:00 -07005829#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5831 psa_des_set_key_parity(data, bytes);
5832 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005833#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 } else {
5835 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005836 }
Ronald Crondd04d422020-11-28 15:14:42 +01005837
Ronald Cronbf33c932020-11-28 18:06:53 +01005838 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005839 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005841 };
5842
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5844 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5845 &storage_size);
5846 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305847 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 }
Archanad8a83dc2021-06-14 10:04:16 +05305849 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 status = psa_allocate_buffer_to_slot(slot, storage_size);
5851 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305852 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 }
Archanad8a83dc2021-06-14 10:04:16 +05305854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 status = psa_driver_wrapper_import_key(&attributes,
5856 data, bytes,
5857 slot->key.data,
5858 slot->key.bytes,
5859 &slot->key.bytes, &bits);
5860 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005861 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005863
5864exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 mbedtls_free(data);
5866 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005867}
5868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5870 psa_key_derivation_operation_t *operation,
5871 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005872{
5873 psa_status_t status;
5874 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005875 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005876
Ronald Cron81709fc2020-11-14 12:10:32 +01005877 *key = MBEDTLS_SVC_KEY_ID_INIT;
5878
Gilles Peskine0f84d622019-09-12 19:03:13 +02005879 /* Reject any attempt to create a zero-length key so that we don't
5880 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 if (psa_get_key_bits(attributes) == 0) {
5882 return PSA_ERROR_INVALID_ARGUMENT;
5883 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (operation->alg == PSA_ALG_NONE) {
5886 return PSA_ERROR_BAD_STATE;
5887 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005888
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 if (!operation->can_output_key) {
5890 return PSA_ERROR_NOT_PERMITTED;
5891 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5894 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005895#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005897 /* Deriving a key in a secure element is not implemented yet. */
5898 status = PSA_ERROR_NOT_SUPPORTED;
5899 }
5900#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 if (status == PSA_SUCCESS) {
5902 status = psa_generate_derived_key_internal(slot,
5903 attributes->core.bits,
5904 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005905 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (status == PSA_SUCCESS) {
5907 status = psa_finish_key_creation(slot, driver, key);
5908 }
5909 if (status != PSA_SUCCESS) {
5910 psa_fail_key_creation(slot, driver);
5911 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005914}
5915
Gilles Peskineeab56e42018-07-12 17:12:33 +02005916
5917
5918/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005919/* Key derivation */
5920/****************************************************************/
5921
Steven Cooreman932ffb72021-02-15 12:14:32 +01005922#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005923static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005924{
5925#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5927 return 1;
5928 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005929#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005930#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5932 return 1;
5933 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005934#endif
5935#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5937 return 1;
5938 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005939#endif
5940#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5942 return 1;
5943 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005944#endif
5945#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5947 return 1;
5948 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005949#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005950#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5952 return 1;
5953 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005954#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05305955#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5956 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5957 return 1;
5958 }
5959#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005961}
5962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005964{
5965 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 psa_status_t status = psa_hash_setup(&operation, alg);
5967 psa_hash_abort(&operation);
5968 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005969}
5970
Gilles Peskine969c5d62019-01-16 15:53:06 +01005971static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005972 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005974{
Janos Follath5fe19732019-06-20 15:09:30 +01005975 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5976 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005978
Gilles Peskine969c5d62019-01-16 15:53:06 +01005979 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 if (!is_kdf_alg_supported(kdf_alg)) {
5981 return PSA_ERROR_NOT_SUPPORTED;
5982 }
John Durkop07cc04a2020-11-16 22:08:34 -08005983
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005984 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005985 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5987 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5988 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5989 if (hash_size == 0) {
5990 return PSA_ERROR_NOT_SUPPORTED;
5991 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005992
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005993 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5994 * we might fail later, which is somewhat unfriendly and potentially
5995 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 psa_status_t status = psa_hash_try_support(hash_alg);
5997 if (status != PSA_SUCCESS) {
5998 return status;
5999 }
6000 } else {
6001 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006002 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006003
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6005 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6006 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6007 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006008 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006009#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006010 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6012 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006013 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006014 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006015#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6016 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 operation->capacity = 255 * hash_size;
6018 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006019}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006022{
6023#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 if (alg == PSA_ALG_ECDH) {
6025 return PSA_SUCCESS;
6026 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006027#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006028#if defined(PSA_WANT_ALG_FFDH)
6029 if (alg == PSA_ALG_FFDH) {
6030 return PSA_SUCCESS;
6031 }
6032#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006033 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006035}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006036
6037static int psa_key_derivation_allows_free_form_secret_input(
6038 psa_algorithm_t kdf_alg)
6039{
6040#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6041 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6042 return 0;
6043 }
6044#endif
6045 (void) kdf_alg;
6046 return 1;
6047}
John Durkop07cc04a2020-11-16 22:08:34 -08006048#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006049
Gilles Peskine449bd832023-01-11 14:50:10 +01006050psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6051 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006052{
6053 psa_status_t status;
6054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 if (operation->alg != 0) {
6056 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006057 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006058
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6060 return PSA_ERROR_INVALID_ARGUMENT;
6061 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6062#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6063 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6064 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6065 status = psa_key_agreement_try_support(ka_alg);
6066 if (status != PSA_SUCCESS) {
6067 return status;
6068 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006069 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6070 return PSA_ERROR_INVALID_ARGUMENT;
6071 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6073#else
6074 return PSA_ERROR_NOT_SUPPORTED;
6075#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6076 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6077#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6078 status = psa_key_derivation_setup_kdf(operation, alg);
6079#else
6080 return PSA_ERROR_NOT_SUPPORTED;
6081#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6082 } else {
6083 return PSA_ERROR_INVALID_ARGUMENT;
6084 }
6085
6086 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006087 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 }
6089 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006090}
6091
Przemek Stekiel69c46792022-06-10 12:59:51 +02006092#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006093static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6094 psa_algorithm_t kdf_alg,
6095 psa_key_derivation_step_t step,
6096 const uint8_t *data,
6097 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006098{
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006100 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006102 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006103#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6105 return PSA_ERROR_INVALID_ARGUMENT;
6106 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006107#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (hkdf->state != HKDF_STATE_INIT) {
6109 return PSA_ERROR_BAD_STATE;
6110 } else {
6111 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6112 hash_alg,
6113 data, data_length);
6114 if (status != PSA_SUCCESS) {
6115 return status;
6116 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006117 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006119 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006120 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006121#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006123 /* We shouldn't be in different state as HKDF_EXPAND only allows
6124 * two inputs: SECRET (this case) and INFO which does not modify
6125 * the state. It could happen only if the hkdf
6126 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 if (hkdf->state != HKDF_STATE_INIT) {
6128 return PSA_ERROR_BAD_STATE;
6129 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006130
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006131 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6133 return PSA_ERROR_INVALID_ARGUMENT;
6134 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 memcpy(hkdf->prk, data, data_length);
6137 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006138#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006139 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006140 /* HKDF: If no salt was provided, use an empty salt.
6141 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006143#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6145 return PSA_ERROR_BAD_STATE;
6146 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006147#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6149 hash_alg,
6150 NULL, 0);
6151 if (status != PSA_SUCCESS) {
6152 return status;
6153 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006154 hkdf->state = HKDF_STATE_STARTED;
6155 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 if (hkdf->state != HKDF_STATE_STARTED) {
6157 return PSA_ERROR_BAD_STATE;
6158 }
6159 status = psa_mac_update(&hkdf->hmac,
6160 data, data_length);
6161 if (status != PSA_SUCCESS) {
6162 return status;
6163 }
6164 status = psa_mac_sign_finish(&hkdf->hmac,
6165 hkdf->prk,
6166 sizeof(hkdf->prk),
6167 &data_length);
6168 if (status != PSA_SUCCESS) {
6169 return status;
6170 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006171 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006172
Gilles Peskine2b522db2019-04-12 15:11:49 +02006173 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006174 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006175#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006177 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006179 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006181#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006182 {
6183 /* Block 0 is empty, and the next block will be
6184 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006186 }
6187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006189 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006190#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6192 return PSA_ERROR_INVALID_ARGUMENT;
6193 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006194#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006195#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6197 hkdf->state == HKDF_STATE_INIT) {
6198 return PSA_ERROR_BAD_STATE;
6199 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006200#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (hkdf->state == HKDF_STATE_OUTPUT) {
6202 return PSA_ERROR_BAD_STATE;
6203 }
6204 if (hkdf->info_set) {
6205 return PSA_ERROR_BAD_STATE;
6206 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006207 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 if (data_length != 0) {
6209 hkdf->info = mbedtls_calloc(1, data_length);
6210 if (hkdf->info == NULL) {
6211 return PSA_ERROR_INSUFFICIENT_MEMORY;
6212 }
6213 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006214 }
6215 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006217 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006219 }
6220}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006221#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006222
John Durkop07cc04a2020-11-16 22:08:34 -08006223#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6224 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006225static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6226 const uint8_t *data,
6227 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006228{
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6230 return PSA_ERROR_BAD_STATE;
6231 }
Janos Follathf08e2652019-06-13 09:05:41 +01006232
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 if (data_length != 0) {
6234 prf->seed = mbedtls_calloc(1, data_length);
6235 if (prf->seed == NULL) {
6236 return PSA_ERROR_INSUFFICIENT_MEMORY;
6237 }
Janos Follathf08e2652019-06-13 09:05:41 +01006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006240 prf->seed_length = data_length;
6241 }
Janos Follathf08e2652019-06-13 09:05:41 +01006242
Gilles Peskine43f958b2020-12-13 14:55:14 +01006243 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006244
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006246}
6247
Gilles Peskine449bd832023-01-11 14:50:10 +01006248static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6249 const uint8_t *data,
6250 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006251{
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6253 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6254 return PSA_ERROR_BAD_STATE;
6255 }
Janos Follath81550542019-06-13 14:26:34 +01006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 if (data_length != 0) {
6258 prf->secret = mbedtls_calloc(1, data_length);
6259 if (prf->secret == NULL) {
6260 return PSA_ERROR_INSUFFICIENT_MEMORY;
6261 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006264 prf->secret_length = data_length;
6265 }
Janos Follath81550542019-06-13 14:26:34 +01006266
Gilles Peskine43f958b2020-12-13 14:55:14 +01006267 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006268
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006270}
6271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6273 const uint8_t *data,
6274 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006275{
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6277 return PSA_ERROR_BAD_STATE;
6278 }
Janos Follath63028dd2019-06-13 09:15:47 +01006279
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 if (data_length != 0) {
6281 prf->label = mbedtls_calloc(1, data_length);
6282 if (prf->label == NULL) {
6283 return PSA_ERROR_INSUFFICIENT_MEMORY;
6284 }
Janos Follath63028dd2019-06-13 09:15:47 +01006285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006287 prf->label_length = data_length;
6288 }
Janos Follath63028dd2019-06-13 09:15:47 +01006289
Gilles Peskine43f958b2020-12-13 14:55:14 +01006290 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006293}
6294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6296 psa_key_derivation_step_t step,
6297 const uint8_t *data,
6298 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006299{
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006301 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006303 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006305 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006307 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006309 }
6310}
John Durkop07cc04a2020-11-16 22:08:34 -08006311#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6312 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6313
6314#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6315static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6316 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006317 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006319{
6320 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6322 4 + data_length + prf->other_secret_length :
6323 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6326 return PSA_ERROR_INVALID_ARGUMENT;
6327 }
John Durkop07cc04a2020-11-16 22:08:34 -08006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 uint8_t *pms = mbedtls_calloc(1, pms_len);
6330 if (pms == NULL) {
6331 return PSA_ERROR_INSUFFICIENT_MEMORY;
6332 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006333 uint8_t *cur = pms;
6334
6335 /* pure-PSK:
6336 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006337 *
6338 * The premaster secret is formed as follows: if the PSK is N octets
6339 * long, concatenate a uint16 with the value N, N zero octets, a second
6340 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006341 *
6342 * mixed-PSK:
6343 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6344 * follows: concatenate a uint16 with the length of the other secret,
6345 * the other secret itself, uint16 with the length of PSK, and the
6346 * PSK itself.
6347 * For details please check:
6348 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6349 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6350 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006351 */
6352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6354 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6355 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6356 if (prf->other_secret_length != 0) {
6357 memcpy(cur, prf->other_secret, prf->other_secret_length);
6358 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006359 cur += prf->other_secret_length;
6360 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 } else {
6362 *cur++ = MBEDTLS_BYTE_1(data_length);
6363 *cur++ = MBEDTLS_BYTE_0(data_length);
6364 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006365 cur += data_length;
6366 }
6367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 *cur++ = MBEDTLS_BYTE_1(data_length);
6369 *cur++ = MBEDTLS_BYTE_0(data_length);
6370 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006371 cur += data_length;
6372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 mbedtls_platform_zeroize(pms, pms_len);
6376 mbedtls_free(pms);
6377 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006378}
Janos Follath6660f0e2019-06-17 08:44:03 +01006379
Przemek Stekiele47201b2022-04-19 13:53:28 +02006380static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6381 psa_tls12_prf_key_derivation_t *prf,
6382 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006384{
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6386 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006387 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006388
6389 if (data_length != 0) {
6390 prf->other_secret = mbedtls_calloc(1, data_length);
6391 if (prf->other_secret == NULL) {
6392 return PSA_ERROR_INSUFFICIENT_MEMORY;
6393 }
6394
6395 memcpy(prf->other_secret, data, data_length);
6396 prf->other_secret_length = data_length;
6397 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006398 prf->other_secret_length = 0;
6399 }
6400
6401 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6402
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006404}
6405
Janos Follath6660f0e2019-06-17 08:44:03 +01006406static psa_status_t psa_tls12_prf_psk_to_ms_input(
6407 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006408 psa_key_derivation_step_t step,
6409 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006411{
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006413 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 return psa_tls12_prf_psk_to_ms_set_key(prf,
6415 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006416 break;
6417 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6419 data,
6420 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006421 break;
6422 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006424 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006425
Przemek Stekiele47201b2022-04-19 13:53:28 +02006426 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006427}
John Durkop07cc04a2020-11-16 22:08:34 -08006428#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006429
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006430#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6431static psa_status_t psa_tls12_ecjpake_to_pms_input(
6432 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006433 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006434 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006436{
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6438 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6439 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006440 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006441
6442 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 if (data[0] != 0x04) {
6444 return PSA_ERROR_INVALID_ARGUMENT;
6445 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006446
6447 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006449
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006451}
6452#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306453
6454#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6455static psa_status_t psa_pbkdf2_set_input_cost(
6456 psa_pbkdf2_key_derivation_t *pbkdf2,
6457 psa_key_derivation_step_t step,
6458 uint64_t data)
6459{
6460 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6461 return PSA_ERROR_INVALID_ARGUMENT;
6462 }
6463
6464 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6465 return PSA_ERROR_BAD_STATE;
6466 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306467
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306468 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306469 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306470 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306471
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306472 if (data == 0) {
6473 return PSA_ERROR_INVALID_ARGUMENT;
6474 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306475
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306476 pbkdf2->input_cost = data;
6477 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6478
6479 return PSA_SUCCESS;
6480}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306481
6482static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6483 const uint8_t *data,
6484 size_t data_length)
6485{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306486 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6487 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6488 return PSA_ERROR_BAD_STATE;
6489 }
6490
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306491 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6492 pbkdf2->salt = mbedtls_calloc(1, data_length);
6493 if (pbkdf2->salt == NULL) {
6494 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306495 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306496
6497 memcpy(pbkdf2->salt, data, data_length);
6498 pbkdf2->salt_length = data_length;
6499 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306500 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306501
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306502 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6503 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306504 return PSA_ERROR_INSUFFICIENT_MEMORY;
6505 }
6506
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306507 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6508 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306509 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306510 mbedtls_free(pbkdf2->salt);
6511 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306512 }
6513
6514 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6515
6516 return PSA_SUCCESS;
6517}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306518
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306519static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306520 const uint8_t *input,
6521 size_t input_len,
6522 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306523 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306524{
6525 psa_status_t status = PSA_SUCCESS;
6526 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6527 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306528 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306529 } else {
6530 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306531 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306532 }
6533 return status;
6534}
6535
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306536static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306537 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306538 const uint8_t *data,
6539 size_t data_length)
6540{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306541 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306542 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6543 return PSA_ERROR_BAD_STATE;
6544 }
6545
6546 if (data_length != 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306547 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6548 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6549 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6550 pbkdf2->password,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306551 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306552 }
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306553 }
6554
6555 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6556
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306557 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306558}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306559
6560static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306561 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306562 psa_key_derivation_step_t step,
6563 const uint8_t *data,
6564 size_t data_length)
6565{
6566 switch (step) {
6567 case PSA_KEY_DERIVATION_INPUT_SALT:
6568 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6569 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306570 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306571 default:
6572 return PSA_ERROR_INVALID_ARGUMENT;
6573 }
6574}
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306575#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6576
Gilles Peskineb8965192019-09-24 16:21:10 +02006577/** Check whether the given key type is acceptable for the given
6578 * input step of a key derivation.
6579 *
6580 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6581 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6582 * Both secret and non-secret inputs can alternatively have the type
6583 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6584 * that the input was passed as a buffer rather than via a key object.
6585 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006586static int psa_key_derivation_check_input_type(
6587 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006589{
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006591 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 if (key_type == PSA_KEY_TYPE_DERIVE) {
6593 return PSA_SUCCESS;
6594 }
6595 if (key_type == PSA_KEY_TYPE_NONE) {
6596 return PSA_SUCCESS;
6597 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006598 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006599 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 if (key_type == PSA_KEY_TYPE_DERIVE) {
6601 return PSA_SUCCESS;
6602 }
6603 if (key_type == PSA_KEY_TYPE_NONE) {
6604 return PSA_SUCCESS;
6605 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006606 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006607 case PSA_KEY_DERIVATION_INPUT_LABEL:
6608 case PSA_KEY_DERIVATION_INPUT_SALT:
6609 case PSA_KEY_DERIVATION_INPUT_INFO:
6610 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6612 return PSA_SUCCESS;
6613 }
6614 if (key_type == PSA_KEY_TYPE_NONE) {
6615 return PSA_SUCCESS;
6616 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006617 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306618 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6619 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6620 return PSA_SUCCESS;
6621 }
6622 if (key_type == PSA_KEY_TYPE_DERIVE) {
6623 return PSA_SUCCESS;
6624 }
6625 if (key_type == PSA_KEY_TYPE_NONE) {
6626 return PSA_SUCCESS;
6627 }
6628 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006629 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006631}
6632
Janos Follathb80a94e2019-06-12 15:54:46 +01006633static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006634 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006635 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006636 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006637 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006639{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006640 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006642
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 status = psa_key_derivation_check_input_type(step, key_type);
6644 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006645 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006647
Przemek Stekiel69c46792022-06-10 12:59:51 +02006648#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006649 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6650 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6651 step, data, data_length);
6652 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006653#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006654#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6656 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6657 step, data, data_length);
6658 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006659#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6660#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6662 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6663 step, data, data_length);
6664 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006665#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006666#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006668 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6670 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006671#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306672#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6673 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306674 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6675 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306676 } else
6677#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006678 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006679 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006680 (void) data;
6681 (void) data_length;
6682 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006684 }
6685
Gilles Peskine224b0d62019-09-23 18:13:17 +02006686exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 if (status != PSA_SUCCESS) {
6688 psa_key_derivation_abort(operation);
6689 }
6690 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006691}
6692
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306693static psa_status_t psa_key_derivation_input_integer_internal(
6694 psa_key_derivation_operation_t *operation,
6695 psa_key_derivation_step_t step,
6696 uint64_t value)
6697{
6698 psa_status_t status;
6699 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6700
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306701#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6702 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6703 status = psa_pbkdf2_set_input_cost(
6704 &operation->ctx.pbkdf2, step, value);
6705 } else
6706#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306707 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306708 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306709 (void) value;
6710 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306711 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306712 }
6713
6714 if (status != PSA_SUCCESS) {
6715 psa_key_derivation_abort(operation);
6716 }
6717 return status;
6718}
6719
Janos Follath51f4a0f2019-06-14 11:35:55 +01006720psa_status_t psa_key_derivation_input_bytes(
6721 psa_key_derivation_operation_t *operation,
6722 psa_key_derivation_step_t step,
6723 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006725{
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 return psa_key_derivation_input_internal(operation, step,
6727 PSA_KEY_TYPE_NONE,
6728 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006729}
6730
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306731psa_status_t psa_key_derivation_input_integer(
6732 psa_key_derivation_operation_t *operation,
6733 psa_key_derivation_step_t step,
6734 uint64_t value)
6735{
6736 return psa_key_derivation_input_integer_internal(operation, step, value);
6737}
6738
Janos Follath51f4a0f2019-06-14 11:35:55 +01006739psa_status_t psa_key_derivation_input_key(
6740 psa_key_derivation_operation_t *operation,
6741 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006743{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006744 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006745 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006746 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006747
Ronald Cron5c522922020-11-14 16:35:34 +01006748 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6750 if (status != PSA_SUCCESS) {
6751 psa_key_derivation_abort(operation);
6752 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006753 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006754
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306755 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6756 * permission to output to a key object. */
6757 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6758 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006759 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006761
Gilles Peskine449bd832023-01-11 14:50:10 +01006762 status = psa_key_derivation_input_internal(operation,
6763 step, slot->attr.type,
6764 slot->key.data,
6765 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006766
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006768
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006770}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006771
6772
6773
6774/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006775/* Key agreement */
6776/****************************************************************/
6777
Gilles Peskine449bd832023-01-11 14:50:10 +01006778psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6779 const uint8_t *key_buffer,
6780 size_t key_buffer_size,
6781 psa_algorithm_t alg,
6782 const uint8_t *peer_key,
6783 size_t peer_key_length,
6784 uint8_t *shared_secret,
6785 size_t shared_secret_size,
6786 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006787{
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006789#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006790 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6792 key_buffer_size, alg,
6793 peer_key, peer_key_length,
6794 shared_secret,
6795 shared_secret_size,
6796 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006797#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006798
6799#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6800 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006801 return mbedtls_psa_key_agreement_ffdh(attributes,
6802 peer_key,
6803 peer_key_length,
6804 key_buffer,
6805 key_buffer_size,
6806 shared_secret,
6807 shared_secret_size,
6808 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006809#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6810
Gilles Peskine01d718c2018-09-18 12:01:02 +02006811 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006812 (void) attributes;
6813 (void) key_buffer;
6814 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006815 (void) peer_key;
6816 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006817 (void) shared_secret;
6818 (void) shared_secret_size;
6819 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006821 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006822}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006823
Aditya Deshpande17845b82022-10-13 17:21:01 +01006824/** Internal function for raw key agreement
6825 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006826 * to the driver's implementation if a driver is present.
6827 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006828 * (psa_key_agreement_raw_builtin).
6829 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006830static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6831 psa_key_slot_t *private_key,
6832 const uint8_t *peer_key,
6833 size_t peer_key_length,
6834 uint8_t *shared_secret,
6835 size_t shared_secret_size,
6836 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006837{
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6839 return PSA_ERROR_NOT_SUPPORTED;
6840 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006841
6842 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006844 };
6845
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 return psa_driver_wrapper_key_agreement(&attributes,
6847 private_key->key.data,
6848 private_key->key.bytes, alg,
6849 peer_key, peer_key_length,
6850 shared_secret,
6851 shared_secret_size,
6852 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006853}
6854
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006855/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006856 * to potentially free embedded data structures and wipe confidential data.
6857 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006858static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6859 psa_key_derivation_step_t step,
6860 psa_key_slot_t *private_key,
6861 const uint8_t *peer_key,
6862 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006863{
6864 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006865 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006866 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006868
6869 /* Step 1: run the secret agreement algorithm to generate the shared
6870 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 status = psa_key_agreement_raw_internal(ka_alg,
6872 private_key,
6873 peer_key, peer_key_length,
6874 shared_secret,
6875 sizeof(shared_secret),
6876 &shared_secret_length);
6877 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006878 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006880
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006881 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006882 * the shared secret. A shared secret is permitted wherever a key
6883 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 status = psa_key_derivation_input_internal(operation, step,
6885 PSA_KEY_TYPE_DERIVE,
6886 shared_secret,
6887 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006888exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6890 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006891}
6892
Gilles Peskine449bd832023-01-11 14:50:10 +01006893psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6894 psa_key_derivation_step_t step,
6895 mbedtls_svc_key_id_t private_key,
6896 const uint8_t *peer_key,
6897 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006898{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006900 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006901 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006902
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6904 return PSA_ERROR_INVALID_ARGUMENT;
6905 }
Ronald Cron5c522922020-11-14 16:35:34 +01006906 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6908 if (status != PSA_SUCCESS) {
6909 return status;
6910 }
6911 status = psa_key_agreement_internal(operation, step,
6912 slot,
6913 peer_key, peer_key_length);
6914 if (status != PSA_SUCCESS) {
6915 psa_key_derivation_abort(operation);
6916 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006917 /* If a private key has been added as SECRET, we allow the derived
6918 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006919 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006920 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006922 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006923
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006925
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006927}
6928
Gilles Peskine449bd832023-01-11 14:50:10 +01006929psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6930 mbedtls_svc_key_id_t private_key,
6931 const uint8_t *peer_key,
6932 size_t peer_key_length,
6933 uint8_t *output,
6934 size_t output_size,
6935 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006936{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006937 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006938 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006939 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006942 status = PSA_ERROR_INVALID_ARGUMENT;
6943 goto exit;
6944 }
Ronald Cron5c522922020-11-14 16:35:34 +01006945 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6947 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006948 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006950
Gilles Peskine3e561302022-04-14 00:17:15 +02006951 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6952 * for the output size. The PSA specification only guarantees that this
6953 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6954 * but it might be nice to allow smaller buffers if the output fits.
6955 * At the time of writing this comment, with only ECDH implemented,
6956 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6957 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6958 * be exact for it as well. */
6959 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6961 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006962 status = PSA_ERROR_BUFFER_TOO_SMALL;
6963 goto exit;
6964 }
6965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 status = psa_key_agreement_raw_internal(alg, slot,
6967 peer_key, peer_key_length,
6968 output, output_size,
6969 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006970
6971exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006972 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006973 /* If an error happens and is not handled properly, the output
6974 * may be used as a key to protect sensitive data. Arrange for such
6975 * a key to be random, which is likely to result in decryption or
6976 * verification errors. This is better than filling the buffer with
6977 * some constant data such as zeros, which would result in the data
6978 * being protected with a reproducible, easily knowable key.
6979 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006981 *output_length = output_size;
6982 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006983
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006985
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006987}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006988
6989
Gilles Peskine30524eb2020-11-13 17:02:26 +01006990
Gilles Peskine86a440b2018-11-12 18:39:40 +01006991/****************************************************************/
6992/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006993/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006994
Gilles Peskine30524eb2020-11-13 17:02:26 +01006995/** Initialize the PSA random generator.
6996 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006997static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006998{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006999#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007001#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7002
Gilles Peskine30524eb2020-11-13 17:02:26 +01007003 /* Set default configuration if
7004 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007006 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 }
7008 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007009 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007011
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007013#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7014 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7015 /* The PSA entropy injection feature depends on using NV seed as an entropy
7016 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 mbedtls_entropy_add_source(&rng->entropy,
7018 mbedtls_nv_seed_poll, NULL,
7019 MBEDTLS_ENTROPY_BLOCK_SIZE,
7020 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007021#endif
7022
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007024#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007025}
7026
7027/** Deinitialize the PSA random generator.
7028 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007029static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007030{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007031#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007032 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007033#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7035 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007036#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007037}
7038
7039/** Seed the PSA random generator.
7040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007041static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007042{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007043#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7044 /* Do nothing: the external RNG seeds itself. */
7045 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007047#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007048 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007049 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7050 drbg_seed, sizeof(drbg_seed) - 1);
7051 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007052#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007053}
7054
Gilles Peskine449bd832023-01-11 14:50:10 +01007055psa_status_t psa_generate_random(uint8_t *output,
7056 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007057{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007058 GUARD_MODULE_INITIALIZED;
7059
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007060#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7061
7062 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7064 output, output_size,
7065 &output_length);
7066 if (status != PSA_SUCCESS) {
7067 return status;
7068 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007069 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007070 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 if (output_length != output_size) {
7072 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7073 }
7074 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007075
7076#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7077
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007079 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7081 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7082 output_size);
7083 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7084 output, request_size);
7085 if (ret != 0) {
7086 return mbedtls_to_psa_error(ret);
7087 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007088 output_size -= request_size;
7089 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007090 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007092#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007093}
7094
Gilles Peskine8814fc42020-12-14 15:33:44 +01007095/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007096 *
7097 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7098 * `psa_generate_random(...)`. The state parameter is ignored since the
7099 * PSA API doesn't support passing an explicit state.
7100 *
7101 * In the non-external case, psa_generate_random() calls an
7102 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7103 * and semantics as mbedtls_psa_get_random(). As an optimization,
7104 * instead of doing this back-and-forth between the PSA API and the
7105 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7106 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007108#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7109int mbedtls_psa_get_random(void *p_rng,
7110 unsigned char *output,
7111 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007112{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007113 /* This function takes a pointer to the RNG state because that's what
7114 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7115 * its own state internally and doesn't let the caller access that state.
7116 * So we just ignore the state parameter, and in practice we'll pass
7117 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007118 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 psa_status_t status = psa_generate_random(output, output_size);
7120 if (status == PSA_SUCCESS) {
7121 return 0;
7122 } else {
7123 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7124 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007125}
7126#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7127
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007128#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00007129#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007130
Gilles Peskine449bd832023-01-11 14:50:10 +01007131psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7132 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007133{
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 if (global_data.initialized) {
7135 return PSA_ERROR_NOT_PERMITTED;
7136 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007137
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7139 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7140 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7141 return PSA_ERROR_INVALID_ARGUMENT;
7142 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007143
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007145}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007146#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007147
Ronald Cron3772afe2021-02-08 16:10:05 +01007148/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007149 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007150 * \param type The key type
7151 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007152 *
7153 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007154 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007155 * \retval #PSA_ERROR_INVALID_ARGUMENT
7156 * The size in bits of the key is not valid.
7157 * \retval #PSA_ERROR_NOT_SUPPORTED
7158 * The type and/or the size in bits of the key or the combination of
7159 * the two is not supported.
7160 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007161static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007163{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007165
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 if (key_type_is_raw_bytes(type)) {
7167 status = psa_validate_unstructured_key_bit_size(type, bits);
7168 if (status != PSA_SUCCESS) {
7169 return status;
7170 }
7171 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007172#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7174 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7175 return PSA_ERROR_NOT_SUPPORTED;
7176 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007177
Ronald Cron01b2aba2020-10-05 09:42:02 +02007178 /* Accept only byte-aligned keys, for the same reasons as
7179 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007180 if (bits % 8 != 0) {
7181 return PSA_ERROR_NOT_SUPPORTED;
7182 }
7183 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007184#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007185
Ronald Cron5c4d3862020-12-07 11:07:24 +01007186#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007188 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 return PSA_SUCCESS;
7190 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007191#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007192
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007193#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007194 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007195 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007196 return PSA_ERROR_NOT_SUPPORTED;
7197 }
7198 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007199#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007200 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007202 }
7203
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007205}
7206
Ronald Cron55ed0592020-10-05 10:30:40 +02007207psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007208 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007210{
7211 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007212 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007213
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 if ((attributes->domain_parameters == NULL) &&
7215 (attributes->domain_parameters_size != 0)) {
7216 return PSA_ERROR_INVALID_ARGUMENT;
7217 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007218
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 if (key_type_is_raw_bytes(type)) {
7220 status = psa_generate_random(key_buffer, key_buffer_size);
7221 if (status != PSA_SUCCESS) {
7222 return status;
7223 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007224
David Brown12ca5032021-02-11 11:02:00 -07007225#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 if (type == PSA_KEY_TYPE_DES) {
7227 psa_des_set_key_parity(key_buffer, key_buffer_size);
7228 }
David Brown8107e312021-02-12 08:08:46 -07007229#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007231
Jaeden Amero424fa932021-05-14 08:34:32 +01007232#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7233 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7235 return mbedtls_psa_rsa_generate_key(attributes,
7236 key_buffer,
7237 key_buffer_size,
7238 key_buffer_length);
7239 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007240#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7241 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007242
John Durkop9814fa22020-11-04 12:28:15 -08007243#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7245 return mbedtls_psa_ecp_generate_key(attributes,
7246 key_buffer,
7247 key_buffer_size,
7248 key_buffer_length);
7249 } else
John Durkop9814fa22020-11-04 12:28:15 -08007250#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007251
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007252#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007253 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7254 return mbedtls_psa_ffdh_generate_key(attributes,
7255 key_buffer,
7256 key_buffer_size,
7257 key_buffer_length);
7258 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007259#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007260 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 (void) key_buffer_length;
7262 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007263 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007264
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007266}
Darryl Green0c6575a2018-11-07 16:05:30 +00007267
Gilles Peskine449bd832023-01-11 14:50:10 +01007268psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7269 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007270{
7271 psa_status_t status;
7272 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007273 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007274 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007275
Ronald Cron81709fc2020-11-14 12:10:32 +01007276 *key = MBEDTLS_SVC_KEY_ID_INIT;
7277
Gilles Peskine0f84d622019-09-12 19:03:13 +02007278 /* Reject any attempt to create a zero-length key so that we don't
7279 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 if (psa_get_key_bits(attributes) == 0) {
7281 return PSA_ERROR_INVALID_ARGUMENT;
7282 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007283
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007284 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007285 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7286 return PSA_ERROR_INVALID_ARGUMENT;
7287 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007288
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7290 &slot, &driver);
7291 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007292 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 }
Gilles Peskine11792082019-08-06 18:36:36 +02007294
Ronald Cron977c2472020-10-13 08:32:21 +02007295 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307296 * storage ( thus not in the case of generating a key in a secure element
7297 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7298 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007299 if (slot->key.data == NULL) {
7300 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7301 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007302 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007303 attributes->core.type, attributes->core.bits);
7304 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007305 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007306 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007307
7308 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 attributes->core.type,
7310 attributes->core.bits);
7311 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007312 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 attributes, &key_buffer_size);
7314 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007315 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 }
Ronald Cron977c2472020-10-13 08:32:21 +02007317 }
Ronald Cron977c2472020-10-13 08:32:21 +02007318
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7320 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007321 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 }
Ronald Cron977c2472020-10-13 08:32:21 +02007323 }
7324
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 status = psa_driver_wrapper_generate_key(attributes,
7326 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007327
Gilles Peskine449bd832023-01-11 14:50:10 +01007328 if (status != PSA_SUCCESS) {
7329 psa_remove_key_data_from_memory(slot);
7330 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007331
Gilles Peskine11792082019-08-06 18:36:36 +02007332exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 if (status == PSA_SUCCESS) {
7334 status = psa_finish_key_creation(slot, driver, key);
7335 }
7336 if (status != PSA_SUCCESS) {
7337 psa_fail_key_creation(slot, driver);
7338 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007339
Gilles Peskine449bd832023-01-11 14:50:10 +01007340 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007341}
7342
Gilles Peskinee59236f2018-01-27 23:32:46 +01007343/****************************************************************/
7344/* Module setup */
7345/****************************************************************/
7346
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007347#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007348psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 void (* entropy_init)(mbedtls_entropy_context *ctx),
7350 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007351{
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7353 return PSA_ERROR_BAD_STATE;
7354 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007355 global_data.rng.entropy_init = entropy_init;
7356 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007357 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007358}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007359#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007360
Gilles Peskine449bd832023-01-11 14:50:10 +01007361void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007362{
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 psa_wipe_all_key_slots();
7364 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7365 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007366 }
7367 /* Wipe all remaining data, including configuration.
7368 * In particular, this sets all state indicator to the value
7369 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007370 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007371
7372 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007374}
7375
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007376#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7377/** Recover a transaction that was interrupted by a power failure.
7378 *
7379 * This function is called during initialization, before psa_crypto_init()
7380 * returns. If this function returns a failure status, the initialization
7381 * fails.
7382 */
7383static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007385{
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007387 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7388 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007389 /* TODO - fall through to the failure case until this
7390 * is implemented.
7391 * https://github.com/ARMmbed/mbed-crypto/issues/218
7392 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007393 default:
7394 /* We found an unsupported transaction in the storage.
7395 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007397 }
7398}
7399#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7400
Gilles Peskine449bd832023-01-11 14:50:10 +01007401psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007402{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007403 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007404
Gilles Peskinec6b69072018-11-20 21:42:52 +01007405 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007406 if (global_data.initialized != 0) {
7407 return PSA_SUCCESS;
7408 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007409
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007410 /* Init drivers */
7411 status = psa_driver_wrapper_init();
7412 if (status != PSA_SUCCESS) {
7413 goto exit;
7414 }
7415 global_data.drivers_initialized = 1;
7416
Gilles Peskine30524eb2020-11-13 17:02:26 +01007417 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007419 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 status = mbedtls_psa_random_seed(&global_data.rng);
7421 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007422 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007424 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007425
Gilles Peskine449bd832023-01-11 14:50:10 +01007426 status = psa_initialize_key_slots();
7427 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007428 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007430
Gilles Peskine4b734222019-07-24 15:56:31 +02007431#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 status = psa_crypto_load_transaction();
7433 if (status == PSA_SUCCESS) {
7434 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7435 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007436 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 }
7438 status = psa_crypto_stop_transaction();
7439 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007440 /* There's no transaction to complete. It's all good. */
7441 status = PSA_SUCCESS;
7442 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007443#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007444
Gilles Peskinec6b69072018-11-20 21:42:52 +01007445 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007446 global_data.initialized = 1;
7447
7448exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 if (status != PSA_SUCCESS) {
7450 mbedtls_psa_crypto_free();
7451 }
7452 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007453}
7454
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007455psa_status_t psa_crypto_driver_pake_get_password_len(
7456 const psa_crypto_driver_pake_inputs_t *inputs,
7457 size_t *password_len)
7458{
7459 if (inputs->password_len == 0) {
7460 return PSA_ERROR_BAD_STATE;
7461 }
7462
7463 *password_len = inputs->password_len;
7464
7465 return PSA_SUCCESS;
7466}
7467
7468psa_status_t psa_crypto_driver_pake_get_password(
7469 const psa_crypto_driver_pake_inputs_t *inputs,
7470 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7471{
7472 if (inputs->password_len == 0) {
7473 return PSA_ERROR_BAD_STATE;
7474 }
7475
7476 if (buffer_size < inputs->password_len) {
7477 return PSA_ERROR_BUFFER_TOO_SMALL;
7478 }
7479
7480 memcpy(buffer, inputs->password, inputs->password_len);
7481 *buffer_length = inputs->password_len;
7482
7483 return PSA_SUCCESS;
7484}
7485
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007486psa_status_t psa_crypto_driver_pake_get_user_len(
7487 const psa_crypto_driver_pake_inputs_t *inputs,
7488 size_t *user_len)
7489{
7490 if (inputs->user_len == 0) {
7491 return PSA_ERROR_BAD_STATE;
7492 }
7493
7494 *user_len = inputs->user_len;
7495
7496 return PSA_SUCCESS;
7497}
7498
7499psa_status_t psa_crypto_driver_pake_get_user(
7500 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007501 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007502{
7503 if (inputs->user_len == 0) {
7504 return PSA_ERROR_BAD_STATE;
7505 }
7506
Przemek Stekielc0e62502023-03-14 11:49:36 +01007507 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007508 return PSA_ERROR_BUFFER_TOO_SMALL;
7509 }
7510
Przemek Stekielc0e62502023-03-14 11:49:36 +01007511 memcpy(user_id, inputs->user, inputs->user_len);
7512 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007513
7514 return PSA_SUCCESS;
7515}
7516
7517psa_status_t psa_crypto_driver_pake_get_peer_len(
7518 const psa_crypto_driver_pake_inputs_t *inputs,
7519 size_t *peer_len)
7520{
7521 if (inputs->peer_len == 0) {
7522 return PSA_ERROR_BAD_STATE;
7523 }
7524
7525 *peer_len = inputs->peer_len;
7526
7527 return PSA_SUCCESS;
7528}
7529
7530psa_status_t psa_crypto_driver_pake_get_peer(
7531 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007532 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007533{
7534 if (inputs->peer_len == 0) {
7535 return PSA_ERROR_BAD_STATE;
7536 }
7537
Przemek Stekielc0e62502023-03-14 11:49:36 +01007538 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007539 return PSA_ERROR_BUFFER_TOO_SMALL;
7540 }
7541
Przemek Stekielc0e62502023-03-14 11:49:36 +01007542 memcpy(peer_id, inputs->peer, inputs->peer_len);
7543 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007544
7545 return PSA_SUCCESS;
7546}
7547
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007548psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7549 const psa_crypto_driver_pake_inputs_t *inputs,
7550 psa_pake_cipher_suite_t *cipher_suite)
7551{
7552 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7553 return PSA_ERROR_BAD_STATE;
7554 }
7555
7556 *cipher_suite = inputs->cipher_suite;
7557
7558 return PSA_SUCCESS;
7559}
7560
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01007561#if defined(PSA_WANT_ALG_SOME_PAKE)
Neil Armstronga7d08c32022-06-01 18:21:20 +02007562psa_status_t psa_pake_setup(
7563 psa_pake_operation_t *operation,
7564 const psa_pake_cipher_suite_t *cipher_suite)
7565{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007566 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007567
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007568 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007569 status = PSA_ERROR_BAD_STATE;
7570 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007571 }
7572
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007573 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007574 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007575 status = PSA_ERROR_INVALID_ARGUMENT;
7576 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007577 }
7578
Przemek Stekiel51eac532022-12-07 11:04:51 +01007579 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7580
Przemek Stekiele12ed362022-12-21 12:54:46 +01007581 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007582 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7583 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007584 operation->data.inputs.cipher_suite = *cipher_suite;
7585
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007586#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007587 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007588 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007589 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007590
Przemek Stekiele12ed362022-12-21 12:54:46 +01007591 computation_stage->state = PSA_PAKE_STATE_SETUP;
7592 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7593 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7594 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007595 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007596#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007597 {
7598 status = PSA_ERROR_NOT_SUPPORTED;
7599 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007600 }
7601
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007602 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7603
Przemek Stekiel51eac532022-12-07 11:04:51 +01007604 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007605exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007606 psa_pake_abort(operation);
7607 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007608}
7609
7610psa_status_t psa_pake_set_password_key(
7611 psa_pake_operation_t *operation,
7612 mbedtls_svc_key_id_t password)
7613{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007614 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007615 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7616 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007617
Przemek Stekiel51eac532022-12-07 11:04:51 +01007618 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007619 status = PSA_ERROR_BAD_STATE;
7620 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007621 }
7622
Przemek Stekiel6c764412022-11-22 14:05:12 +01007623 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7624 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007625 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007626 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007627 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007628 }
7629
Przemek Stekiel6c764412022-11-22 14:05:12 +01007630 psa_key_attributes_t attributes = {
7631 .core = slot->attr
7632 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007633
Przemek Stekiel51eac532022-12-07 11:04:51 +01007634 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007635
Przemek Stekiel51eac532022-12-07 11:04:51 +01007636 if (type != PSA_KEY_TYPE_PASSWORD &&
7637 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7638 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007639 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007640 }
7641
Przemek Stekiel51eac532022-12-07 11:04:51 +01007642 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7643 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007644 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007645 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007646 }
7647
7648 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7649 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007650 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007651exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007652 if (status != PSA_SUCCESS) {
7653 psa_pake_abort(operation);
7654 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007655 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007656 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007657}
7658
7659psa_status_t psa_pake_set_user(
7660 psa_pake_operation_t *operation,
7661 const uint8_t *user_id,
7662 size_t user_id_len)
7663{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007665
7666 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007667 status = PSA_ERROR_BAD_STATE;
7668 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007669 }
7670
Przemek Stekiel51eac532022-12-07 11:04:51 +01007671 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007672 status = PSA_ERROR_INVALID_ARGUMENT;
7673 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007674 }
7675
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007676 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007677 status = PSA_ERROR_BAD_STATE;
7678 goto exit;
7679 }
7680
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007681 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7682 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007683 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7684 goto exit;
7685 }
7686
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007687 memcpy(operation->data.inputs.user, user_id, user_id_len);
7688 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007689
7690 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007691exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007692 psa_pake_abort(operation);
7693 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007694}
7695
7696psa_status_t psa_pake_set_peer(
7697 psa_pake_operation_t *operation,
7698 const uint8_t *peer_id,
7699 size_t peer_id_len)
7700{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007701 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007702
7703 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007704 status = PSA_ERROR_BAD_STATE;
7705 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007706 }
7707
Przemek Stekiel51eac532022-12-07 11:04:51 +01007708 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007709 status = PSA_ERROR_INVALID_ARGUMENT;
7710 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007711 }
7712
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007713 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007714 status = PSA_ERROR_BAD_STATE;
7715 goto exit;
7716 }
7717
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007718 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7719 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007720 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7721 goto exit;
7722 }
7723
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007724 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7725 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007726
7727 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007728exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007729 psa_pake_abort(operation);
7730 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007731}
7732
7733psa_status_t psa_pake_set_role(
7734 psa_pake_operation_t *operation,
7735 psa_pake_role_t role)
7736{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007737 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007738
Przemek Stekiel51eac532022-12-07 11:04:51 +01007739 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007740 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007741 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007742 }
7743
Przemek Stekiel09104b82023-03-06 14:11:51 +01007744 switch (operation->alg) {
7745#if defined(PSA_WANT_ALG_JPAKE)
7746 case PSA_ALG_JPAKE:
7747 if (role == PSA_PAKE_ROLE_NONE) {
7748 return PSA_SUCCESS;
7749 }
7750 status = PSA_ERROR_INVALID_ARGUMENT;
7751 break;
7752#endif
7753 default:
7754 (void) role;
7755 status = PSA_ERROR_NOT_SUPPORTED;
7756 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007757 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007758exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007759 psa_pake_abort(operation);
7760 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007761}
7762
Przemek Stekielb09c4872023-01-17 12:05:38 +01007763/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007764#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007765static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007766 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007767{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007768 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007769 case PSA_PAKE_OUTPUT_X1_X2:
7770 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007771 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007772 case PSA_PAKE_X1_STEP_KEY_SHARE:
7773 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007774 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7775 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007776 case PSA_PAKE_X1_STEP_ZK_PROOF:
7777 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007778 case PSA_PAKE_X2_STEP_KEY_SHARE:
7779 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007780 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7781 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007782 case PSA_PAKE_X2_STEP_ZK_PROOF:
7783 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007784 default:
7785 return PSA_JPAKE_STEP_INVALID;
7786 }
7787 break;
7788 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007789 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007790 case PSA_PAKE_X1_STEP_KEY_SHARE:
7791 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007792 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7793 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007794 case PSA_PAKE_X1_STEP_ZK_PROOF:
7795 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007796 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007797 return PSA_JPAKE_STEP_INVALID;
7798 }
7799 break;
7800 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007801 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007802 case PSA_PAKE_X1_STEP_KEY_SHARE:
7803 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007804 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7805 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007806 case PSA_PAKE_X1_STEP_ZK_PROOF:
7807 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007808 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007809 return PSA_JPAKE_STEP_INVALID;
7810 }
7811 break;
7812 default:
7813 return PSA_JPAKE_STEP_INVALID;
7814 }
7815 return PSA_JPAKE_STEP_INVALID;
7816}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007817#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007818
Przemek Stekiel2797d372022-12-22 11:19:22 +01007819static psa_status_t psa_pake_complete_inputs(
7820 psa_pake_operation_t *operation)
7821{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007822 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007823 /* Create copy of the inputs on stack as inputs share memory
7824 with the driver context which will be setup by the driver. */
7825 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007826
Przemek Stekielfde11282023-03-13 16:06:09 +01007827 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007828 return PSA_ERROR_BAD_STATE;
7829 }
7830
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007831 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007832 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7833 return PSA_ERROR_BAD_STATE;
7834 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007835 }
7836
Przemek Stekiel18620a32023-01-17 16:34:52 +01007837 /* Clear driver context */
7838 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7839
7840 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007841
7842 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007843 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7844 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007845
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007846 /* User and peer are translated to role. */
7847 mbedtls_free(inputs.user);
7848 mbedtls_free(inputs.peer);
7849
Przemek Stekiel2797d372022-12-22 11:19:22 +01007850 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007851#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007852 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007853 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007854 psa_jpake_computation_stage_t *computation_stage =
7855 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007856 computation_stage->state = PSA_PAKE_STATE_READY;
7857 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7858 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7859 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007860 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007861#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007862 {
7863 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007864 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007865 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007866 return status;
7867}
7868
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007869#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007870static psa_status_t psa_jpake_output_prologue(
7871 psa_pake_operation_t *operation,
7872 psa_pake_step_t step)
7873{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007874 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7875 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7876 step != PSA_PAKE_STEP_ZK_PROOF) {
7877 return PSA_ERROR_INVALID_ARGUMENT;
7878 }
7879
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007880 psa_jpake_computation_stage_t *computation_stage =
7881 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007882
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007883 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7884 return PSA_ERROR_BAD_STATE;
7885 }
7886
7887 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7888 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7889 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7890 return PSA_ERROR_BAD_STATE;
7891 }
7892
7893 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7894 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007895 return PSA_ERROR_BAD_STATE;
7896 }
7897
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007898 switch (computation_stage->output_step) {
7899 case PSA_PAKE_STEP_X1_X2:
7900 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007901 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007902 case PSA_PAKE_STEP_X2S:
7903 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007904 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007905 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007906 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007907 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007908
7909 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7910 }
7911
7912 /* Check if step matches current sequence */
7913 switch (computation_stage->sequence) {
7914 case PSA_PAKE_X1_STEP_KEY_SHARE:
7915 case PSA_PAKE_X2_STEP_KEY_SHARE:
7916 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7917 return PSA_ERROR_BAD_STATE;
7918 }
7919 break;
7920
7921 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7922 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7923 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7924 return PSA_ERROR_BAD_STATE;
7925 }
7926 break;
7927
7928 case PSA_PAKE_X1_STEP_ZK_PROOF:
7929 case PSA_PAKE_X2_STEP_ZK_PROOF:
7930 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7931 return PSA_ERROR_BAD_STATE;
7932 }
7933 break;
7934
7935 default:
7936 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007937 }
7938
7939 return PSA_SUCCESS;
7940}
7941
7942static psa_status_t psa_jpake_output_epilogue(
7943 psa_pake_operation_t *operation)
7944{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007945 psa_jpake_computation_stage_t *computation_stage =
7946 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007947
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007948 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007949 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007950 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007951 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007952 computation_stage->state = PSA_PAKE_STATE_READY;
7953 computation_stage->output_step++;
7954 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7955 } else {
7956 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007957 }
7958
7959 return PSA_SUCCESS;
7960}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007961#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007962
Neil Armstronga7d08c32022-06-01 18:21:20 +02007963psa_status_t psa_pake_output(
7964 psa_pake_operation_t *operation,
7965 psa_pake_step_t step,
7966 uint8_t *output,
7967 size_t output_size,
7968 size_t *output_length)
7969{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007970 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007971 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007972 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007973
7974 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007975 status = psa_pake_complete_inputs(operation);
7976 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007977 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007978 }
7979 }
7980
7981 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007982 status = PSA_ERROR_BAD_STATE;
7983 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007984 }
7985
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007986 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007987 status = PSA_ERROR_INVALID_ARGUMENT;
7988 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007989 }
7990
Przemek Stekiele12ed362022-12-21 12:54:46 +01007991 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007992#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007993 case PSA_ALG_JPAKE:
7994 status = psa_jpake_output_prologue(operation, step);
7995 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007996 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007997 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007998 driver_step = convert_jpake_computation_stage_to_driver_step(
7999 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008000 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008001#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008002 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008003 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008004 status = PSA_ERROR_NOT_SUPPORTED;
8005 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008006 }
8007
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008008 status = psa_driver_wrapper_pake_output(operation, driver_step,
8009 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008010
8011 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008012 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008013 }
8014
8015 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008016#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008017 case PSA_ALG_JPAKE:
8018 status = psa_jpake_output_epilogue(operation);
8019 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008020 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008021 }
8022 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008023#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008024 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008025 status = PSA_ERROR_NOT_SUPPORTED;
8026 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008027 }
8028
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008029 return PSA_SUCCESS;
8030exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008031 psa_pake_abort(operation);
8032 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008033}
8034
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008035#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008036static psa_status_t psa_jpake_input_prologue(
8037 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008038 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008039{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008040 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8041 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8042 step != PSA_PAKE_STEP_ZK_PROOF) {
8043 return PSA_ERROR_INVALID_ARGUMENT;
8044 }
8045
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008046 psa_jpake_computation_stage_t *computation_stage =
8047 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008048
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008049 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
8050 return PSA_ERROR_BAD_STATE;
8051 }
8052
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008053 if (computation_stage->state != PSA_PAKE_STATE_READY &&
8054 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
8055 computation_stage->state != PSA_PAKE_INPUT_X4S) {
8056 return PSA_ERROR_BAD_STATE;
8057 }
8058
8059 if (computation_stage->state == PSA_PAKE_STATE_READY) {
8060 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008061 return PSA_ERROR_BAD_STATE;
8062 }
8063
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008064 switch (computation_stage->input_step) {
8065 case PSA_PAKE_STEP_X1_X2:
8066 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008067 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008068 case PSA_PAKE_STEP_X2S:
8069 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008070 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008071 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01008072 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008073 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008074
8075 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
8076 }
8077
8078 /* Check if step matches current sequence */
8079 switch (computation_stage->sequence) {
8080 case PSA_PAKE_X1_STEP_KEY_SHARE:
8081 case PSA_PAKE_X2_STEP_KEY_SHARE:
8082 if (step != PSA_PAKE_STEP_KEY_SHARE) {
8083 return PSA_ERROR_BAD_STATE;
8084 }
8085 break;
8086
8087 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
8088 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
8089 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
8090 return PSA_ERROR_BAD_STATE;
8091 }
8092 break;
8093
8094 case PSA_PAKE_X1_STEP_ZK_PROOF:
8095 case PSA_PAKE_X2_STEP_ZK_PROOF:
8096 if (step != PSA_PAKE_STEP_ZK_PROOF) {
8097 return PSA_ERROR_BAD_STATE;
8098 }
8099 break;
8100
8101 default:
8102 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008103 }
8104
8105 return PSA_SUCCESS;
8106}
8107
Przemek Stekiele12ed362022-12-21 12:54:46 +01008108static psa_status_t psa_jpake_input_epilogue(
8109 psa_pake_operation_t *operation)
8110{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008111 psa_jpake_computation_stage_t *computation_stage =
8112 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008113
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008114 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008115 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008116 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008117 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008118 computation_stage->state = PSA_PAKE_STATE_READY;
8119 computation_stage->input_step++;
8120 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8121 } else {
8122 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008123 }
8124
8125 return PSA_SUCCESS;
8126}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008127#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008128
Neil Armstronga7d08c32022-06-01 18:21:20 +02008129psa_status_t psa_pake_input(
8130 psa_pake_operation_t *operation,
8131 psa_pake_step_t step,
8132 const uint8_t *input,
8133 size_t input_length)
8134{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008135 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008136 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008137 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8138 operation->primitive,
8139 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008140
8141 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008142 status = psa_pake_complete_inputs(operation);
8143 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008144 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008145 }
8146 }
8147
8148 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008149 status = PSA_ERROR_BAD_STATE;
8150 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008151 }
8152
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008153 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008154 status = PSA_ERROR_INVALID_ARGUMENT;
8155 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008156 }
8157
Przemek Stekiele12ed362022-12-21 12:54:46 +01008158 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008159#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008160 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008161 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008162 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008163 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008164 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008165 driver_step = convert_jpake_computation_stage_to_driver_step(
8166 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008167 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008168#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008169 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008170 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008171 status = PSA_ERROR_NOT_SUPPORTED;
8172 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008173 }
8174
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008175 status = psa_driver_wrapper_pake_input(operation, driver_step,
8176 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008177
8178 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008179 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008180 }
8181
8182 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008183#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008184 case PSA_ALG_JPAKE:
8185 status = psa_jpake_input_epilogue(operation);
8186 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008187 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008188 }
8189 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008190#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008191 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008192 status = PSA_ERROR_NOT_SUPPORTED;
8193 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008194 }
8195
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008196 return PSA_SUCCESS;
8197exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008198 psa_pake_abort(operation);
8199 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008200}
8201
8202psa_status_t psa_pake_get_implicit_key(
8203 psa_pake_operation_t *operation,
8204 psa_key_derivation_operation_t *output)
8205{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008206 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008207 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008208 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008209 size_t shared_key_len = 0;
8210
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008211 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008212 status = PSA_ERROR_BAD_STATE;
8213 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008214 }
8215
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008216#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008217 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008218 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008219 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008220 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8221 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008222 status = PSA_ERROR_BAD_STATE;
8223 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008224 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008225 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008226#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008227 {
8228 status = PSA_ERROR_NOT_SUPPORTED;
8229 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008230 }
8231
Przemek Stekiel0c781802022-11-29 14:53:13 +01008232 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8233 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008234 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008235 &shared_key_len);
8236
8237 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008238 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008239 }
8240
8241 status = psa_key_derivation_input_bytes(output,
8242 PSA_KEY_DERIVATION_INPUT_SECRET,
8243 shared_key,
8244 shared_key_len);
8245
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008246 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008247exit:
8248 abort_status = psa_pake_abort(operation);
8249 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008250}
8251
8252psa_status_t psa_pake_abort(
8253 psa_pake_operation_t *operation)
8254{
Przemek Stekield69dca92023-01-31 19:59:20 +01008255 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008256
Przemek Stekield69dca92023-01-31 19:59:20 +01008257 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008258 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008259 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008260
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008261 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8262 if (operation->data.inputs.password != NULL) {
8263 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008264 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008265 mbedtls_free(operation->data.inputs.password);
8266 }
8267 if (operation->data.inputs.user != NULL) {
8268 mbedtls_free(operation->data.inputs.user);
8269 }
8270 if (operation->data.inputs.peer != NULL) {
8271 mbedtls_free(operation->data.inputs.peer);
8272 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008273 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008274 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008275
Przemek Stekield69dca92023-01-31 19:59:20 +01008276 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008277}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008278#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008279
Gilles Peskinee59236f2018-01-27 23:32:46 +01008280#endif /* MBEDTLS_PSA_CRYPTO_C */