blob: 242eb8571f0197fcf3832e6485a09e03d9d68c57 [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"
Paul Elliott588f8ed2022-12-02 18:10:26 +000085#include "hash_info.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020088
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020089#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
91 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020092#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020093#endif
94
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010095/****************************************************************/
96/* Global data, support functions and library management */
97/****************************************************************/
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100{
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102}
103
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100104/* Values for psa_global_data_t::rng_state */
105#define RNG_NOT_INITIALIZED 0
106#define RNG_INITIALIZED 1
107#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100110 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100111 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100112 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100113 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114} psa_global_data_t;
115
116static psa_global_data_t global_data;
117
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100118#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
119mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
120 &global_data.rng.drbg;
121#endif
122
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (global_data.initialized == 0) \
125 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100127int psa_can_do_hash(psa_algorithm_t hash_alg)
128{
129 (void) hash_alg;
130 return global_data.drivers_initialized;
131}
Przemek Stekiel53410502023-04-28 13:18:43 +0200132#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
133 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
134 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200135static int psa_is_dh_key_size_valid(size_t bits)
136{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200137 if (bits != 2048 && bits != 3072 && bits != 4096 &&
138 bits != 6144 && bits != 8192) {
139 return 0;
140 }
141
142 return 1;
143}
Przemek Stekiel53410502023-04-28 13:18:43 +0200144#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR ||
145 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
146 PSA_WANT_KEY_TYPE_DH_KEY_PAIR */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100149{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100150 /* Mbed TLS error codes can combine a high-level error code and a
151 * low-level error code. The low-level error usually reflects the
152 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 int low_level_ret = -(-ret & 0x007f);
154 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100155 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100157
158 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
159 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200161 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
162 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
163 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
164 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
165 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200167 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200169 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182
Gilles Peskine26869f22019-05-06 15:25:00 +0200183 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200185
186 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200190
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
207 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100208 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
209 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
213 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100217#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100218
219 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100221
Gilles Peskinee59236f2018-01-27 23:32:46 +0100222 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
223 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
224 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100226
227 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200229 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100236 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
237 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100238 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100240 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
241 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100245#endif
246
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
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_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
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_DIVISION_BY_ZERO:
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_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200296 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100312 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
313 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100334 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100336
Paul Elliott588f8ed2022-12-02 18:10:26 +0000337 case MBEDTLS_ERR_ECP_IN_PROGRESS:
338 return PSA_OPERATION_INCOMPLETE;
339
Janos Follatha13b9052019-11-22 12:48:59 +0000340 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300342
Gilles Peskinee59236f2018-01-27 23:32:46 +0100343 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100345 }
346}
347
Paul Elliottdc42ca82023-02-24 18:11:59 +0000348/**
349 * \brief For output buffers which contain "tags"
350 * (outputs that may be checked for validity like
351 * hashes, MACs and signatures), fill the unused
352 * part of the output buffer (the whole buffer on
353 * error, the trailing part on success) with
354 * something that isn't a valid tag (barring an
355 * attack on the tag and deliberately-crafted
356 * input), in case the caller doesn't check the
357 * return status properly.
358 *
359 * \param output_buffer Pointer to buffer to wipe. May not be NULL
360 * unless \p output_buffer_size is zero.
361 * \param status Status of function called to generate
362 * output_buffer originally
363 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
364 * could be NULL.
365 * \param output_buffer_length Length of data written to output_buffer, must be
366 * less than \p output_buffer_size
367 */
368static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000369 size_t output_buffer_size, size_t output_buffer_length)
370{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000371 size_t offset = 0;
372
373 if (output_buffer_size == 0) {
374 /* If output_buffer_size is 0 then we have nothing to do. We must not
375 call memset because output_buffer may be NULL in this case */
376 return;
377 }
378
379 if (status == PSA_SUCCESS) {
380 offset = output_buffer_length;
381 }
382
383 memset(output_buffer + offset, '!', output_buffer_size - offset);
384}
385
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200386
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200387
388
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100389/****************************************************************/
390/* Key management */
391/****************************************************************/
392
Valerio Settid4a5d462023-04-05 18:19:01 +0200393#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100394mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
395 size_t bits,
396 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200397{
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100399 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100401#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100402 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100404#endif
405#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100406 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100408#endif
409#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100410 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100412#endif
413#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100414 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100416#endif
417#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100418 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if (bits_is_sloppy) {
422 return MBEDTLS_ECP_DP_SECP521R1;
423 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100424 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100425#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 }
427 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100428
Paul Elliott8ff510a2020-06-02 17:19:28 +0100429 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100431#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100432 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100434#endif
435#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100436 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100438#endif
439#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100440 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100442#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100443 }
444 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100445
Paul Elliott8ff510a2020-06-02 17:19:28 +0100446 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100448#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (bits_is_sloppy) {
453 return MBEDTLS_ECP_DP_CURVE25519;
454 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100456#endif
457#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100458 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100460#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100461 }
462 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100463
Paul Elliott8ff510a2020-06-02 17:19:28 +0100464 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100466#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100467 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100469#endif
470#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100471 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100473#endif
474#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100475 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100477#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100478 }
479 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200480 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100481
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100482 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200484}
Valerio Settid4a5d462023-04-05 18:19:01 +0200485#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
488 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489{
490 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200493 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200494 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200495 case PSA_KEY_TYPE_PASSWORD:
496 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200497 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100498#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (bits != 128 && bits != 192 && bits != 256) {
501 return PSA_ERROR_INVALID_ARGUMENT;
502 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200503 break;
504#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200505#if defined(PSA_WANT_KEY_TYPE_ARIA)
506 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (bits != 128 && bits != 192 && bits != 256) {
508 return PSA_ERROR_INVALID_ARGUMENT;
509 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200510 break;
511#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100512#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200513 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (bits != 128 && bits != 192 && bits != 256) {
515 return PSA_ERROR_INVALID_ARGUMENT;
516 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517 break;
518#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100519#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200520 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (bits != 64 && bits != 128 && bits != 192) {
522 return PSA_ERROR_INVALID_ARGUMENT;
523 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200524 break;
525#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100526#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200527 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (bits != 256) {
529 return PSA_ERROR_INVALID_ARGUMENT;
530 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200531 break;
532#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200535 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (bits % 8 != 0) {
537 return PSA_ERROR_INVALID_ARGUMENT;
538 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200541}
542
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100543/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100544 *
Steven Cooremancd640932021-03-08 12:00:27 +0100545 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
546 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100547 *
548 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 * \param[in] key_type The key type of the key to be used with the
550 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100551 *
552 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100553 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100555 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100556 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100557MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100558 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100560{
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (PSA_ALG_IS_HMAC(algorithm)) {
562 if (key_type == PSA_KEY_TYPE_HMAC) {
563 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100565 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
568 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
569 * key. */
570 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
571 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
572 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
573 * the block length (larger than 1) for block ciphers. */
574 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
575 return PSA_SUCCESS;
576 }
577 }
578 }
579
580 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100581}
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
584 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200585{
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (slot->key.data != NULL) {
587 return PSA_ERROR_ALREADY_EXISTS;
588 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 slot->key.data = mbedtls_calloc(1, buffer_length);
591 if (slot->key.data == NULL) {
592 return PSA_ERROR_INSUFFICIENT_MEMORY;
593 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200594
Ronald Cronea0f8a62020-11-25 17:52:23 +0100595 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200597}
598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
600 const uint8_t *data,
601 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200602{
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 psa_status_t status = psa_allocate_buffer_to_slot(slot,
604 data_length);
605 if (status != PSA_SUCCESS) {
606 return status;
607 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 memcpy(slot->key.data, data, data_length);
610 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200611}
612
Ronald Crona0fe59f2020-11-29 11:14:53 +0100613psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100614 const psa_key_attributes_t *attributes,
615 const uint8_t *data, size_t data_length,
616 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100619 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
620 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100621
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200622 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if (data_length == 0) {
624 return PSA_ERROR_NOT_SUPPORTED;
625 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (key_type_is_raw_bytes(type)) {
628 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
631 *bits);
632 if (status != PSA_SUCCESS) {
633 return status;
634 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200635
Ronald Crondd04d422020-11-28 15:14:42 +0100636 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100638 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 return PSA_SUCCESS;
642 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800643#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
645 if (PSA_KEY_TYPE_IS_ECC(type)) {
646 return mbedtls_psa_ecp_import_key(attributes,
647 data, data_length,
648 key_buffer, key_buffer_size,
649 key_buffer_length,
650 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100651 }
John Durkop9814fa22020-11-04 12:28:15 -0800652#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
653 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700654#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
656 if (PSA_KEY_TYPE_IS_RSA(type)) {
657 return mbedtls_psa_rsa_import_key(attributes,
658 data, data_length,
659 key_buffer, key_buffer_size,
660 key_buffer_length,
661 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200662 }
John Durkop9814fa22020-11-04 12:28:15 -0800663#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
664 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100665 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100668}
669
Gilles Peskinef603c712019-01-19 13:40:11 +0100670/** Calculate the intersection of two algorithm usage policies.
671 *
672 * Return 0 (which allows no operation) on incompatibility.
673 */
674static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100675 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100676 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100678{
Gilles Peskine549ea862019-05-22 11:45:59 +0200679 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (alg1 == alg2) {
681 return alg1;
682 }
Steven Cooreman03488022021-02-18 12:07:20 +0100683 /* If the policies are from the same hash-and-sign family, check
684 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
686 PSA_ALG_IS_SIGN_HASH(alg2) &&
687 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
688 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
689 return alg2;
690 }
691 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
692 return alg1;
693 }
Steven Cooreman03488022021-02-18 12:07:20 +0100694 }
695 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100696 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100697 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
699 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
700 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
701 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
702 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100703 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100704
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100705 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
707 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
708 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
709 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100710 }
711 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
713 (alg1_len <= alg2_len)) {
714 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100715 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
717 (alg2_len <= alg1_len)) {
718 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100719 }
720 }
721 /* If the policies are from the same MAC family, check whether one
722 * of them is a minimum-MAC-length policy. Calculate the most
723 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
725 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
726 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100727 /* Validate the combination of key type and algorithm. Since the base
728 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
730 return 0;
731 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100732
Steven Cooreman31a876d2021-03-03 20:47:40 +0100733 /* Get the (exact or at-least) output lengths for both sides of the
734 * requested intersection. None of the currently supported algorithms
735 * have an output length dependent on the actual key size, so setting it
736 * to a bogus value of 0 is currently OK.
737 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100738 * Note that for at-least-this-length wildcard algorithms, the output
739 * length is set to the shortest allowed length, which allows us to
740 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
742 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100743 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100744
Steven Cooremana1d83222021-02-25 10:20:29 +0100745 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
747 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
748 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100749 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100750
751 /* If only one is an at-least-this-length policy, the intersection would
752 * be the other (fixed-length) policy as long as said fixed length is
753 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
755 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100756 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
758 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100759 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100760
Steven Cooremancd640932021-03-08 12:00:27 +0100761 /* If none of them are wildcards, check whether they define the same tag
762 * length. This is still possible here when one is default-length and
763 * the other specific-length. Ensure to always return the
764 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (alg1_len == alg2_len) {
766 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
767 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100768 }
769 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100771}
772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773static int psa_key_algorithm_permits(psa_key_type_t key_type,
774 psa_algorithm_t policy_alg,
775 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200776{
Gilles Peskine549ea862019-05-22 11:45:59 +0200777 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (requested_alg == policy_alg) {
779 return 1;
780 }
Steven Cooreman03488022021-02-18 12:07:20 +0100781 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
782 * and requested_alg is the same hash-and-sign family with any hash,
783 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
785 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
786 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
787 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100788 }
789 /* If policy_alg is a wildcard AEAD algorithm of the same base as
790 * the requested algorithm, check the requested tag length to be
791 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (PSA_ALG_IS_AEAD(policy_alg) &&
793 PSA_ALG_IS_AEAD(requested_alg) &&
794 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
795 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
796 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
797 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
798 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100799 }
Steven Cooremancd640932021-03-08 12:00:27 +0100800 /* If policy_alg is a MAC algorithm of the same base as the requested
801 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (PSA_ALG_IS_MAC(policy_alg) &&
803 PSA_ALG_IS_MAC(requested_alg) &&
804 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
805 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100806 /* Validate the combination of key type and algorithm. Since the policy
807 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
809 return 0;
810 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100811
Steven Cooreman31a876d2021-03-03 20:47:40 +0100812 /* Get both the requested output length for the algorithm which is to be
813 * verified, and the default output length for the base algorithm.
814 * Note that none of the currently supported algorithms have an output
815 * length dependent on actual key size, so setting it to a bogus value
816 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100817 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100819 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 key_type, 0,
821 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100822
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100823 /* If the policy is default-length, only allow an algorithm with
824 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
826 return requested_output_length == default_output_length;
827 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100828
829 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100830 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
832 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
833 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100834 }
835
Steven Cooremancd640932021-03-08 12:00:27 +0100836 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
837 * check for the requested MAC length to be equal to or longer than the
838 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
840 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
841 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100842 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200843 }
Steven Cooremance48e852020-10-05 16:02:45 +0200844 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200845 * a key derivation with that key agreement should also be allowed. This
846 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
848 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
849 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
850 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200851 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100852 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200854}
855
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100856/** Test whether a policy permits an algorithm.
857 *
858 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100859 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100860 * \note This function requires providing the key type for which the policy is
861 * being validated, since some algorithm policy definitions (e.g. MAC)
862 * have different properties depending on what kind of cipher it is
863 * combined with.
864 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100865 * \retval PSA_SUCCESS When \p alg is a specific algorithm
866 * allowed by the \p policy.
867 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
868 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
869 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100870 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
872 psa_key_type_t key_type,
873 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100874{
Steven Cooremand788fab2021-02-25 11:29:17 +0100875 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if (alg == 0) {
877 return PSA_ERROR_INVALID_ARGUMENT;
878 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100879
Steven Cooreman7e39f052021-02-23 14:18:32 +0100880 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 if (PSA_ALG_IS_WILDCARD(alg)) {
882 return PSA_ERROR_INVALID_ARGUMENT;
883 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
886 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
887 return PSA_SUCCESS;
888 } else {
889 return PSA_ERROR_NOT_PERMITTED;
890 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100891}
892
Gilles Peskinef603c712019-01-19 13:40:11 +0100893/** Restrict a key policy based on a constraint.
894 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100895 * \note This function requires providing the key type for which the policy is
896 * being restricted, since some algorithm policy definitions (e.g. MAC)
897 * have different properties depending on what kind of cipher it is
898 * combined with.
899 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100900 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100901 * \param[in,out] policy The policy to restrict.
902 * \param[in] constraint The policy constraint to apply.
903 *
904 * \retval #PSA_SUCCESS
905 * \c *policy contains the intersection of the original value of
906 * \c *policy and \c *constraint.
907 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100908 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100909 * \c *policy is unchanged.
910 */
911static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100912 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100913 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100915{
916 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 psa_key_policy_algorithm_intersection(key_type, policy->alg,
918 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200919 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
921 constraint->alg2);
922 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
923 return PSA_ERROR_INVALID_ARGUMENT;
924 }
925 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
926 return PSA_ERROR_INVALID_ARGUMENT;
927 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100928 policy->usage &= constraint->usage;
929 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200930 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100932}
933
Przemek Stekiel061f6942022-11-30 10:51:35 +0100934/** Get the description of a key given its identifier and policy constraints
935 * and lock it.
936 *
937 * The key must have allow all the usage flags set in \p usage. If \p alg is
938 * nonzero, the key must allow operations with this algorithm. If \p alg is
939 * zero, the algorithm is not checked.
940 *
941 * In case of a persistent key, the function loads the description of the key
942 * into a key slot if not already done.
943 *
944 * On success, the returned key slot is locked. It is the responsibility of
945 * the caller to unlock the key slot when it does not access it anymore.
946 */
947static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100948 mbedtls_svc_key_id_t key,
949 psa_key_slot_t **p_slot,
950 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100952{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200953 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100954 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 status = psa_get_and_lock_key_slot(key, p_slot);
957 if (status != PSA_SUCCESS) {
958 return status;
959 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200960 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100961
962 /* Enforce that usage policy for the key slot contains all the flags
963 * required by the usage parameter. There is one exception: public
964 * keys can always be exported, so we treat public key objects as
965 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100967 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100971 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200972 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100973 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100974
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800975 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if (alg != 0) {
977 status = psa_key_policy_permits(&slot->attr.policy,
978 slot->attr.type,
979 alg);
980 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100981 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100983 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200986
987error:
988 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100992}
Darryl Green940d72c2018-07-13 13:18:51 +0100993
Ronald Cron5c522922020-11-14 16:35:34 +0100994/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200995 *
996 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200997 * available, as opposed to a key in a secure element and/or to be used
998 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200999 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001000 * This is a temporary function that may be used instead of
1001 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1002 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001003 *
Ronald Cron5c522922020-11-14 16:35:34 +01001004 * On success, the returned key slot is locked. It is the responsibility of the
1005 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001006 */
Ronald Cron5c522922020-11-14 16:35:34 +01001007static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1008 mbedtls_svc_key_id_t key,
1009 psa_key_slot_t **p_slot,
1010 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001012{
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1014 usage, alg);
1015 if (status != PSA_SUCCESS) {
1016 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001017 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1020 psa_unlock_key_slot(*p_slot);
1021 *p_slot = NULL;
1022 return PSA_ERROR_NOT_SUPPORTED;
1023 }
1024
1025 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001026}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001029{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001030 /* Data pointer will always be either a valid pointer or NULL in an
1031 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if (slot->key.data != NULL) {
1033 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1034 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001037 slot->key.data = NULL;
1038 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001041}
1042
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001043/** Completely wipe a slot in memory, including its policy.
1044 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001045psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001046{
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 /*
1050 * As the return error code may not be handled in case of multiple errors,
1051 * do our best to report an unexpected lock counter. Assert with
1052 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1053 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1054 * function is called as part of the execution of a test suite, the
1055 * execution of the test suite is stopped in error if the assertion fails.
1056 */
1057 if (slot->lock_count != 1) {
1058 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001059 status = PSA_ERROR_CORRUPTION_DETECTED;
1060 }
1061
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001062 /* Multipart operations may still be using the key. This is safe
1063 * because all multipart operation objects are independent from
1064 * the key slot: if they need to access the key after the setup
1065 * phase, they have a copy of the key. Note that this means that
1066 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001067 /* At this point, key material and other type-specific content has
1068 * been wiped. Clear remaining metadata. We can call memset and not
1069 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 memset(slot, 0, sizeof(*slot));
1071 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001072}
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001075{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001076 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001077 psa_status_t status; /* status of the last operation */
1078 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001079#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1080 psa_se_drv_table_entry_t *driver;
1081#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 if (mbedtls_svc_key_id_is_null(key)) {
1084 return PSA_SUCCESS;
1085 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001086
Ronald Cronf2911112020-10-29 17:51:10 +01001087 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001088 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001089 * key, this will load the key description from persistent memory if not
1090 * done yet. We cannot avoid this loading as without it we don't know if
1091 * the key is operated by an SE or not and this information is needed by
1092 * the current implementation.
1093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 status = psa_get_and_lock_key_slot(key, &slot);
1095 if (status != PSA_SUCCESS) {
1096 return status;
1097 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001098
Ronald Cronf2911112020-10-29 17:51:10 +01001099 /*
1100 * If the key slot containing the key description is under access by the
1101 * library (apart from the present access), the key cannot be destroyed
1102 * yet. For the time being, just return in error. Eventually (to be
1103 * implemented), the key should be destroyed when all accesses have
1104 * stopped.
1105 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if (slot->lock_count > 1) {
1107 psa_unlock_key_slot(slot);
1108 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001109 }
1110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001112 /* Refuse the destruction of a read-only key (which may or may not work
1113 * if we attempt it, depending on whether the key is merely read-only
1114 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001115 * Just do the best we can, which is to wipe the copy in memory
1116 * (done in this function's cleanup code). */
1117 overall_status = PSA_ERROR_NOT_PERMITTED;
1118 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001119 }
1120
Gilles Peskine354f7672019-07-12 23:46:38 +02001121#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1123 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001124 /* For a key in a secure element, we need to do three things:
1125 * remove the key file in internal storage, destroy the
1126 * key inside the secure element, and update the driver's
1127 * persistent data. Start a transaction that will encompass these
1128 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001130 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001132 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 status = psa_crypto_save_transaction();
1134 if (status != PSA_SUCCESS) {
1135 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001136 /* We should still try to destroy the key in the secure
1137 * element and the key metadata in storage. This is especially
1138 * important if the error is that the storage is full.
1139 * But how to do it exactly without risking an inconsistent
1140 * state after a reset?
1141 * https://github.com/ARMmbed/mbed-crypto/issues/215
1142 */
1143 overall_status = status;
1144 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001145 }
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 status = psa_destroy_se_key(driver,
1148 psa_key_slot_get_slot_number(slot));
1149 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001150 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001152 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001153#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1154
Darryl Greend49a4992018-06-18 17:27:26 +01001155#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1157 status = psa_destroy_persistent_key(slot->attr.id);
1158 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001159 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001161
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001162 /* TODO: other slots may have a copy of the same key. We should
1163 * invalidate them.
1164 * https://github.com/ARMmbed/mbed-crypto/issues/214
1165 */
Darryl Greend49a4992018-06-18 17:27:26 +01001166 }
1167#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001168
Gilles Peskinefc762652019-07-22 19:30:34 +02001169#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (driver != NULL) {
1171 status = psa_save_se_persistent_data(driver);
1172 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001173 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 }
1175 status = psa_crypto_stop_transaction();
1176 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001177 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001179 }
1180#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1181
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001182exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001184 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001186 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 }
1188 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001189}
1190
John Durkop07cc04a2020-11-16 22:08:34 -08001191#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001192 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001193static psa_status_t psa_get_rsa_public_exponent(
1194 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001196{
1197 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001199 uint8_t *buffer = NULL;
1200 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1204 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 }
1207 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001208 /* It's the default value, which is reported as an empty string,
1209 * so there's nothing to do. */
1210 goto exit;
1211 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 buflen = mbedtls_mpi_size(&mpi);
1214 buffer = mbedtls_calloc(1, buflen);
1215 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001216 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1217 goto exit;
1218 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1220 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001221 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 attributes->domain_parameters = buffer;
1224 attributes->domain_parameters_size = buflen;
1225
1226exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 mbedtls_mpi_free(&mpi);
1228 if (ret != 0) {
1229 mbedtls_free(buffer);
1230 }
1231 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001232}
John Durkop07cc04a2020-11-16 22:08:34 -08001233#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001234 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001235
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001236/** Retrieve all the publicly-accessible attributes of a key.
1237 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001238psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1239 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001240{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001241 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001242 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001243 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1248 if (status != PSA_SUCCESS) {
1249 return status;
1250 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001251
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001252 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1254 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001255
1256#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1258 psa_set_key_slot_number(attributes,
1259 psa_key_slot_get_slot_number(slot));
1260 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001261#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001264#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001265 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001266 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001267 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001268 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001269 * is not yet implemented.
1270 * https://github.com/ARMmbed/mbed-crypto/issues/216
1271 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001273 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001274
Ronald Cron90857082020-11-25 14:58:33 +01001275 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 slot->attr.type,
1277 slot->key.data,
1278 slot->key.bytes,
1279 &rsa);
1280 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001281 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 status = psa_get_rsa_public_exponent(rsa,
1285 attributes);
1286 mbedtls_rsa_free(rsa);
1287 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001288 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001289 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001290#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001291 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001292 default:
1293 /* Nothing else to do. */
1294 break;
1295 }
1296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (status != PSA_SUCCESS) {
1298 psa_reset_key_attributes(attributes);
1299 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304}
1305
Gilles Peskinec8000c02019-08-02 20:15:51 +02001306#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1307psa_status_t psa_get_key_slot_number(
1308 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001310{
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001312 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 return PSA_SUCCESS;
1314 } else {
1315 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001316 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001317}
1318#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1321 size_t key_buffer_size,
1322 uint8_t *data,
1323 size_t data_size,
1324 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001325{
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 if (key_buffer_size > data_size) {
1327 return PSA_ERROR_BUFFER_TOO_SMALL;
1328 }
1329 memcpy(data, key_buffer, key_buffer_size);
1330 memset(data + key_buffer_size, 0,
1331 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001332 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001334}
1335
Ronald Cron7285cda2020-11-26 14:46:37 +01001336psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001337 const psa_key_attributes_t *attributes,
1338 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340{
Ronald Crond18b5f82020-11-25 16:46:05 +01001341 psa_key_type_t type = attributes->core.type;
1342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if (key_type_is_raw_bytes(type) ||
1344 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001345 PSA_KEY_TYPE_IS_ECC(type) ||
1346 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 return psa_export_key_buffer_internal(
1348 key_buffer, key_buffer_size,
1349 data, data_size, data_length);
1350 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001351 /* This shouldn't happen in the reference implementation, but
1352 it is valid for a special-purpose implementation to omit
1353 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001355 }
1356}
1357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1359 uint8_t *data,
1360 size_t data_size,
1361 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001362{
1363 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1364 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1365 psa_key_slot_t *slot;
1366
Ronald Crone907e552021-01-18 13:22:38 +01001367 /* Reject a zero-length output buffer now, since this can never be a
1368 * valid key representation. This way we know that data must be a valid
1369 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 if (data_size == 0) {
1371 return PSA_ERROR_BUFFER_TOO_SMALL;
1372 }
Ronald Crone907e552021-01-18 13:22:38 +01001373
Ronald Cron9486f9d2020-11-26 13:36:23 +01001374 /* Set the key to empty now, so that even when there are errors, we always
1375 * set data_length to a value between 0 and data_size. On error, setting
1376 * the key to empty is a good choice because an empty key representation is
1377 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001378 *data_length = 0;
1379
Ronald Cron9486f9d2020-11-26 13:36:23 +01001380 /* Export requires the EXPORT flag. There is an exception for public keys,
1381 * which don't require any flag, but
1382 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1385 PSA_KEY_USAGE_EXPORT, 0);
1386 if (status != PSA_SUCCESS) {
1387 return status;
1388 }
mohammad160306e79202018-03-28 13:17:44 +03001389
Ronald Crond18b5f82020-11-25 16:46:05 +01001390 psa_key_attributes_t attributes = {
1391 .core = slot->attr
1392 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 status = psa_driver_wrapper_export_key(&attributes,
1394 slot->key.data, slot->key.bytes,
1395 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001400}
1401
Ronald Cron7285cda2020-11-26 14:46:37 +01001402psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001403 const psa_key_attributes_t *attributes,
1404 const uint8_t *key_buffer,
1405 size_t key_buffer_size,
1406 uint8_t *data,
1407 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001409{
Ronald Crond18b5f82020-11-25 16:46:05 +01001410 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001411
Przemek Stekielc80e7502023-05-11 11:14:25 +02001412 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001413#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001414 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1415 return mbedtls_psa_rsa_export_public_key(attributes,
1416 key_buffer,
1417 key_buffer_size,
1418 data,
1419 data_size,
1420 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001421#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001422 /* We don't know how to convert a private RSA key to public. */
1423 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001424#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1425 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001426 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001427#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001428 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1429 return mbedtls_psa_ecp_export_public_key(attributes,
1430 key_buffer,
1431 key_buffer_size,
1432 data,
1433 data_size,
1434 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001435#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001436 /* We don't know how to convert a private ECC key to public */
1437 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001438#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1439 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001440 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001441#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
1442 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001443 return mbedtls_psa_export_ffdh_public_key(attributes,
1444 key_buffer,
1445 key_buffer_size,
1446 data, data_size,
1447 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001448#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001449 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001450#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
1451 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001454 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001455}
Ronald Crond18b5f82020-11-25 16:46:05 +01001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1458 uint8_t *data,
1459 size_t data_size,
1460 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001461{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001463 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001464 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001465
Ronald Crone907e552021-01-18 13:22:38 +01001466 /* Reject a zero-length output buffer now, since this can never be a
1467 * valid key representation. This way we know that data must be a valid
1468 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (data_size == 0) {
1470 return PSA_ERROR_BUFFER_TOO_SMALL;
1471 }
Ronald Crone907e552021-01-18 13:22:38 +01001472
Darryl Greendd8fb772018-11-07 16:00:44 +00001473 /* Set the key to empty now, so that even when there are errors, we always
1474 * set data_length to a value between 0 and data_size. On error, setting
1475 * the key to empty is a good choice because an empty key representation is
1476 * unlikely to be accepted anywhere. */
1477 *data_length = 0;
1478
1479 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1481 if (status != PSA_SUCCESS) {
1482 return status;
1483 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1486 status = PSA_ERROR_INVALID_ARGUMENT;
1487 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001488 }
1489
Ronald Crond18b5f82020-11-25 16:46:05 +01001490 psa_key_attributes_t attributes = {
1491 .core = slot->attr
1492 };
Przemek Stekielc80e7502023-05-11 11:14:25 +02001493
1494 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
1495 psa_get_key_lifetime(&attributes));
1496
1497 if (location == PSA_KEY_LOCATION_LOCAL_STORAGE &&
1498 PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) &&
1499 (PSA_KEY_TYPE_IS_RSA(slot->attr.type) || PSA_KEY_TYPE_IS_ECC(slot->attr.type) ||
1500 PSA_KEY_TYPE_IS_DH(slot->attr.type))) {
1501 /* Exporting public -> public */
1502 status = psa_export_key_buffer_internal(
1503 slot->key.data, slot->key.bytes,
1504 data, data_size, data_length);
1505 } else {
1506 status = psa_driver_wrapper_export_public_key(
1507 &attributes, slot->key.data, slot->key.bytes,
1508 data, data_size, data_length);
1509 }
Ronald Cron9486f9d2020-11-26 13:36:23 +01001510
1511exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001515}
1516
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001517MBEDTLS_STATIC_ASSERT(
1518 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1519 "One or more key attribute flag is listed as both external-only and dual-use")
1520MBEDTLS_STATIC_ASSERT(
1521 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1522 "One or more key attribute flag is listed as both internal-only and dual-use")
1523MBEDTLS_STATIC_ASSERT(
1524 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1525 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001526
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001527/** Validate that a key policy is internally well-formed.
1528 *
1529 * This function only rejects invalid policies. It does not validate the
1530 * consistency of the policy with respect to other attributes of the key
1531 * such as the key type.
1532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001534{
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1536 PSA_KEY_USAGE_COPY |
1537 PSA_KEY_USAGE_ENCRYPT |
1538 PSA_KEY_USAGE_DECRYPT |
1539 PSA_KEY_USAGE_SIGN_MESSAGE |
1540 PSA_KEY_USAGE_VERIFY_MESSAGE |
1541 PSA_KEY_USAGE_SIGN_HASH |
1542 PSA_KEY_USAGE_VERIFY_HASH |
1543 PSA_KEY_USAGE_VERIFY_DERIVATION |
1544 PSA_KEY_USAGE_DERIVE)) != 0) {
1545 return PSA_ERROR_INVALID_ARGUMENT;
1546 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001549}
1550
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001551/** Validate the internal consistency of key attributes.
1552 *
1553 * This function only rejects invalid attribute values. If does not
1554 * validate the consistency of the attributes with any key data that may
1555 * be involved in the creation of the key.
1556 *
1557 * Call this function early in the key creation process.
1558 *
1559 * \param[in] attributes Key attributes for the new key.
1560 * \param[out] p_drv On any return, the driver for the key, if any.
1561 * NULL for a transparent key.
1562 *
1563 */
1564static psa_status_t psa_validate_key_attributes(
1565 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001567{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001568 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1570 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 status = psa_validate_key_location(lifetime, p_drv);
1573 if (status != PSA_SUCCESS) {
1574 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001575 }
1576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 status = psa_validate_key_persistence(lifetime);
1578 if (status != PSA_SUCCESS) {
1579 return status;
1580 }
1581
1582 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1583 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1584 return PSA_ERROR_INVALID_ARGUMENT;
1585 }
1586 } else {
1587 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1588 return PSA_ERROR_INVALID_ARGUMENT;
1589 }
1590 }
1591
1592 status = psa_validate_key_policy(&attributes->core.policy);
1593 if (status != PSA_SUCCESS) {
1594 return status;
1595 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001596
1597 /* Refuse to create overly large keys.
1598 * Note that this doesn't trigger on import if the attributes don't
1599 * explicitly specify a size (so psa_get_key_bits returns 0), so
1600 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1602 return PSA_ERROR_NOT_SUPPORTED;
1603 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001604
Gilles Peskine91e8c332019-08-02 19:19:39 +02001605 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1607 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1608 return PSA_ERROR_INVALID_ARGUMENT;
1609 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001612}
1613
Gilles Peskine4747d192019-04-17 15:05:45 +02001614/** Prepare a key slot to receive key material.
1615 *
1616 * This function allocates a key slot and sets its metadata.
1617 *
1618 * If this function fails, call psa_fail_key_creation().
1619 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001620 * This function is intended to be used as follows:
1621 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001622 * it with the specified attributes, and in case of a volatile key assign it
1623 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001624 * -# Populate the slot with the key material.
1625 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1626 * In case of failure at any step, stop the sequence and call
1627 * psa_fail_key_creation().
1628 *
Ronald Cron5c522922020-11-14 16:35:34 +01001629 * On success, the key slot is locked. It is the responsibility of the caller
1630 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001631 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001632 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001633 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001634 * \param[out] p_slot On success, a pointer to the prepared slot.
1635 * \param[out] p_drv On any return, the driver for the key, if any.
1636 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001637 *
1638 * \retval #PSA_SUCCESS
1639 * The key slot is ready to receive key material.
1640 * \return If this function fails, the key slot is an invalid state.
1641 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001642 */
1643static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001644 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001645 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001646 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001648{
1649 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001650 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001651 psa_key_slot_t *slot;
1652
Gilles Peskinedf179142019-07-15 22:02:14 +02001653 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001654 *p_drv = NULL;
1655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 status = psa_validate_key_attributes(attributes, p_drv);
1657 if (status != PSA_SUCCESS) {
1658 return status;
1659 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1662 if (status != PSA_SUCCESS) {
1663 return status;
1664 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001665 slot = *p_slot;
1666
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001667 /* We're storing the declared bit-size of the key. It's up to each
1668 * creation mechanism to verify that this information is correct.
1669 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001670 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001671 * is optional (import, copy). In case of a volatile key, assign it the
1672 * volatile key identifier associated to the slot returned to contain its
1673 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001674
1675 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001677#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1678 slot->attr.id = volatile_key_id;
1679#else
1680 slot->attr.id.key_id = volatile_key_id;
1681#endif
1682 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001683
Gilles Peskine91e8c332019-08-02 19:19:39 +02001684 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001685 * external-only flags, query `attributes`. Thanks to the check
1686 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001687 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001688 * may have set. */
1689 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001690
Gilles Peskinecbaff462019-07-12 23:46:04 +02001691#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001692 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001693 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001694 * create the key file in internal storage, create the
1695 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001696 * persistent data. This is done by starting a transaction that will
1697 * encompass these three actions.
1698 * For registering a volatile key, we just need to find an appropriate
1699 * slot number inside the SE. Since the key is designated volatile, creating
1700 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001701 /* The first thing to do is to find a slot number for the new key.
1702 * We save the slot number in persistent storage as part of the
1703 * transaction data. It will be needed to recover if the power
1704 * fails during the key creation process, to clean up on the secure
1705 * element side after restarting. Obtaining a slot number from the
1706 * secure element driver updates its persistent state, but we do not yet
1707 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001708 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001710 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1712 &slot_number);
1713 if (status != PSA_SUCCESS) {
1714 return status;
1715 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1718 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001719 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001720 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001721 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 status = psa_crypto_save_transaction();
1723 if (status != PSA_SUCCESS) {
1724 (void) psa_crypto_stop_transaction();
1725 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001726 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001727 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001728
1729 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001731 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001734 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001736 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001737#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001740}
Gilles Peskine4747d192019-04-17 15:05:45 +02001741
1742/** Finalize the creation of a key once its key material has been set.
1743 *
1744 * This entails writing the key to persistent storage.
1745 *
1746 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001747 * See the documentation of psa_start_key_creation() for the intended use
1748 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001749 *
Ronald Cron5c522922020-11-14 16:35:34 +01001750 * If the finalization succeeds, the function unlocks the key slot (it was
1751 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1752 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001753 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001754 * \param[in,out] slot Pointer to the slot with key material.
1755 * \param[in] driver The secure element driver for the key,
1756 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001757 * \param[out] key On success, identifier of the key. Note that the
1758 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001759 *
1760 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001761 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001762 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1763 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1764 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1765 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1766 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1767 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001768 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001769 * \return If this function fails, the key slot is an invalid state.
1770 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001771 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001772static psa_status_t psa_finish_key_creation(
1773 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001774 psa_se_drv_table_entry_t *driver,
1775 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001776{
1777 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001778 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001779 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001780
1781#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001783#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001785 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001786 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001788
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001789 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1790 sizeof(data.slot_number),
1791 "Slot number size does not match psa_se_key_data_storage_t");
1792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1794 status = psa_save_persistent_key(&slot->attr,
1795 (uint8_t *) &data,
1796 sizeof(data));
1797 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001798#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1799 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001800 /* Key material is saved in export representation in the slot, so
1801 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 status = psa_save_persistent_key(&slot->attr,
1803 slot->key.data,
1804 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001805 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001806 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001807#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1808
Gilles Peskinecbaff462019-07-12 23:46:04 +02001809#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001810 /* Finish the transaction for a key creation. This does not
1811 * happen when registering an existing key. Detect this case
1812 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001813 * creation of a persistent key in a secure element requires a transaction,
1814 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 if (driver != NULL &&
1816 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1817 status = psa_save_se_persistent_data(driver);
1818 if (status != PSA_SUCCESS) {
1819 psa_destroy_persistent_key(slot->attr.id);
1820 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001821 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001823 }
1824#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001827 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 status = psa_unlock_key_slot(slot);
1829 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001830 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001832 }
Ronald Cron50972942020-11-14 11:28:25 +01001833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001835}
1836
1837/** Abort the creation of a key.
1838 *
1839 * You may call this function after calling psa_start_key_creation(),
1840 * or after psa_finish_key_creation() fails. In other circumstances, this
1841 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001842 * See the documentation of psa_start_key_creation() for the intended use
1843 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001844 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001845 * \param[in,out] slot Pointer to the slot with key material.
1846 * \param[in] driver The secure element driver for the key,
1847 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001848 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001849static void psa_fail_key_creation(psa_key_slot_t *slot,
1850 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001851{
Gilles Peskine011e4282019-06-26 18:34:38 +02001852 (void) driver;
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001857
1858#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001859 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001860 * element, and the failure happened later (when saving metadata
1861 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001862 * element.
1863 * https://github.com/ARMmbed/mbed-crypto/issues/217
1864 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001865
Gilles Peskined7729582019-08-05 15:55:54 +02001866 /* Abort the ongoing transaction if any (there may not be one if
1867 * the creation process failed before starting one, or if the
1868 * key creation is a registration of a key in a secure element).
1869 * Earlier functions must already have done what it takes to undo any
1870 * partial creation. All that's left is to update the transaction data
1871 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001873#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001876}
1877
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001878/** Validate optional attributes during key creation.
1879 *
1880 * Some key attributes are optional during key creation. If they are
1881 * specified in the attributes structure, check that they are consistent
1882 * with the data in the slot.
1883 *
1884 * This function should be called near the end of key creation, after
1885 * the slot in memory is fully populated but before saving persistent data.
1886 */
1887static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001888 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001890{
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 if (attributes->core.type != 0) {
1892 if (attributes->core.type != slot->attr.type) {
1893 return PSA_ERROR_INVALID_ARGUMENT;
1894 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001895 }
1896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001898#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1900 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001901 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001902 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001904
Ronald Cron90857082020-11-25 14:58:33 +01001905 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 slot->attr.type,
1907 slot->key.data,
1908 slot->key.bytes,
1909 &rsa);
1910 if (status != PSA_SUCCESS) {
1911 return status;
1912 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 mbedtls_mpi_init(&actual);
1915 mbedtls_mpi_init(&required);
1916 ret = mbedtls_rsa_export(rsa,
1917 NULL, NULL, NULL, NULL, &actual);
1918 mbedtls_rsa_free(rsa);
1919 mbedtls_free(rsa);
1920 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001921 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 }
1923 ret = mbedtls_mpi_read_binary(&required,
1924 attributes->domain_parameters,
1925 attributes->domain_parameters_size);
1926 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001927 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 }
1929 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001930 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 }
1932rsa_exit:
1933 mbedtls_mpi_free(&actual);
1934 mbedtls_mpi_free(&required);
1935 if (ret != 0) {
1936 return mbedtls_to_psa_error(ret);
1937 }
1938 } else
John Durkop9814fa22020-11-04 12:28:15 -08001939#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1940 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001941 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001943 }
1944 }
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (attributes->core.bits != 0) {
1947 if (attributes->core.bits != slot->attr.bits) {
1948 return PSA_ERROR_INVALID_ARGUMENT;
1949 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001950 }
1951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953}
1954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1956 const uint8_t *data,
1957 size_t data_length,
1958 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001959{
1960 psa_status_t status;
1961 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001962 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001963 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301964 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001965
Ronald Cron81709fc2020-11-14 12:10:32 +01001966 *key = MBEDTLS_SVC_KEY_ID_INIT;
1967
Gilles Peskine0f84d622019-09-12 19:03:13 +02001968 /* Reject zero-length symmetric keys (including raw data key objects).
1969 * This also rejects any key which might be encoded as an empty string,
1970 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 if (data_length == 0) {
1972 return PSA_ERROR_INVALID_ARGUMENT;
1973 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001974
Archana449608b2021-09-08 15:36:05 +05301975 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 if (data_length > SIZE_MAX / 8) {
1977 return PSA_ERROR_NOT_SUPPORTED;
1978 }
Archana6ed4bda2021-08-04 10:47:15 +05301979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1981 &slot, &driver);
1982 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001983 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001985
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001986 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301987 * storage ( thus not in the case of importing a key in a secure element
1988 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1989 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 if (slot->key.data == NULL) {
1991 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301992 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 attributes, data, data_length, &storage_size);
1994 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301995 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 }
Archanad8a83dc2021-06-14 10:04:16 +05301997 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 status = psa_allocate_buffer_to_slot(slot, storage_size);
1999 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002000 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002002 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002003
Przemek Stekielc80e7502023-05-11 11:14:25 +02002004 if (PSA_KEY_TYPE_IS_ASYMMETRIC(attributes->core.type) &&
2005 PSA_KEY_TYPE_IS_DH(attributes->core.type)) {
2006 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
2007 status = PSA_ERROR_INVALID_ARGUMENT;
2008 goto exit;
2009 }
2010
2011 /* Copy the key material. */
2012 memcpy(slot->key.data, data, data_length);
2013 bits = PSA_BYTES_TO_BITS(data_length);
2014
2015 status = PSA_SUCCESS;
2016 } else {
2017 bits = slot->attr.bits;
2018 status = psa_driver_wrapper_import_key(attributes,
2019 data, data_length,
2020 slot->key.data,
2021 slot->key.bytes,
2022 &slot->key.bytes, &bits);
2023 }
2024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002026 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002030 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002032 status = PSA_ERROR_INVALID_ARGUMENT;
2033 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002034 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002035
Archana6ed4bda2021-08-04 10:47:15 +05302036 /* Enforce a size limit, and in particular ensure that the bit
2037 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302039 status = PSA_ERROR_NOT_SUPPORTED;
2040 goto exit;
2041 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 status = psa_validate_optional_attributes(slot, attributes);
2043 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002044 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002048exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (status != PSA_SUCCESS) {
2050 psa_fail_key_creation(slot, driver);
2051 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002054}
2055
Gilles Peskined7729582019-08-05 15:55:54 +02002056#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2057psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002059{
2060 psa_status_t status;
2061 psa_key_slot_t *slot = NULL;
2062 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002064
2065 /* Leaving attributes unspecified is not currently supported.
2066 * It could make sense to query the key type and size from the
2067 * secure element, but not all secure elements support this
2068 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2070 return PSA_ERROR_NOT_SUPPORTED;
2071 }
2072 if (psa_get_key_bits(attributes) == 0) {
2073 return PSA_ERROR_NOT_SUPPORTED;
2074 }
Gilles Peskined7729582019-08-05 15:55:54 +02002075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2077 &slot, &driver);
2078 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 }
Gilles Peskined7729582019-08-05 15:55:54 +02002081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002083
2084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (status != PSA_SUCCESS) {
2086 psa_fail_key_creation(slot, driver);
2087 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002088
Gilles Peskined7729582019-08-05 15:55:54 +02002089 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 psa_close_key(key);
2091 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002092}
2093#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2096 const psa_key_attributes_t *specified_attributes,
2097 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002098{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002100 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002101 psa_key_slot_t *source_slot = NULL;
2102 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002103 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002104 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302105 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002106
Ronald Cron81709fc2020-11-14 12:10:32 +01002107 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2108
Archana8a180362021-07-05 02:18:48 +05302109 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2111 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 status = psa_validate_optional_attributes(source_slot,
2116 specified_attributes);
2117 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002120
Archana9d17bf42021-09-10 06:22:44 +05302121 /* The target key type and number of bits have been validated by
2122 * psa_validate_optional_attributes() to be either equal to zero or
2123 * equal to the ones of the source key. So it is safe to inherit
2124 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302125 * */
Archana9d17bf42021-09-10 06:22:44 +05302126 actual_attributes.core.bits = source_slot->attr.bits;
2127 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302128
2129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 status = psa_restrict_key_policy(source_slot->attr.type,
2131 &actual_attributes.core.policy,
2132 &source_slot->attr.policy);
2133 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2138 &target_slot, &driver);
2139 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 }
2142 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2143 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302144 /*
Archana9d17bf42021-09-10 06:22:44 +05302145 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302146 * the source key would need to be exported as plaintext and re-imported
2147 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302148 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302149 * appropriate API invocations from the application, if needed.
2150 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002151 status = PSA_ERROR_NOT_SUPPORTED;
2152 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002153 }
Archana8a180362021-07-05 02:18:48 +05302154 /*
2155 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302156 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302157 * - For opaque keys this translates to an invocation of the drivers'
2158 * copy_key entry point through the dispatch layer.
2159 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2161 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2162 &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_allocate_buffer_to_slot(target_slot, storage_size);
2168 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302169 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 }
Archana374fe5b2021-09-08 15:50:28 +05302171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 status = psa_driver_wrapper_copy_key(&actual_attributes,
2173 source_slot->key.data,
2174 source_slot->key.bytes,
2175 target_slot->key.data,
2176 target_slot->key.bytes,
2177 &target_slot->key.bytes);
2178 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302179 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 }
2181 } else {
2182 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302183 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 source_slot->key.bytes);
2185 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302186 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 }
Archana8a180362021-07-05 02:18:48 +05302188 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (status != PSA_SUCCESS) {
2192 psa_fail_key_creation(target_slot, driver);
2193 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002198}
2199
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002200
2201
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002202/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002203/* Message digests */
2204/****************************************************************/
2205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002207{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002208 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 if (operation->id == 0) {
2210 return PSA_SUCCESS;
2211 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002214 operation->id = 0;
2215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002217}
2218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2220 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002222 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002223
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002224 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002226 status = PSA_ERROR_BAD_STATE;
2227 goto exit;
2228 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002231 status = PSA_ERROR_INVALID_ARGUMENT;
2232 goto exit;
2233 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002234
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002235 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2236 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002240
2241exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 if (status != PSA_SUCCESS) {
2243 psa_hash_abort(operation);
2244 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002245
2246 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247}
2248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2250 const uint8_t *input,
2251 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002252{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002253 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002256 status = PSA_ERROR_BAD_STATE;
2257 goto exit;
2258 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002259
Steven Cooremanc8288352021-03-04 14:02:19 +01002260 /* Don't require hash implementations to behave correctly on a
2261 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (input_length == 0) {
2263 return PSA_SUCCESS;
2264 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002267
2268exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 if (status != PSA_SUCCESS) {
2270 psa_hash_abort(operation);
2271 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002274}
2275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2277 uint8_t *hash,
2278 size_t hash_size,
2279 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002281 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 if (operation->id == 0) {
2283 return PSA_ERROR_BAD_STATE;
2284 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285
Steven Cooreman1e582352021-02-18 17:24:37 +01002286 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 operation, hash, hash_size, hash_length);
2288 psa_hash_abort(operation);
2289 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290}
2291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2293 const uint8_t *hash,
2294 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295{
Ronald Cron69a63422021-10-18 09:47:58 +02002296 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002297 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002298 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 operation,
2300 actual_hash, sizeof(actual_hash),
2301 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002304 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002308 status = PSA_ERROR_INVALID_SIGNATURE;
2309 goto exit;
2310 }
2311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002313 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002315
2316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2318 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002319 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323}
2324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325psa_status_t psa_hash_compute(psa_algorithm_t alg,
2326 const uint8_t *input, size_t input_length,
2327 uint8_t *hash, size_t hash_size,
2328 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002329{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002330 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (!PSA_ALG_IS_HASH(alg)) {
2332 return PSA_ERROR_INVALID_ARGUMENT;
2333 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2336 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002337}
2338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339psa_status_t psa_hash_compare(psa_algorithm_t alg,
2340 const uint8_t *input, size_t input_length,
2341 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002342{
Ronald Cron69a63422021-10-18 09:47:58 +02002343 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002344 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 if (!PSA_ALG_IS_HASH(alg)) {
2347 return PSA_ERROR_INVALID_ARGUMENT;
2348 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002349
Steven Cooreman1e582352021-02-18 17:24:37 +01002350 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 alg, input, input_length,
2352 actual_hash, sizeof(actual_hash),
2353 &actual_hash_length);
2354 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002355 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 }
2357 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002358 status = PSA_ERROR_INVALID_SIGNATURE;
2359 goto exit;
2360 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002362 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002364
2365exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2367 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002368}
2369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2371 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002372{
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 if (source_operation->id == 0 ||
2374 target_operation->id != 0) {
2375 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002376 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2379 target_operation);
2380 if (status != PSA_SUCCESS) {
2381 psa_hash_abort(target_operation);
2382 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002385}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386
2387
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002389/* MAC */
2390/****************************************************************/
2391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002393{
Steven Cooremane6804192021-03-19 18:28:56 +01002394 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 if (operation->id == 0) {
2396 return PSA_SUCCESS;
2397 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002400 operation->mac_size = 0;
2401 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002402 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002405}
2406
Ronald Cron2dff3b22021-06-17 16:33:22 +02002407static psa_status_t psa_mac_finalize_alg_and_key_validation(
2408 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002409 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002412 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 psa_key_type_t key_type = psa_get_key_type(attributes);
2414 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (!PSA_ALG_IS_MAC(alg)) {
2417 return PSA_ERROR_INVALID_ARGUMENT;
2418 }
Steven Cooremane6804192021-03-19 18:28:56 +01002419
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002420 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 status = psa_mac_key_can_do(alg, key_type);
2422 if (status != PSA_SUCCESS) {
2423 return status;
2424 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002425
Steven Cooremand1a68f12021-05-10 11:13:41 +02002426 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002430 /* A very short MAC is too short for security since it can be
2431 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2432 * so we make this our minimum, even though 32 bits is still
2433 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002435 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2438 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002439 /* It's impossible to "truncate" to a larger length than the full length
2440 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002442 }
2443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002445 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2446 * that is disabled in the compile-time configuration. The result can
2447 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2448 * configuration into account. In this case, force a return of
2449 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2450 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2451 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2452 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2453 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002455 }
2456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002458}
2459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2461 mbedtls_svc_key_id_t key,
2462 psa_algorithm_t alg,
2463 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002464{
2465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2466 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002467 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002468
2469 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002471 status = PSA_ERROR_BAD_STATE;
2472 goto exit;
2473 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002474
2475 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 key,
2477 &slot,
2478 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2479 alg);
2480 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002481 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483
2484 psa_key_attributes_t attributes = {
2485 .core = slot->attr
2486 };
2487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2489 &operation->mac_size);
2490 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493
2494 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002495 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (is_sign) {
2497 status = psa_driver_wrapper_mac_sign_setup(operation,
2498 &attributes,
2499 slot->key.data,
2500 slot->key.bytes,
2501 alg);
2502 } else {
2503 status = psa_driver_wrapper_mac_verify_setup(operation,
2504 &attributes,
2505 slot->key.data,
2506 slot->key.bytes,
2507 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002509
Gilles Peskinefbfac682018-07-08 20:51:54 +02002510exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if (status != PSA_SUCCESS) {
2512 psa_mac_abort(operation);
2513 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518}
2519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2521 mbedtls_svc_key_id_t key,
2522 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002523{
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002525}
2526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2528 mbedtls_svc_key_id_t key,
2529 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002530{
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002532}
2533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2535 const uint8_t *input,
2536 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537{
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 if (operation->id == 0) {
2539 return PSA_ERROR_BAD_STATE;
2540 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002542 /* Don't require hash implementations to behave correctly on a
2543 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if (input_length == 0) {
2545 return PSA_SUCCESS;
2546 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2549 input, input_length);
2550 if (status != PSA_SUCCESS) {
2551 psa_mac_abort(operation);
2552 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555}
2556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2558 uint8_t *mac,
2559 size_t mac_size,
2560 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002561{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2563 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (operation->id == 0) {
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 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002571 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002572 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002573 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002574
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002575 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2576 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002578 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002579 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002580 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002583 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002584 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002585 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 status = psa_driver_wrapper_mac_sign_finish(operation,
2588 mac, operation->mac_size,
2589 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002590
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002591exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002592 /* In case of success, set the potential excess room in the output buffer
2593 * to an invalid value, to avoid potentially leaking a longer MAC.
2594 * In case of error, set the output length and content to a safe default,
2595 * such that in case the caller misses an error check, the output would be
2596 * an unachievable MAC.
2597 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002599 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002600 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002601 }
mohammad16036df908f2018-04-02 08:34:15 -07002602
Paul Elliottdc42ca82023-02-24 18:11:59 +00002603 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002608}
2609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2611 const uint8_t *mac,
2612 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002614 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2615 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (operation->id == 0) {
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 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002623 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002624 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002625 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002628 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002629 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002630 }
2631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 status = psa_driver_wrapper_mac_verify_finish(operation,
2633 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002634
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002635exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639}
2640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2642 psa_algorithm_t alg,
2643 const uint8_t *input,
2644 size_t input_length,
2645 uint8_t *mac,
2646 size_t mac_size,
2647 size_t *mac_length,
2648 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002649{
2650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002651 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2652 psa_key_slot_t *slot;
2653 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002654
Ronald Cron51131b52021-06-17 17:17:20 +02002655 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 key,
2657 &slot,
2658 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2659 alg);
2660 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002661 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002663
Ronald Cron51131b52021-06-17 17:17:20 +02002664 psa_key_attributes_t attributes = {
2665 .core = slot->attr
2666 };
2667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2669 &operation_mac_size);
2670 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002671 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002675 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002676 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002677 }
2678
2679 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 &attributes,
2681 slot->key.data, slot->key.bytes,
2682 alg,
2683 input, input_length,
2684 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002685
2686exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002687 /* In case of success, set the potential excess room in the output buffer
2688 * to an invalid value, to avoid potentially leaking a longer MAC.
2689 * In case of error, set the output length and content to a safe default,
2690 * such that in case the caller misses an error check, the output would be
2691 * an unachievable MAC.
2692 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002694 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002695 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002696 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002697
2698 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002703}
2704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002706 psa_algorithm_t alg,
2707 const uint8_t *input,
2708 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 uint8_t *mac,
2710 size_t mac_size,
2711 size_t *mac_length)
2712{
2713 return psa_mac_compute_internal(key, alg,
2714 input, input_length,
2715 mac, mac_size, mac_length, 1);
2716}
2717
2718psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2719 psa_algorithm_t alg,
2720 const uint8_t *input,
2721 size_t input_length,
2722 const uint8_t *mac,
2723 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002724{
2725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002726 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2727 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 status = psa_mac_compute_internal(key, alg,
2730 input, input_length,
2731 actual_mac, sizeof(actual_mac),
2732 &actual_mac_length, 0);
2733 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002734 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002738 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002739 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002740 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002742 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002743 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002744 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002745
2746exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002750}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002751
Gilles Peskinee59236f2018-01-27 23:32:46 +01002752/****************************************************************/
2753/* Asymmetric cryptography */
2754/****************************************************************/
2755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2757 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002758{
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 if (input_is_message) {
2760 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2761 return PSA_ERROR_INVALID_ARGUMENT;
2762 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002763
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2765 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2766 return PSA_ERROR_INVALID_ARGUMENT;
2767 }
2768 }
2769 } else {
2770 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2771 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002772 }
2773 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002776}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2779 int input_is_message,
2780 psa_algorithm_t alg,
2781 const uint8_t *input,
2782 size_t input_length,
2783 uint8_t *signature,
2784 size_t signature_size,
2785 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002786{
2787 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2788 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2789 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002790
2791 *signature_length = 0;
2792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 status = psa_sign_verify_check_alg(input_is_message, alg);
2794 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002795 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002797
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002798 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002799 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800 * buffer can in principle be empty since it doesn't actually have
2801 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 if (signature_size == 0) {
2803 return PSA_ERROR_BUFFER_TOO_SMALL;
2804 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002805
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002806 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 key, &slot,
2808 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2809 PSA_KEY_USAGE_SIGN_HASH,
2810 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002813 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002817 status = PSA_ERROR_INVALID_ARGUMENT;
2818 goto exit;
2819 }
2820
2821 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002823 };
2824
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002826 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002827 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002828 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 signature, signature_size, signature_length);
2830 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002831
2832 status = psa_driver_wrapper_sign_hash(
2833 &attributes, slot->key.data, slot->key.bytes,
2834 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002836 }
2837
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002838
2839exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002840 psa_wipe_tag_output_buffer(signature, status, signature_size,
2841 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002846}
2847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2849 int input_is_message,
2850 psa_algorithm_t alg,
2851 const uint8_t *input,
2852 size_t input_length,
2853 const uint8_t *signature,
2854 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002855{
2856 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2857 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2858 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 status = psa_sign_verify_check_alg(input_is_message, alg);
2861 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002862 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002864
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002865 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 key, &slot,
2867 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2868 PSA_KEY_USAGE_VERIFY_HASH,
2869 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 if (status != PSA_SUCCESS) {
2872 return status;
2873 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002874
2875 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002877 };
2878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002880 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002881 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002882 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 signature, signature_length);
2884 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002885 status = psa_driver_wrapper_verify_hash(
2886 &attributes, slot->key.data, slot->key.bytes,
2887 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002889 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002890
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002894
2895}
2896
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002897psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002898 const psa_key_attributes_t *attributes,
2899 const uint8_t *key_buffer,
2900 size_t key_buffer_size,
2901 psa_algorithm_t alg,
2902 const uint8_t *input,
2903 size_t input_length,
2904 uint8_t *signature,
2905 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002907{
2908 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002911 size_t hash_length;
2912 uint8_t hash[PSA_HASH_MAX_SIZE];
2913
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002914 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 PSA_ALG_SIGN_GET_HASH(alg),
2916 input, input_length,
2917 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002918
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002920 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002922
2923 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 attributes, key_buffer, key_buffer_size,
2925 alg, hash, hash_length,
2926 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002927 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002930}
2931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2933 psa_algorithm_t alg,
2934 const uint8_t *input,
2935 size_t input_length,
2936 uint8_t *signature,
2937 size_t signature_size,
2938 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002939{
2940 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002941 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002943}
2944
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002945psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002946 const psa_key_attributes_t *attributes,
2947 const uint8_t *key_buffer,
2948 size_t key_buffer_size,
2949 psa_algorithm_t alg,
2950 const uint8_t *input,
2951 size_t input_length,
2952 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002954{
2955 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002958 size_t hash_length;
2959 uint8_t hash[PSA_HASH_MAX_SIZE];
2960
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002961 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 PSA_ALG_SIGN_GET_HASH(alg),
2963 input, input_length,
2964 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002965
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002967 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002969
2970 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 attributes, key_buffer, key_buffer_size,
2972 alg, hash, hash_length,
2973 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002974 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002977}
2978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2980 psa_algorithm_t alg,
2981 const uint8_t *input,
2982 size_t input_length,
2983 const uint8_t *signature,
2984 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002985{
2986 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002987 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002989}
2990
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002991psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002992 const psa_key_attributes_t *attributes,
2993 const uint8_t *key_buffer, size_t key_buffer_size,
2994 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002996{
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2998 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2999 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003000#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3002 return mbedtls_psa_rsa_sign_hash(
3003 attributes,
3004 key_buffer, key_buffer_size,
3005 alg, hash, hash_length,
3006 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003007#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3008 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 } else {
3010 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003011 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3013 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003014#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3016 return mbedtls_psa_ecdsa_sign_hash(
3017 attributes,
3018 key_buffer, key_buffer_size,
3019 alg, hash, hash_length,
3020 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003021#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3022 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 } else {
3024 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003025 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003026 }
Ronald Cron4501c982021-03-19 20:09:32 +01003027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 (void) key_buffer;
3029 (void) key_buffer_size;
3030 (void) hash;
3031 (void) hash_length;
3032 (void) signature;
3033 (void) signature_size;
3034 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003037}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3040 psa_algorithm_t alg,
3041 const uint8_t *hash,
3042 size_t hash_length,
3043 uint8_t *signature,
3044 size_t signature_size,
3045 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003046{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003047 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003048 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003050}
3051
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003052psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003053 const psa_key_attributes_t *attributes,
3054 const uint8_t *key_buffer, size_t key_buffer_size,
3055 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003057{
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3059 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3060 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003061#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3063 return mbedtls_psa_rsa_verify_hash(
3064 attributes,
3065 key_buffer, key_buffer_size,
3066 alg, hash, hash_length,
3067 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003068#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3069 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 } else {
3071 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003072 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3074 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003075#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3077 return mbedtls_psa_ecdsa_verify_hash(
3078 attributes,
3079 key_buffer, key_buffer_size,
3080 alg, hash, hash_length,
3081 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003082#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3083 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 } else {
3085 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003086 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003087 }
Ronald Cron4501c982021-03-19 20:09:32 +01003088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 (void) key_buffer;
3090 (void) key_buffer_size;
3091 (void) hash;
3092 (void) hash_length;
3093 (void) signature;
3094 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003095
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003097}
3098
Gilles Peskine449bd832023-01-11 14:50:10 +01003099psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3100 psa_algorithm_t alg,
3101 const uint8_t *hash,
3102 size_t hash_length,
3103 const uint8_t *signature,
3104 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003105{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003106 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003107 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003109}
3110
Gilles Peskine449bd832023-01-11 14:50:10 +01003111psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3112 psa_algorithm_t alg,
3113 const uint8_t *input,
3114 size_t input_length,
3115 const uint8_t *salt,
3116 size_t salt_length,
3117 uint8_t *output,
3118 size_t output_size,
3119 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003120{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003121 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003122 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003123 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003124
Darryl Green5cc689a2018-07-24 15:34:10 +01003125 (void) input;
3126 (void) input_length;
3127 (void) salt;
3128 (void) output;
3129 (void) output_size;
3130
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003131 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3134 return PSA_ERROR_INVALID_ARGUMENT;
3135 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003136
Ronald Cron5c522922020-11-14 16:35:34 +01003137 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3139 if (status != PSA_SUCCESS) {
3140 return status;
3141 }
3142 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3143 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003144 status = PSA_ERROR_INVALID_ARGUMENT;
3145 goto exit;
3146 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003148 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003150 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003151
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003152 status = psa_driver_wrapper_asymmetric_encrypt(
3153 &attributes, slot->key.data, slot->key.bytes,
3154 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003156exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003158
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003160}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003161
Gilles Peskine449bd832023-01-11 14:50:10 +01003162psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3163 psa_algorithm_t alg,
3164 const uint8_t *input,
3165 size_t input_length,
3166 const uint8_t *salt,
3167 size_t salt_length,
3168 uint8_t *output,
3169 size_t output_size,
3170 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003171{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003172 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003173 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003174 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003175
Darryl Green5cc689a2018-07-24 15:34:10 +01003176 (void) input;
3177 (void) input_length;
3178 (void) salt;
3179 (void) output;
3180 (void) output_size;
3181
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003182 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003183
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3185 return PSA_ERROR_INVALID_ARGUMENT;
3186 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003187
Ronald Cron5c522922020-11-14 16:35:34 +01003188 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3190 if (status != PSA_SUCCESS) {
3191 return status;
3192 }
3193 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003194 status = PSA_ERROR_INVALID_ARGUMENT;
3195 goto exit;
3196 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003198 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003200 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003202 status = psa_driver_wrapper_asymmetric_decrypt(
3203 &attributes, slot->key.data, slot->key.bytes,
3204 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003206
3207exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003211}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003212
Paul Elliott9fe12f62022-11-30 19:16:02 +00003213/****************************************************************/
3214/* Asymmetric interruptible cryptography */
3215/****************************************************************/
3216
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003217static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3218
Paul Elliott9fe12f62022-11-30 19:16:02 +00003219void psa_interruptible_set_max_ops(uint32_t max_ops)
3220{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003221 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003222}
3223
3224uint32_t psa_interruptible_get_max_ops(void)
3225{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003226 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003227}
3228
Paul Elliott9fe12f62022-11-30 19:16:02 +00003229uint32_t psa_sign_hash_get_num_ops(
3230 const psa_sign_hash_interruptible_operation_t *operation)
3231{
Paul Elliott296ede92022-12-15 17:00:30 +00003232 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003233}
3234
3235uint32_t psa_verify_hash_get_num_ops(
3236 const psa_verify_hash_interruptible_operation_t *operation)
3237{
Paul Elliott296ede92022-12-15 17:00:30 +00003238 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003239}
3240
Paul Elliott59ad9452022-12-18 15:09:02 +00003241static psa_status_t psa_sign_hash_abort_internal(
3242 psa_sign_hash_interruptible_operation_t *operation)
3243{
3244 if (operation->id == 0) {
3245 /* The object has (apparently) been initialized but it is not (yet)
3246 * in use. It's ok to call abort on such an object, and there's
3247 * nothing to do. */
3248 return PSA_SUCCESS;
3249 }
3250
3251 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3252
3253 status = psa_driver_wrapper_sign_hash_abort(operation);
3254
3255 operation->id = 0;
3256
Paul Elliotte6145dc2023-02-07 12:51:21 +00003257 /* Do not clear either the error_occurred or num_ops elements here as they
3258 * only want to be cleared by the application calling abort, not by abort
3259 * being called at completion of an operation. */
3260
Paul Elliott59ad9452022-12-18 15:09:02 +00003261 return status;
3262}
3263
Paul Elliott9fe12f62022-11-30 19:16:02 +00003264psa_status_t psa_sign_hash_start(
3265 psa_sign_hash_interruptible_operation_t *operation,
3266 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3267 const uint8_t *hash, size_t hash_length)
3268{
3269 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3270 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3271 psa_key_slot_t *slot;
3272
Paul Elliottc9774412023-02-06 15:14:07 +00003273 /* Check that start has not been previously called, or operation has not
3274 * previously errored. */
3275 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003276 return PSA_ERROR_BAD_STATE;
3277 }
3278
Paul Elliott9fe12f62022-11-30 19:16:02 +00003279 status = psa_sign_verify_check_alg(0, alg);
3280 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003281 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003282 return status;
3283 }
3284
3285 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3286 PSA_KEY_USAGE_SIGN_HASH,
3287 alg);
3288
3289 if (status != PSA_SUCCESS) {
3290 goto exit;
3291 }
3292
3293 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3294 status = PSA_ERROR_INVALID_ARGUMENT;
3295 goto exit;
3296 }
3297
3298 psa_key_attributes_t attributes = {
3299 .core = slot->attr
3300 };
3301
Paul Elliott296ede92022-12-15 17:00:30 +00003302 /* Ensure ops count gets reset, in case of operation re-use. */
3303 operation->num_ops = 0;
3304
Paul Elliott9fe12f62022-11-30 19:16:02 +00003305 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3306 slot->key.data,
3307 slot->key.bytes, alg,
3308 hash, hash_length);
3309exit:
3310
3311 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003312 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003313 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003314 }
3315
3316 unlock_status = psa_unlock_key_slot(slot);
3317
Paul Elliottc9774412023-02-06 15:14:07 +00003318 if (unlock_status != PSA_SUCCESS) {
3319 operation->error_occurred = 1;
3320 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003321
Paul Elliottc9774412023-02-06 15:14:07 +00003322 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003323}
3324
3325
3326psa_status_t psa_sign_hash_complete(
3327 psa_sign_hash_interruptible_operation_t *operation,
3328 uint8_t *signature, size_t signature_size,
3329 size_t *signature_length)
3330{
3331 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3332
3333 *signature_length = 0;
3334
Paul Elliottc9774412023-02-06 15:14:07 +00003335 /* Check that start has been called first, and that operation has not
3336 * previously errored. */
3337 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003338 status = PSA_ERROR_BAD_STATE;
3339 goto exit;
3340 }
3341
Paul Elliott096abc42023-02-03 18:33:23 +00003342 /* Immediately reject a zero-length signature buffer. This guarantees that
3343 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003344 if (signature_size == 0) {
3345 status = PSA_ERROR_BUFFER_TOO_SMALL;
3346 goto exit;
3347 }
3348
3349 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3350 signature_size,
3351 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003352
Paul Elliott296ede92022-12-15 17:00:30 +00003353 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003354 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003355
Paul Elliottebe225c2023-02-10 14:32:53 +00003356exit:
3357
Paul Elliott59200a22023-02-21 15:07:40 +00003358 psa_wipe_tag_output_buffer(signature, status, signature_size,
3359 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003360
Paul Elliott53bb3122023-02-10 14:22:22 +00003361 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003362 if (status != PSA_SUCCESS) {
3363 operation->error_occurred = 1;
3364 }
3365
Paul Elliott59ad9452022-12-18 15:09:02 +00003366 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003367 }
3368
3369 return status;
3370}
3371
3372psa_status_t psa_sign_hash_abort(
3373 psa_sign_hash_interruptible_operation_t *operation)
3374{
Paul Elliott59ad9452022-12-18 15:09:02 +00003375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3376
3377 status = psa_sign_hash_abort_internal(operation);
3378
3379 /* We clear the number of ops done here, so that it is not cleared when
3380 * the operation fails or succeeds, only on manual abort. */
3381 operation->num_ops = 0;
3382
Paul Elliottc9774412023-02-06 15:14:07 +00003383 /* Likewise, failure state. */
3384 operation->error_occurred = 0;
3385
Paul Elliott59ad9452022-12-18 15:09:02 +00003386 return status;
3387}
3388
3389static psa_status_t psa_verify_hash_abort_internal(
3390 psa_verify_hash_interruptible_operation_t *operation)
3391{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003392 if (operation->id == 0) {
3393 /* The object has (apparently) been initialized but it is not (yet)
3394 * in use. It's ok to call abort on such an object, and there's
3395 * nothing to do. */
3396 return PSA_SUCCESS;
3397 }
3398
Paul Elliott59ad9452022-12-18 15:09:02 +00003399 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3400
3401 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003402
3403 operation->id = 0;
3404
Paul Elliotte6145dc2023-02-07 12:51:21 +00003405 /* Do not clear either the error_occurred or num_ops elements here as they
3406 * only want to be cleared by the application calling abort, not by abort
3407 * being called at completion of an operation. */
3408
Paul Elliott59ad9452022-12-18 15:09:02 +00003409 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003410}
3411
3412psa_status_t psa_verify_hash_start(
3413 psa_verify_hash_interruptible_operation_t *operation,
3414 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3415 const uint8_t *hash, size_t hash_length,
3416 const uint8_t *signature, size_t signature_length)
3417{
3418 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3419 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3420 psa_key_slot_t *slot;
3421
Paul Elliottc9774412023-02-06 15:14:07 +00003422 /* Check that start has not been previously called, or operation has not
3423 * previously errored. */
3424 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003425 return PSA_ERROR_BAD_STATE;
3426 }
3427
3428 status = psa_sign_verify_check_alg(0, alg);
3429 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003430 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003431 return status;
3432 }
3433
3434 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3435 PSA_KEY_USAGE_VERIFY_HASH,
3436 alg);
3437
3438 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003439 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003440 return status;
3441 }
3442
3443 psa_key_attributes_t attributes = {
3444 .core = slot->attr
3445 };
3446
Paul Elliott296ede92022-12-15 17:00:30 +00003447 /* Ensure ops count gets reset, in case of operation re-use. */
3448 operation->num_ops = 0;
3449
Paul Elliott9fe12f62022-11-30 19:16:02 +00003450 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3451 slot->key.data,
3452 slot->key.bytes,
3453 alg, hash, hash_length,
3454 signature, signature_length);
3455
3456 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003457 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003458 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459 }
3460
3461 unlock_status = psa_unlock_key_slot(slot);
3462
Paul Elliottc9774412023-02-06 15:14:07 +00003463 if (unlock_status != PSA_SUCCESS) {
3464 operation->error_occurred = 1;
3465 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003466
Paul Elliottc9774412023-02-06 15:14:07 +00003467 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003468}
3469
3470psa_status_t psa_verify_hash_complete(
3471 psa_verify_hash_interruptible_operation_t *operation)
3472{
3473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3474
Paul Elliottc9774412023-02-06 15:14:07 +00003475 /* Check that start has been called first, and that operation has not
3476 * previously errored. */
3477 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003478 status = PSA_ERROR_BAD_STATE;
3479 goto exit;
3480 }
3481
3482 status = psa_driver_wrapper_verify_hash_complete(operation);
3483
Paul Elliott296ede92022-12-15 17:00:30 +00003484 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003485 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003486 operation);
3487
Paul Elliottebe225c2023-02-10 14:32:53 +00003488exit:
3489
Paul Elliott9fe12f62022-11-30 19:16:02 +00003490 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003491 if (status != PSA_SUCCESS) {
3492 operation->error_occurred = 1;
3493 }
3494
Paul Elliott59ad9452022-12-18 15:09:02 +00003495 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003496 }
3497
3498 return status;
3499}
3500
3501psa_status_t psa_verify_hash_abort(
3502 psa_verify_hash_interruptible_operation_t *operation)
3503{
Paul Elliott59ad9452022-12-18 15:09:02 +00003504 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003505
Paul Elliott59ad9452022-12-18 15:09:02 +00003506 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003507
Paul Elliott59ad9452022-12-18 15:09:02 +00003508 /* We clear the number of ops done here, so that it is not cleared when
3509 * the operation fails or succeeds, only on manual abort. */
3510 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003511
Paul Elliottc9774412023-02-06 15:14:07 +00003512 /* Likewise, failure state. */
3513 operation->error_occurred = 0;
3514
Paul Elliott59ad9452022-12-18 15:09:02 +00003515 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003516}
3517
Paul Elliott588f8ed2022-12-02 18:10:26 +00003518/****************************************************************/
3519/* Asymmetric interruptible cryptography internal */
3520/* implementations */
3521/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003522
Paul Elliott588f8ed2022-12-02 18:10:26 +00003523void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3524{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003525
Paul Elliott588f8ed2022-12-02 18:10:26 +00003526#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3527 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3528 defined(MBEDTLS_ECP_RESTARTABLE)
3529
3530 /* Internal implementation uses zero to indicate infinite number max ops,
3531 * therefore avoid this value, and set to minimum possible. */
3532 if (max_ops == 0) {
3533 max_ops = 1;
3534 }
3535
Paul Elliott588f8ed2022-12-02 18:10:26 +00003536 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003537#else
3538 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003539#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3540 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3541 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3542}
3543
Paul Elliott588f8ed2022-12-02 18:10:26 +00003544uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003545 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003546{
3547#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3548 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3549 defined(MBEDTLS_ECP_RESTARTABLE)
3550
Paul Elliott93d9ca82023-02-15 18:14:21 +00003551 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003552#else
3553 (void) operation;
3554 return 0;
3555#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3556 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3557 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3558}
3559
3560uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003561 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003562{
3563 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3564 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3565 defined(MBEDTLS_ECP_RESTARTABLE)
3566
Paul Elliott93d9ca82023-02-15 18:14:21 +00003567 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003568#else
3569 (void) operation;
3570 return 0;
3571#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3572 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3573 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3574}
3575
3576psa_status_t mbedtls_psa_sign_hash_start(
3577 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3578 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3579 size_t key_buffer_size, psa_algorithm_t alg,
3580 const uint8_t *hash, size_t hash_length)
3581{
3582 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003583 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003584
Paul Elliott068fe072023-01-16 13:59:15 +00003585 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3586 return PSA_ERROR_NOT_SUPPORTED;
3587 }
3588
3589 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003590 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003591 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003592
3593#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003594 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3595 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003596
Paul Elliotta1c94092023-02-15 16:38:04 +00003597 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3598
Paul Elliott93d9ca82023-02-15 18:14:21 +00003599 /* Ensure num_ops is zero'ed in case of context re-use. */
3600 operation->num_ops = 0;
3601
Paul Elliott068fe072023-01-16 13:59:15 +00003602 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3603 attributes->core.bits,
3604 key_buffer,
3605 key_buffer_size,
3606 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003607
Paul Elliott068fe072023-01-16 13:59:15 +00003608 if (status != PSA_SUCCESS) {
3609 return status;
3610 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003611
Paul Elliott1bc59df2023-02-05 13:41:57 +00003612 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003613 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003614
Paul Elliott068fe072023-01-16 13:59:15 +00003615 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3616 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3617 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003618
Paul Elliott0290a762023-02-09 14:30:24 +00003619 /* We only need to store the same length of hash as the private key size
3620 * here, it would be truncated by the internal implementation anyway. */
3621 required_hash_length = (hash_length < operation->coordinate_bytes ?
3622 hash_length : operation->coordinate_bytes);
3623
Paul Elliottba70ad42023-02-15 18:23:53 +00003624 if (required_hash_length > sizeof(operation->hash)) {
3625 /* Shouldn't happen, but better safe than sorry. */
3626 return PSA_ERROR_CORRUPTION_DETECTED;
3627 }
3628
Paul Elliott0290a762023-02-09 14:30:24 +00003629 memcpy(operation->hash, hash, required_hash_length);
3630 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003631
3632 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003633
3634#else
Paul Elliott068fe072023-01-16 13:59:15 +00003635 (void) operation;
3636 (void) key_buffer;
3637 (void) key_buffer_size;
3638 (void) alg;
3639 (void) hash;
3640 (void) hash_length;
3641 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003642 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643
Paul Elliott068fe072023-01-16 13:59:15 +00003644 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003645#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3646 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3647 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003648}
3649
3650psa_status_t mbedtls_psa_sign_hash_complete(
3651 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3652 uint8_t *signature, size_t signature_size,
3653 size_t *signature_length)
3654{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003655#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3656 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3657 defined(MBEDTLS_ECP_RESTARTABLE)
3658
Paul Elliotta1c94092023-02-15 16:38:04 +00003659 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003660 mbedtls_mpi r;
3661 mbedtls_mpi s;
3662
Paul Elliott46845252023-02-03 14:59:11 +00003663 mbedtls_mpi_init(&r);
3664 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003665
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003666 /* Ensure max_ops is set to the current value (or default). */
3667 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3668
Paul Elliotta1c94092023-02-15 16:38:04 +00003669 if (signature_size < 2 * operation->coordinate_bytes) {
3670 status = PSA_ERROR_BUFFER_TOO_SMALL;
3671 goto exit;
3672 }
3673
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003675
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3677 status = mbedtls_to_psa_error(
3678 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003679 &r,
3680 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003681 &operation->ctx->d,
3682 operation->hash,
3683 operation->hash_length,
3684 operation->md_alg,
3685 mbedtls_psa_get_random,
3686 MBEDTLS_PSA_RANDOM_STATE,
3687 &operation->restart_ctx));
3688#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003689 status = PSA_ERROR_NOT_SUPPORTED;
3690 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003691#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3692 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693 status = mbedtls_to_psa_error(
3694 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003695 &r,
3696 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003697 &operation->ctx->d,
3698 operation->hash,
3699 operation->hash_length,
3700 mbedtls_psa_get_random,
3701 MBEDTLS_PSA_RANDOM_STATE,
3702 mbedtls_psa_get_random,
3703 MBEDTLS_PSA_RANDOM_STATE,
3704 &operation->restart_ctx));
3705 }
3706
Paul Elliottf8e5b562023-02-19 18:43:45 +00003707 /* Hide the fact that the restart context only holds a delta of number of
3708 * ops done during the last operation, not an absolute value. */
3709 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3710
Paul Elliott724bd252023-02-08 12:35:08 +00003711 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003713 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003714 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003715 operation->coordinate_bytes)
3716 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717
3718 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003719 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003720 }
3721
3722 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003723 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003725 operation->coordinate_bytes,
3726 operation->coordinate_bytes)
3727 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003728
3729 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003730 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731 }
3732
Paul Elliott1bc59df2023-02-05 13:41:57 +00003733 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734
Paul Elliott724bd252023-02-08 12:35:08 +00003735 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003736 }
Paul Elliott724bd252023-02-08 12:35:08 +00003737
3738exit:
3739
3740 mbedtls_mpi_free(&r);
3741 mbedtls_mpi_free(&s);
3742 return status;
3743
Paul Elliott588f8ed2022-12-02 18:10:26 +00003744 #else
3745
3746 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003747 (void) signature;
3748 (void) signature_size;
3749 (void) signature_length;
3750
3751 return PSA_ERROR_NOT_SUPPORTED;
3752
3753#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3754 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3755 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3756}
3757
3758psa_status_t mbedtls_psa_sign_hash_abort(
3759 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3760{
3761
3762#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3763 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3764 defined(MBEDTLS_ECP_RESTARTABLE)
3765
3766 if (operation->ctx) {
3767 mbedtls_ecdsa_free(operation->ctx);
3768 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003769 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770 }
3771
3772 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3773
Paul Elliott93d9ca82023-02-15 18:14:21 +00003774 operation->num_ops = 0;
3775
Paul Elliott588f8ed2022-12-02 18:10:26 +00003776 return PSA_SUCCESS;
3777
3778#else
3779
3780 (void) operation;
3781
3782 return PSA_ERROR_NOT_SUPPORTED;
3783
3784#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3785 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3786 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3787}
3788
3789psa_status_t mbedtls_psa_verify_hash_start(
3790 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3791 const psa_key_attributes_t *attributes,
3792 const uint8_t *key_buffer, size_t key_buffer_size,
3793 psa_algorithm_t alg,
3794 const uint8_t *hash, size_t hash_length,
3795 const uint8_t *signature, size_t signature_length)
3796{
3797 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003798 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003799 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003800
Paul Elliott068fe072023-01-16 13:59:15 +00003801 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3802 return PSA_ERROR_NOT_SUPPORTED;
3803 }
3804
3805 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003806 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003807 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003808
3809#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003810 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3811 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003812
Paul Elliotta1c94092023-02-15 16:38:04 +00003813 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3814 mbedtls_mpi_init(&operation->r);
3815 mbedtls_mpi_init(&operation->s);
3816
Paul Elliott93d9ca82023-02-15 18:14:21 +00003817 /* Ensure num_ops is zero'ed in case of context re-use. */
3818 operation->num_ops = 0;
3819
Paul Elliott068fe072023-01-16 13:59:15 +00003820 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3821 attributes->core.bits,
3822 key_buffer,
3823 key_buffer_size,
3824 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003825
Paul Elliott068fe072023-01-16 13:59:15 +00003826 if (status != PSA_SUCCESS) {
3827 return status;
3828 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003829
Paul Elliottc569fc22023-02-10 13:02:54 +00003830 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831
Paul Elliott1bc59df2023-02-05 13:41:57 +00003832 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003833 return PSA_ERROR_INVALID_SIGNATURE;
3834 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835
Paul Elliott068fe072023-01-16 13:59:15 +00003836 status = mbedtls_to_psa_error(
3837 mbedtls_mpi_read_binary(&operation->r,
3838 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003839 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840
Paul Elliott068fe072023-01-16 13:59:15 +00003841 if (status != PSA_SUCCESS) {
3842 return status;
3843 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844
Paul Elliott068fe072023-01-16 13:59:15 +00003845 status = mbedtls_to_psa_error(
3846 mbedtls_mpi_read_binary(&operation->s,
3847 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003848 coordinate_bytes,
3849 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850
Paul Elliott068fe072023-01-16 13:59:15 +00003851 if (status != PSA_SUCCESS) {
3852 return status;
3853 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854
Paul Elliott2c9843f2023-02-15 17:32:42 +00003855 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856
Paul Elliott2c9843f2023-02-15 17:32:42 +00003857 if (status != PSA_SUCCESS) {
3858 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003859 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003860
Paul Elliott0290a762023-02-09 14:30:24 +00003861 /* We only need to store the same length of hash as the private key size
3862 * here, it would be truncated by the internal implementation anyway. */
3863 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3864 coordinate_bytes);
3865
Paul Elliottba70ad42023-02-15 18:23:53 +00003866 if (required_hash_length > sizeof(operation->hash)) {
3867 /* Shouldn't happen, but better safe than sorry. */
3868 return PSA_ERROR_CORRUPTION_DETECTED;
3869 }
3870
Paul Elliott0290a762023-02-09 14:30:24 +00003871 memcpy(operation->hash, hash, required_hash_length);
3872 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003873
3874 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003875#else
Paul Elliott068fe072023-01-16 13:59:15 +00003876 (void) operation;
3877 (void) key_buffer;
3878 (void) key_buffer_size;
3879 (void) alg;
3880 (void) hash;
3881 (void) hash_length;
3882 (void) signature;
3883 (void) signature_length;
3884 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003885 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003886 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003887
Paul Elliott068fe072023-01-16 13:59:15 +00003888 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003889#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3890 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3891 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003892}
3893
3894psa_status_t mbedtls_psa_verify_hash_complete(
3895 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3896{
3897
3898#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3899 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3900 defined(MBEDTLS_ECP_RESTARTABLE)
3901
Paul Elliottf8e5b562023-02-19 18:43:45 +00003902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3903
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003904 /* Ensure max_ops is set to the current value (or default). */
3905 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3906
Paul Elliottf8e5b562023-02-19 18:43:45 +00003907 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003908 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3909 operation->hash,
3910 operation->hash_length,
3911 &operation->ctx->Q,
3912 &operation->r,
3913 &operation->s,
3914 &operation->restart_ctx));
3915
Paul Elliottf8e5b562023-02-19 18:43:45 +00003916 /* Hide the fact that the restart context only holds a delta of number of
3917 * ops done during the last operation, not an absolute value. */
3918 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3919
3920 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003921#else
3922 (void) operation;
3923
3924 return PSA_ERROR_NOT_SUPPORTED;
3925
3926#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3927 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3928 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3929}
3930
3931psa_status_t mbedtls_psa_verify_hash_abort(
3932 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3933{
3934
3935#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3936 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3937 defined(MBEDTLS_ECP_RESTARTABLE)
3938
3939 if (operation->ctx) {
3940 mbedtls_ecdsa_free(operation->ctx);
3941 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003942 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003943 }
3944
3945 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3946
Paul Elliott93d9ca82023-02-15 18:14:21 +00003947 operation->num_ops = 0;
3948
Paul Elliott588f8ed2022-12-02 18:10:26 +00003949 mbedtls_mpi_free(&operation->r);
3950 mbedtls_mpi_free(&operation->s);
3951
3952 return PSA_SUCCESS;
3953
3954#else
3955 (void) operation;
3956
3957 return PSA_ERROR_NOT_SUPPORTED;
3958
3959#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3960 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3961 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3962}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003963
mohammad1603503973b2018-03-12 15:59:30 +02003964/****************************************************************/
3965/* Symmetric cryptography */
3966/****************************************************************/
3967
Gilles Peskine449bd832023-01-11 14:50:10 +01003968static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3969 mbedtls_svc_key_id_t key,
3970 psa_algorithm_t alg,
3971 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003972{
3973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3974 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003975 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3977 PSA_KEY_USAGE_ENCRYPT :
3978 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003979
3980 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003982 status = PSA_ERROR_BAD_STATE;
3983 goto exit;
3984 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003987 status = PSA_ERROR_INVALID_ARGUMENT;
3988 goto exit;
3989 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003990
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3992 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003993 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003995
Ronald Cron49fafa92021-03-10 08:34:23 +01003996 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003997 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003998 * so we only set it (in the driver wrapper) after resources have been
3999 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004000 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004002 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004004 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 }
4006 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004007
Ronald Crona4af55f2020-12-14 14:36:06 +01004008 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004010 };
4011
Ronald Cronab99ac22020-10-01 16:38:28 +02004012 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004013 if (cipher_operation == MBEDTLS_ENCRYPT) {
4014 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4015 &attributes,
4016 slot->key.data,
4017 slot->key.bytes,
4018 alg);
4019 } else {
4020 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4021 &attributes,
4022 slot->key.data,
4023 slot->key.bytes,
4024 alg);
4025 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004026
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004027exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 if (status != PSA_SUCCESS) {
4029 psa_cipher_abort(operation);
4030 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004033
Gilles Peskine449bd832023-01-11 14:50:10 +01004034 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004035}
4036
Gilles Peskine449bd832023-01-11 14:50:10 +01004037psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4038 mbedtls_svc_key_id_t key,
4039 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004040{
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004042}
4043
Gilles Peskine449bd832023-01-11 14:50:10 +01004044psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4045 mbedtls_svc_key_id_t key,
4046 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004047{
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004049}
4050
Gilles Peskine449bd832023-01-11 14:50:10 +01004051psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4052 uint8_t *iv,
4053 size_t iv_size,
4054 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004055{
Ronald Cron6d051732020-10-01 14:10:20 +02004056 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004057 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4058 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004059
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004061 status = PSA_ERROR_BAD_STATE;
4062 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004063 }
4064
Gilles Peskine449bd832023-01-11 14:50:10 +01004065 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004066 status = PSA_ERROR_BAD_STATE;
4067 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004068 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004069
Ronald Cron2fb90522021-07-05 12:31:44 +02004070 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004072 status = PSA_ERROR_BUFFER_TOO_SMALL;
4073 goto exit;
4074 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004077 status = PSA_ERROR_GENERIC_ERROR;
4078 goto exit;
4079 }
4080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 status = psa_generate_random(local_iv, default_iv_length);
4082 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004083 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 }
Ronald Cron5618a392021-03-26 09:52:26 +01004085
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 status = psa_driver_wrapper_cipher_set_iv(operation,
4087 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004088
4089exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 if (status == PSA_SUCCESS) {
4091 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004092 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004093 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004095 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004097 }
Ronald Cron6d051732020-10-01 14:10:20 +02004098
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004100}
4101
Gilles Peskine449bd832023-01-11 14:50:10 +01004102psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4103 const uint8_t *iv,
4104 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004105{
Ronald Cron6d051732020-10-01 14:10:20 +02004106 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004107
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004109 status = PSA_ERROR_BAD_STATE;
4110 goto exit;
4111 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004114 status = PSA_ERROR_BAD_STATE;
4115 goto exit;
4116 }
Ronald Crona0d68172021-03-26 10:15:08 +01004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004119 status = PSA_ERROR_INVALID_ARGUMENT;
4120 goto exit;
4121 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004122
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 status = psa_driver_wrapper_cipher_set_iv(operation,
4124 iv,
4125 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004126
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004127exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004129 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 } else {
4131 psa_cipher_abort(operation);
4132 }
4133 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004134}
4135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4137 const uint8_t *input,
4138 size_t input_length,
4139 uint8_t *output,
4140 size_t output_size,
4141 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004142{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004143 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004146 status = PSA_ERROR_BAD_STATE;
4147 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004148 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004151 status = PSA_ERROR_BAD_STATE;
4152 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004153 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 status = psa_driver_wrapper_cipher_update(operation,
4156 input,
4157 input_length,
4158 output,
4159 output_size,
4160 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004161
4162exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 if (status != PSA_SUCCESS) {
4164 psa_cipher_abort(operation);
4165 }
Ronald Cron6d051732020-10-01 14:10:20 +02004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004168}
4169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4171 uint8_t *output,
4172 size_t output_size,
4173 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004174{
David Saadab4ecc272019-02-14 13:48:10 +02004175 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004178 status = PSA_ERROR_BAD_STATE;
4179 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004180 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004183 status = PSA_ERROR_BAD_STATE;
4184 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004185 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004186
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 status = psa_driver_wrapper_cipher_finish(operation,
4188 output,
4189 output_size,
4190 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004191
4192exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 if (status == PSA_SUCCESS) {
4194 return psa_cipher_abort(operation);
4195 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004196 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004200 }
mohammad1603503973b2018-03-12 15:59:30 +02004201}
4202
Gilles Peskine449bd832023-01-11 14:50:10 +01004203psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004204{
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004206 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004207 * in use. It's ok to call abort on such an object, and there's
4208 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004210 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004213
Ronald Cron49fafa92021-03-10 08:34:23 +01004214 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004215 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004216 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004219}
4220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4222 psa_algorithm_t alg,
4223 const uint8_t *input,
4224 size_t input_length,
4225 uint8_t *output,
4226 size_t output_size,
4227 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004228{
4229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004230 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004231 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004232 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4233 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004236 status = PSA_ERROR_INVALID_ARGUMENT;
4237 goto exit;
4238 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4241 PSA_KEY_USAGE_ENCRYPT,
4242 alg);
4243 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004244 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004246
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004247 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004249 };
4250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4252 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004253 status = PSA_ERROR_GENERIC_ERROR;
4254 goto exit;
4255 }
4256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 if (default_iv_length > 0) {
4258 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004259 status = PSA_ERROR_BUFFER_TOO_SMALL;
4260 goto exit;
4261 }
4262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 status = psa_generate_random(local_iv, default_iv_length);
4264 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004265 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004267 }
4268
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004269 status = psa_driver_wrapper_cipher_encrypt(
4270 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004271 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004272 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004274
4275exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 unlock_status = psa_unlock_key_slot(slot);
4277 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004278 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004279 }
Ronald Cron23919522021-07-08 19:00:07 +02004280
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 if (status == PSA_SUCCESS) {
4282 if (default_iv_length > 0) {
4283 memcpy(output, local_iv, default_iv_length);
4284 }
4285 *output_length += default_iv_length;
4286 } else {
4287 *output_length = 0;
4288 }
4289
4290 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004291}
4292
Gilles Peskine449bd832023-01-11 14:50:10 +01004293psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4294 psa_algorithm_t alg,
4295 const uint8_t *input,
4296 size_t input_length,
4297 uint8_t *output,
4298 size_t output_size,
4299 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004300{
4301 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004302 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004303 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004304
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004306 status = PSA_ERROR_INVALID_ARGUMENT;
4307 goto exit;
4308 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4311 PSA_KEY_USAGE_DECRYPT,
4312 alg);
4313 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004314 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004316
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004317 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004319 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4322 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004323 status = PSA_ERROR_INVALID_ARGUMENT;
4324 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004326 status = PSA_ERROR_INVALID_ARGUMENT;
4327 goto exit;
4328 }
4329
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004330 status = psa_driver_wrapper_cipher_decrypt(
4331 &attributes, slot->key.data, slot->key.bytes,
4332 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004334
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004335exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 unlock_status = psa_unlock_key_slot(slot);
4337 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004338 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004342 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 }
Ronald Cron23919522021-07-08 19:00:07 +02004344
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004346}
4347
4348
mohammad16035955c982018-04-26 00:53:03 +03004349/****************************************************************/
4350/* AEAD */
4351/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004352
Paul Elliottbaff51c2021-09-28 17:44:45 +01004353/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004354static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004355{
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004357}
4358
4359/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004360static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4361 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004362{
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004366#if defined(PSA_WANT_ALG_GCM)
4367 case PSA_ALG_GCM:
4368 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 * arbitrarily large nonces. Please note that we do not generally
4370 * recommend the usage of nonces of greater length than
4371 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4372 * size, which can then lead to collisions if you encrypt a very
4373 * large number of messages.*/
4374 if (nonce_length != 0) {
4375 return PSA_SUCCESS;
4376 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004377 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004378#endif /* PSA_WANT_ALG_GCM */
4379#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004380 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 if (nonce_length >= 7 && nonce_length <= 13) {
4382 return PSA_SUCCESS;
4383 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004384 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004385#endif /* PSA_WANT_ALG_CCM */
4386#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004387 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 if (nonce_length == 12) {
4389 return PSA_SUCCESS;
4390 } else if (nonce_length == 8) {
4391 return PSA_ERROR_NOT_SUPPORTED;
4392 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004393 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004394#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004395 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004396 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004398 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004401}
4402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004404{
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4406 return PSA_ERROR_INVALID_ARGUMENT;
4407 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004410}
4411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4413 psa_algorithm_t alg,
4414 const uint8_t *nonce,
4415 size_t nonce_length,
4416 const uint8_t *additional_data,
4417 size_t additional_data_length,
4418 const uint8_t *plaintext,
4419 size_t plaintext_length,
4420 uint8_t *ciphertext,
4421 size_t ciphertext_size,
4422 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004423{
Ronald Cron215633c2021-03-16 17:15:37 +01004424 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4425 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004426
mohammad1603f08a5502018-06-03 15:05:47 +03004427 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004428
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 status = psa_aead_check_algorithm(alg);
4430 if (status != PSA_SUCCESS) {
4431 return status;
4432 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004433
Ronald Cron9a986162021-03-26 12:40:07 +01004434 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4436 if (status != PSA_SUCCESS) {
4437 return status;
4438 }
mohammad16035955c982018-04-26 00:53:03 +03004439
Ronald Cron215633c2021-03-16 17:15:37 +01004440 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004442 };
mohammad16035955c982018-04-26 00:53:03 +03004443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 status = psa_aead_check_nonce_length(alg, nonce_length);
4445 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004446 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004448
Ronald Cronde822812021-03-17 16:08:20 +01004449 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004450 &attributes, slot->key.data, slot->key.bytes,
4451 alg,
4452 nonce, nonce_length,
4453 additional_data, additional_data_length,
4454 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4458 memset(ciphertext, 0, ciphertext_size);
4459 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004460
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004461exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004465}
4466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4468 psa_algorithm_t alg,
4469 const uint8_t *nonce,
4470 size_t nonce_length,
4471 const uint8_t *additional_data,
4472 size_t additional_data_length,
4473 const uint8_t *ciphertext,
4474 size_t ciphertext_length,
4475 uint8_t *plaintext,
4476 size_t plaintext_size,
4477 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004478{
Ronald Cron215633c2021-03-16 17:15:37 +01004479 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4480 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004481
Gilles Peskineee652a32018-06-01 19:23:52 +02004482 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004483
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 status = psa_aead_check_algorithm(alg);
4485 if (status != PSA_SUCCESS) {
4486 return status;
4487 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004488
Ronald Cron9a986162021-03-26 12:40:07 +01004489 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4491 if (status != PSA_SUCCESS) {
4492 return status;
4493 }
mohammad16035955c982018-04-26 00:53:03 +03004494
Ronald Cron215633c2021-03-16 17:15:37 +01004495 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004497 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 status = psa_aead_check_nonce_length(alg, nonce_length);
4500 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004501 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004503
Ronald Cronde822812021-03-17 16:08:20 +01004504 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004505 &attributes, slot->key.data, slot->key.bytes,
4506 alg,
4507 nonce, nonce_length,
4508 additional_data, additional_data_length,
4509 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 if (status != PSA_SUCCESS && plaintext_size != 0) {
4513 memset(plaintext, 0, plaintext_size);
4514 }
mohammad160360a64d02018-06-03 17:20:42 +03004515
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004516exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 return status;
mohammad16035955c982018-04-26 00:53:03 +03004520}
4521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4523{
4524 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004527#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004529 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4531 return PSA_ERROR_INVALID_ARGUMENT;
4532 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004533 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004534#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004535
Przemek Stekiel86679c72022-10-06 17:06:56 +02004536#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004538 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4540 return PSA_ERROR_INVALID_ARGUMENT;
4541 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004542 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004543#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004544
Przemek Stekiel86679c72022-10-06 17:06:56 +02004545#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004547 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 if (tag_len != 16) {
4549 return PSA_ERROR_INVALID_ARGUMENT;
4550 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004551 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004552#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004553
4554 default:
4555 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004557 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004559}
4560
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004561/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004562static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4563 int is_encrypt,
4564 mbedtls_svc_key_id_t key,
4565 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004566{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4568 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4569 psa_key_slot_t *slot = NULL;
4570 psa_key_usage_t key_usage = 0;
4571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 status = psa_aead_check_algorithm(alg);
4573 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004574 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004578 status = PSA_ERROR_BAD_STATE;
4579 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004580 }
4581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 if (operation->nonce_set || operation->lengths_set ||
4583 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004584 status = PSA_ERROR_BAD_STATE;
4585 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004586 }
4587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004589 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004591 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4595 alg);
4596 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004597 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004599
4600 psa_key_attributes_t attributes = {
4601 .core = slot->attr
4602 };
4603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004605 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 if (is_encrypt) {
4609 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4610 &attributes,
4611 slot->key.data,
4612 slot->key.bytes,
4613 alg);
4614 } else {
4615 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4616 &attributes,
4617 slot->key.data,
4618 slot->key.bytes,
4619 alg);
4620 }
4621 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004622 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004626
4627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004631 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004633 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 } else {
4635 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004636 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004639}
4640
Paul Elliott302ff6b2021-04-20 18:10:30 +01004641/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004642psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4643 mbedtls_svc_key_id_t key,
4644 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004645{
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004647}
4648
4649/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004650psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4651 mbedtls_svc_key_id_t key,
4652 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004653{
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004655}
4656
4657/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004658psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4659 uint8_t *nonce,
4660 size_t nonce_size,
4661 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004662{
4663 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004664 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004665 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004666
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004667 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004670 status = PSA_ERROR_BAD_STATE;
4671 goto exit;
4672 }
4673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004675 status = PSA_ERROR_BAD_STATE;
4676 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004677 }
4678
Paul Elliotte193ea82021-10-01 13:00:16 +01004679 /* For CCM, this size may not be correct according to the PSA
4680 * specification. The PSA Crypto 1.0.1 specification states:
4681 *
4682 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4683 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4684 *
4685 * However this restriction that L has to be the smallest integer is not
4686 * applied in practice, and it is not implementable here since the
4687 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4689 operation->alg);
4690 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004691 status = PSA_ERROR_BUFFER_TOO_SMALL;
4692 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004693 }
4694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 status = psa_generate_random(local_nonce, required_nonce_size);
4696 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004697 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004699
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004701
Paul Elliott39dc6b82021-05-11 19:16:09 +01004702exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 if (status == PSA_SUCCESS) {
4704 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004705 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 } else {
4707 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004708 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004711}
4712
4713/* Set the nonce for a multipart authenticated encryption or decryption
4714 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004715psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4716 const uint8_t *nonce,
4717 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004718{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004719 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004722 status = PSA_ERROR_BAD_STATE;
4723 goto exit;
4724 }
4725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004727 status = PSA_ERROR_BAD_STATE;
4728 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004729 }
4730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4732 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004733 status = PSA_ERROR_INVALID_ARGUMENT;
4734 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004735 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004736
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4738 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004739
Paul Elliott39dc6b82021-05-11 19:16:09 +01004740exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004742 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 } else {
4744 psa_aead_abort(operation);
4745 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004746
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004748}
4749
4750/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004751psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4752 size_t ad_length,
4753 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004754{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004755 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004758 status = PSA_ERROR_BAD_STATE;
4759 goto exit;
4760 }
4761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if (operation->lengths_set || operation->ad_started ||
4763 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004764 status = PSA_ERROR_BAD_STATE;
4765 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004766 }
4767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004769#if defined(PSA_WANT_ALG_GCM)
4770 case PSA_ALG_GCM:
4771 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 * bits. Without the guard this code will generate warnings on 32bit
4773 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004774#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 if (((uint64_t) ad_length) >> 61 != 0 ||
4776 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004777 status = PSA_ERROR_INVALID_ARGUMENT;
4778 goto exit;
4779 }
Paul Elliott325d3742021-09-27 17:56:28 +01004780#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004781 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004782#endif /* PSA_WANT_ALG_GCM */
4783#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004784 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004786 status = PSA_ERROR_INVALID_ARGUMENT;
4787 goto exit;
4788 }
4789 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004790#endif /* PSA_WANT_ALG_CCM */
4791#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004792 case PSA_ALG_CHACHA20_POLY1305:
4793 /* No length restrictions for ChaChaPoly. */
4794 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004795#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004796 default:
4797 break;
4798 }
Paul Elliott325d3742021-09-27 17:56:28 +01004799
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4801 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004802
Paul Elliott39dc6b82021-05-11 19:16:09 +01004803exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004805 operation->ad_remaining = ad_length;
4806 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004807 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 } else {
4809 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004810 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004813}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004814
4815/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004816psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4817 const uint8_t *input,
4818 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004819{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004820 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004823 status = PSA_ERROR_BAD_STATE;
4824 goto exit;
4825 }
4826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004828 status = PSA_ERROR_BAD_STATE;
4829 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004830 }
4831
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (operation->lengths_set) {
4833 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004834 status = PSA_ERROR_INVALID_ARGUMENT;
4835 goto exit;
4836 }
4837
4838 operation->ad_remaining -= input_length;
4839 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004840#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004842 status = PSA_ERROR_BAD_STATE;
4843 goto exit;
4844 }
4845#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 status = psa_driver_wrapper_aead_update_ad(operation, input,
4848 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004849
Paul Elliott39dc6b82021-05-11 19:16:09 +01004850exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004852 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 } else {
4854 psa_aead_abort(operation);
4855 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004858}
4859
4860/* Encrypt or decrypt a message fragment in an active multipart AEAD
4861 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004862psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4863 const uint8_t *input,
4864 size_t input_length,
4865 uint8_t *output,
4866 size_t output_size,
4867 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004868{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004869 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004870
4871 *output_length = 0;
4872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004874 status = PSA_ERROR_BAD_STATE;
4875 goto exit;
4876 }
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004879 status = PSA_ERROR_BAD_STATE;
4880 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004881 }
4882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004884 /* Additional data length was supplied, but not all the additional
4885 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004887 status = PSA_ERROR_INVALID_ARGUMENT;
4888 goto exit;
4889 }
4890
4891 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004893 status = PSA_ERROR_INVALID_ARGUMENT;
4894 goto exit;
4895 }
4896
4897 operation->body_remaining -= input_length;
4898 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004899#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004901 status = PSA_ERROR_BAD_STATE;
4902 goto exit;
4903 }
4904#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004905
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4907 output, output_size,
4908 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004909
Paul Elliott39dc6b82021-05-11 19:16:09 +01004910exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004912 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 } else {
4914 psa_aead_abort(operation);
4915 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004918}
4919
Gilles Peskine449bd832023-01-11 14:50:10 +01004920static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004921{
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 if (operation->id == 0 || !operation->nonce_set) {
4923 return PSA_ERROR_BAD_STATE;
4924 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4927 operation->body_remaining != 0)) {
4928 return PSA_ERROR_INVALID_ARGUMENT;
4929 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004930
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004932}
4933
Paul Elliott302ff6b2021-04-20 18:10:30 +01004934/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004935psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4936 uint8_t *ciphertext,
4937 size_t ciphertext_size,
4938 size_t *ciphertext_length,
4939 uint8_t *tag,
4940 size_t tag_size,
4941 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004943 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4944
Paul Elliott302ff6b2021-04-20 18:10:30 +01004945 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004946 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 status = psa_aead_final_checks(operation);
4949 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004950 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004954 status = PSA_ERROR_BAD_STATE;
4955 goto exit;
4956 }
4957
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4959 ciphertext_size,
4960 ciphertext_length,
4961 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004962
Paul Elliott39dc6b82021-05-11 19:16:09 +01004963exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004964
4965
Paul Elliottf47b0952021-05-21 18:02:33 +01004966 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004967 * the zero tag size, make sure the tag is set to something implausible.
4968 * Even if the operation succeeds, make sure we clear the rest of the
4969 * buffer to prevent potential leakage of anything previously placed in
4970 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004971 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004972
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004974
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004976}
4977
4978/* Finish authenticating and decrypting a message in a multipart AEAD
4979 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004980psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4981 uint8_t *plaintext,
4982 size_t plaintext_size,
4983 size_t *plaintext_length,
4984 const uint8_t *tag,
4985 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004986{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004987 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4988
Paul Elliott302ff6b2021-04-20 18:10:30 +01004989 *plaintext_length = 0;
4990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 status = psa_aead_final_checks(operation);
4992 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004993 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004995
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004997 status = PSA_ERROR_BAD_STATE;
4998 goto exit;
4999 }
5000
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5002 plaintext_size,
5003 plaintext_length,
5004 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005005
5006exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005008
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005010}
5011
5012/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005013psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005014{
5015 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5016
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005018 /* The object has (apparently) been initialized but it is not (yet)
5019 * in use. It's ok to call abort on such an object, and there's
5020 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005022 }
5023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005025
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005027
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005029}
5030
Gilles Peskinee59236f2018-01-27 23:32:46 +01005031/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005032/* Generators */
5033/****************************************************************/
5034
Przemek Stekiel69c46792022-06-10 12:59:51 +02005035#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005036 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005037 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5038 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005039#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005040#endif /* At least one builtin KDF */
5041
Przemek Stekiel69c46792022-06-10 12:59:51 +02005042#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005043 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5044 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5045static psa_status_t psa_key_derivation_start_hmac(
5046 psa_mac_operation_t *operation,
5047 psa_algorithm_t hash_alg,
5048 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005050{
5051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5052 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5054 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5055 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005056
Steven Cooreman72f736a2021-05-07 14:14:37 +02005057 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005059
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 status = psa_driver_wrapper_mac_sign_setup(operation,
5061 &attributes,
5062 hmac_key, hmac_key_length,
5063 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005064
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 psa_reset_key_attributes(&attributes);
5066 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005067}
5068#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005069
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005070#define HKDF_STATE_INIT 0 /* no input yet */
5071#define HKDF_STATE_STARTED 1 /* got salt */
5072#define HKDF_STATE_KEYED 2 /* got key */
5073#define HKDF_STATE_OUTPUT 3 /* output started */
5074
Gilles Peskinecbe66502019-05-16 16:59:18 +02005075static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005077{
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5079 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5080 } else {
5081 return operation->alg;
5082 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005083}
5084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005086{
5087 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5089 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005090 /* The object has (apparently) been initialized but it is not
5091 * in use. It's ok to call abort on such an object, and there's
5092 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005094#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5096 mbedtls_free(operation->ctx.hkdf.info);
5097 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5098 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005099#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005100#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5101 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5103 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5104 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5105 if (operation->ctx.tls12_prf.secret != NULL) {
5106 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5107 operation->ctx.tls12_prf.secret_length);
5108 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005109 }
5110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 if (operation->ctx.tls12_prf.seed != NULL) {
5112 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5113 operation->ctx.tls12_prf.seed_length);
5114 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005115 }
5116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (operation->ctx.tls12_prf.label != NULL) {
5118 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5119 operation->ctx.tls12_prf.label_length);
5120 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005121 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005122#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 if (operation->ctx.tls12_prf.other_secret != NULL) {
5124 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5125 operation->ctx.tls12_prf.other_secret_length);
5126 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005127 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005128#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005129 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005130
5131 /* We leave the fields Ai and output_block to be erased safely by the
5132 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005134#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5135 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005136#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5138 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5139 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5140 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005141#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005142 {
5143 status = PSA_ERROR_BAD_STATE;
5144 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 mbedtls_platform_zeroize(operation, sizeof(*operation));
5146 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005147}
5148
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005149psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005151{
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005153 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005155 }
5156
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005157 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005159}
5160
Gilles Peskine449bd832023-01-11 14:50:10 +01005161psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5162 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005163{
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 if (operation->alg == 0) {
5165 return PSA_ERROR_BAD_STATE;
5166 }
5167 if (capacity > operation->capacity) {
5168 return PSA_ERROR_INVALID_ARGUMENT;
5169 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005170 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005172}
5173
Przemek Stekiel69c46792022-06-10 12:59:51 +02005174#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005175/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005176static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5177 psa_algorithm_t kdf_alg,
5178 uint8_t *output,
5179 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005180{
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5182 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005183 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005184 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005185#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005187#else
5188 const uint8_t last_block = 0xff;
5189#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005190
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 if (hkdf->state < HKDF_STATE_KEYED ||
5192 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005193#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005195#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 )) {
5197 return PSA_ERROR_BAD_STATE;
5198 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005199 hkdf->state = HKDF_STATE_OUTPUT;
5200
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005202 /* Copy what remains of the current block */
5203 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005205 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 }
5207 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005208 output += n;
5209 output_length -= n;
5210 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005212 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005214 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005215 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005217 * object was corrupted or if this function is called directly
5218 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 if (hkdf->block_number == last_block) {
5220 return PSA_ERROR_BAD_STATE;
5221 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005222
5223 /* We need a new block */
5224 ++hkdf->block_number;
5225 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005226
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5228 hash_alg,
5229 hkdf->prk,
5230 hash_length);
5231 if (status != PSA_SUCCESS) {
5232 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005233 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005234
5235 if (hkdf->block_number != 1) {
5236 status = psa_mac_update(&hkdf->hmac,
5237 hkdf->output_block,
5238 hash_length);
5239 if (status != PSA_SUCCESS) {
5240 return status;
5241 }
5242 }
5243 status = psa_mac_update(&hkdf->hmac,
5244 hkdf->info,
5245 hkdf->info_length);
5246 if (status != PSA_SUCCESS) {
5247 return status;
5248 }
5249 status = psa_mac_update(&hkdf->hmac,
5250 &hkdf->block_number, 1);
5251 if (status != PSA_SUCCESS) {
5252 return status;
5253 }
5254 status = psa_mac_sign_finish(&hkdf->hmac,
5255 hkdf->output_block,
5256 sizeof(hkdf->output_block),
5257 &hmac_output_length);
5258 if (status != PSA_SUCCESS) {
5259 return status;
5260 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005261 }
5262
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005264}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005265#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005266
John Durkop07cc04a2020-11-16 22:08:34 -08005267#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5268 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005269static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5270 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005272{
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5274 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005275 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5276 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005277 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005278
Janos Follath7742fee2019-06-17 12:58:10 +01005279 /* We can't be wanting more output after block 0xff, otherwise
5280 * the capacity check in psa_key_derivation_output_bytes() would have
5281 * prevented this call. It could happen only if the operation
5282 * object was corrupted or if this function is called directly
5283 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 if (tls12_prf->block_number == 0xff) {
5285 return PSA_ERROR_CORRUPTION_DETECTED;
5286 }
Janos Follath7742fee2019-06-17 12:58:10 +01005287
5288 /* We need a new block */
5289 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005290 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005291
5292 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5293 *
5294 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5295 *
5296 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5297 * HMAC_hash(secret, A(2) + seed) +
5298 * HMAC_hash(secret, A(3) + seed) + ...
5299 *
5300 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005301 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005302 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005303 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005304 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005305 * is currently extracted as `output_block` and where i is
5306 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005307 */
5308
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 status = psa_key_derivation_start_hmac(&hmac,
5310 hash_alg,
5311 tls12_prf->secret,
5312 tls12_prf->secret_length);
5313 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005314 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005316
5317 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005319 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5320 * the variable seed and in this instance means it in the context of the
5321 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 status = psa_mac_update(&hmac,
5323 tls12_prf->label,
5324 tls12_prf->label_length);
5325 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005326 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 }
5328 status = psa_mac_update(&hmac,
5329 tls12_prf->seed,
5330 tls12_prf->seed_length);
5331 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005332 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 }
5334 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005335 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5337 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005338 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005340 }
5341
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 status = psa_mac_sign_finish(&hmac,
5343 tls12_prf->Ai, hash_length,
5344 &hmac_output_length);
5345 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005346 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 }
5348 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005349 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005351
5352 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 status = psa_key_derivation_start_hmac(&hmac,
5354 hash_alg,
5355 tls12_prf->secret,
5356 tls12_prf->secret_length);
5357 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005358 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 }
5360 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5361 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005362 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 }
5364 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5365 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005366 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 }
5368 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5369 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005370 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 }
5372 status = psa_mac_sign_finish(&hmac,
5373 tls12_prf->output_block, hash_length,
5374 &hmac_output_length);
5375 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005376 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005378
Janos Follath7742fee2019-06-17 12:58:10 +01005379
5380cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 cleanup_status = psa_mac_abort(&hmac);
5382 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005383 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005387}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005388
Janos Follath844eb0e2019-06-19 12:10:49 +01005389static psa_status_t psa_key_derivation_tls12_prf_read(
5390 psa_tls12_prf_key_derivation_t *tls12_prf,
5391 psa_algorithm_t alg,
5392 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005394{
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5396 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005397 psa_status_t status;
5398 uint8_t offset, length;
5399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005401 case PSA_TLS12_PRF_STATE_LABEL_SET:
5402 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5403 break;
5404 case PSA_TLS12_PRF_STATE_OUTPUT:
5405 break;
5406 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005408 }
5409
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005411 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 if (tls12_prf->left_in_block == 0) {
5413 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5414 alg);
5415 if (status != PSA_SUCCESS) {
5416 return status;
5417 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005418
5419 continue;
5420 }
5421
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005423 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005425 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005427
5428 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005430 output += length;
5431 output_length -= length;
5432 tls12_prf->left_in_block -= length;
5433 }
5434
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005436}
John Durkop07cc04a2020-11-16 22:08:34 -08005437#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5438 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005439
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005440#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5441static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5442 psa_tls12_ecjpake_to_pms_t *ecjpake,
5443 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005445{
5446 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005447 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 if (output_length != 32) {
5450 return PSA_ERROR_INVALID_ARGUMENT;
5451 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005452
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5454 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5455 &output_size);
5456 if (status != PSA_SUCCESS) {
5457 return status;
5458 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005459
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 if (output_size != output_length) {
5461 return PSA_ERROR_GENERIC_ERROR;
5462 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005465}
5466#endif
5467
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005468psa_status_t psa_key_derivation_output_bytes(
5469 psa_key_derivation_operation_t *operation,
5470 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005472{
5473 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005477 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005479 }
5480
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005482 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005483 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005484 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005485 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005486 goto exit;
5487 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005489 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005490 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005491 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5492 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005493 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005494 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005496 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005497 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005498
Przemek Stekiel69c46792022-06-10 12:59:51 +02005499#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5501 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5502 output, output_length);
5503 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005504#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005505#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5506 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5508 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5509 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5510 kdf_alg, output,
5511 output_length);
5512 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005513#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5514 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005515#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005517 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5519 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005520#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5521
Gilles Peskineeab56e42018-07-12 17:12:33 +02005522 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005523 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005525 }
5526
5527exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005529 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005530 * This allows us to differentiate between exhausted operations and
5531 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5532 * operations. */
5533 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005535 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005537 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005539}
5540
David Brown7807bf72021-02-09 16:28:23 -07005541#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005542static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005543{
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 if (data_size >= 8) {
5545 mbedtls_des_key_set_parity(data);
5546 }
5547 if (data_size >= 16) {
5548 mbedtls_des_key_set_parity(data + 8);
5549 }
5550 if (data_size >= 24) {
5551 mbedtls_des_key_set_parity(data + 16);
5552 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005553}
David Brown7807bf72021-02-09 16:28:23 -07005554#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005555
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005556/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 * ECC keys on a Weierstrass elliptic curve require the generation
5558 * of a private key which is an integer
5559 * in the range [1, N - 1], where N is the boundary of the private key domain:
5560 * N is the prime p for Diffie-Hellman, or the order of the
5561 * curve’s base point for ECC.
5562 *
5563 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5564 * This function generates the private key using the following process:
5565 *
5566 * 1. Draw a byte string of length ceiling(m/8) bytes.
5567 * 2. If m is not a multiple of 8, set the most significant
5568 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5569 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5570 * 4. If k > N - 2, discard the result and return to step 1.
5571 * 5. Output k + 1 as the private key.
5572 *
5573 * This method allows compliance to NIST standards, specifically the methods titled
5574 * Key-Pair Generation by Testing Candidates in the following publications:
5575 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5576 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5577 * Diffie-Hellman keys.
5578 *
5579 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5580 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5581 *
5582 * Note: Function allocates memory for *data buffer, so given *data should be
5583 * always NULL.
5584 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005585#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5586 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005587 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5588 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5589 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005590static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005591 psa_key_slot_t *slot,
5592 size_t bits,
5593 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005594 uint8_t **data
5595 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005596{
Valerio Setti52789862023-04-07 12:13:11 +02005597#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005598 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005599 mbedtls_mpi k;
5600 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005603
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 mbedtls_mpi_init(&k);
5605 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005606
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005607 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005609 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005611
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005613 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005614 goto cleanup;
5615 }
5616
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005617 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005621
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005622 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005623 /* Let m be the bit size of N. */
5624 size_t m = ecp_group.nbits;
5625
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005627
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005628 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005630
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005631 /* Note: This function is always called with *data == NULL and it
5632 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 *data = mbedtls_calloc(1, m_bytes);
5634 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005635 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005636 goto cleanup;
5637 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005640 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005642 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005644
5645 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005647 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005648 * (8 * ceiling(m/8) - m) bits of the first byte in
5649 * the string to zero.
5650 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005652 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005653 }
5654
5655 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 * big-endian byte string.
5657 */
5658 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005659
5660 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 * Result of comparison is returned. When it indicates error
5662 * then this function is called again.
5663 */
5664 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005665 }
5666
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005667 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5669 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005670cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 if (ret != 0) {
5672 status = mbedtls_to_psa_error(ret);
5673 }
5674 if (status != PSA_SUCCESS) {
5675 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005676 *data = NULL;
5677 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 mbedtls_mpi_free(&k);
5679 mbedtls_mpi_free(&diff_N_2);
5680 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005681#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005682 (void) slot;
5683 (void) bits;
5684 (void) operation;
5685 (void) data;
5686 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005687#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005688}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005689
5690/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5691 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5692 *
5693 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5694 * draw a 32-byte string and process it as specified in
5695 * Elliptic Curves for Security [RFC7748] §5.
5696 *
5697 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5698 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5699 *
5700 * Note: Function allocates memory for *data buffer, so given *data should be
5701 * always NULL.
5702 */
5703
5704static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5705 size_t bits,
5706 psa_key_derivation_operation_t *operation,
5707 uint8_t **data
5708 )
5709{
5710 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005711 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005714 case 255:
5715 output_length = 32;
5716 break;
5717 case 448:
5718 output_length = 56;
5719 break;
5720 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005722 break;
5723 }
5724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 if (*data == NULL) {
5728 return PSA_ERROR_INSUFFICIENT_MEMORY;
5729 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005734 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005738 case 255:
5739 (*data)[0] &= 248;
5740 (*data)[31] &= 127;
5741 (*data)[31] |= 64;
5742 break;
5743 case 448:
5744 (*data)[0] &= 252;
5745 (*data)[55] |= 128;
5746 break;
5747 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005749 break;
5750 }
5751
5752 return status;
5753}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005754#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5755 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005756 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5757 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5758 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005759
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005760static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005761 psa_key_slot_t *slot,
5762 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005764{
5765 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305767 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005768 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5771 return PSA_ERROR_INVALID_ARGUMENT;
5772 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005773
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005774#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5775 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005776 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5777 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5778 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5780 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5781 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005782 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5784 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005785 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 }
5787 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005788 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5790 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005791 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005793 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005794 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005795#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5796 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005797 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5798 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5799 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 if (key_type_is_raw_bytes(slot->attr.type)) {
5801 if (bits % 8 != 0) {
5802 return PSA_ERROR_INVALID_ARGUMENT;
5803 }
5804 data = mbedtls_calloc(1, bytes);
5805 if (data == NULL) {
5806 return PSA_ERROR_INSUFFICIENT_MEMORY;
5807 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 status = psa_key_derivation_output_bytes(operation, data, bytes);
5810 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005811 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 }
David Brown12ca5032021-02-11 11:02:00 -07005813#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5815 psa_des_set_key_parity(data, bytes);
5816 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005817#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 } else {
5819 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005820 }
Ronald Crondd04d422020-11-28 15:14:42 +01005821
Ronald Cronbf33c932020-11-28 18:06:53 +01005822 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005823 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005825 };
5826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5828 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5829 &storage_size);
5830 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305831 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 }
Archanad8a83dc2021-06-14 10:04:16 +05305833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 status = psa_allocate_buffer_to_slot(slot, storage_size);
5835 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305836 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 }
Archanad8a83dc2021-06-14 10:04:16 +05305838
Przemek Stekielc80e7502023-05-11 11:14:25 +02005839 if (PSA_KEY_TYPE_IS_ASYMMETRIC(attributes.core.type) &&
5840 PSA_KEY_TYPE_IS_DH(attributes.core.type)) {
5841 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(bytes)) == 0) {
5842 status = PSA_ERROR_INVALID_ARGUMENT;
5843 goto exit;
5844 }
5845
5846 /* Copy the key material. */
5847 memcpy(slot->key.data, data, bytes);
5848 bits = PSA_BYTES_TO_BITS(bytes);
5849
5850 status = PSA_SUCCESS;
5851 } else {
5852 status = psa_driver_wrapper_import_key(&attributes,
5853 data, bytes,
5854 slot->key.data,
5855 slot->key.bytes,
5856 &slot->key.bytes, &bits);
5857 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005859 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005861
5862exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 mbedtls_free(data);
5864 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005865}
5866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5868 psa_key_derivation_operation_t *operation,
5869 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005870{
5871 psa_status_t status;
5872 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005873 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005874
Ronald Cron81709fc2020-11-14 12:10:32 +01005875 *key = MBEDTLS_SVC_KEY_ID_INIT;
5876
Gilles Peskine0f84d622019-09-12 19:03:13 +02005877 /* Reject any attempt to create a zero-length key so that we don't
5878 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 if (psa_get_key_bits(attributes) == 0) {
5880 return PSA_ERROR_INVALID_ARGUMENT;
5881 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 if (operation->alg == PSA_ALG_NONE) {
5884 return PSA_ERROR_BAD_STATE;
5885 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 if (!operation->can_output_key) {
5888 return PSA_ERROR_NOT_PERMITTED;
5889 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5892 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005893#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005895 /* Deriving a key in a secure element is not implemented yet. */
5896 status = PSA_ERROR_NOT_SUPPORTED;
5897 }
5898#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 if (status == PSA_SUCCESS) {
5900 status = psa_generate_derived_key_internal(slot,
5901 attributes->core.bits,
5902 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005903 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 if (status == PSA_SUCCESS) {
5905 status = psa_finish_key_creation(slot, driver, key);
5906 }
5907 if (status != PSA_SUCCESS) {
5908 psa_fail_key_creation(slot, driver);
5909 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005912}
5913
Gilles Peskineeab56e42018-07-12 17:12:33 +02005914
5915
5916/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005917/* Key derivation */
5918/****************************************************************/
5919
Steven Cooreman932ffb72021-02-15 12:14:32 +01005920#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005921static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005922{
5923#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5925 return 1;
5926 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005927#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005928#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5930 return 1;
5931 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005932#endif
5933#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5935 return 1;
5936 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005937#endif
5938#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5940 return 1;
5941 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005942#endif
5943#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5945 return 1;
5946 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005947#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005948#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5950 return 1;
5951 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005952#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005954}
5955
Gilles Peskine449bd832023-01-11 14:50:10 +01005956static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005957{
5958 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 psa_status_t status = psa_hash_setup(&operation, alg);
5960 psa_hash_abort(&operation);
5961 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005962}
5963
Gilles Peskine969c5d62019-01-16 15:53:06 +01005964static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005965 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005967{
Janos Follath5fe19732019-06-20 15:09:30 +01005968 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5969 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005971
Gilles Peskine969c5d62019-01-16 15:53:06 +01005972 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 if (!is_kdf_alg_supported(kdf_alg)) {
5974 return PSA_ERROR_NOT_SUPPORTED;
5975 }
John Durkop07cc04a2020-11-16 22:08:34 -08005976
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005977 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005978 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5980 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5981 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5982 if (hash_size == 0) {
5983 return PSA_ERROR_NOT_SUPPORTED;
5984 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005985
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005986 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5987 * we might fail later, which is somewhat unfriendly and potentially
5988 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 psa_status_t status = psa_hash_try_support(hash_alg);
5990 if (status != PSA_SUCCESS) {
5991 return status;
5992 }
5993 } else {
5994 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005995 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5998 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5999 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6000 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006001 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006002#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006003 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6005 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006006 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006008#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6009 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 operation->capacity = 255 * hash_size;
6011 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006012}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006013
Gilles Peskine449bd832023-01-11 14:50:10 +01006014static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006015{
6016#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 if (alg == PSA_ALG_ECDH) {
6018 return PSA_SUCCESS;
6019 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006020#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006021#if defined(PSA_WANT_ALG_FFDH)
6022 if (alg == PSA_ALG_FFDH) {
6023 return PSA_SUCCESS;
6024 }
6025#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006026 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006028}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006029
6030static int psa_key_derivation_allows_free_form_secret_input(
6031 psa_algorithm_t kdf_alg)
6032{
6033#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6034 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6035 return 0;
6036 }
6037#endif
6038 (void) kdf_alg;
6039 return 1;
6040}
John Durkop07cc04a2020-11-16 22:08:34 -08006041#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006042
Gilles Peskine449bd832023-01-11 14:50:10 +01006043psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6044 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006045{
6046 psa_status_t status;
6047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 if (operation->alg != 0) {
6049 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006050 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6053 return PSA_ERROR_INVALID_ARGUMENT;
6054 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6055#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6056 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6057 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6058 status = psa_key_agreement_try_support(ka_alg);
6059 if (status != PSA_SUCCESS) {
6060 return status;
6061 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006062 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6063 return PSA_ERROR_INVALID_ARGUMENT;
6064 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6066#else
6067 return PSA_ERROR_NOT_SUPPORTED;
6068#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6069 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6070#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6071 status = psa_key_derivation_setup_kdf(operation, alg);
6072#else
6073 return PSA_ERROR_NOT_SUPPORTED;
6074#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6075 } else {
6076 return PSA_ERROR_INVALID_ARGUMENT;
6077 }
6078
6079 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006080 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 }
6082 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006083}
6084
Przemek Stekiel69c46792022-06-10 12:59:51 +02006085#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006086static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6087 psa_algorithm_t kdf_alg,
6088 psa_key_derivation_step_t step,
6089 const uint8_t *data,
6090 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006091{
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006093 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006095 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006096#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6098 return PSA_ERROR_INVALID_ARGUMENT;
6099 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006100#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 if (hkdf->state != HKDF_STATE_INIT) {
6102 return PSA_ERROR_BAD_STATE;
6103 } else {
6104 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6105 hash_alg,
6106 data, data_length);
6107 if (status != PSA_SUCCESS) {
6108 return status;
6109 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006110 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006112 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006113 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006114#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006116 /* We shouldn't be in different state as HKDF_EXPAND only allows
6117 * two inputs: SECRET (this case) and INFO which does not modify
6118 * the state. It could happen only if the hkdf
6119 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 if (hkdf->state != HKDF_STATE_INIT) {
6121 return PSA_ERROR_BAD_STATE;
6122 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006123
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006124 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6126 return PSA_ERROR_INVALID_ARGUMENT;
6127 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006128
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 memcpy(hkdf->prk, data, data_length);
6130 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006131#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006132 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006133 /* HKDF: If no salt was provided, use an empty salt.
6134 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006136#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6138 return PSA_ERROR_BAD_STATE;
6139 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006140#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6142 hash_alg,
6143 NULL, 0);
6144 if (status != PSA_SUCCESS) {
6145 return status;
6146 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006147 hkdf->state = HKDF_STATE_STARTED;
6148 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 if (hkdf->state != HKDF_STATE_STARTED) {
6150 return PSA_ERROR_BAD_STATE;
6151 }
6152 status = psa_mac_update(&hkdf->hmac,
6153 data, data_length);
6154 if (status != PSA_SUCCESS) {
6155 return status;
6156 }
6157 status = psa_mac_sign_finish(&hkdf->hmac,
6158 hkdf->prk,
6159 sizeof(hkdf->prk),
6160 &data_length);
6161 if (status != PSA_SUCCESS) {
6162 return status;
6163 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006164 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006165
Gilles Peskine2b522db2019-04-12 15:11:49 +02006166 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006167 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006168#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006170 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006172 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006174#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006175 {
6176 /* Block 0 is empty, and the next block will be
6177 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006179 }
6180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006182 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006183#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6185 return PSA_ERROR_INVALID_ARGUMENT;
6186 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006187#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006188#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6190 hkdf->state == HKDF_STATE_INIT) {
6191 return PSA_ERROR_BAD_STATE;
6192 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006193#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 if (hkdf->state == HKDF_STATE_OUTPUT) {
6195 return PSA_ERROR_BAD_STATE;
6196 }
6197 if (hkdf->info_set) {
6198 return PSA_ERROR_BAD_STATE;
6199 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006200 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (data_length != 0) {
6202 hkdf->info = mbedtls_calloc(1, data_length);
6203 if (hkdf->info == NULL) {
6204 return PSA_ERROR_INSUFFICIENT_MEMORY;
6205 }
6206 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006207 }
6208 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006210 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006212 }
6213}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006214#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006215
John Durkop07cc04a2020-11-16 22:08:34 -08006216#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6217 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006218static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6219 const uint8_t *data,
6220 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006221{
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6223 return PSA_ERROR_BAD_STATE;
6224 }
Janos Follathf08e2652019-06-13 09:05:41 +01006225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 if (data_length != 0) {
6227 prf->seed = mbedtls_calloc(1, data_length);
6228 if (prf->seed == NULL) {
6229 return PSA_ERROR_INSUFFICIENT_MEMORY;
6230 }
Janos Follathf08e2652019-06-13 09:05:41 +01006231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006233 prf->seed_length = data_length;
6234 }
Janos Follathf08e2652019-06-13 09:05:41 +01006235
Gilles Peskine43f958b2020-12-13 14:55:14 +01006236 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006239}
6240
Gilles Peskine449bd832023-01-11 14:50:10 +01006241static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6242 const uint8_t *data,
6243 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006244{
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6246 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6247 return PSA_ERROR_BAD_STATE;
6248 }
Janos Follath81550542019-06-13 14:26:34 +01006249
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 if (data_length != 0) {
6251 prf->secret = mbedtls_calloc(1, data_length);
6252 if (prf->secret == NULL) {
6253 return PSA_ERROR_INSUFFICIENT_MEMORY;
6254 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006257 prf->secret_length = data_length;
6258 }
Janos Follath81550542019-06-13 14:26:34 +01006259
Gilles Peskine43f958b2020-12-13 14:55:14 +01006260 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006261
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006263}
6264
Gilles Peskine449bd832023-01-11 14:50:10 +01006265static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6266 const uint8_t *data,
6267 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006268{
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6270 return PSA_ERROR_BAD_STATE;
6271 }
Janos Follath63028dd2019-06-13 09:15:47 +01006272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 if (data_length != 0) {
6274 prf->label = mbedtls_calloc(1, data_length);
6275 if (prf->label == NULL) {
6276 return PSA_ERROR_INSUFFICIENT_MEMORY;
6277 }
Janos Follath63028dd2019-06-13 09:15:47 +01006278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006280 prf->label_length = data_length;
6281 }
Janos Follath63028dd2019-06-13 09:15:47 +01006282
Gilles Peskine43f958b2020-12-13 14:55:14 +01006283 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006286}
6287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6289 psa_key_derivation_step_t step,
6290 const uint8_t *data,
6291 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006292{
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006294 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006296 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006298 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006300 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006302 }
6303}
John Durkop07cc04a2020-11-16 22:08:34 -08006304#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6305 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6306
6307#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6308static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6309 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006310 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006312{
6313 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6315 4 + data_length + prf->other_secret_length :
6316 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6319 return PSA_ERROR_INVALID_ARGUMENT;
6320 }
John Durkop07cc04a2020-11-16 22:08:34 -08006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 uint8_t *pms = mbedtls_calloc(1, pms_len);
6323 if (pms == NULL) {
6324 return PSA_ERROR_INSUFFICIENT_MEMORY;
6325 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006326 uint8_t *cur = pms;
6327
6328 /* pure-PSK:
6329 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006330 *
6331 * The premaster secret is formed as follows: if the PSK is N octets
6332 * long, concatenate a uint16 with the value N, N zero octets, a second
6333 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006334 *
6335 * mixed-PSK:
6336 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6337 * follows: concatenate a uint16 with the length of the other secret,
6338 * the other secret itself, uint16 with the length of PSK, and the
6339 * PSK itself.
6340 * For details please check:
6341 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6342 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6343 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006344 */
6345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6347 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6348 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6349 if (prf->other_secret_length != 0) {
6350 memcpy(cur, prf->other_secret, prf->other_secret_length);
6351 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006352 cur += prf->other_secret_length;
6353 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 } else {
6355 *cur++ = MBEDTLS_BYTE_1(data_length);
6356 *cur++ = MBEDTLS_BYTE_0(data_length);
6357 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006358 cur += data_length;
6359 }
6360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 *cur++ = MBEDTLS_BYTE_1(data_length);
6362 *cur++ = MBEDTLS_BYTE_0(data_length);
6363 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006364 cur += data_length;
6365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 mbedtls_platform_zeroize(pms, pms_len);
6369 mbedtls_free(pms);
6370 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006371}
Janos Follath6660f0e2019-06-17 08:44:03 +01006372
Przemek Stekiele47201b2022-04-19 13:53:28 +02006373static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6374 psa_tls12_prf_key_derivation_t *prf,
6375 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006377{
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6379 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006380 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006381
6382 if (data_length != 0) {
6383 prf->other_secret = mbedtls_calloc(1, data_length);
6384 if (prf->other_secret == NULL) {
6385 return PSA_ERROR_INSUFFICIENT_MEMORY;
6386 }
6387
6388 memcpy(prf->other_secret, data, data_length);
6389 prf->other_secret_length = data_length;
6390 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006391 prf->other_secret_length = 0;
6392 }
6393
6394 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006397}
6398
Janos Follath6660f0e2019-06-17 08:44:03 +01006399static psa_status_t psa_tls12_prf_psk_to_ms_input(
6400 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006401 psa_key_derivation_step_t step,
6402 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006404{
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006406 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 return psa_tls12_prf_psk_to_ms_set_key(prf,
6408 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006409 break;
6410 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6412 data,
6413 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006414 break;
6415 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006417 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006418
Przemek Stekiele47201b2022-04-19 13:53:28 +02006419 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006420}
John Durkop07cc04a2020-11-16 22:08:34 -08006421#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006422
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006423#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6424static psa_status_t psa_tls12_ecjpake_to_pms_input(
6425 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006426 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006427 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006429{
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6431 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6432 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006433 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006434
6435 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 if (data[0] != 0x04) {
6437 return PSA_ERROR_INVALID_ARGUMENT;
6438 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006439
6440 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006442
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006444}
6445#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006446/** Check whether the given key type is acceptable for the given
6447 * input step of a key derivation.
6448 *
6449 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6450 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6451 * Both secret and non-secret inputs can alternatively have the type
6452 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6453 * that the input was passed as a buffer rather than via a key object.
6454 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006455static int psa_key_derivation_check_input_type(
6456 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006458{
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006460 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 if (key_type == PSA_KEY_TYPE_DERIVE) {
6462 return PSA_SUCCESS;
6463 }
6464 if (key_type == PSA_KEY_TYPE_NONE) {
6465 return PSA_SUCCESS;
6466 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006467 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006468 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 if (key_type == PSA_KEY_TYPE_DERIVE) {
6470 return PSA_SUCCESS;
6471 }
6472 if (key_type == PSA_KEY_TYPE_NONE) {
6473 return PSA_SUCCESS;
6474 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006475 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006476 case PSA_KEY_DERIVATION_INPUT_LABEL:
6477 case PSA_KEY_DERIVATION_INPUT_SALT:
6478 case PSA_KEY_DERIVATION_INPUT_INFO:
6479 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6481 return PSA_SUCCESS;
6482 }
6483 if (key_type == PSA_KEY_TYPE_NONE) {
6484 return PSA_SUCCESS;
6485 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006486 break;
6487 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006489}
6490
Janos Follathb80a94e2019-06-12 15:54:46 +01006491static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006492 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006493 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006494 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006495 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006497{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006498 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006500
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 status = psa_key_derivation_check_input_type(step, key_type);
6502 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006503 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006505
Przemek Stekiel69c46792022-06-10 12:59:51 +02006506#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6508 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6509 step, data, data_length);
6510 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006511#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006512#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6514 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6515 step, data, data_length);
6516 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006517#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6518#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6520 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6521 step, data, data_length);
6522 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006523#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006524#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006526 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6528 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006529#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006530 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006531 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006532 (void) data;
6533 (void) data_length;
6534 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006536 }
6537
Gilles Peskine224b0d62019-09-23 18:13:17 +02006538exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 if (status != PSA_SUCCESS) {
6540 psa_key_derivation_abort(operation);
6541 }
6542 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006543}
6544
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306545static psa_status_t psa_key_derivation_input_integer_internal(
6546 psa_key_derivation_operation_t *operation,
6547 psa_key_derivation_step_t step,
6548 uint64_t value)
6549{
6550 psa_status_t status;
6551 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6552
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306553 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306554 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306555 (void) value;
6556 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306557 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306558 }
6559
6560 if (status != PSA_SUCCESS) {
6561 psa_key_derivation_abort(operation);
6562 }
6563 return status;
6564}
6565
Janos Follath51f4a0f2019-06-14 11:35:55 +01006566psa_status_t psa_key_derivation_input_bytes(
6567 psa_key_derivation_operation_t *operation,
6568 psa_key_derivation_step_t step,
6569 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006571{
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 return psa_key_derivation_input_internal(operation, step,
6573 PSA_KEY_TYPE_NONE,
6574 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006575}
6576
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306577psa_status_t psa_key_derivation_input_integer(
6578 psa_key_derivation_operation_t *operation,
6579 psa_key_derivation_step_t step,
6580 uint64_t value)
6581{
6582 return psa_key_derivation_input_integer_internal(operation, step, value);
6583}
6584
Janos Follath51f4a0f2019-06-14 11:35:55 +01006585psa_status_t psa_key_derivation_input_key(
6586 psa_key_derivation_operation_t *operation,
6587 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006589{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006590 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006591 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006592 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006593
Ronald Cron5c522922020-11-14 16:35:34 +01006594 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6596 if (status != PSA_SUCCESS) {
6597 psa_key_derivation_abort(operation);
6598 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006599 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006600
6601 /* Passing a key object as a SECRET input unlocks the permission
6602 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006604 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006606
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 status = psa_key_derivation_input_internal(operation,
6608 step, slot->attr.type,
6609 slot->key.data,
6610 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006611
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006613
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006615}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006616
6617
6618
6619/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006620/* Key agreement */
6621/****************************************************************/
6622
Gilles Peskine449bd832023-01-11 14:50:10 +01006623psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6624 const uint8_t *key_buffer,
6625 size_t key_buffer_size,
6626 psa_algorithm_t alg,
6627 const uint8_t *peer_key,
6628 size_t peer_key_length,
6629 uint8_t *shared_secret,
6630 size_t shared_secret_size,
6631 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006632{
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006634#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006635 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6637 key_buffer_size, alg,
6638 peer_key, peer_key_length,
6639 shared_secret,
6640 shared_secret_size,
6641 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006642#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006643
6644#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6645 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006646 return mbedtls_psa_key_agreement_ffdh(attributes,
6647 peer_key,
6648 peer_key_length,
6649 key_buffer,
6650 key_buffer_size,
6651 shared_secret,
6652 shared_secret_size,
6653 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006654#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6655
Gilles Peskine01d718c2018-09-18 12:01:02 +02006656 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006657 (void) attributes;
6658 (void) key_buffer;
6659 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006660 (void) peer_key;
6661 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006662 (void) shared_secret;
6663 (void) shared_secret_size;
6664 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006666 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006667}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006668
Aditya Deshpande17845b82022-10-13 17:21:01 +01006669/** Internal function for raw key agreement
6670 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006671 * to the driver's implementation if a driver is present.
6672 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006673 * (psa_key_agreement_raw_builtin).
6674 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006675static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6676 psa_key_slot_t *private_key,
6677 const uint8_t *peer_key,
6678 size_t peer_key_length,
6679 uint8_t *shared_secret,
6680 size_t shared_secret_size,
6681 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006682{
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6684 return PSA_ERROR_NOT_SUPPORTED;
6685 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006686
6687 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006689 };
6690
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 return psa_driver_wrapper_key_agreement(&attributes,
6692 private_key->key.data,
6693 private_key->key.bytes, alg,
6694 peer_key, peer_key_length,
6695 shared_secret,
6696 shared_secret_size,
6697 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006698}
6699
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006700/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006701 * to potentially free embedded data structures and wipe confidential data.
6702 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006703static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6704 psa_key_derivation_step_t step,
6705 psa_key_slot_t *private_key,
6706 const uint8_t *peer_key,
6707 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006708{
6709 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006710 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006711 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006713
6714 /* Step 1: run the secret agreement algorithm to generate the shared
6715 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 status = psa_key_agreement_raw_internal(ka_alg,
6717 private_key,
6718 peer_key, peer_key_length,
6719 shared_secret,
6720 sizeof(shared_secret),
6721 &shared_secret_length);
6722 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006723 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006725
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006726 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006727 * the shared secret. A shared secret is permitted wherever a key
6728 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 status = psa_key_derivation_input_internal(operation, step,
6730 PSA_KEY_TYPE_DERIVE,
6731 shared_secret,
6732 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006733exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6735 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006736}
6737
Gilles Peskine449bd832023-01-11 14:50:10 +01006738psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6739 psa_key_derivation_step_t step,
6740 mbedtls_svc_key_id_t private_key,
6741 const uint8_t *peer_key,
6742 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006743{
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 Peskine2f060a82018-12-04 17:12:32 +01006746 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006747
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6749 return PSA_ERROR_INVALID_ARGUMENT;
6750 }
Ronald Cron5c522922020-11-14 16:35:34 +01006751 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6753 if (status != PSA_SUCCESS) {
6754 return status;
6755 }
6756 status = psa_key_agreement_internal(operation, step,
6757 slot,
6758 peer_key, peer_key_length);
6759 if (status != PSA_SUCCESS) {
6760 psa_key_derivation_abort(operation);
6761 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006762 /* If a private key has been added as SECRET, we allow the derived
6763 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006765 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006767 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006768
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006770
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006772}
6773
Gilles Peskine449bd832023-01-11 14:50:10 +01006774psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6775 mbedtls_svc_key_id_t private_key,
6776 const uint8_t *peer_key,
6777 size_t peer_key_length,
6778 uint8_t *output,
6779 size_t output_size,
6780 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006781{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006782 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006783 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006784 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006785
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006787 status = PSA_ERROR_INVALID_ARGUMENT;
6788 goto exit;
6789 }
Ronald Cron5c522922020-11-14 16:35:34 +01006790 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6792 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006793 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006795
Gilles Peskine3e561302022-04-14 00:17:15 +02006796 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6797 * for the output size. The PSA specification only guarantees that this
6798 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6799 * but it might be nice to allow smaller buffers if the output fits.
6800 * At the time of writing this comment, with only ECDH implemented,
6801 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6802 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6803 * be exact for it as well. */
6804 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6806 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006807 status = PSA_ERROR_BUFFER_TOO_SMALL;
6808 goto exit;
6809 }
6810
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 status = psa_key_agreement_raw_internal(alg, slot,
6812 peer_key, peer_key_length,
6813 output, output_size,
6814 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006815
6816exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006818 /* If an error happens and is not handled properly, the output
6819 * may be used as a key to protect sensitive data. Arrange for such
6820 * a key to be random, which is likely to result in decryption or
6821 * verification errors. This is better than filling the buffer with
6822 * some constant data such as zeros, which would result in the data
6823 * being protected with a reproducible, easily knowable key.
6824 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006826 *output_length = output_size;
6827 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006828
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006830
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006832}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006833
6834
Gilles Peskine30524eb2020-11-13 17:02:26 +01006835
Gilles Peskine86a440b2018-11-12 18:39:40 +01006836/****************************************************************/
6837/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006838/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006839
Gilles Peskine30524eb2020-11-13 17:02:26 +01006840/** Initialize the PSA random generator.
6841 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006842static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006843{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006844#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006846#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6847
Gilles Peskine30524eb2020-11-13 17:02:26 +01006848 /* Set default configuration if
6849 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006850 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006851 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 }
6853 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006854 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006856
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006858#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6859 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6860 /* The PSA entropy injection feature depends on using NV seed as an entropy
6861 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 mbedtls_entropy_add_source(&rng->entropy,
6863 mbedtls_nv_seed_poll, NULL,
6864 MBEDTLS_ENTROPY_BLOCK_SIZE,
6865 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006866#endif
6867
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006869#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006870}
6871
6872/** Deinitialize the PSA random generator.
6873 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006874static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006875{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006876#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006878#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6880 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006881#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006882}
6883
6884/** Seed the PSA random generator.
6885 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006886static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006887{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006888#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6889 /* Do nothing: the external RNG seeds itself. */
6890 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006892#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006893 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6895 drbg_seed, sizeof(drbg_seed) - 1);
6896 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006897#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006898}
6899
Gilles Peskine449bd832023-01-11 14:50:10 +01006900psa_status_t psa_generate_random(uint8_t *output,
6901 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006902{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006903 GUARD_MODULE_INITIALIZED;
6904
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006905#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6906
6907 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6909 output, output_size,
6910 &output_length);
6911 if (status != PSA_SUCCESS) {
6912 return status;
6913 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006914 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006915 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006916 if (output_length != output_size) {
6917 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6918 }
6919 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006920
6921#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6922
Gilles Peskine449bd832023-01-11 14:50:10 +01006923 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006924 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6926 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6927 output_size);
6928 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6929 output, request_size);
6930 if (ret != 0) {
6931 return mbedtls_to_psa_error(ret);
6932 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006933 output_size -= request_size;
6934 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006935 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006937#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006938}
6939
Gilles Peskine8814fc42020-12-14 15:33:44 +01006940/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006941 *
6942 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6943 * `psa_generate_random(...)`. The state parameter is ignored since the
6944 * PSA API doesn't support passing an explicit state.
6945 *
6946 * In the non-external case, psa_generate_random() calls an
6947 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6948 * and semantics as mbedtls_psa_get_random(). As an optimization,
6949 * instead of doing this back-and-forth between the PSA API and the
6950 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6951 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006952 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006953#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6954int mbedtls_psa_get_random(void *p_rng,
6955 unsigned char *output,
6956 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006957{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006958 /* This function takes a pointer to the RNG state because that's what
6959 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6960 * its own state internally and doesn't let the caller access that state.
6961 * So we just ignore the state parameter, and in practice we'll pass
6962 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006963 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 psa_status_t status = psa_generate_random(output, output_size);
6965 if (status == PSA_SUCCESS) {
6966 return 0;
6967 } else {
6968 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6969 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006970}
6971#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6972
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006973#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006974#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006975
Gilles Peskine449bd832023-01-11 14:50:10 +01006976psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6977 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006978{
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 if (global_data.initialized) {
6980 return PSA_ERROR_NOT_PERMITTED;
6981 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006982
Gilles Peskine449bd832023-01-11 14:50:10 +01006983 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6984 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6985 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6986 return PSA_ERROR_INVALID_ARGUMENT;
6987 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006988
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006990}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006991#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006992
Ronald Cron3772afe2021-02-08 16:10:05 +01006993/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006994 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006995 * \param type The key type
6996 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006997 *
6998 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006999 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007000 * \retval #PSA_ERROR_INVALID_ARGUMENT
7001 * The size in bits of the key is not valid.
7002 * \retval #PSA_ERROR_NOT_SUPPORTED
7003 * The type and/or the size in bits of the key or the combination of
7004 * the two is not supported.
7005 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007006static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007008{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007010
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 if (key_type_is_raw_bytes(type)) {
7012 status = psa_validate_unstructured_key_bit_size(type, bits);
7013 if (status != PSA_SUCCESS) {
7014 return status;
7015 }
7016 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007017#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7019 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7020 return PSA_ERROR_NOT_SUPPORTED;
7021 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007022
Ronald Cron01b2aba2020-10-05 09:42:02 +02007023 /* Accept only byte-aligned keys, for the same reasons as
7024 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 if (bits % 8 != 0) {
7026 return PSA_ERROR_NOT_SUPPORTED;
7027 }
7028 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007029#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007030
Ronald Cron5c4d3862020-12-07 11:07:24 +01007031#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007032 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007033 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 return PSA_SUCCESS;
7035 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007036#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007037
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007038#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007039 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007040 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007041 return PSA_ERROR_NOT_SUPPORTED;
7042 }
7043 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007044#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007045 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007047 }
7048
Gilles Peskine449bd832023-01-11 14:50:10 +01007049 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007050}
7051
Ronald Cron55ed0592020-10-05 10:30:40 +02007052psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007053 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007055{
7056 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007057 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007058
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 if ((attributes->domain_parameters == NULL) &&
7060 (attributes->domain_parameters_size != 0)) {
7061 return PSA_ERROR_INVALID_ARGUMENT;
7062 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007063
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 if (key_type_is_raw_bytes(type)) {
7065 status = psa_generate_random(key_buffer, key_buffer_size);
7066 if (status != PSA_SUCCESS) {
7067 return status;
7068 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007069
David Brown12ca5032021-02-11 11:02:00 -07007070#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 if (type == PSA_KEY_TYPE_DES) {
7072 psa_des_set_key_parity(key_buffer, key_buffer_size);
7073 }
David Brown8107e312021-02-12 08:08:46 -07007074#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007076
Jaeden Amero424fa932021-05-14 08:34:32 +01007077#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7078 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7080 return mbedtls_psa_rsa_generate_key(attributes,
7081 key_buffer,
7082 key_buffer_size,
7083 key_buffer_length);
7084 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007085#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7086 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007087
John Durkop9814fa22020-11-04 12:28:15 -08007088#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7090 return mbedtls_psa_ecp_generate_key(attributes,
7091 key_buffer,
7092 key_buffer_size,
7093 key_buffer_length);
7094 } else
John Durkop9814fa22020-11-04 12:28:15 -08007095#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007096
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007097#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007098 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7099 return mbedtls_psa_ffdh_generate_key(attributes,
7100 key_buffer,
7101 key_buffer_size,
7102 key_buffer_length);
7103 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007104#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007105 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 (void) key_buffer_length;
7107 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007108 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007109
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007111}
Darryl Green0c6575a2018-11-07 16:05:30 +00007112
Gilles Peskine449bd832023-01-11 14:50:10 +01007113psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7114 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007115{
7116 psa_status_t status;
7117 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007118 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007119 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007120
Ronald Cron81709fc2020-11-14 12:10:32 +01007121 *key = MBEDTLS_SVC_KEY_ID_INIT;
7122
Gilles Peskine0f84d622019-09-12 19:03:13 +02007123 /* Reject any attempt to create a zero-length key so that we don't
7124 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 if (psa_get_key_bits(attributes) == 0) {
7126 return PSA_ERROR_INVALID_ARGUMENT;
7127 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007128
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007129 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7131 return PSA_ERROR_INVALID_ARGUMENT;
7132 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007133
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7135 &slot, &driver);
7136 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 }
Gilles Peskine11792082019-08-06 18:36:36 +02007139
Ronald Cron977c2472020-10-13 08:32:21 +02007140 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307141 * storage ( thus not in the case of generating a key in a secure element
7142 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7143 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 if (slot->key.data == NULL) {
7145 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7146 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007147 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 attributes->core.type, attributes->core.bits);
7149 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007150 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007152
7153 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 attributes->core.type,
7155 attributes->core.bits);
7156 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007157 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 attributes, &key_buffer_size);
7159 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 }
Ronald Cron977c2472020-10-13 08:32:21 +02007162 }
Ronald Cron977c2472020-10-13 08:32:21 +02007163
Gilles Peskine449bd832023-01-11 14:50:10 +01007164 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7165 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007166 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 }
Ronald Cron977c2472020-10-13 08:32:21 +02007168 }
7169
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 status = psa_driver_wrapper_generate_key(attributes,
7171 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007172
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 if (status != PSA_SUCCESS) {
7174 psa_remove_key_data_from_memory(slot);
7175 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007176
Gilles Peskine11792082019-08-06 18:36:36 +02007177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 if (status == PSA_SUCCESS) {
7179 status = psa_finish_key_creation(slot, driver, key);
7180 }
7181 if (status != PSA_SUCCESS) {
7182 psa_fail_key_creation(slot, driver);
7183 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007184
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007186}
7187
Gilles Peskinee59236f2018-01-27 23:32:46 +01007188/****************************************************************/
7189/* Module setup */
7190/****************************************************************/
7191
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007192#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007193psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 void (* entropy_init)(mbedtls_entropy_context *ctx),
7195 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007196{
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7198 return PSA_ERROR_BAD_STATE;
7199 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007200 global_data.rng.entropy_init = entropy_init;
7201 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007203}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007204#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007205
Gilles Peskine449bd832023-01-11 14:50:10 +01007206void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007207{
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 psa_wipe_all_key_slots();
7209 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7210 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007211 }
7212 /* Wipe all remaining data, including configuration.
7213 * In particular, this sets all state indicator to the value
7214 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007216
7217 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007219}
7220
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007221#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7222/** Recover a transaction that was interrupted by a power failure.
7223 *
7224 * This function is called during initialization, before psa_crypto_init()
7225 * returns. If this function returns a failure status, the initialization
7226 * fails.
7227 */
7228static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007230{
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007232 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7233 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 /* TODO - fall through to the failure case until this
7235 * is implemented.
7236 * https://github.com/ARMmbed/mbed-crypto/issues/218
7237 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007238 default:
7239 /* We found an unsupported transaction in the storage.
7240 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007241 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007242 }
7243}
7244#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7245
Gilles Peskine449bd832023-01-11 14:50:10 +01007246psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007247{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007248 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007249
Gilles Peskinec6b69072018-11-20 21:42:52 +01007250 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 if (global_data.initialized != 0) {
7252 return PSA_SUCCESS;
7253 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007254
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007255 /* Init drivers */
7256 status = psa_driver_wrapper_init();
7257 if (status != PSA_SUCCESS) {
7258 goto exit;
7259 }
7260 global_data.drivers_initialized = 1;
7261
Gilles Peskine30524eb2020-11-13 17:02:26 +01007262 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007264 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 status = mbedtls_psa_random_seed(&global_data.rng);
7266 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007267 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007269 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007270
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 status = psa_initialize_key_slots();
7272 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007273 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007275
Gilles Peskine4b734222019-07-24 15:56:31 +02007276#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 status = psa_crypto_load_transaction();
7278 if (status == PSA_SUCCESS) {
7279 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7280 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007281 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007282 }
7283 status = psa_crypto_stop_transaction();
7284 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007285 /* There's no transaction to complete. It's all good. */
7286 status = PSA_SUCCESS;
7287 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007288#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007289
Gilles Peskinec6b69072018-11-20 21:42:52 +01007290 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007291 global_data.initialized = 1;
7292
7293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 if (status != PSA_SUCCESS) {
7295 mbedtls_psa_crypto_free();
7296 }
7297 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007298}
7299
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007300psa_status_t psa_crypto_driver_pake_get_password_len(
7301 const psa_crypto_driver_pake_inputs_t *inputs,
7302 size_t *password_len)
7303{
7304 if (inputs->password_len == 0) {
7305 return PSA_ERROR_BAD_STATE;
7306 }
7307
7308 *password_len = inputs->password_len;
7309
7310 return PSA_SUCCESS;
7311}
7312
7313psa_status_t psa_crypto_driver_pake_get_password(
7314 const psa_crypto_driver_pake_inputs_t *inputs,
7315 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7316{
7317 if (inputs->password_len == 0) {
7318 return PSA_ERROR_BAD_STATE;
7319 }
7320
7321 if (buffer_size < inputs->password_len) {
7322 return PSA_ERROR_BUFFER_TOO_SMALL;
7323 }
7324
7325 memcpy(buffer, inputs->password, inputs->password_len);
7326 *buffer_length = inputs->password_len;
7327
7328 return PSA_SUCCESS;
7329}
7330
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007331psa_status_t psa_crypto_driver_pake_get_user_len(
7332 const psa_crypto_driver_pake_inputs_t *inputs,
7333 size_t *user_len)
7334{
7335 if (inputs->user_len == 0) {
7336 return PSA_ERROR_BAD_STATE;
7337 }
7338
7339 *user_len = inputs->user_len;
7340
7341 return PSA_SUCCESS;
7342}
7343
7344psa_status_t psa_crypto_driver_pake_get_user(
7345 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007346 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007347{
7348 if (inputs->user_len == 0) {
7349 return PSA_ERROR_BAD_STATE;
7350 }
7351
Przemek Stekielc0e62502023-03-14 11:49:36 +01007352 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007353 return PSA_ERROR_BUFFER_TOO_SMALL;
7354 }
7355
Przemek Stekielc0e62502023-03-14 11:49:36 +01007356 memcpy(user_id, inputs->user, inputs->user_len);
7357 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007358
7359 return PSA_SUCCESS;
7360}
7361
7362psa_status_t psa_crypto_driver_pake_get_peer_len(
7363 const psa_crypto_driver_pake_inputs_t *inputs,
7364 size_t *peer_len)
7365{
7366 if (inputs->peer_len == 0) {
7367 return PSA_ERROR_BAD_STATE;
7368 }
7369
7370 *peer_len = inputs->peer_len;
7371
7372 return PSA_SUCCESS;
7373}
7374
7375psa_status_t psa_crypto_driver_pake_get_peer(
7376 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007377 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007378{
7379 if (inputs->peer_len == 0) {
7380 return PSA_ERROR_BAD_STATE;
7381 }
7382
Przemek Stekielc0e62502023-03-14 11:49:36 +01007383 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007384 return PSA_ERROR_BUFFER_TOO_SMALL;
7385 }
7386
Przemek Stekielc0e62502023-03-14 11:49:36 +01007387 memcpy(peer_id, inputs->peer, inputs->peer_len);
7388 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007389
7390 return PSA_SUCCESS;
7391}
7392
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007393psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7394 const psa_crypto_driver_pake_inputs_t *inputs,
7395 psa_pake_cipher_suite_t *cipher_suite)
7396{
7397 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7398 return PSA_ERROR_BAD_STATE;
7399 }
7400
7401 *cipher_suite = inputs->cipher_suite;
7402
7403 return PSA_SUCCESS;
7404}
7405
Neil Armstronga7d08c32022-06-01 18:21:20 +02007406psa_status_t psa_pake_setup(
7407 psa_pake_operation_t *operation,
7408 const psa_pake_cipher_suite_t *cipher_suite)
7409{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007410 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007411
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007412 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007413 status = PSA_ERROR_BAD_STATE;
7414 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007415 }
7416
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007417 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007418 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007419 status = PSA_ERROR_INVALID_ARGUMENT;
7420 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007421 }
7422
Przemek Stekiel51eac532022-12-07 11:04:51 +01007423 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7424
Przemek Stekiele12ed362022-12-21 12:54:46 +01007425 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007426 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7427 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007428 operation->data.inputs.cipher_suite = *cipher_suite;
7429
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007430#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007431 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007432 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007433 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007434
Przemek Stekiele12ed362022-12-21 12:54:46 +01007435 computation_stage->state = PSA_PAKE_STATE_SETUP;
7436 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7437 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7438 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007439 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007440#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007441 {
7442 status = PSA_ERROR_NOT_SUPPORTED;
7443 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007444 }
7445
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007446 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7447
Przemek Stekiel51eac532022-12-07 11:04:51 +01007448 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007449exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007450 psa_pake_abort(operation);
7451 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007452}
7453
7454psa_status_t psa_pake_set_password_key(
7455 psa_pake_operation_t *operation,
7456 mbedtls_svc_key_id_t password)
7457{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007458 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007459 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7460 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007461
Przemek Stekiel51eac532022-12-07 11:04:51 +01007462 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007463 status = PSA_ERROR_BAD_STATE;
7464 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007465 }
7466
Przemek Stekiel6c764412022-11-22 14:05:12 +01007467 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7468 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007469 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007470 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007471 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007472 }
7473
Przemek Stekiel6c764412022-11-22 14:05:12 +01007474 psa_key_attributes_t attributes = {
7475 .core = slot->attr
7476 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007477
Przemek Stekiel51eac532022-12-07 11:04:51 +01007478 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007479
Przemek Stekiel51eac532022-12-07 11:04:51 +01007480 if (type != PSA_KEY_TYPE_PASSWORD &&
7481 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7482 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007483 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007484 }
7485
Przemek Stekiel51eac532022-12-07 11:04:51 +01007486 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7487 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007488 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007489 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007490 }
7491
7492 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7493 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007494 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007495exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007496 if (status != PSA_SUCCESS) {
7497 psa_pake_abort(operation);
7498 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007499 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007500 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007501}
7502
7503psa_status_t psa_pake_set_user(
7504 psa_pake_operation_t *operation,
7505 const uint8_t *user_id,
7506 size_t user_id_len)
7507{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007508 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007509
7510 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007511 status = PSA_ERROR_BAD_STATE;
7512 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007513 }
7514
Przemek Stekiel51eac532022-12-07 11:04:51 +01007515 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007516 status = PSA_ERROR_INVALID_ARGUMENT;
7517 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007518 }
7519
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007520 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007521 status = PSA_ERROR_BAD_STATE;
7522 goto exit;
7523 }
7524
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007525 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7526 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007527 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7528 goto exit;
7529 }
7530
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007531 memcpy(operation->data.inputs.user, user_id, user_id_len);
7532 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007533
7534 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007535exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007536 psa_pake_abort(operation);
7537 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007538}
7539
7540psa_status_t psa_pake_set_peer(
7541 psa_pake_operation_t *operation,
7542 const uint8_t *peer_id,
7543 size_t peer_id_len)
7544{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007545 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007546
7547 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007548 status = PSA_ERROR_BAD_STATE;
7549 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007550 }
7551
Przemek Stekiel51eac532022-12-07 11:04:51 +01007552 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007553 status = PSA_ERROR_INVALID_ARGUMENT;
7554 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007555 }
7556
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007557 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007558 status = PSA_ERROR_BAD_STATE;
7559 goto exit;
7560 }
7561
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007562 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7563 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007564 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7565 goto exit;
7566 }
7567
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007568 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7569 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007570
7571 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007572exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007573 psa_pake_abort(operation);
7574 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007575}
7576
7577psa_status_t psa_pake_set_role(
7578 psa_pake_operation_t *operation,
7579 psa_pake_role_t role)
7580{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007581 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007582
Przemek Stekiel51eac532022-12-07 11:04:51 +01007583 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007584 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007585 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007586 }
7587
Przemek Stekiel09104b82023-03-06 14:11:51 +01007588 switch (operation->alg) {
7589#if defined(PSA_WANT_ALG_JPAKE)
7590 case PSA_ALG_JPAKE:
7591 if (role == PSA_PAKE_ROLE_NONE) {
7592 return PSA_SUCCESS;
7593 }
7594 status = PSA_ERROR_INVALID_ARGUMENT;
7595 break;
7596#endif
7597 default:
7598 (void) role;
7599 status = PSA_ERROR_NOT_SUPPORTED;
7600 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007601 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007602exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007603 psa_pake_abort(operation);
7604 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007605}
7606
Przemek Stekielb09c4872023-01-17 12:05:38 +01007607/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007608#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007609static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007610 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007611{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007612 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007613 case PSA_PAKE_OUTPUT_X1_X2:
7614 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007615 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007616 case PSA_PAKE_X1_STEP_KEY_SHARE:
7617 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007618 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7619 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007620 case PSA_PAKE_X1_STEP_ZK_PROOF:
7621 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007622 case PSA_PAKE_X2_STEP_KEY_SHARE:
7623 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007624 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7625 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007626 case PSA_PAKE_X2_STEP_ZK_PROOF:
7627 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007628 default:
7629 return PSA_JPAKE_STEP_INVALID;
7630 }
7631 break;
7632 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007633 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007634 case PSA_PAKE_X1_STEP_KEY_SHARE:
7635 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007636 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7637 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007638 case PSA_PAKE_X1_STEP_ZK_PROOF:
7639 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007640 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007641 return PSA_JPAKE_STEP_INVALID;
7642 }
7643 break;
7644 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007645 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007646 case PSA_PAKE_X1_STEP_KEY_SHARE:
7647 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007648 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7649 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007650 case PSA_PAKE_X1_STEP_ZK_PROOF:
7651 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007652 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007653 return PSA_JPAKE_STEP_INVALID;
7654 }
7655 break;
7656 default:
7657 return PSA_JPAKE_STEP_INVALID;
7658 }
7659 return PSA_JPAKE_STEP_INVALID;
7660}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007661#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007662
Przemek Stekiel2797d372022-12-22 11:19:22 +01007663static psa_status_t psa_pake_complete_inputs(
7664 psa_pake_operation_t *operation)
7665{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007667 /* Create copy of the inputs on stack as inputs share memory
7668 with the driver context which will be setup by the driver. */
7669 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007670
Przemek Stekielfde11282023-03-13 16:06:09 +01007671 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007672 return PSA_ERROR_BAD_STATE;
7673 }
7674
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007675 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007676 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7677 return PSA_ERROR_BAD_STATE;
7678 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007679 }
7680
Przemek Stekiel18620a32023-01-17 16:34:52 +01007681 /* Clear driver context */
7682 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7683
7684 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007685
7686 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007687 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7688 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007689
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007690 /* User and peer are translated to role. */
7691 mbedtls_free(inputs.user);
7692 mbedtls_free(inputs.peer);
7693
Przemek Stekiel2797d372022-12-22 11:19:22 +01007694 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007695#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007696 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007697 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007698 psa_jpake_computation_stage_t *computation_stage =
7699 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007700 computation_stage->state = PSA_PAKE_STATE_READY;
7701 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7702 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7703 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007704 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007705#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007706 {
7707 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007708 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007709 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007710 return status;
7711}
7712
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007713#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007714static psa_status_t psa_jpake_output_prologue(
7715 psa_pake_operation_t *operation,
7716 psa_pake_step_t step)
7717{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007718 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7719 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7720 step != PSA_PAKE_STEP_ZK_PROOF) {
7721 return PSA_ERROR_INVALID_ARGUMENT;
7722 }
7723
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007724 psa_jpake_computation_stage_t *computation_stage =
7725 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007726
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007727 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7728 return PSA_ERROR_BAD_STATE;
7729 }
7730
7731 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7732 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7733 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7734 return PSA_ERROR_BAD_STATE;
7735 }
7736
7737 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7738 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007739 return PSA_ERROR_BAD_STATE;
7740 }
7741
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007742 switch (computation_stage->output_step) {
7743 case PSA_PAKE_STEP_X1_X2:
7744 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007745 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007746 case PSA_PAKE_STEP_X2S:
7747 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007748 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007749 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007750 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007751 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007752
7753 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7754 }
7755
7756 /* Check if step matches current sequence */
7757 switch (computation_stage->sequence) {
7758 case PSA_PAKE_X1_STEP_KEY_SHARE:
7759 case PSA_PAKE_X2_STEP_KEY_SHARE:
7760 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7761 return PSA_ERROR_BAD_STATE;
7762 }
7763 break;
7764
7765 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7766 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7767 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7768 return PSA_ERROR_BAD_STATE;
7769 }
7770 break;
7771
7772 case PSA_PAKE_X1_STEP_ZK_PROOF:
7773 case PSA_PAKE_X2_STEP_ZK_PROOF:
7774 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7775 return PSA_ERROR_BAD_STATE;
7776 }
7777 break;
7778
7779 default:
7780 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007781 }
7782
7783 return PSA_SUCCESS;
7784}
7785
7786static psa_status_t psa_jpake_output_epilogue(
7787 psa_pake_operation_t *operation)
7788{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007789 psa_jpake_computation_stage_t *computation_stage =
7790 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007791
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007792 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007793 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007794 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007795 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007796 computation_stage->state = PSA_PAKE_STATE_READY;
7797 computation_stage->output_step++;
7798 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7799 } else {
7800 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007801 }
7802
7803 return PSA_SUCCESS;
7804}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007805#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007806
Neil Armstronga7d08c32022-06-01 18:21:20 +02007807psa_status_t psa_pake_output(
7808 psa_pake_operation_t *operation,
7809 psa_pake_step_t step,
7810 uint8_t *output,
7811 size_t output_size,
7812 size_t *output_length)
7813{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007814 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007815 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007816 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007817
7818 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007819 status = psa_pake_complete_inputs(operation);
7820 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007821 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007822 }
7823 }
7824
7825 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007826 status = PSA_ERROR_BAD_STATE;
7827 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007828 }
7829
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007830 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007831 status = PSA_ERROR_INVALID_ARGUMENT;
7832 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007833 }
7834
Przemek Stekiele12ed362022-12-21 12:54:46 +01007835 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007836#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007837 case PSA_ALG_JPAKE:
7838 status = psa_jpake_output_prologue(operation, step);
7839 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007840 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007841 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007842 driver_step = convert_jpake_computation_stage_to_driver_step(
7843 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007844 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007845#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007846 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007847 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007848 status = PSA_ERROR_NOT_SUPPORTED;
7849 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007850 }
7851
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007852 status = psa_driver_wrapper_pake_output(operation, driver_step,
7853 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007854
7855 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007856 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007857 }
7858
7859 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007860#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007861 case PSA_ALG_JPAKE:
7862 status = psa_jpake_output_epilogue(operation);
7863 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007864 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007865 }
7866 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007867#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007868 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007869 status = PSA_ERROR_NOT_SUPPORTED;
7870 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007871 }
7872
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007873 return PSA_SUCCESS;
7874exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007875 psa_pake_abort(operation);
7876 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007877}
7878
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007879#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007880static psa_status_t psa_jpake_input_prologue(
7881 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007882 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007883{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007884 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7885 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7886 step != PSA_PAKE_STEP_ZK_PROOF) {
7887 return PSA_ERROR_INVALID_ARGUMENT;
7888 }
7889
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007890 psa_jpake_computation_stage_t *computation_stage =
7891 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007892
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007893 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7894 return PSA_ERROR_BAD_STATE;
7895 }
7896
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007897 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7898 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7899 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7900 return PSA_ERROR_BAD_STATE;
7901 }
7902
7903 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7904 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007905 return PSA_ERROR_BAD_STATE;
7906 }
7907
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007908 switch (computation_stage->input_step) {
7909 case PSA_PAKE_STEP_X1_X2:
7910 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007911 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007912 case PSA_PAKE_STEP_X2S:
7913 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007914 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007915 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007916 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007917 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007918
7919 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7920 }
7921
7922 /* Check if step matches current sequence */
7923 switch (computation_stage->sequence) {
7924 case PSA_PAKE_X1_STEP_KEY_SHARE:
7925 case PSA_PAKE_X2_STEP_KEY_SHARE:
7926 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7927 return PSA_ERROR_BAD_STATE;
7928 }
7929 break;
7930
7931 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7932 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7933 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7934 return PSA_ERROR_BAD_STATE;
7935 }
7936 break;
7937
7938 case PSA_PAKE_X1_STEP_ZK_PROOF:
7939 case PSA_PAKE_X2_STEP_ZK_PROOF:
7940 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7941 return PSA_ERROR_BAD_STATE;
7942 }
7943 break;
7944
7945 default:
7946 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007947 }
7948
7949 return PSA_SUCCESS;
7950}
7951
Przemek Stekiele12ed362022-12-21 12:54:46 +01007952static psa_status_t psa_jpake_input_epilogue(
7953 psa_pake_operation_t *operation)
7954{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007955 psa_jpake_computation_stage_t *computation_stage =
7956 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007957
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007958 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007959 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007960 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007961 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007962 computation_stage->state = PSA_PAKE_STATE_READY;
7963 computation_stage->input_step++;
7964 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7965 } else {
7966 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007967 }
7968
7969 return PSA_SUCCESS;
7970}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007971#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007972
Neil Armstronga7d08c32022-06-01 18:21:20 +02007973psa_status_t psa_pake_input(
7974 psa_pake_operation_t *operation,
7975 psa_pake_step_t step,
7976 const uint8_t *input,
7977 size_t input_length)
7978{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007979 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007980 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007981 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
7982 operation->primitive,
7983 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007984
7985 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007986 status = psa_pake_complete_inputs(operation);
7987 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007988 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007989 }
7990 }
7991
7992 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007993 status = PSA_ERROR_BAD_STATE;
7994 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007995 }
7996
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007997 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007998 status = PSA_ERROR_INVALID_ARGUMENT;
7999 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008000 }
8001
Przemek Stekiele12ed362022-12-21 12:54:46 +01008002 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008003#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008004 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008005 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008006 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008007 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008008 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008009 driver_step = convert_jpake_computation_stage_to_driver_step(
8010 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008011 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008012#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008013 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008014 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008015 status = PSA_ERROR_NOT_SUPPORTED;
8016 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008017 }
8018
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008019 status = psa_driver_wrapper_pake_input(operation, driver_step,
8020 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008021
8022 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008023 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008024 }
8025
8026 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008027#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008028 case PSA_ALG_JPAKE:
8029 status = psa_jpake_input_epilogue(operation);
8030 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008031 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008032 }
8033 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008034#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008035 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008036 status = PSA_ERROR_NOT_SUPPORTED;
8037 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008038 }
8039
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008040 return PSA_SUCCESS;
8041exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008042 psa_pake_abort(operation);
8043 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008044}
8045
8046psa_status_t psa_pake_get_implicit_key(
8047 psa_pake_operation_t *operation,
8048 psa_key_derivation_operation_t *output)
8049{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008050 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008051 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008052 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008053 size_t shared_key_len = 0;
8054
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008055 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008056 status = PSA_ERROR_BAD_STATE;
8057 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008058 }
8059
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008060#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008061 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008062 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008063 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008064 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8065 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008066 status = PSA_ERROR_BAD_STATE;
8067 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008068 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008069 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008070#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008071 {
8072 status = PSA_ERROR_NOT_SUPPORTED;
8073 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008074 }
8075
Przemek Stekiel0c781802022-11-29 14:53:13 +01008076 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8077 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008078 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008079 &shared_key_len);
8080
8081 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008082 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008083 }
8084
8085 status = psa_key_derivation_input_bytes(output,
8086 PSA_KEY_DERIVATION_INPUT_SECRET,
8087 shared_key,
8088 shared_key_len);
8089
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008090 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008091exit:
8092 abort_status = psa_pake_abort(operation);
8093 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008094}
8095
8096psa_status_t psa_pake_abort(
8097 psa_pake_operation_t *operation)
8098{
Przemek Stekield69dca92023-01-31 19:59:20 +01008099 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008100
Przemek Stekield69dca92023-01-31 19:59:20 +01008101 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008102 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008103 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008104
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008105 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8106 if (operation->data.inputs.password != NULL) {
8107 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008108 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008109 mbedtls_free(operation->data.inputs.password);
8110 }
8111 if (operation->data.inputs.user != NULL) {
8112 mbedtls_free(operation->data.inputs.user);
8113 }
8114 if (operation->data.inputs.peer != NULL) {
8115 mbedtls_free(operation->data.inputs.peer);
8116 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008117 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008118 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008119
Przemek Stekield69dca92023-01-31 19:59:20 +01008120 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008121}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008122
Gilles Peskinee59236f2018-01-27 23:32:46 +01008123#endif /* MBEDTLS_PSA_CRYPTO_C */