blob: 7b6f05be311d7cf537f7073f9b303a50f16c3c7c [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)) {
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200643#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
644 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100645 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200646 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100647 return PSA_ERROR_INVALID_ARGUMENT;
648 }
649
650 /* Copy the key material. */
651 memcpy(key_buffer, data, data_length);
652 *key_buffer_length = data_length;
653 *bits = PSA_BYTES_TO_BITS(data_length);
654 (void) key_buffer_size;
655
656 return PSA_SUCCESS;
657 }
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200658#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
659 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
John Durkop9814fa22020-11-04 12:28:15 -0800660#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
662 if (PSA_KEY_TYPE_IS_ECC(type)) {
663 return mbedtls_psa_ecp_import_key(attributes,
664 data, data_length,
665 key_buffer, key_buffer_size,
666 key_buffer_length,
667 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100668 }
John Durkop9814fa22020-11-04 12:28:15 -0800669#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
670 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700671#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
673 if (PSA_KEY_TYPE_IS_RSA(type)) {
674 return mbedtls_psa_rsa_import_key(attributes,
675 data, data_length,
676 key_buffer, key_buffer_size,
677 key_buffer_length,
678 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200679 }
John Durkop9814fa22020-11-04 12:28:15 -0800680#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
681 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100682 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100685}
686
Gilles Peskinef603c712019-01-19 13:40:11 +0100687/** Calculate the intersection of two algorithm usage policies.
688 *
689 * Return 0 (which allows no operation) on incompatibility.
690 */
691static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100692 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100693 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100695{
Gilles Peskine549ea862019-05-22 11:45:59 +0200696 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (alg1 == alg2) {
698 return alg1;
699 }
Steven Cooreman03488022021-02-18 12:07:20 +0100700 /* If the policies are from the same hash-and-sign family, check
701 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
703 PSA_ALG_IS_SIGN_HASH(alg2) &&
704 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
705 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
706 return alg2;
707 }
708 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
709 return alg1;
710 }
Steven Cooreman03488022021-02-18 12:07:20 +0100711 }
712 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100713 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100714 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
716 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
717 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
718 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
719 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100720 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100721
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100722 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
724 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
725 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
726 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100727 }
728 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
730 (alg1_len <= alg2_len)) {
731 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100732 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
734 (alg2_len <= alg1_len)) {
735 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100736 }
737 }
738 /* If the policies are from the same MAC family, check whether one
739 * of them is a minimum-MAC-length policy. Calculate the most
740 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
742 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
743 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100744 /* Validate the combination of key type and algorithm. Since the base
745 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
747 return 0;
748 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100749
Steven Cooreman31a876d2021-03-03 20:47:40 +0100750 /* Get the (exact or at-least) output lengths for both sides of the
751 * requested intersection. None of the currently supported algorithms
752 * have an output length dependent on the actual key size, so setting it
753 * to a bogus value of 0 is currently OK.
754 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100755 * Note that for at-least-this-length wildcard algorithms, the output
756 * length is set to the shortest allowed length, which allows us to
757 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
759 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100760 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100761
Steven Cooremana1d83222021-02-25 10:20:29 +0100762 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
764 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
765 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100766 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100767
768 /* If only one is an at-least-this-length policy, the intersection would
769 * be the other (fixed-length) policy as long as said fixed length is
770 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
772 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100773 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
775 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100776 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100777
Steven Cooremancd640932021-03-08 12:00:27 +0100778 /* If none of them are wildcards, check whether they define the same tag
779 * length. This is still possible here when one is default-length and
780 * the other specific-length. Ensure to always return the
781 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (alg1_len == alg2_len) {
783 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
784 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100785 }
786 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100788}
789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790static int psa_key_algorithm_permits(psa_key_type_t key_type,
791 psa_algorithm_t policy_alg,
792 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200793{
Gilles Peskine549ea862019-05-22 11:45:59 +0200794 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (requested_alg == policy_alg) {
796 return 1;
797 }
Steven Cooreman03488022021-02-18 12:07:20 +0100798 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
799 * and requested_alg is the same hash-and-sign family with any hash,
800 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
802 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
803 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
804 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100805 }
806 /* If policy_alg is a wildcard AEAD algorithm of the same base as
807 * the requested algorithm, check the requested tag length to be
808 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (PSA_ALG_IS_AEAD(policy_alg) &&
810 PSA_ALG_IS_AEAD(requested_alg) &&
811 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
812 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
813 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
814 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
815 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100816 }
Steven Cooremancd640932021-03-08 12:00:27 +0100817 /* If policy_alg is a MAC algorithm of the same base as the requested
818 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (PSA_ALG_IS_MAC(policy_alg) &&
820 PSA_ALG_IS_MAC(requested_alg) &&
821 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
822 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100823 /* Validate the combination of key type and algorithm. Since the policy
824 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
826 return 0;
827 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100828
Steven Cooreman31a876d2021-03-03 20:47:40 +0100829 /* Get both the requested output length for the algorithm which is to be
830 * verified, and the default output length for the base algorithm.
831 * Note that none of the currently supported algorithms have an output
832 * length dependent on actual key size, so setting it to a bogus value
833 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100834 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100836 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 key_type, 0,
838 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100839
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100840 /* If the policy is default-length, only allow an algorithm with
841 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
843 return requested_output_length == default_output_length;
844 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100845
846 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100847 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
849 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
850 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100851 }
852
Steven Cooremancd640932021-03-08 12:00:27 +0100853 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
854 * check for the requested MAC length to be equal to or longer than the
855 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
857 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
858 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100859 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200860 }
Steven Cooremance48e852020-10-05 16:02:45 +0200861 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200862 * a key derivation with that key agreement should also be allowed. This
863 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
865 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
866 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
867 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200868 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100869 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200871}
872
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100873/** Test whether a policy permits an algorithm.
874 *
875 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100876 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100877 * \note This function requires providing the key type for which the policy is
878 * being validated, since some algorithm policy definitions (e.g. MAC)
879 * have different properties depending on what kind of cipher it is
880 * combined with.
881 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100882 * \retval PSA_SUCCESS When \p alg is a specific algorithm
883 * allowed by the \p policy.
884 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
885 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
886 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100887 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
889 psa_key_type_t key_type,
890 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100891{
Steven Cooremand788fab2021-02-25 11:29:17 +0100892 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (alg == 0) {
894 return PSA_ERROR_INVALID_ARGUMENT;
895 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100896
Steven Cooreman7e39f052021-02-23 14:18:32 +0100897 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if (PSA_ALG_IS_WILDCARD(alg)) {
899 return PSA_ERROR_INVALID_ARGUMENT;
900 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
903 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
904 return PSA_SUCCESS;
905 } else {
906 return PSA_ERROR_NOT_PERMITTED;
907 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100908}
909
Gilles Peskinef603c712019-01-19 13:40:11 +0100910/** Restrict a key policy based on a constraint.
911 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100912 * \note This function requires providing the key type for which the policy is
913 * being restricted, since some algorithm policy definitions (e.g. MAC)
914 * have different properties depending on what kind of cipher it is
915 * combined with.
916 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100917 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100918 * \param[in,out] policy The policy to restrict.
919 * \param[in] constraint The policy constraint to apply.
920 *
921 * \retval #PSA_SUCCESS
922 * \c *policy contains the intersection of the original value of
923 * \c *policy and \c *constraint.
924 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100925 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100926 * \c *policy is unchanged.
927 */
928static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100929 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100930 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100932{
933 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 psa_key_policy_algorithm_intersection(key_type, policy->alg,
935 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200936 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
938 constraint->alg2);
939 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
940 return PSA_ERROR_INVALID_ARGUMENT;
941 }
942 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
943 return PSA_ERROR_INVALID_ARGUMENT;
944 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100945 policy->usage &= constraint->usage;
946 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200947 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100949}
950
Przemek Stekiel061f6942022-11-30 10:51:35 +0100951/** Get the description of a key given its identifier and policy constraints
952 * and lock it.
953 *
954 * The key must have allow all the usage flags set in \p usage. If \p alg is
955 * nonzero, the key must allow operations with this algorithm. If \p alg is
956 * zero, the algorithm is not checked.
957 *
958 * In case of a persistent key, the function loads the description of the key
959 * into a key slot if not already done.
960 *
961 * On success, the returned key slot is locked. It is the responsibility of
962 * the caller to unlock the key slot when it does not access it anymore.
963 */
964static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100965 mbedtls_svc_key_id_t key,
966 psa_key_slot_t **p_slot,
967 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100969{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200970 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100971 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 status = psa_get_and_lock_key_slot(key, p_slot);
974 if (status != PSA_SUCCESS) {
975 return status;
976 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200977 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100978
979 /* Enforce that usage policy for the key slot contains all the flags
980 * required by the usage parameter. There is one exception: public
981 * keys can always be exported, so we treat public key objects as
982 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100984 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100988 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200989 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100990 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100991
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800992 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 if (alg != 0) {
994 status = psa_key_policy_permits(&slot->attr.policy,
995 slot->attr.type,
996 alg);
997 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100998 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001000 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001003
1004error:
1005 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001009}
Darryl Green940d72c2018-07-13 13:18:51 +01001010
Ronald Cron5c522922020-11-14 16:35:34 +01001011/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001012 *
1013 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001014 * available, as opposed to a key in a secure element and/or to be used
1015 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001016 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001017 * This is a temporary function that may be used instead of
1018 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1019 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001020 *
Ronald Cron5c522922020-11-14 16:35:34 +01001021 * On success, the returned key slot is locked. It is the responsibility of the
1022 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001023 */
Ronald Cron5c522922020-11-14 16:35:34 +01001024static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1025 mbedtls_svc_key_id_t key,
1026 psa_key_slot_t **p_slot,
1027 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001029{
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1031 usage, alg);
1032 if (status != PSA_SUCCESS) {
1033 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001034 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1037 psa_unlock_key_slot(*p_slot);
1038 *p_slot = NULL;
1039 return PSA_ERROR_NOT_SUPPORTED;
1040 }
1041
1042 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001043}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001046{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001047 /* Data pointer will always be either a valid pointer or NULL in an
1048 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if (slot->key.data != NULL) {
1050 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1051 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001054 slot->key.data = NULL;
1055 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001058}
1059
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001060/** Completely wipe a slot in memory, including its policy.
1061 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001063{
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 /*
1067 * As the return error code may not be handled in case of multiple errors,
1068 * do our best to report an unexpected lock counter. Assert with
1069 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1070 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1071 * function is called as part of the execution of a test suite, the
1072 * execution of the test suite is stopped in error if the assertion fails.
1073 */
1074 if (slot->lock_count != 1) {
1075 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001076 status = PSA_ERROR_CORRUPTION_DETECTED;
1077 }
1078
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001079 /* Multipart operations may still be using the key. This is safe
1080 * because all multipart operation objects are independent from
1081 * the key slot: if they need to access the key after the setup
1082 * phase, they have a copy of the key. Note that this means that
1083 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001084 /* At this point, key material and other type-specific content has
1085 * been wiped. Clear remaining metadata. We can call memset and not
1086 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 memset(slot, 0, sizeof(*slot));
1088 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001089}
1090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001092{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001093 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001094 psa_status_t status; /* status of the last operation */
1095 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001096#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1097 psa_se_drv_table_entry_t *driver;
1098#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if (mbedtls_svc_key_id_is_null(key)) {
1101 return PSA_SUCCESS;
1102 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001103
Ronald Cronf2911112020-10-29 17:51:10 +01001104 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001105 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001106 * key, this will load the key description from persistent memory if not
1107 * done yet. We cannot avoid this loading as without it we don't know if
1108 * the key is operated by an SE or not and this information is needed by
1109 * the current implementation.
1110 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 status = psa_get_and_lock_key_slot(key, &slot);
1112 if (status != PSA_SUCCESS) {
1113 return status;
1114 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001115
Ronald Cronf2911112020-10-29 17:51:10 +01001116 /*
1117 * If the key slot containing the key description is under access by the
1118 * library (apart from the present access), the key cannot be destroyed
1119 * yet. For the time being, just return in error. Eventually (to be
1120 * implemented), the key should be destroyed when all accesses have
1121 * stopped.
1122 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (slot->lock_count > 1) {
1124 psa_unlock_key_slot(slot);
1125 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001126 }
1127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001129 /* Refuse the destruction of a read-only key (which may or may not work
1130 * if we attempt it, depending on whether the key is merely read-only
1131 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001132 * Just do the best we can, which is to wipe the copy in memory
1133 * (done in this function's cleanup code). */
1134 overall_status = PSA_ERROR_NOT_PERMITTED;
1135 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001136 }
1137
Gilles Peskine354f7672019-07-12 23:46:38 +02001138#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1140 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001141 /* For a key in a secure element, we need to do three things:
1142 * remove the key file in internal storage, destroy the
1143 * key inside the secure element, and update the driver's
1144 * persistent data. Start a transaction that will encompass these
1145 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001147 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001149 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 status = psa_crypto_save_transaction();
1151 if (status != PSA_SUCCESS) {
1152 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001153 /* We should still try to destroy the key in the secure
1154 * element and the key metadata in storage. This is especially
1155 * important if the error is that the storage is full.
1156 * But how to do it exactly without risking an inconsistent
1157 * state after a reset?
1158 * https://github.com/ARMmbed/mbed-crypto/issues/215
1159 */
1160 overall_status = status;
1161 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001162 }
1163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 status = psa_destroy_se_key(driver,
1165 psa_key_slot_get_slot_number(slot));
1166 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001167 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001169 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001170#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1171
Darryl Greend49a4992018-06-18 17:27:26 +01001172#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1174 status = psa_destroy_persistent_key(slot->attr.id);
1175 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001176 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001178
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001179 /* TODO: other slots may have a copy of the same key. We should
1180 * invalidate them.
1181 * https://github.com/ARMmbed/mbed-crypto/issues/214
1182 */
Darryl Greend49a4992018-06-18 17:27:26 +01001183 }
1184#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001185
Gilles Peskinefc762652019-07-22 19:30:34 +02001186#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (driver != NULL) {
1188 status = psa_save_se_persistent_data(driver);
1189 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001190 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 }
1192 status = psa_crypto_stop_transaction();
1193 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001194 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001196 }
1197#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1198
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001201 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001203 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 }
1205 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001206}
1207
John Durkop07cc04a2020-11-16 22:08:34 -08001208#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001209 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001210static psa_status_t psa_get_rsa_public_exponent(
1211 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001213{
1214 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001216 uint8_t *buffer = NULL;
1217 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1221 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001222 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 }
1224 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001225 /* It's the default value, which is reported as an empty string,
1226 * so there's nothing to do. */
1227 goto exit;
1228 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 buflen = mbedtls_mpi_size(&mpi);
1231 buffer = mbedtls_calloc(1, buflen);
1232 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001233 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1234 goto exit;
1235 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1237 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001238 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001240 attributes->domain_parameters = buffer;
1241 attributes->domain_parameters_size = buflen;
1242
1243exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 mbedtls_mpi_free(&mpi);
1245 if (ret != 0) {
1246 mbedtls_free(buffer);
1247 }
1248 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001249}
John Durkop07cc04a2020-11-16 22:08:34 -08001250#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001251 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001252
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001253/** Retrieve all the publicly-accessible attributes of a key.
1254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001255psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1256 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001257{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001258 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001259 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001260 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1265 if (status != PSA_SUCCESS) {
1266 return status;
1267 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001268
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001269 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1271 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001272
1273#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1275 psa_set_key_slot_number(attributes,
1276 psa_key_slot_get_slot_number(slot));
1277 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001278#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001281#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001282 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001283 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001284 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001285 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001286 * is not yet implemented.
1287 * https://github.com/ARMmbed/mbed-crypto/issues/216
1288 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001290 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001291
Ronald Cron90857082020-11-25 14:58:33 +01001292 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 slot->attr.type,
1294 slot->key.data,
1295 slot->key.bytes,
1296 &rsa);
1297 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001298 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 status = psa_get_rsa_public_exponent(rsa,
1302 attributes);
1303 mbedtls_rsa_free(rsa);
1304 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001305 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001306 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001307#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001308 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001309 default:
1310 /* Nothing else to do. */
1311 break;
1312 }
1313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (status != PSA_SUCCESS) {
1315 psa_reset_key_attributes(attributes);
1316 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001321}
1322
Gilles Peskinec8000c02019-08-02 20:15:51 +02001323#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1324psa_status_t psa_get_key_slot_number(
1325 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001327{
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001329 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 return PSA_SUCCESS;
1331 } else {
1332 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001333 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001334}
1335#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1338 size_t key_buffer_size,
1339 uint8_t *data,
1340 size_t data_size,
1341 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001342{
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if (key_buffer_size > data_size) {
1344 return PSA_ERROR_BUFFER_TOO_SMALL;
1345 }
1346 memcpy(data, key_buffer, key_buffer_size);
1347 memset(data + key_buffer_size, 0,
1348 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001351}
1352
Ronald Cron7285cda2020-11-26 14:46:37 +01001353psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001354 const psa_key_attributes_t *attributes,
1355 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001357{
Ronald Crond18b5f82020-11-25 16:46:05 +01001358 psa_key_type_t type = attributes->core.type;
1359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (key_type_is_raw_bytes(type) ||
1361 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001362 PSA_KEY_TYPE_IS_ECC(type) ||
1363 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 return psa_export_key_buffer_internal(
1365 key_buffer, key_buffer_size,
1366 data, data_size, data_length);
1367 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001368 /* This shouldn't happen in the reference implementation, but
1369 it is valid for a special-purpose implementation to omit
1370 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001372 }
1373}
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1376 uint8_t *data,
1377 size_t data_size,
1378 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001379{
1380 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1381 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1382 psa_key_slot_t *slot;
1383
Ronald Crone907e552021-01-18 13:22:38 +01001384 /* Reject a zero-length output buffer now, since this can never be a
1385 * valid key representation. This way we know that data must be a valid
1386 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 if (data_size == 0) {
1388 return PSA_ERROR_BUFFER_TOO_SMALL;
1389 }
Ronald Crone907e552021-01-18 13:22:38 +01001390
Ronald Cron9486f9d2020-11-26 13:36:23 +01001391 /* Set the key to empty now, so that even when there are errors, we always
1392 * set data_length to a value between 0 and data_size. On error, setting
1393 * the key to empty is a good choice because an empty key representation is
1394 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001395 *data_length = 0;
1396
Ronald Cron9486f9d2020-11-26 13:36:23 +01001397 /* Export requires the EXPORT flag. There is an exception for public keys,
1398 * which don't require any flag, but
1399 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1400 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1402 PSA_KEY_USAGE_EXPORT, 0);
1403 if (status != PSA_SUCCESS) {
1404 return status;
1405 }
mohammad160306e79202018-03-28 13:17:44 +03001406
Ronald Crond18b5f82020-11-25 16:46:05 +01001407 psa_key_attributes_t attributes = {
1408 .core = slot->attr
1409 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 status = psa_driver_wrapper_export_key(&attributes,
1411 slot->key.data, slot->key.bytes,
1412 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001415
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001417}
1418
Ronald Cron7285cda2020-11-26 14:46:37 +01001419psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001420 const psa_key_attributes_t *attributes,
1421 const uint8_t *key_buffer,
1422 size_t key_buffer_size,
1423 uint8_t *data,
1424 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001426{
Ronald Crond18b5f82020-11-25 16:46:05 +01001427 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001428
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001429 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001430 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1431 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001432 /* Exporting public -> public */
1433 return psa_export_key_buffer_internal(
1434 key_buffer, key_buffer_size,
1435 data, data_size, data_length);
1436 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001437#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001438 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1439 return mbedtls_psa_rsa_export_public_key(attributes,
1440 key_buffer,
1441 key_buffer_size,
1442 data,
1443 data_size,
1444 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001445#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001446 /* We don't know how to convert a private RSA key to public. */
1447 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001448#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1449 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001450 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001451#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001452 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1453 return mbedtls_psa_ecp_export_public_key(attributes,
1454 key_buffer,
1455 key_buffer_size,
1456 data,
1457 data_size,
1458 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001459#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001460 /* We don't know how to convert a private ECC key to public */
1461 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001462#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1463 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001464 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001465#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
1466 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001467 return mbedtls_psa_export_ffdh_public_key(attributes,
1468 key_buffer,
1469 key_buffer_size,
1470 data, data_size,
1471 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001472#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001473 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001474#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
1475 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001478 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001479}
Ronald Crond18b5f82020-11-25 16:46:05 +01001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1482 uint8_t *data,
1483 size_t data_size,
1484 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001485{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001486 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001487 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001488 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001489
Ronald Crone907e552021-01-18 13:22:38 +01001490 /* Reject a zero-length output buffer now, since this can never be a
1491 * valid key representation. This way we know that data must be a valid
1492 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (data_size == 0) {
1494 return PSA_ERROR_BUFFER_TOO_SMALL;
1495 }
Ronald Crone907e552021-01-18 13:22:38 +01001496
Darryl Greendd8fb772018-11-07 16:00:44 +00001497 /* Set the key to empty now, so that even when there are errors, we always
1498 * set data_length to a value between 0 and data_size. On error, setting
1499 * the key to empty is a good choice because an empty key representation is
1500 * unlikely to be accepted anywhere. */
1501 *data_length = 0;
1502
1503 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1505 if (status != PSA_SUCCESS) {
1506 return status;
1507 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1510 status = PSA_ERROR_INVALID_ARGUMENT;
1511 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001512 }
1513
Ronald Crond18b5f82020-11-25 16:46:05 +01001514 psa_key_attributes_t attributes = {
1515 .core = slot->attr
1516 };
Ronald Cron67227982020-11-26 15:16:05 +01001517 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001518 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001520
1521exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001525}
1526
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001527MBEDTLS_STATIC_ASSERT(
1528 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1529 "One or more key attribute flag is listed as both external-only and dual-use")
1530MBEDTLS_STATIC_ASSERT(
1531 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1532 "One or more key attribute flag is listed as both internal-only and dual-use")
1533MBEDTLS_STATIC_ASSERT(
1534 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1535 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001536
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001537/** Validate that a key policy is internally well-formed.
1538 *
1539 * This function only rejects invalid policies. It does not validate the
1540 * consistency of the policy with respect to other attributes of the key
1541 * such as the key type.
1542 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001543static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001544{
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1546 PSA_KEY_USAGE_COPY |
1547 PSA_KEY_USAGE_ENCRYPT |
1548 PSA_KEY_USAGE_DECRYPT |
1549 PSA_KEY_USAGE_SIGN_MESSAGE |
1550 PSA_KEY_USAGE_VERIFY_MESSAGE |
1551 PSA_KEY_USAGE_SIGN_HASH |
1552 PSA_KEY_USAGE_VERIFY_HASH |
1553 PSA_KEY_USAGE_VERIFY_DERIVATION |
1554 PSA_KEY_USAGE_DERIVE)) != 0) {
1555 return PSA_ERROR_INVALID_ARGUMENT;
1556 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001559}
1560
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001561/** Validate the internal consistency of key attributes.
1562 *
1563 * This function only rejects invalid attribute values. If does not
1564 * validate the consistency of the attributes with any key data that may
1565 * be involved in the creation of the key.
1566 *
1567 * Call this function early in the key creation process.
1568 *
1569 * \param[in] attributes Key attributes for the new key.
1570 * \param[out] p_drv On any return, the driver for the key, if any.
1571 * NULL for a transparent key.
1572 *
1573 */
1574static psa_status_t psa_validate_key_attributes(
1575 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001577{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001578 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1580 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 status = psa_validate_key_location(lifetime, p_drv);
1583 if (status != PSA_SUCCESS) {
1584 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001585 }
1586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 status = psa_validate_key_persistence(lifetime);
1588 if (status != PSA_SUCCESS) {
1589 return status;
1590 }
1591
1592 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1593 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1594 return PSA_ERROR_INVALID_ARGUMENT;
1595 }
1596 } else {
1597 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1598 return PSA_ERROR_INVALID_ARGUMENT;
1599 }
1600 }
1601
1602 status = psa_validate_key_policy(&attributes->core.policy);
1603 if (status != PSA_SUCCESS) {
1604 return status;
1605 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001606
1607 /* Refuse to create overly large keys.
1608 * Note that this doesn't trigger on import if the attributes don't
1609 * explicitly specify a size (so psa_get_key_bits returns 0), so
1610 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1612 return PSA_ERROR_NOT_SUPPORTED;
1613 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001614
Gilles Peskine91e8c332019-08-02 19:19:39 +02001615 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1617 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1618 return PSA_ERROR_INVALID_ARGUMENT;
1619 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001622}
1623
Gilles Peskine4747d192019-04-17 15:05:45 +02001624/** Prepare a key slot to receive key material.
1625 *
1626 * This function allocates a key slot and sets its metadata.
1627 *
1628 * If this function fails, call psa_fail_key_creation().
1629 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001630 * This function is intended to be used as follows:
1631 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001632 * it with the specified attributes, and in case of a volatile key assign it
1633 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001634 * -# Populate the slot with the key material.
1635 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1636 * In case of failure at any step, stop the sequence and call
1637 * psa_fail_key_creation().
1638 *
Ronald Cron5c522922020-11-14 16:35:34 +01001639 * On success, the key slot is locked. It is the responsibility of the caller
1640 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001641 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001642 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001643 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001644 * \param[out] p_slot On success, a pointer to the prepared slot.
1645 * \param[out] p_drv On any return, the driver for the key, if any.
1646 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001647 *
1648 * \retval #PSA_SUCCESS
1649 * The key slot is ready to receive key material.
1650 * \return If this function fails, the key slot is an invalid state.
1651 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 */
1653static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001654 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001656 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001658{
1659 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001660 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001661 psa_key_slot_t *slot;
1662
Gilles Peskinedf179142019-07-15 22:02:14 +02001663 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001664 *p_drv = NULL;
1665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 status = psa_validate_key_attributes(attributes, p_drv);
1667 if (status != PSA_SUCCESS) {
1668 return status;
1669 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1672 if (status != PSA_SUCCESS) {
1673 return status;
1674 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001675 slot = *p_slot;
1676
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001677 /* We're storing the declared bit-size of the key. It's up to each
1678 * creation mechanism to verify that this information is correct.
1679 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001680 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001681 * is optional (import, copy). In case of a volatile key, assign it the
1682 * volatile key identifier associated to the slot returned to contain its
1683 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001684
1685 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001687#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1688 slot->attr.id = volatile_key_id;
1689#else
1690 slot->attr.id.key_id = volatile_key_id;
1691#endif
1692 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001693
Gilles Peskine91e8c332019-08-02 19:19:39 +02001694 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001695 * external-only flags, query `attributes`. Thanks to the check
1696 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001697 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001698 * may have set. */
1699 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001700
Gilles Peskinecbaff462019-07-12 23:46:04 +02001701#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001702 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001703 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001704 * create the key file in internal storage, create the
1705 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001706 * persistent data. This is done by starting a transaction that will
1707 * encompass these three actions.
1708 * For registering a volatile key, we just need to find an appropriate
1709 * slot number inside the SE. Since the key is designated volatile, creating
1710 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001711 /* The first thing to do is to find a slot number for the new key.
1712 * We save the slot number in persistent storage as part of the
1713 * transaction data. It will be needed to recover if the power
1714 * fails during the key creation process, to clean up on the secure
1715 * element side after restarting. Obtaining a slot number from the
1716 * secure element driver updates its persistent state, but we do not yet
1717 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001718 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001720 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1722 &slot_number);
1723 if (status != PSA_SUCCESS) {
1724 return status;
1725 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1728 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001729 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001730 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001731 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 status = psa_crypto_save_transaction();
1733 if (status != PSA_SUCCESS) {
1734 (void) psa_crypto_stop_transaction();
1735 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001736 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001737 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001738
1739 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001741 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001744 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001746 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001747#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001750}
Gilles Peskine4747d192019-04-17 15:05:45 +02001751
1752/** Finalize the creation of a key once its key material has been set.
1753 *
1754 * This entails writing the key to persistent storage.
1755 *
1756 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001757 * See the documentation of psa_start_key_creation() for the intended use
1758 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 *
Ronald Cron5c522922020-11-14 16:35:34 +01001760 * If the finalization succeeds, the function unlocks the key slot (it was
1761 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1762 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001763 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001764 * \param[in,out] slot Pointer to the slot with key material.
1765 * \param[in] driver The secure element driver for the key,
1766 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001767 * \param[out] key On success, identifier of the key. Note that the
1768 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001769 *
1770 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001771 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001772 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1773 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1774 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1775 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1776 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1777 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001778 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001779 * \return If this function fails, the key slot is an invalid state.
1780 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001781 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001782static psa_status_t psa_finish_key_creation(
1783 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001784 psa_se_drv_table_entry_t *driver,
1785 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001786{
1787 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001788 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001789 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001790
1791#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001793#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001795 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001796 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001798
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001799 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1800 sizeof(data.slot_number),
1801 "Slot number size does not match psa_se_key_data_storage_t");
1802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1804 status = psa_save_persistent_key(&slot->attr,
1805 (uint8_t *) &data,
1806 sizeof(data));
1807 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001808#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1809 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001810 /* Key material is saved in export representation in the slot, so
1811 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 status = psa_save_persistent_key(&slot->attr,
1813 slot->key.data,
1814 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001815 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001816 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001817#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1818
Gilles Peskinecbaff462019-07-12 23:46:04 +02001819#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001820 /* Finish the transaction for a key creation. This does not
1821 * happen when registering an existing key. Detect this case
1822 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001823 * creation of a persistent key in a secure element requires a transaction,
1824 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 if (driver != NULL &&
1826 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1827 status = psa_save_se_persistent_data(driver);
1828 if (status != PSA_SUCCESS) {
1829 psa_destroy_persistent_key(slot->attr.id);
1830 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001831 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001833 }
1834#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001837 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 status = psa_unlock_key_slot(slot);
1839 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001840 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001842 }
Ronald Cron50972942020-11-14 11:28:25 +01001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001845}
1846
1847/** Abort the creation of a key.
1848 *
1849 * You may call this function after calling psa_start_key_creation(),
1850 * or after psa_finish_key_creation() fails. In other circumstances, this
1851 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001852 * See the documentation of psa_start_key_creation() for the intended use
1853 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001854 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001855 * \param[in,out] slot Pointer to the slot with key material.
1856 * \param[in] driver The secure element driver for the key,
1857 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001858 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001859static void psa_fail_key_creation(psa_key_slot_t *slot,
1860 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001861{
Gilles Peskine011e4282019-06-26 18:34:38 +02001862 (void) driver;
1863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001865 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001867
1868#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001869 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001870 * element, and the failure happened later (when saving metadata
1871 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001872 * element.
1873 * https://github.com/ARMmbed/mbed-crypto/issues/217
1874 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001875
Gilles Peskined7729582019-08-05 15:55:54 +02001876 /* Abort the ongoing transaction if any (there may not be one if
1877 * the creation process failed before starting one, or if the
1878 * key creation is a registration of a key in a secure element).
1879 * Earlier functions must already have done what it takes to undo any
1880 * partial creation. All that's left is to update the transaction data
1881 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001883#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001886}
1887
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001888/** Validate optional attributes during key creation.
1889 *
1890 * Some key attributes are optional during key creation. If they are
1891 * specified in the attributes structure, check that they are consistent
1892 * with the data in the slot.
1893 *
1894 * This function should be called near the end of key creation, after
1895 * the slot in memory is fully populated but before saving persistent data.
1896 */
1897static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900{
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (attributes->core.type != 0) {
1902 if (attributes->core.type != slot->attr.type) {
1903 return PSA_ERROR_INVALID_ARGUMENT;
1904 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001905 }
1906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001908#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1910 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001911 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001912 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001914
Ronald Cron90857082020-11-25 14:58:33 +01001915 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 slot->attr.type,
1917 slot->key.data,
1918 slot->key.bytes,
1919 &rsa);
1920 if (status != PSA_SUCCESS) {
1921 return status;
1922 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 mbedtls_mpi_init(&actual);
1925 mbedtls_mpi_init(&required);
1926 ret = mbedtls_rsa_export(rsa,
1927 NULL, NULL, NULL, NULL, &actual);
1928 mbedtls_rsa_free(rsa);
1929 mbedtls_free(rsa);
1930 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001931 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 }
1933 ret = mbedtls_mpi_read_binary(&required,
1934 attributes->domain_parameters,
1935 attributes->domain_parameters_size);
1936 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001937 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 }
1939 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 }
1942rsa_exit:
1943 mbedtls_mpi_free(&actual);
1944 mbedtls_mpi_free(&required);
1945 if (ret != 0) {
1946 return mbedtls_to_psa_error(ret);
1947 }
1948 } else
John Durkop9814fa22020-11-04 12:28:15 -08001949#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1950 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953 }
1954 }
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (attributes->core.bits != 0) {
1957 if (attributes->core.bits != slot->attr.bits) {
1958 return PSA_ERROR_INVALID_ARGUMENT;
1959 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001960 }
1961
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963}
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1966 const uint8_t *data,
1967 size_t data_length,
1968 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001969{
1970 psa_status_t status;
1971 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001972 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001973 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301974 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001975
Ronald Cron81709fc2020-11-14 12:10:32 +01001976 *key = MBEDTLS_SVC_KEY_ID_INIT;
1977
Gilles Peskine0f84d622019-09-12 19:03:13 +02001978 /* Reject zero-length symmetric keys (including raw data key objects).
1979 * This also rejects any key which might be encoded as an empty string,
1980 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 if (data_length == 0) {
1982 return PSA_ERROR_INVALID_ARGUMENT;
1983 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001984
Archana449608b2021-09-08 15:36:05 +05301985 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 if (data_length > SIZE_MAX / 8) {
1987 return PSA_ERROR_NOT_SUPPORTED;
1988 }
Archana6ed4bda2021-08-04 10:47:15 +05301989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1991 &slot, &driver);
1992 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001993 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001995
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001996 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301997 * storage ( thus not in the case of importing a key in a secure element
1998 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1999 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 if (slot->key.data == NULL) {
2001 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302002 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 attributes, data, data_length, &storage_size);
2004 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302005 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 }
Archanad8a83dc2021-06-14 10:04:16 +05302007 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 status = psa_allocate_buffer_to_slot(slot, storage_size);
2009 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002010 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002012 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002013
2014 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 status = psa_driver_wrapper_import_key(attributes,
2016 data, data_length,
2017 slot->key.data,
2018 slot->key.bytes,
2019 &slot->key.bytes, &bits);
2020 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002021 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002025 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002027 status = PSA_ERROR_INVALID_ARGUMENT;
2028 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002029 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002030
Archana6ed4bda2021-08-04 10:47:15 +05302031 /* Enforce a size limit, and in particular ensure that the bit
2032 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302034 status = PSA_ERROR_NOT_SUPPORTED;
2035 goto exit;
2036 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 status = psa_validate_optional_attributes(slot, attributes);
2038 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (status != PSA_SUCCESS) {
2045 psa_fail_key_creation(slot, driver);
2046 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002049}
2050
Gilles Peskined7729582019-08-05 15:55:54 +02002051#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2052psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002054{
2055 psa_status_t status;
2056 psa_key_slot_t *slot = NULL;
2057 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002058 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002059
2060 /* Leaving attributes unspecified is not currently supported.
2061 * It could make sense to query the key type and size from the
2062 * secure element, but not all secure elements support this
2063 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2065 return PSA_ERROR_NOT_SUPPORTED;
2066 }
2067 if (psa_get_key_bits(attributes) == 0) {
2068 return PSA_ERROR_NOT_SUPPORTED;
2069 }
Gilles Peskined7729582019-08-05 15:55:54 +02002070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2072 &slot, &driver);
2073 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002074 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 }
Gilles Peskined7729582019-08-05 15:55:54 +02002076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002078
2079exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (status != PSA_SUCCESS) {
2081 psa_fail_key_creation(slot, driver);
2082 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002083
Gilles Peskined7729582019-08-05 15:55:54 +02002084 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 psa_close_key(key);
2086 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002087}
2088#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2091 const psa_key_attributes_t *specified_attributes,
2092 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002093{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002095 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002096 psa_key_slot_t *source_slot = NULL;
2097 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002098 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002099 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302100 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002101
Ronald Cron81709fc2020-11-14 12:10:32 +01002102 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2103
Archana8a180362021-07-05 02:18:48 +05302104 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2106 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 status = psa_validate_optional_attributes(source_slot,
2111 specified_attributes);
2112 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002115
Archana9d17bf42021-09-10 06:22:44 +05302116 /* The target key type and number of bits have been validated by
2117 * psa_validate_optional_attributes() to be either equal to zero or
2118 * equal to the ones of the source key. So it is safe to inherit
2119 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302120 * */
Archana9d17bf42021-09-10 06:22:44 +05302121 actual_attributes.core.bits = source_slot->attr.bits;
2122 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302123
2124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 status = psa_restrict_key_policy(source_slot->attr.type,
2126 &actual_attributes.core.policy,
2127 &source_slot->attr.policy);
2128 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2133 &target_slot, &driver);
2134 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 }
2137 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2138 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302139 /*
Archana9d17bf42021-09-10 06:22:44 +05302140 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302141 * the source key would need to be exported as plaintext and re-imported
2142 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302143 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302144 * appropriate API invocations from the application, if needed.
2145 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002146 status = PSA_ERROR_NOT_SUPPORTED;
2147 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002148 }
Archana8a180362021-07-05 02:18:48 +05302149 /*
2150 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302151 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302152 * - For opaque keys this translates to an invocation of the drivers'
2153 * copy_key entry point through the dispatch layer.
2154 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2156 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2157 &storage_size);
2158 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302159 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 }
Archana374fe5b2021-09-08 15:50:28 +05302161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2163 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 }
Archana374fe5b2021-09-08 15:50:28 +05302166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 status = psa_driver_wrapper_copy_key(&actual_attributes,
2168 source_slot->key.data,
2169 source_slot->key.bytes,
2170 target_slot->key.data,
2171 target_slot->key.bytes,
2172 &target_slot->key.bytes);
2173 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 }
2176 } else {
2177 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302178 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 source_slot->key.bytes);
2180 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 }
Archana8a180362021-07-05 02:18:48 +05302183 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (status != PSA_SUCCESS) {
2187 psa_fail_key_creation(target_slot, driver);
2188 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002193}
2194
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002195
2196
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002197/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002198/* Message digests */
2199/****************************************************************/
2200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002203 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (operation->id == 0) {
2205 return PSA_SUCCESS;
2206 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002209 operation->id = 0;
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212}
2213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2215 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002218
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002219 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002221 status = PSA_ERROR_BAD_STATE;
2222 goto exit;
2223 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002226 status = PSA_ERROR_INVALID_ARGUMENT;
2227 goto exit;
2228 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002229
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002230 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2231 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002235
2236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (status != PSA_SUCCESS) {
2238 psa_hash_abort(operation);
2239 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002240
2241 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242}
2243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2245 const uint8_t *input,
2246 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002248 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002251 status = PSA_ERROR_BAD_STATE;
2252 goto exit;
2253 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002254
Steven Cooremanc8288352021-03-04 14:02:19 +01002255 /* Don't require hash implementations to behave correctly on a
2256 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if (input_length == 0) {
2258 return PSA_SUCCESS;
2259 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002262
2263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (status != PSA_SUCCESS) {
2265 psa_hash_abort(operation);
2266 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269}
2270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2272 uint8_t *hash,
2273 size_t hash_size,
2274 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002276 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 if (operation->id == 0) {
2278 return PSA_ERROR_BAD_STATE;
2279 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280
Steven Cooreman1e582352021-02-18 17:24:37 +01002281 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 operation, hash, hash_size, hash_length);
2283 psa_hash_abort(operation);
2284 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285}
2286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2288 const uint8_t *hash,
2289 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290{
Ronald Cron69a63422021-10-18 09:47:58 +02002291 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002293 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 operation,
2295 actual_hash, sizeof(actual_hash),
2296 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002299 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002303 status = PSA_ERROR_INVALID_SIGNATURE;
2304 goto exit;
2305 }
2306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002308 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002310
2311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2313 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002314 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318}
2319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320psa_status_t psa_hash_compute(psa_algorithm_t alg,
2321 const uint8_t *input, size_t input_length,
2322 uint8_t *hash, size_t hash_size,
2323 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002324{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002325 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (!PSA_ALG_IS_HASH(alg)) {
2327 return PSA_ERROR_INVALID_ARGUMENT;
2328 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2331 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002332}
2333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334psa_status_t psa_hash_compare(psa_algorithm_t alg,
2335 const uint8_t *input, size_t input_length,
2336 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002337{
Ronald Cron69a63422021-10-18 09:47:58 +02002338 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002339 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (!PSA_ALG_IS_HASH(alg)) {
2342 return PSA_ERROR_INVALID_ARGUMENT;
2343 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002344
Steven Cooreman1e582352021-02-18 17:24:37 +01002345 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 alg, input, input_length,
2347 actual_hash, sizeof(actual_hash),
2348 &actual_hash_length);
2349 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002350 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 }
2352 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002353 status = PSA_ERROR_INVALID_SIGNATURE;
2354 goto exit;
2355 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002357 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002359
2360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2362 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002363}
2364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2366 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002367{
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 if (source_operation->id == 0 ||
2369 target_operation->id != 0) {
2370 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002371 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2374 target_operation);
2375 if (status != PSA_SUCCESS) {
2376 psa_hash_abort(target_operation);
2377 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002380}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381
2382
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002383/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002384/* MAC */
2385/****************************************************************/
2386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002388{
Steven Cooremane6804192021-03-19 18:28:56 +01002389 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (operation->id == 0) {
2391 return PSA_SUCCESS;
2392 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002395 operation->mac_size = 0;
2396 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002397 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002400}
2401
Ronald Cron2dff3b22021-06-17 16:33:22 +02002402static psa_status_t psa_mac_finalize_alg_and_key_validation(
2403 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002404 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002406{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 psa_key_type_t key_type = psa_get_key_type(attributes);
2409 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (!PSA_ALG_IS_MAC(alg)) {
2412 return PSA_ERROR_INVALID_ARGUMENT;
2413 }
Steven Cooremane6804192021-03-19 18:28:56 +01002414
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002415 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 status = psa_mac_key_can_do(alg, key_type);
2417 if (status != PSA_SUCCESS) {
2418 return status;
2419 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002420
Steven Cooremand1a68f12021-05-10 11:13:41 +02002421 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002425 /* A very short MAC is too short for security since it can be
2426 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2427 * so we make this our minimum, even though 32 bits is still
2428 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002430 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2433 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002434 /* It's impossible to "truncate" to a larger length than the full length
2435 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002437 }
2438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002440 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2441 * that is disabled in the compile-time configuration. The result can
2442 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2443 * configuration into account. In this case, force a return of
2444 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2445 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2446 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2447 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2448 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002450 }
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002453}
2454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2456 mbedtls_svc_key_id_t key,
2457 psa_algorithm_t alg,
2458 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002459{
2460 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2461 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002462 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002463
2464 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002466 status = PSA_ERROR_BAD_STATE;
2467 goto exit;
2468 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002469
2470 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 key,
2472 &slot,
2473 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2474 alg);
2475 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002476 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478
2479 psa_key_attributes_t attributes = {
2480 .core = slot->attr
2481 };
2482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2484 &operation->mac_size);
2485 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488
2489 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002490 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (is_sign) {
2492 status = psa_driver_wrapper_mac_sign_setup(operation,
2493 &attributes,
2494 slot->key.data,
2495 slot->key.bytes,
2496 alg);
2497 } else {
2498 status = psa_driver_wrapper_mac_verify_setup(operation,
2499 &attributes,
2500 slot->key.data,
2501 slot->key.bytes,
2502 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002504
Gilles Peskinefbfac682018-07-08 20:51:54 +02002505exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (status != PSA_SUCCESS) {
2507 psa_mac_abort(operation);
2508 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513}
2514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2516 mbedtls_svc_key_id_t key,
2517 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002518{
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002520}
2521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2523 mbedtls_svc_key_id_t key,
2524 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002525{
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002527}
2528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2530 const uint8_t *input,
2531 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532{
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 if (operation->id == 0) {
2534 return PSA_ERROR_BAD_STATE;
2535 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002537 /* Don't require hash implementations to behave correctly on a
2538 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (input_length == 0) {
2540 return PSA_SUCCESS;
2541 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2544 input, input_length);
2545 if (status != PSA_SUCCESS) {
2546 psa_mac_abort(operation);
2547 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550}
2551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2553 uint8_t *mac,
2554 size_t mac_size,
2555 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002556{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2558 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002561 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002562 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002563 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002566 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002567 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002568 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002569
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002570 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2571 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002573 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002574 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002575 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002578 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002579 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002580 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 status = psa_driver_wrapper_mac_sign_finish(operation,
2583 mac, operation->mac_size,
2584 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002585
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002586exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002587 /* In case of success, set the potential excess room in the output buffer
2588 * to an invalid value, to avoid potentially leaking a longer MAC.
2589 * In case of error, set the output length and content to a safe default,
2590 * such that in case the caller misses an error check, the output would be
2591 * an unachievable MAC.
2592 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002594 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002595 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002596 }
mohammad16036df908f2018-04-02 08:34:15 -07002597
Paul Elliottdc42ca82023-02-24 18:11:59 +00002598 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002603}
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2606 const uint8_t *mac,
2607 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002609 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2610 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002613 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002614 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002615 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002618 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002619 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002620 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002623 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002624 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002625 }
2626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 status = psa_driver_wrapper_mac_verify_finish(operation,
2628 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002629
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002630exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002634}
2635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2637 psa_algorithm_t alg,
2638 const uint8_t *input,
2639 size_t input_length,
2640 uint8_t *mac,
2641 size_t mac_size,
2642 size_t *mac_length,
2643 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002644{
2645 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002646 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2647 psa_key_slot_t *slot;
2648 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002649
Ronald Cron51131b52021-06-17 17:17:20 +02002650 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 key,
2652 &slot,
2653 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2654 alg);
2655 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002656 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002658
Ronald Cron51131b52021-06-17 17:17:20 +02002659 psa_key_attributes_t attributes = {
2660 .core = slot->attr
2661 };
2662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2664 &operation_mac_size);
2665 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002670 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002671 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002672 }
2673
2674 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 &attributes,
2676 slot->key.data, slot->key.bytes,
2677 alg,
2678 input, input_length,
2679 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002680
2681exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002682 /* In case of success, set the potential excess room in the output buffer
2683 * to an invalid value, to avoid potentially leaking a longer MAC.
2684 * In case of error, set the output length and content to a safe default,
2685 * such that in case the caller misses an error check, the output would be
2686 * an unachievable MAC.
2687 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002689 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002690 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002691 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002692
2693 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002698}
2699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002701 psa_algorithm_t alg,
2702 const uint8_t *input,
2703 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 uint8_t *mac,
2705 size_t mac_size,
2706 size_t *mac_length)
2707{
2708 return psa_mac_compute_internal(key, alg,
2709 input, input_length,
2710 mac, mac_size, mac_length, 1);
2711}
2712
2713psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2714 psa_algorithm_t alg,
2715 const uint8_t *input,
2716 size_t input_length,
2717 const uint8_t *mac,
2718 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002719{
2720 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002721 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2722 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 status = psa_mac_compute_internal(key, alg,
2725 input, input_length,
2726 actual_mac, sizeof(actual_mac),
2727 &actual_mac_length, 0);
2728 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002729 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002733 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002734 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002735 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002737 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002738 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002739 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002740
2741exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002745}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002746
Gilles Peskinee59236f2018-01-27 23:32:46 +01002747/****************************************************************/
2748/* Asymmetric cryptography */
2749/****************************************************************/
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2752 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002753{
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 if (input_is_message) {
2755 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2756 return PSA_ERROR_INVALID_ARGUMENT;
2757 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2760 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2761 return PSA_ERROR_INVALID_ARGUMENT;
2762 }
2763 }
2764 } else {
2765 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2766 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002767 }
2768 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002771}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2774 int input_is_message,
2775 psa_algorithm_t alg,
2776 const uint8_t *input,
2777 size_t input_length,
2778 uint8_t *signature,
2779 size_t signature_size,
2780 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002781{
2782 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2783 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2784 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002785
2786 *signature_length = 0;
2787
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 status = psa_sign_verify_check_alg(input_is_message, alg);
2789 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002790 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002792
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002793 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002794 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002795 * buffer can in principle be empty since it doesn't actually have
2796 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 if (signature_size == 0) {
2798 return PSA_ERROR_BUFFER_TOO_SMALL;
2799 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002801 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 key, &slot,
2803 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2804 PSA_KEY_USAGE_SIGN_HASH,
2805 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002808 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002812 status = PSA_ERROR_INVALID_ARGUMENT;
2813 goto exit;
2814 }
2815
2816 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002818 };
2819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002821 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002822 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002823 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 signature, signature_size, signature_length);
2825 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002826
2827 status = psa_driver_wrapper_sign_hash(
2828 &attributes, slot->key.data, slot->key.bytes,
2829 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002831 }
2832
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002833
2834exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002835 psa_wipe_tag_output_buffer(signature, status, signature_size,
2836 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002841}
2842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2844 int input_is_message,
2845 psa_algorithm_t alg,
2846 const uint8_t *input,
2847 size_t input_length,
2848 const uint8_t *signature,
2849 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002850{
2851 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2852 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2853 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 status = psa_sign_verify_check_alg(input_is_message, alg);
2856 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002857 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002859
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002860 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 key, &slot,
2862 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2863 PSA_KEY_USAGE_VERIFY_HASH,
2864 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (status != PSA_SUCCESS) {
2867 return status;
2868 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002869
2870 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002872 };
2873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002875 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002876 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002877 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 signature, signature_length);
2879 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002880 status = psa_driver_wrapper_verify_hash(
2881 &attributes, slot->key.data, slot->key.bytes,
2882 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002884 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002889
2890}
2891
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002892psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002893 const psa_key_attributes_t *attributes,
2894 const uint8_t *key_buffer,
2895 size_t key_buffer_size,
2896 psa_algorithm_t alg,
2897 const uint8_t *input,
2898 size_t input_length,
2899 uint8_t *signature,
2900 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002902{
2903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002906 size_t hash_length;
2907 uint8_t hash[PSA_HASH_MAX_SIZE];
2908
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002909 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 PSA_ALG_SIGN_GET_HASH(alg),
2911 input, input_length,
2912 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002915 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002917
2918 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 attributes, key_buffer, key_buffer_size,
2920 alg, hash, hash_length,
2921 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002922 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002925}
2926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2928 psa_algorithm_t alg,
2929 const uint8_t *input,
2930 size_t input_length,
2931 uint8_t *signature,
2932 size_t signature_size,
2933 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002934{
2935 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002936 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002938}
2939
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002940psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002941 const psa_key_attributes_t *attributes,
2942 const uint8_t *key_buffer,
2943 size_t key_buffer_size,
2944 psa_algorithm_t alg,
2945 const uint8_t *input,
2946 size_t input_length,
2947 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002949{
2950 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002951
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002953 size_t hash_length;
2954 uint8_t hash[PSA_HASH_MAX_SIZE];
2955
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002956 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 PSA_ALG_SIGN_GET_HASH(alg),
2958 input, input_length,
2959 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002962 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002964
2965 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 attributes, key_buffer, key_buffer_size,
2967 alg, hash, hash_length,
2968 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002969 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002972}
2973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2975 psa_algorithm_t alg,
2976 const uint8_t *input,
2977 size_t input_length,
2978 const uint8_t *signature,
2979 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002980{
2981 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002982 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002984}
2985
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002986psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002987 const psa_key_attributes_t *attributes,
2988 const uint8_t *key_buffer, size_t key_buffer_size,
2989 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002991{
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2993 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2994 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002995#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2997 return mbedtls_psa_rsa_sign_hash(
2998 attributes,
2999 key_buffer, key_buffer_size,
3000 alg, hash, hash_length,
3001 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003002#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3003 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 } else {
3005 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003006 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3008 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003009#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3011 return mbedtls_psa_ecdsa_sign_hash(
3012 attributes,
3013 key_buffer, key_buffer_size,
3014 alg, hash, hash_length,
3015 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003016#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3017 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 } else {
3019 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003020 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003021 }
Ronald Cron4501c982021-03-19 20:09:32 +01003022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 (void) key_buffer;
3024 (void) key_buffer_size;
3025 (void) hash;
3026 (void) hash_length;
3027 (void) signature;
3028 (void) signature_size;
3029 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003032}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3035 psa_algorithm_t alg,
3036 const uint8_t *hash,
3037 size_t hash_length,
3038 uint8_t *signature,
3039 size_t signature_size,
3040 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003041{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003042 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003043 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003045}
3046
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003047psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003048 const psa_key_attributes_t *attributes,
3049 const uint8_t *key_buffer, size_t key_buffer_size,
3050 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003052{
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3054 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3055 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003056#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3058 return mbedtls_psa_rsa_verify_hash(
3059 attributes,
3060 key_buffer, key_buffer_size,
3061 alg, hash, hash_length,
3062 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003063#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3064 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 } else {
3066 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003067 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3069 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003070#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3072 return mbedtls_psa_ecdsa_verify_hash(
3073 attributes,
3074 key_buffer, key_buffer_size,
3075 alg, hash, hash_length,
3076 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003077#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3078 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 } else {
3080 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003081 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003082 }
Ronald Cron4501c982021-03-19 20:09:32 +01003083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 (void) key_buffer;
3085 (void) key_buffer_size;
3086 (void) hash;
3087 (void) hash_length;
3088 (void) signature;
3089 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003092}
3093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3095 psa_algorithm_t alg,
3096 const uint8_t *hash,
3097 size_t hash_length,
3098 const uint8_t *signature,
3099 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003100{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003101 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003102 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003104}
3105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3107 psa_algorithm_t alg,
3108 const uint8_t *input,
3109 size_t input_length,
3110 const uint8_t *salt,
3111 size_t salt_length,
3112 uint8_t *output,
3113 size_t output_size,
3114 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003116 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003117 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003118 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003119
Darryl Green5cc689a2018-07-24 15:34:10 +01003120 (void) input;
3121 (void) input_length;
3122 (void) salt;
3123 (void) output;
3124 (void) output_size;
3125
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003126 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003127
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3129 return PSA_ERROR_INVALID_ARGUMENT;
3130 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003131
Ronald Cron5c522922020-11-14 16:35:34 +01003132 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3134 if (status != PSA_SUCCESS) {
3135 return status;
3136 }
3137 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3138 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003139 status = PSA_ERROR_INVALID_ARGUMENT;
3140 goto exit;
3141 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003143 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003145 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003146
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003147 status = psa_driver_wrapper_asymmetric_encrypt(
3148 &attributes, slot->key.data, slot->key.bytes,
3149 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003153
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003155}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3158 psa_algorithm_t alg,
3159 const uint8_t *input,
3160 size_t input_length,
3161 const uint8_t *salt,
3162 size_t salt_length,
3163 uint8_t *output,
3164 size_t output_size,
3165 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003168 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003169 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003170
Darryl Green5cc689a2018-07-24 15:34:10 +01003171 (void) input;
3172 (void) input_length;
3173 (void) salt;
3174 (void) output;
3175 (void) output_size;
3176
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003177 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3180 return PSA_ERROR_INVALID_ARGUMENT;
3181 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003182
Ronald Cron5c522922020-11-14 16:35:34 +01003183 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3185 if (status != PSA_SUCCESS) {
3186 return status;
3187 }
3188 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003189 status = PSA_ERROR_INVALID_ARGUMENT;
3190 goto exit;
3191 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003193 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003195 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003197 status = psa_driver_wrapper_asymmetric_decrypt(
3198 &attributes, slot->key.data, slot->key.bytes,
3199 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003201
3202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003206}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003207
Paul Elliott9fe12f62022-11-30 19:16:02 +00003208/****************************************************************/
3209/* Asymmetric interruptible cryptography */
3210/****************************************************************/
3211
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003212static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3213
Paul Elliott9fe12f62022-11-30 19:16:02 +00003214void psa_interruptible_set_max_ops(uint32_t max_ops)
3215{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003216 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003217}
3218
3219uint32_t psa_interruptible_get_max_ops(void)
3220{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003221 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003222}
3223
Paul Elliott9fe12f62022-11-30 19:16:02 +00003224uint32_t psa_sign_hash_get_num_ops(
3225 const psa_sign_hash_interruptible_operation_t *operation)
3226{
Paul Elliott296ede92022-12-15 17:00:30 +00003227 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003228}
3229
3230uint32_t psa_verify_hash_get_num_ops(
3231 const psa_verify_hash_interruptible_operation_t *operation)
3232{
Paul Elliott296ede92022-12-15 17:00:30 +00003233 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003234}
3235
Paul Elliott59ad9452022-12-18 15:09:02 +00003236static psa_status_t psa_sign_hash_abort_internal(
3237 psa_sign_hash_interruptible_operation_t *operation)
3238{
3239 if (operation->id == 0) {
3240 /* The object has (apparently) been initialized but it is not (yet)
3241 * in use. It's ok to call abort on such an object, and there's
3242 * nothing to do. */
3243 return PSA_SUCCESS;
3244 }
3245
3246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3247
3248 status = psa_driver_wrapper_sign_hash_abort(operation);
3249
3250 operation->id = 0;
3251
Paul Elliotte6145dc2023-02-07 12:51:21 +00003252 /* Do not clear either the error_occurred or num_ops elements here as they
3253 * only want to be cleared by the application calling abort, not by abort
3254 * being called at completion of an operation. */
3255
Paul Elliott59ad9452022-12-18 15:09:02 +00003256 return status;
3257}
3258
Paul Elliott9fe12f62022-11-30 19:16:02 +00003259psa_status_t psa_sign_hash_start(
3260 psa_sign_hash_interruptible_operation_t *operation,
3261 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3262 const uint8_t *hash, size_t hash_length)
3263{
3264 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3265 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3266 psa_key_slot_t *slot;
3267
Paul Elliottc9774412023-02-06 15:14:07 +00003268 /* Check that start has not been previously called, or operation has not
3269 * previously errored. */
3270 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003271 return PSA_ERROR_BAD_STATE;
3272 }
3273
Paul Elliott9fe12f62022-11-30 19:16:02 +00003274 status = psa_sign_verify_check_alg(0, alg);
3275 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003276 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003277 return status;
3278 }
3279
3280 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3281 PSA_KEY_USAGE_SIGN_HASH,
3282 alg);
3283
3284 if (status != PSA_SUCCESS) {
3285 goto exit;
3286 }
3287
3288 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3289 status = PSA_ERROR_INVALID_ARGUMENT;
3290 goto exit;
3291 }
3292
3293 psa_key_attributes_t attributes = {
3294 .core = slot->attr
3295 };
3296
Paul Elliott296ede92022-12-15 17:00:30 +00003297 /* Ensure ops count gets reset, in case of operation re-use. */
3298 operation->num_ops = 0;
3299
Paul Elliott9fe12f62022-11-30 19:16:02 +00003300 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3301 slot->key.data,
3302 slot->key.bytes, alg,
3303 hash, hash_length);
3304exit:
3305
3306 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003307 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003308 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003309 }
3310
3311 unlock_status = psa_unlock_key_slot(slot);
3312
Paul Elliottc9774412023-02-06 15:14:07 +00003313 if (unlock_status != PSA_SUCCESS) {
3314 operation->error_occurred = 1;
3315 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003316
Paul Elliottc9774412023-02-06 15:14:07 +00003317 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003318}
3319
3320
3321psa_status_t psa_sign_hash_complete(
3322 psa_sign_hash_interruptible_operation_t *operation,
3323 uint8_t *signature, size_t signature_size,
3324 size_t *signature_length)
3325{
3326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3327
3328 *signature_length = 0;
3329
Paul Elliottc9774412023-02-06 15:14:07 +00003330 /* Check that start has been called first, and that operation has not
3331 * previously errored. */
3332 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003333 status = PSA_ERROR_BAD_STATE;
3334 goto exit;
3335 }
3336
Paul Elliott096abc42023-02-03 18:33:23 +00003337 /* Immediately reject a zero-length signature buffer. This guarantees that
3338 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003339 if (signature_size == 0) {
3340 status = PSA_ERROR_BUFFER_TOO_SMALL;
3341 goto exit;
3342 }
3343
3344 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3345 signature_size,
3346 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003347
Paul Elliott296ede92022-12-15 17:00:30 +00003348 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003349 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003350
Paul Elliottebe225c2023-02-10 14:32:53 +00003351exit:
3352
Paul Elliott59200a22023-02-21 15:07:40 +00003353 psa_wipe_tag_output_buffer(signature, status, signature_size,
3354 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003355
Paul Elliott53bb3122023-02-10 14:22:22 +00003356 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003357 if (status != PSA_SUCCESS) {
3358 operation->error_occurred = 1;
3359 }
3360
Paul Elliott59ad9452022-12-18 15:09:02 +00003361 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003362 }
3363
3364 return status;
3365}
3366
3367psa_status_t psa_sign_hash_abort(
3368 psa_sign_hash_interruptible_operation_t *operation)
3369{
Paul Elliott59ad9452022-12-18 15:09:02 +00003370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3371
3372 status = psa_sign_hash_abort_internal(operation);
3373
3374 /* We clear the number of ops done here, so that it is not cleared when
3375 * the operation fails or succeeds, only on manual abort. */
3376 operation->num_ops = 0;
3377
Paul Elliottc9774412023-02-06 15:14:07 +00003378 /* Likewise, failure state. */
3379 operation->error_occurred = 0;
3380
Paul Elliott59ad9452022-12-18 15:09:02 +00003381 return status;
3382}
3383
3384static psa_status_t psa_verify_hash_abort_internal(
3385 psa_verify_hash_interruptible_operation_t *operation)
3386{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003387 if (operation->id == 0) {
3388 /* The object has (apparently) been initialized but it is not (yet)
3389 * in use. It's ok to call abort on such an object, and there's
3390 * nothing to do. */
3391 return PSA_SUCCESS;
3392 }
3393
Paul Elliott59ad9452022-12-18 15:09:02 +00003394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3395
3396 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003397
3398 operation->id = 0;
3399
Paul Elliotte6145dc2023-02-07 12:51:21 +00003400 /* Do not clear either the error_occurred or num_ops elements here as they
3401 * only want to be cleared by the application calling abort, not by abort
3402 * being called at completion of an operation. */
3403
Paul Elliott59ad9452022-12-18 15:09:02 +00003404 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003405}
3406
3407psa_status_t psa_verify_hash_start(
3408 psa_verify_hash_interruptible_operation_t *operation,
3409 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3410 const uint8_t *hash, size_t hash_length,
3411 const uint8_t *signature, size_t signature_length)
3412{
3413 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3414 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3415 psa_key_slot_t *slot;
3416
Paul Elliottc9774412023-02-06 15:14:07 +00003417 /* Check that start has not been previously called, or operation has not
3418 * previously errored. */
3419 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420 return PSA_ERROR_BAD_STATE;
3421 }
3422
3423 status = psa_sign_verify_check_alg(0, alg);
3424 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003425 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003426 return status;
3427 }
3428
3429 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3430 PSA_KEY_USAGE_VERIFY_HASH,
3431 alg);
3432
3433 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003434 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003435 return status;
3436 }
3437
3438 psa_key_attributes_t attributes = {
3439 .core = slot->attr
3440 };
3441
Paul Elliott296ede92022-12-15 17:00:30 +00003442 /* Ensure ops count gets reset, in case of operation re-use. */
3443 operation->num_ops = 0;
3444
Paul Elliott9fe12f62022-11-30 19:16:02 +00003445 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3446 slot->key.data,
3447 slot->key.bytes,
3448 alg, hash, hash_length,
3449 signature, signature_length);
3450
3451 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003452 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003453 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003454 }
3455
3456 unlock_status = psa_unlock_key_slot(slot);
3457
Paul Elliottc9774412023-02-06 15:14:07 +00003458 if (unlock_status != PSA_SUCCESS) {
3459 operation->error_occurred = 1;
3460 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461
Paul Elliottc9774412023-02-06 15:14:07 +00003462 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003463}
3464
3465psa_status_t psa_verify_hash_complete(
3466 psa_verify_hash_interruptible_operation_t *operation)
3467{
3468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3469
Paul Elliottc9774412023-02-06 15:14:07 +00003470 /* Check that start has been called first, and that operation has not
3471 * previously errored. */
3472 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003473 status = PSA_ERROR_BAD_STATE;
3474 goto exit;
3475 }
3476
3477 status = psa_driver_wrapper_verify_hash_complete(operation);
3478
Paul Elliott296ede92022-12-15 17:00:30 +00003479 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003480 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003481 operation);
3482
Paul Elliottebe225c2023-02-10 14:32:53 +00003483exit:
3484
Paul Elliott9fe12f62022-11-30 19:16:02 +00003485 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003486 if (status != PSA_SUCCESS) {
3487 operation->error_occurred = 1;
3488 }
3489
Paul Elliott59ad9452022-12-18 15:09:02 +00003490 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003491 }
3492
3493 return status;
3494}
3495
3496psa_status_t psa_verify_hash_abort(
3497 psa_verify_hash_interruptible_operation_t *operation)
3498{
Paul Elliott59ad9452022-12-18 15:09:02 +00003499 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003500
Paul Elliott59ad9452022-12-18 15:09:02 +00003501 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003502
Paul Elliott59ad9452022-12-18 15:09:02 +00003503 /* We clear the number of ops done here, so that it is not cleared when
3504 * the operation fails or succeeds, only on manual abort. */
3505 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003506
Paul Elliottc9774412023-02-06 15:14:07 +00003507 /* Likewise, failure state. */
3508 operation->error_occurred = 0;
3509
Paul Elliott59ad9452022-12-18 15:09:02 +00003510 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003511}
3512
Paul Elliott588f8ed2022-12-02 18:10:26 +00003513/****************************************************************/
3514/* Asymmetric interruptible cryptography internal */
3515/* implementations */
3516/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003517
Paul Elliott588f8ed2022-12-02 18:10:26 +00003518void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3519{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003520
Paul Elliott588f8ed2022-12-02 18:10:26 +00003521#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3522 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3523 defined(MBEDTLS_ECP_RESTARTABLE)
3524
3525 /* Internal implementation uses zero to indicate infinite number max ops,
3526 * therefore avoid this value, and set to minimum possible. */
3527 if (max_ops == 0) {
3528 max_ops = 1;
3529 }
3530
Paul Elliott588f8ed2022-12-02 18:10:26 +00003531 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003532#else
3533 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003534#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3535 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3536 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3537}
3538
Paul Elliott588f8ed2022-12-02 18:10:26 +00003539uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003540 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003541{
3542#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3543 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3544 defined(MBEDTLS_ECP_RESTARTABLE)
3545
Paul Elliott93d9ca82023-02-15 18:14:21 +00003546 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003547#else
3548 (void) operation;
3549 return 0;
3550#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3551 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3552 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3553}
3554
3555uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003556 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003557{
3558 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3559 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3560 defined(MBEDTLS_ECP_RESTARTABLE)
3561
Paul Elliott93d9ca82023-02-15 18:14:21 +00003562 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003563#else
3564 (void) operation;
3565 return 0;
3566#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3567 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3568 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3569}
3570
3571psa_status_t mbedtls_psa_sign_hash_start(
3572 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3573 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3574 size_t key_buffer_size, psa_algorithm_t alg,
3575 const uint8_t *hash, size_t hash_length)
3576{
3577 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003578 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003579
Paul Elliott068fe072023-01-16 13:59:15 +00003580 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3581 return PSA_ERROR_NOT_SUPPORTED;
3582 }
3583
3584 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003585 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003586 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587
3588#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003589 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3590 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003591
Paul Elliotta1c94092023-02-15 16:38:04 +00003592 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3593
Paul Elliott93d9ca82023-02-15 18:14:21 +00003594 /* Ensure num_ops is zero'ed in case of context re-use. */
3595 operation->num_ops = 0;
3596
Paul Elliott068fe072023-01-16 13:59:15 +00003597 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3598 attributes->core.bits,
3599 key_buffer,
3600 key_buffer_size,
3601 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602
Paul Elliott068fe072023-01-16 13:59:15 +00003603 if (status != PSA_SUCCESS) {
3604 return status;
3605 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003606
Paul Elliott1bc59df2023-02-05 13:41:57 +00003607 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003608 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609
Paul Elliott068fe072023-01-16 13:59:15 +00003610 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3611 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3612 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003613
Paul Elliott0290a762023-02-09 14:30:24 +00003614 /* We only need to store the same length of hash as the private key size
3615 * here, it would be truncated by the internal implementation anyway. */
3616 required_hash_length = (hash_length < operation->coordinate_bytes ?
3617 hash_length : operation->coordinate_bytes);
3618
Paul Elliottba70ad42023-02-15 18:23:53 +00003619 if (required_hash_length > sizeof(operation->hash)) {
3620 /* Shouldn't happen, but better safe than sorry. */
3621 return PSA_ERROR_CORRUPTION_DETECTED;
3622 }
3623
Paul Elliott0290a762023-02-09 14:30:24 +00003624 memcpy(operation->hash, hash, required_hash_length);
3625 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003626
3627 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003628
3629#else
Paul Elliott068fe072023-01-16 13:59:15 +00003630 (void) operation;
3631 (void) key_buffer;
3632 (void) key_buffer_size;
3633 (void) alg;
3634 (void) hash;
3635 (void) hash_length;
3636 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003637 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638
Paul Elliott068fe072023-01-16 13:59:15 +00003639 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3641 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3642 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643}
3644
3645psa_status_t mbedtls_psa_sign_hash_complete(
3646 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3647 uint8_t *signature, size_t signature_size,
3648 size_t *signature_length)
3649{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003650#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3651 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3652 defined(MBEDTLS_ECP_RESTARTABLE)
3653
Paul Elliotta1c94092023-02-15 16:38:04 +00003654 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003655 mbedtls_mpi r;
3656 mbedtls_mpi s;
3657
Paul Elliott46845252023-02-03 14:59:11 +00003658 mbedtls_mpi_init(&r);
3659 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003661 /* Ensure max_ops is set to the current value (or default). */
3662 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3663
Paul Elliotta1c94092023-02-15 16:38:04 +00003664 if (signature_size < 2 * operation->coordinate_bytes) {
3665 status = PSA_ERROR_BUFFER_TOO_SMALL;
3666 goto exit;
3667 }
3668
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003670
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3672 status = mbedtls_to_psa_error(
3673 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003674 &r,
3675 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676 &operation->ctx->d,
3677 operation->hash,
3678 operation->hash_length,
3679 operation->md_alg,
3680 mbedtls_psa_get_random,
3681 MBEDTLS_PSA_RANDOM_STATE,
3682 &operation->restart_ctx));
3683#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003684 status = PSA_ERROR_NOT_SUPPORTED;
3685 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3687 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688 status = mbedtls_to_psa_error(
3689 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003690 &r,
3691 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692 &operation->ctx->d,
3693 operation->hash,
3694 operation->hash_length,
3695 mbedtls_psa_get_random,
3696 MBEDTLS_PSA_RANDOM_STATE,
3697 mbedtls_psa_get_random,
3698 MBEDTLS_PSA_RANDOM_STATE,
3699 &operation->restart_ctx));
3700 }
3701
Paul Elliottf8e5b562023-02-19 18:43:45 +00003702 /* Hide the fact that the restart context only holds a delta of number of
3703 * ops done during the last operation, not an absolute value. */
3704 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3705
Paul Elliott724bd252023-02-08 12:35:08 +00003706 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003707 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003708 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003710 operation->coordinate_bytes)
3711 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712
3713 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003714 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715 }
3716
3717 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003718 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003720 operation->coordinate_bytes,
3721 operation->coordinate_bytes)
3722 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003723
3724 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003725 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003726 }
3727
Paul Elliott1bc59df2023-02-05 13:41:57 +00003728 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729
Paul Elliott724bd252023-02-08 12:35:08 +00003730 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731 }
Paul Elliott724bd252023-02-08 12:35:08 +00003732
3733exit:
3734
3735 mbedtls_mpi_free(&r);
3736 mbedtls_mpi_free(&s);
3737 return status;
3738
Paul Elliott588f8ed2022-12-02 18:10:26 +00003739 #else
3740
3741 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003742 (void) signature;
3743 (void) signature_size;
3744 (void) signature_length;
3745
3746 return PSA_ERROR_NOT_SUPPORTED;
3747
3748#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3749 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3750 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3751}
3752
3753psa_status_t mbedtls_psa_sign_hash_abort(
3754 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3755{
3756
3757#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3758 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3759 defined(MBEDTLS_ECP_RESTARTABLE)
3760
3761 if (operation->ctx) {
3762 mbedtls_ecdsa_free(operation->ctx);
3763 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003764 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765 }
3766
3767 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3768
Paul Elliott93d9ca82023-02-15 18:14:21 +00003769 operation->num_ops = 0;
3770
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771 return PSA_SUCCESS;
3772
3773#else
3774
3775 (void) operation;
3776
3777 return PSA_ERROR_NOT_SUPPORTED;
3778
3779#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3780 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3781 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3782}
3783
3784psa_status_t mbedtls_psa_verify_hash_start(
3785 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3786 const psa_key_attributes_t *attributes,
3787 const uint8_t *key_buffer, size_t key_buffer_size,
3788 psa_algorithm_t alg,
3789 const uint8_t *hash, size_t hash_length,
3790 const uint8_t *signature, size_t signature_length)
3791{
3792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003793 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003794 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003795
Paul Elliott068fe072023-01-16 13:59:15 +00003796 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3797 return PSA_ERROR_NOT_SUPPORTED;
3798 }
3799
3800 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003801 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003802 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803
3804#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003805 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3806 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003807
Paul Elliotta1c94092023-02-15 16:38:04 +00003808 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3809 mbedtls_mpi_init(&operation->r);
3810 mbedtls_mpi_init(&operation->s);
3811
Paul Elliott93d9ca82023-02-15 18:14:21 +00003812 /* Ensure num_ops is zero'ed in case of context re-use. */
3813 operation->num_ops = 0;
3814
Paul Elliott068fe072023-01-16 13:59:15 +00003815 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3816 attributes->core.bits,
3817 key_buffer,
3818 key_buffer_size,
3819 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820
Paul Elliott068fe072023-01-16 13:59:15 +00003821 if (status != PSA_SUCCESS) {
3822 return status;
3823 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003824
Paul Elliottc569fc22023-02-10 13:02:54 +00003825 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826
Paul Elliott1bc59df2023-02-05 13:41:57 +00003827 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003828 return PSA_ERROR_INVALID_SIGNATURE;
3829 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003830
Paul Elliott068fe072023-01-16 13:59:15 +00003831 status = mbedtls_to_psa_error(
3832 mbedtls_mpi_read_binary(&operation->r,
3833 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003834 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835
Paul Elliott068fe072023-01-16 13:59:15 +00003836 if (status != PSA_SUCCESS) {
3837 return status;
3838 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839
Paul Elliott068fe072023-01-16 13:59:15 +00003840 status = mbedtls_to_psa_error(
3841 mbedtls_mpi_read_binary(&operation->s,
3842 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003843 coordinate_bytes,
3844 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845
Paul Elliott068fe072023-01-16 13:59:15 +00003846 if (status != PSA_SUCCESS) {
3847 return status;
3848 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849
Paul Elliott2c9843f2023-02-15 17:32:42 +00003850 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851
Paul Elliott2c9843f2023-02-15 17:32:42 +00003852 if (status != PSA_SUCCESS) {
3853 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003854 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003855
Paul Elliott0290a762023-02-09 14:30:24 +00003856 /* We only need to store the same length of hash as the private key size
3857 * here, it would be truncated by the internal implementation anyway. */
3858 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3859 coordinate_bytes);
3860
Paul Elliottba70ad42023-02-15 18:23:53 +00003861 if (required_hash_length > sizeof(operation->hash)) {
3862 /* Shouldn't happen, but better safe than sorry. */
3863 return PSA_ERROR_CORRUPTION_DETECTED;
3864 }
3865
Paul Elliott0290a762023-02-09 14:30:24 +00003866 memcpy(operation->hash, hash, required_hash_length);
3867 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003868
3869 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003870#else
Paul Elliott068fe072023-01-16 13:59:15 +00003871 (void) operation;
3872 (void) key_buffer;
3873 (void) key_buffer_size;
3874 (void) alg;
3875 (void) hash;
3876 (void) hash_length;
3877 (void) signature;
3878 (void) signature_length;
3879 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003880 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003881 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882
Paul Elliott068fe072023-01-16 13:59:15 +00003883 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3885 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3886 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003887}
3888
3889psa_status_t mbedtls_psa_verify_hash_complete(
3890 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3891{
3892
3893#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3894 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3895 defined(MBEDTLS_ECP_RESTARTABLE)
3896
Paul Elliottf8e5b562023-02-19 18:43:45 +00003897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3898
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003899 /* Ensure max_ops is set to the current value (or default). */
3900 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3901
Paul Elliottf8e5b562023-02-19 18:43:45 +00003902 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003903 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3904 operation->hash,
3905 operation->hash_length,
3906 &operation->ctx->Q,
3907 &operation->r,
3908 &operation->s,
3909 &operation->restart_ctx));
3910
Paul Elliottf8e5b562023-02-19 18:43:45 +00003911 /* Hide the fact that the restart context only holds a delta of number of
3912 * ops done during the last operation, not an absolute value. */
3913 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3914
3915 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003916#else
3917 (void) operation;
3918
3919 return PSA_ERROR_NOT_SUPPORTED;
3920
3921#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3922 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3923 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3924}
3925
3926psa_status_t mbedtls_psa_verify_hash_abort(
3927 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3928{
3929
3930#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3931 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3932 defined(MBEDTLS_ECP_RESTARTABLE)
3933
3934 if (operation->ctx) {
3935 mbedtls_ecdsa_free(operation->ctx);
3936 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003937 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003938 }
3939
3940 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3941
Paul Elliott93d9ca82023-02-15 18:14:21 +00003942 operation->num_ops = 0;
3943
Paul Elliott588f8ed2022-12-02 18:10:26 +00003944 mbedtls_mpi_free(&operation->r);
3945 mbedtls_mpi_free(&operation->s);
3946
3947 return PSA_SUCCESS;
3948
3949#else
3950 (void) operation;
3951
3952 return PSA_ERROR_NOT_SUPPORTED;
3953
3954#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3955 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3956 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3957}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003958
mohammad1603503973b2018-03-12 15:59:30 +02003959/****************************************************************/
3960/* Symmetric cryptography */
3961/****************************************************************/
3962
Gilles Peskine449bd832023-01-11 14:50:10 +01003963static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3964 mbedtls_svc_key_id_t key,
3965 psa_algorithm_t alg,
3966 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003967{
3968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3969 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003970 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3972 PSA_KEY_USAGE_ENCRYPT :
3973 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003974
3975 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003977 status = PSA_ERROR_BAD_STATE;
3978 goto exit;
3979 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003980
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003982 status = PSA_ERROR_INVALID_ARGUMENT;
3983 goto exit;
3984 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3987 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003988 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003990
Ronald Cron49fafa92021-03-10 08:34:23 +01003991 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003992 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003993 * so we only set it (in the driver wrapper) after resources have been
3994 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003995 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003997 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003999 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 }
4001 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004002
Ronald Crona4af55f2020-12-14 14:36:06 +01004003 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004005 };
4006
Ronald Cronab99ac22020-10-01 16:38:28 +02004007 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 if (cipher_operation == MBEDTLS_ENCRYPT) {
4009 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4010 &attributes,
4011 slot->key.data,
4012 slot->key.bytes,
4013 alg);
4014 } else {
4015 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4016 &attributes,
4017 slot->key.data,
4018 slot->key.bytes,
4019 alg);
4020 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004021
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004022exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 if (status != PSA_SUCCESS) {
4024 psa_cipher_abort(operation);
4025 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004028
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004030}
4031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4033 mbedtls_svc_key_id_t key,
4034 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004035{
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004037}
4038
Gilles Peskine449bd832023-01-11 14:50:10 +01004039psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4040 mbedtls_svc_key_id_t key,
4041 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004042{
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004044}
4045
Gilles Peskine449bd832023-01-11 14:50:10 +01004046psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4047 uint8_t *iv,
4048 size_t iv_size,
4049 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004050{
Ronald Cron6d051732020-10-01 14:10:20 +02004051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004052 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4053 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004054
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004056 status = PSA_ERROR_BAD_STATE;
4057 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004058 }
4059
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004061 status = PSA_ERROR_BAD_STATE;
4062 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004063 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004064
Ronald Cron2fb90522021-07-05 12:31:44 +02004065 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004067 status = PSA_ERROR_BUFFER_TOO_SMALL;
4068 goto exit;
4069 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004070
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004072 status = PSA_ERROR_GENERIC_ERROR;
4073 goto exit;
4074 }
4075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 status = psa_generate_random(local_iv, default_iv_length);
4077 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004078 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 }
Ronald Cron5618a392021-03-26 09:52:26 +01004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 status = psa_driver_wrapper_cipher_set_iv(operation,
4082 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004083
4084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 if (status == PSA_SUCCESS) {
4086 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004087 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004088 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004090 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004092 }
Ronald Cron6d051732020-10-01 14:10:20 +02004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004095}
4096
Gilles Peskine449bd832023-01-11 14:50:10 +01004097psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4098 const uint8_t *iv,
4099 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004100{
Ronald Cron6d051732020-10-01 14:10:20 +02004101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004102
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004104 status = PSA_ERROR_BAD_STATE;
4105 goto exit;
4106 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004107
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004109 status = PSA_ERROR_BAD_STATE;
4110 goto exit;
4111 }
Ronald Crona0d68172021-03-26 10:15:08 +01004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004114 status = PSA_ERROR_INVALID_ARGUMENT;
4115 goto exit;
4116 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 status = psa_driver_wrapper_cipher_set_iv(operation,
4119 iv,
4120 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004121
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004122exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004124 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 } else {
4126 psa_cipher_abort(operation);
4127 }
4128 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004129}
4130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4132 const uint8_t *input,
4133 size_t input_length,
4134 uint8_t *output,
4135 size_t output_size,
4136 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004137{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004141 status = PSA_ERROR_BAD_STATE;
4142 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004143 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004146 status = PSA_ERROR_BAD_STATE;
4147 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004148 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 status = psa_driver_wrapper_cipher_update(operation,
4151 input,
4152 input_length,
4153 output,
4154 output_size,
4155 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004156
4157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 if (status != PSA_SUCCESS) {
4159 psa_cipher_abort(operation);
4160 }
Ronald Cron6d051732020-10-01 14:10:20 +02004161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004163}
4164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4166 uint8_t *output,
4167 size_t output_size,
4168 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004169{
David Saadab4ecc272019-02-14 13:48:10 +02004170 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004173 status = PSA_ERROR_BAD_STATE;
4174 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004175 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004178 status = PSA_ERROR_BAD_STATE;
4179 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004180 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 status = psa_driver_wrapper_cipher_finish(operation,
4183 output,
4184 output_size,
4185 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004186
4187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 if (status == PSA_SUCCESS) {
4189 return psa_cipher_abort(operation);
4190 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004191 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004195 }
mohammad1603503973b2018-03-12 15:59:30 +02004196}
4197
Gilles Peskine449bd832023-01-11 14:50:10 +01004198psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004199{
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004201 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004202 * in use. It's ok to call abort on such an object, and there's
4203 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004205 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004208
Ronald Cron49fafa92021-03-10 08:34:23 +01004209 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004210 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004211 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004214}
4215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4217 psa_algorithm_t alg,
4218 const uint8_t *input,
4219 size_t input_length,
4220 uint8_t *output,
4221 size_t output_size,
4222 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004223{
4224 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004225 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004226 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004227 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4228 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004229
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004231 status = PSA_ERROR_INVALID_ARGUMENT;
4232 goto exit;
4233 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4236 PSA_KEY_USAGE_ENCRYPT,
4237 alg);
4238 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004239 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004241
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004242 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004244 };
4245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4247 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004248 status = PSA_ERROR_GENERIC_ERROR;
4249 goto exit;
4250 }
4251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 if (default_iv_length > 0) {
4253 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004254 status = PSA_ERROR_BUFFER_TOO_SMALL;
4255 goto exit;
4256 }
4257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 status = psa_generate_random(local_iv, default_iv_length);
4259 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004260 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004262 }
4263
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004264 status = psa_driver_wrapper_cipher_encrypt(
4265 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004266 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004267 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004269
4270exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 unlock_status = psa_unlock_key_slot(slot);
4272 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004273 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004274 }
Ronald Cron23919522021-07-08 19:00:07 +02004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 if (status == PSA_SUCCESS) {
4277 if (default_iv_length > 0) {
4278 memcpy(output, local_iv, default_iv_length);
4279 }
4280 *output_length += default_iv_length;
4281 } else {
4282 *output_length = 0;
4283 }
4284
4285 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004286}
4287
Gilles Peskine449bd832023-01-11 14:50:10 +01004288psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4289 psa_algorithm_t alg,
4290 const uint8_t *input,
4291 size_t input_length,
4292 uint8_t *output,
4293 size_t output_size,
4294 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004295{
4296 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004297 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004298 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004301 status = PSA_ERROR_INVALID_ARGUMENT;
4302 goto exit;
4303 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004304
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4306 PSA_KEY_USAGE_DECRYPT,
4307 alg);
4308 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004309 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004311
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004312 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004314 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4317 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004318 status = PSA_ERROR_INVALID_ARGUMENT;
4319 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004321 status = PSA_ERROR_INVALID_ARGUMENT;
4322 goto exit;
4323 }
4324
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004325 status = psa_driver_wrapper_cipher_decrypt(
4326 &attributes, slot->key.data, slot->key.bytes,
4327 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004329
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004330exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 unlock_status = psa_unlock_key_slot(slot);
4332 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004333 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004337 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 }
Ronald Cron23919522021-07-08 19:00:07 +02004339
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004341}
4342
4343
mohammad16035955c982018-04-26 00:53:03 +03004344/****************************************************************/
4345/* AEAD */
4346/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004347
Paul Elliottbaff51c2021-09-28 17:44:45 +01004348/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004349static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004350{
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004352}
4353
4354/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004355static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4356 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004357{
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004361#if defined(PSA_WANT_ALG_GCM)
4362 case PSA_ALG_GCM:
4363 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 * arbitrarily large nonces. Please note that we do not generally
4365 * recommend the usage of nonces of greater length than
4366 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4367 * size, which can then lead to collisions if you encrypt a very
4368 * large number of messages.*/
4369 if (nonce_length != 0) {
4370 return PSA_SUCCESS;
4371 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004372 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004373#endif /* PSA_WANT_ALG_GCM */
4374#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004375 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 if (nonce_length >= 7 && nonce_length <= 13) {
4377 return PSA_SUCCESS;
4378 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004379 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004380#endif /* PSA_WANT_ALG_CCM */
4381#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004382 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 if (nonce_length == 12) {
4384 return PSA_SUCCESS;
4385 } else if (nonce_length == 8) {
4386 return PSA_ERROR_NOT_SUPPORTED;
4387 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004388 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004389#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004390 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004391 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004393 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004396}
4397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004399{
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4401 return PSA_ERROR_INVALID_ARGUMENT;
4402 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004403
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004405}
4406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4408 psa_algorithm_t alg,
4409 const uint8_t *nonce,
4410 size_t nonce_length,
4411 const uint8_t *additional_data,
4412 size_t additional_data_length,
4413 const uint8_t *plaintext,
4414 size_t plaintext_length,
4415 uint8_t *ciphertext,
4416 size_t ciphertext_size,
4417 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004418{
Ronald Cron215633c2021-03-16 17:15:37 +01004419 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4420 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004421
mohammad1603f08a5502018-06-03 15:05:47 +03004422 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 status = psa_aead_check_algorithm(alg);
4425 if (status != PSA_SUCCESS) {
4426 return status;
4427 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004428
Ronald Cron9a986162021-03-26 12:40:07 +01004429 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4431 if (status != PSA_SUCCESS) {
4432 return status;
4433 }
mohammad16035955c982018-04-26 00:53:03 +03004434
Ronald Cron215633c2021-03-16 17:15:37 +01004435 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004437 };
mohammad16035955c982018-04-26 00:53:03 +03004438
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 status = psa_aead_check_nonce_length(alg, nonce_length);
4440 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004441 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004443
Ronald Cronde822812021-03-17 16:08:20 +01004444 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004445 &attributes, slot->key.data, slot->key.bytes,
4446 alg,
4447 nonce, nonce_length,
4448 additional_data, additional_data_length,
4449 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004451
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4453 memset(ciphertext, 0, ciphertext_size);
4454 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004455
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004460}
4461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4463 psa_algorithm_t alg,
4464 const uint8_t *nonce,
4465 size_t nonce_length,
4466 const uint8_t *additional_data,
4467 size_t additional_data_length,
4468 const uint8_t *ciphertext,
4469 size_t ciphertext_length,
4470 uint8_t *plaintext,
4471 size_t plaintext_size,
4472 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004473{
Ronald Cron215633c2021-03-16 17:15:37 +01004474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4475 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004476
Gilles Peskineee652a32018-06-01 19:23:52 +02004477 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004478
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 status = psa_aead_check_algorithm(alg);
4480 if (status != PSA_SUCCESS) {
4481 return status;
4482 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004483
Ronald Cron9a986162021-03-26 12:40:07 +01004484 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4486 if (status != PSA_SUCCESS) {
4487 return status;
4488 }
mohammad16035955c982018-04-26 00:53:03 +03004489
Ronald Cron215633c2021-03-16 17:15:37 +01004490 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004492 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004493
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 status = psa_aead_check_nonce_length(alg, nonce_length);
4495 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004496 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004498
Ronald Cronde822812021-03-17 16:08:20 +01004499 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004500 &attributes, slot->key.data, slot->key.bytes,
4501 alg,
4502 nonce, nonce_length,
4503 additional_data, additional_data_length,
4504 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004506
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 if (status != PSA_SUCCESS && plaintext_size != 0) {
4508 memset(plaintext, 0, plaintext_size);
4509 }
mohammad160360a64d02018-06-03 17:20:42 +03004510
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004511exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 return status;
mohammad16035955c982018-04-26 00:53:03 +03004515}
4516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4518{
4519 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004522#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004524 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4526 return PSA_ERROR_INVALID_ARGUMENT;
4527 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004528 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004529#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004530
Przemek Stekiel86679c72022-10-06 17:06:56 +02004531#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004533 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4535 return PSA_ERROR_INVALID_ARGUMENT;
4536 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004537 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004538#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004539
Przemek Stekiel86679c72022-10-06 17:06:56 +02004540#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004542 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 if (tag_len != 16) {
4544 return PSA_ERROR_INVALID_ARGUMENT;
4545 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004546 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004547#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004548
4549 default:
4550 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004552 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004554}
4555
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004556/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004557static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4558 int is_encrypt,
4559 mbedtls_svc_key_id_t key,
4560 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004561{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4563 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4564 psa_key_slot_t *slot = NULL;
4565 psa_key_usage_t key_usage = 0;
4566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 status = psa_aead_check_algorithm(alg);
4568 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004569 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004573 status = PSA_ERROR_BAD_STATE;
4574 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004575 }
4576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 if (operation->nonce_set || operation->lengths_set ||
4578 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004579 status = PSA_ERROR_BAD_STATE;
4580 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004581 }
4582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004584 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004586 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4590 alg);
4591 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004592 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004594
4595 psa_key_attributes_t attributes = {
4596 .core = slot->attr
4597 };
4598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004600 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 if (is_encrypt) {
4604 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4605 &attributes,
4606 slot->key.data,
4607 slot->key.bytes,
4608 alg);
4609 } else {
4610 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4611 &attributes,
4612 slot->key.data,
4613 slot->key.bytes,
4614 alg);
4615 }
4616 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004617 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004621
4622exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004626 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004628 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 } else {
4630 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004631 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004634}
4635
Paul Elliott302ff6b2021-04-20 18:10:30 +01004636/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004637psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4638 mbedtls_svc_key_id_t key,
4639 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004640{
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004642}
4643
4644/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004645psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4646 mbedtls_svc_key_id_t key,
4647 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004648{
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004650}
4651
4652/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004653psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4654 uint8_t *nonce,
4655 size_t nonce_size,
4656 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004657{
4658 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004659 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004660 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004661
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004662 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004665 status = PSA_ERROR_BAD_STATE;
4666 goto exit;
4667 }
4668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004670 status = PSA_ERROR_BAD_STATE;
4671 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004672 }
4673
Paul Elliotte193ea82021-10-01 13:00:16 +01004674 /* For CCM, this size may not be correct according to the PSA
4675 * specification. The PSA Crypto 1.0.1 specification states:
4676 *
4677 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4678 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4679 *
4680 * However this restriction that L has to be the smallest integer is not
4681 * applied in practice, and it is not implementable here since the
4682 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4684 operation->alg);
4685 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004686 status = PSA_ERROR_BUFFER_TOO_SMALL;
4687 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004688 }
4689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 status = psa_generate_random(local_nonce, required_nonce_size);
4691 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004692 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004696
Paul Elliott39dc6b82021-05-11 19:16:09 +01004697exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (status == PSA_SUCCESS) {
4699 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004700 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 } else {
4702 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004703 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004706}
4707
4708/* Set the nonce for a multipart authenticated encryption or decryption
4709 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004710psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4711 const uint8_t *nonce,
4712 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004713{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004714 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004717 status = PSA_ERROR_BAD_STATE;
4718 goto exit;
4719 }
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004722 status = PSA_ERROR_BAD_STATE;
4723 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004724 }
4725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4727 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004728 status = PSA_ERROR_INVALID_ARGUMENT;
4729 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004730 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4733 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004734
Paul Elliott39dc6b82021-05-11 19:16:09 +01004735exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004737 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 } else {
4739 psa_aead_abort(operation);
4740 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004743}
4744
4745/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004746psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4747 size_t ad_length,
4748 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004749{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004750 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004753 status = PSA_ERROR_BAD_STATE;
4754 goto exit;
4755 }
4756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 if (operation->lengths_set || operation->ad_started ||
4758 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004759 status = PSA_ERROR_BAD_STATE;
4760 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004761 }
4762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004764#if defined(PSA_WANT_ALG_GCM)
4765 case PSA_ALG_GCM:
4766 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 * bits. Without the guard this code will generate warnings on 32bit
4768 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004769#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 if (((uint64_t) ad_length) >> 61 != 0 ||
4771 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004772 status = PSA_ERROR_INVALID_ARGUMENT;
4773 goto exit;
4774 }
Paul Elliott325d3742021-09-27 17:56:28 +01004775#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004776 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004777#endif /* PSA_WANT_ALG_GCM */
4778#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004779 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004781 status = PSA_ERROR_INVALID_ARGUMENT;
4782 goto exit;
4783 }
4784 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004785#endif /* PSA_WANT_ALG_CCM */
4786#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004787 case PSA_ALG_CHACHA20_POLY1305:
4788 /* No length restrictions for ChaChaPoly. */
4789 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004790#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004791 default:
4792 break;
4793 }
Paul Elliott325d3742021-09-27 17:56:28 +01004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4796 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004797
Paul Elliott39dc6b82021-05-11 19:16:09 +01004798exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004800 operation->ad_remaining = ad_length;
4801 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004802 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 } else {
4804 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004805 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004808}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004809
4810/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004811psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4812 const uint8_t *input,
4813 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004814{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004818 status = PSA_ERROR_BAD_STATE;
4819 goto exit;
4820 }
4821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004823 status = PSA_ERROR_BAD_STATE;
4824 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004825 }
4826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (operation->lengths_set) {
4828 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004829 status = PSA_ERROR_INVALID_ARGUMENT;
4830 goto exit;
4831 }
4832
4833 operation->ad_remaining -= input_length;
4834 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004835#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004837 status = PSA_ERROR_BAD_STATE;
4838 goto exit;
4839 }
4840#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 status = psa_driver_wrapper_aead_update_ad(operation, input,
4843 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004844
Paul Elliott39dc6b82021-05-11 19:16:09 +01004845exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004847 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 } else {
4849 psa_aead_abort(operation);
4850 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004853}
4854
4855/* Encrypt or decrypt a message fragment in an active multipart AEAD
4856 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004857psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4858 const uint8_t *input,
4859 size_t input_length,
4860 uint8_t *output,
4861 size_t output_size,
4862 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004863{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004864 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004865
4866 *output_length = 0;
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004869 status = PSA_ERROR_BAD_STATE;
4870 goto exit;
4871 }
4872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004874 status = PSA_ERROR_BAD_STATE;
4875 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004876 }
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004879 /* Additional data length was supplied, but not all the additional
4880 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004882 status = PSA_ERROR_INVALID_ARGUMENT;
4883 goto exit;
4884 }
4885
4886 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004888 status = PSA_ERROR_INVALID_ARGUMENT;
4889 goto exit;
4890 }
4891
4892 operation->body_remaining -= input_length;
4893 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004894#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004896 status = PSA_ERROR_BAD_STATE;
4897 goto exit;
4898 }
4899#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004900
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4902 output, output_size,
4903 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004904
Paul Elliott39dc6b82021-05-11 19:16:09 +01004905exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004907 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 } else {
4909 psa_aead_abort(operation);
4910 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004913}
4914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004916{
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 if (operation->id == 0 || !operation->nonce_set) {
4918 return PSA_ERROR_BAD_STATE;
4919 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004920
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4922 operation->body_remaining != 0)) {
4923 return PSA_ERROR_INVALID_ARGUMENT;
4924 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004927}
4928
Paul Elliott302ff6b2021-04-20 18:10:30 +01004929/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004930psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4931 uint8_t *ciphertext,
4932 size_t ciphertext_size,
4933 size_t *ciphertext_length,
4934 uint8_t *tag,
4935 size_t tag_size,
4936 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004937{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004938 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4939
Paul Elliott302ff6b2021-04-20 18:10:30 +01004940 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004941 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 status = psa_aead_final_checks(operation);
4944 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004945 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004949 status = PSA_ERROR_BAD_STATE;
4950 goto exit;
4951 }
4952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4954 ciphertext_size,
4955 ciphertext_length,
4956 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004957
Paul Elliott39dc6b82021-05-11 19:16:09 +01004958exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004959
4960
Paul Elliottf47b0952021-05-21 18:02:33 +01004961 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004962 * the zero tag size, make sure the tag is set to something implausible.
4963 * Even if the operation succeeds, make sure we clear the rest of the
4964 * buffer to prevent potential leakage of anything previously placed in
4965 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004966 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004971}
4972
4973/* Finish authenticating and decrypting a message in a multipart AEAD
4974 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004975psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4976 uint8_t *plaintext,
4977 size_t plaintext_size,
4978 size_t *plaintext_length,
4979 const uint8_t *tag,
4980 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004982 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4983
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984 *plaintext_length = 0;
4985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 status = psa_aead_final_checks(operation);
4987 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004988 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004992 status = PSA_ERROR_BAD_STATE;
4993 goto exit;
4994 }
4995
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4997 plaintext_size,
4998 plaintext_length,
4999 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005000
5001exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005005}
5006
5007/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005008psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005009{
5010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005013 /* The object has (apparently) been initialized but it is not (yet)
5014 * in use. It's ok to call abort on such an object, and there's
5015 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005017 }
5018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005022
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005024}
5025
Gilles Peskinee59236f2018-01-27 23:32:46 +01005026/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005027/* Generators */
5028/****************************************************************/
5029
Przemek Stekiel69c46792022-06-10 12:59:51 +02005030#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005031 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005032 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5033 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005034#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005035#endif /* At least one builtin KDF */
5036
Przemek Stekiel69c46792022-06-10 12:59:51 +02005037#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005038 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5039 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5040static psa_status_t psa_key_derivation_start_hmac(
5041 psa_mac_operation_t *operation,
5042 psa_algorithm_t hash_alg,
5043 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005045{
5046 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5049 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5050 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005051
Steven Cooreman72f736a2021-05-07 14:14:37 +02005052 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 status = psa_driver_wrapper_mac_sign_setup(operation,
5056 &attributes,
5057 hmac_key, hmac_key_length,
5058 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005059
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 psa_reset_key_attributes(&attributes);
5061 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005062}
5063#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005064
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005065#define HKDF_STATE_INIT 0 /* no input yet */
5066#define HKDF_STATE_STARTED 1 /* got salt */
5067#define HKDF_STATE_KEYED 2 /* got key */
5068#define HKDF_STATE_OUTPUT 3 /* output started */
5069
Gilles Peskinecbe66502019-05-16 16:59:18 +02005070static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005072{
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5074 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5075 } else {
5076 return operation->alg;
5077 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005078}
5079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005081{
5082 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5084 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005085 /* The object has (apparently) been initialized but it is not
5086 * in use. It's ok to call abort on such an object, and there's
5087 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005089#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5091 mbedtls_free(operation->ctx.hkdf.info);
5092 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5093 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005094#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005095#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5096 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5098 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5099 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5100 if (operation->ctx.tls12_prf.secret != NULL) {
5101 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5102 operation->ctx.tls12_prf.secret_length);
5103 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005104 }
5105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 if (operation->ctx.tls12_prf.seed != NULL) {
5107 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5108 operation->ctx.tls12_prf.seed_length);
5109 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005110 }
5111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 if (operation->ctx.tls12_prf.label != NULL) {
5113 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5114 operation->ctx.tls12_prf.label_length);
5115 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005116 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005117#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 if (operation->ctx.tls12_prf.other_secret != NULL) {
5119 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5120 operation->ctx.tls12_prf.other_secret_length);
5121 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005122 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005123#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005124 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005125
5126 /* We leave the fields Ai and output_block to be erased safely by the
5127 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005129#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5130 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005131#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5133 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5134 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5135 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005136#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005137 {
5138 status = PSA_ERROR_BAD_STATE;
5139 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 mbedtls_platform_zeroize(operation, sizeof(*operation));
5141 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005142}
5143
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005146{
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005150 }
5151
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005152 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005154}
5155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5157 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005158{
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 if (operation->alg == 0) {
5160 return PSA_ERROR_BAD_STATE;
5161 }
5162 if (capacity > operation->capacity) {
5163 return PSA_ERROR_INVALID_ARGUMENT;
5164 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005165 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005167}
5168
Przemek Stekiel69c46792022-06-10 12:59:51 +02005169#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005170/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005171static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5172 psa_algorithm_t kdf_alg,
5173 uint8_t *output,
5174 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005175{
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5177 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005178 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005179 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005180#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005182#else
5183 const uint8_t last_block = 0xff;
5184#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005185
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 if (hkdf->state < HKDF_STATE_KEYED ||
5187 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005188#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005190#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 )) {
5192 return PSA_ERROR_BAD_STATE;
5193 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194 hkdf->state = HKDF_STATE_OUTPUT;
5195
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005197 /* Copy what remains of the current block */
5198 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005200 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 }
5202 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005203 output += n;
5204 output_length -= n;
5205 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005207 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005209 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005210 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005212 * object was corrupted or if this function is called directly
5213 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 if (hkdf->block_number == last_block) {
5215 return PSA_ERROR_BAD_STATE;
5216 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005217
5218 /* We need a new block */
5219 ++hkdf->block_number;
5220 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5223 hash_alg,
5224 hkdf->prk,
5225 hash_length);
5226 if (status != PSA_SUCCESS) {
5227 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005228 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005229
5230 if (hkdf->block_number != 1) {
5231 status = psa_mac_update(&hkdf->hmac,
5232 hkdf->output_block,
5233 hash_length);
5234 if (status != PSA_SUCCESS) {
5235 return status;
5236 }
5237 }
5238 status = psa_mac_update(&hkdf->hmac,
5239 hkdf->info,
5240 hkdf->info_length);
5241 if (status != PSA_SUCCESS) {
5242 return status;
5243 }
5244 status = psa_mac_update(&hkdf->hmac,
5245 &hkdf->block_number, 1);
5246 if (status != PSA_SUCCESS) {
5247 return status;
5248 }
5249 status = psa_mac_sign_finish(&hkdf->hmac,
5250 hkdf->output_block,
5251 sizeof(hkdf->output_block),
5252 &hmac_output_length);
5253 if (status != PSA_SUCCESS) {
5254 return status;
5255 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005256 }
5257
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005259}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005260#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005261
John Durkop07cc04a2020-11-16 22:08:34 -08005262#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5263 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005264static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5265 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005267{
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5269 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005270 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5271 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005272 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005273
Janos Follath7742fee2019-06-17 12:58:10 +01005274 /* We can't be wanting more output after block 0xff, otherwise
5275 * the capacity check in psa_key_derivation_output_bytes() would have
5276 * prevented this call. It could happen only if the operation
5277 * object was corrupted or if this function is called directly
5278 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (tls12_prf->block_number == 0xff) {
5280 return PSA_ERROR_CORRUPTION_DETECTED;
5281 }
Janos Follath7742fee2019-06-17 12:58:10 +01005282
5283 /* We need a new block */
5284 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005285 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005286
5287 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5288 *
5289 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5290 *
5291 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5292 * HMAC_hash(secret, A(2) + seed) +
5293 * HMAC_hash(secret, A(3) + seed) + ...
5294 *
5295 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005296 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005297 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005298 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005299 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005300 * is currently extracted as `output_block` and where i is
5301 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005302 */
5303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 status = psa_key_derivation_start_hmac(&hmac,
5305 hash_alg,
5306 tls12_prf->secret,
5307 tls12_prf->secret_length);
5308 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005309 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005311
5312 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005314 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5315 * the variable seed and in this instance means it in the context of the
5316 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 status = psa_mac_update(&hmac,
5318 tls12_prf->label,
5319 tls12_prf->label_length);
5320 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005321 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 }
5323 status = psa_mac_update(&hmac,
5324 tls12_prf->seed,
5325 tls12_prf->seed_length);
5326 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005327 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 }
5329 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005330 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5332 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005333 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005335 }
5336
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 status = psa_mac_sign_finish(&hmac,
5338 tls12_prf->Ai, hash_length,
5339 &hmac_output_length);
5340 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005341 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 }
5343 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005344 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005346
5347 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 status = psa_key_derivation_start_hmac(&hmac,
5349 hash_alg,
5350 tls12_prf->secret,
5351 tls12_prf->secret_length);
5352 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005353 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 }
5355 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5356 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005357 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 }
5359 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5360 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005361 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 }
5363 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5364 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005365 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 }
5367 status = psa_mac_sign_finish(&hmac,
5368 tls12_prf->output_block, hash_length,
5369 &hmac_output_length);
5370 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005371 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005373
Janos Follath7742fee2019-06-17 12:58:10 +01005374
5375cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 cleanup_status = psa_mac_abort(&hmac);
5377 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005378 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005382}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005383
Janos Follath844eb0e2019-06-19 12:10:49 +01005384static psa_status_t psa_key_derivation_tls12_prf_read(
5385 psa_tls12_prf_key_derivation_t *tls12_prf,
5386 psa_algorithm_t alg,
5387 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005389{
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5391 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005392 psa_status_t status;
5393 uint8_t offset, length;
5394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005396 case PSA_TLS12_PRF_STATE_LABEL_SET:
5397 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5398 break;
5399 case PSA_TLS12_PRF_STATE_OUTPUT:
5400 break;
5401 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005403 }
5404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005406 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 if (tls12_prf->left_in_block == 0) {
5408 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5409 alg);
5410 if (status != PSA_SUCCESS) {
5411 return status;
5412 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005413
5414 continue;
5415 }
5416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005418 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005420 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005422
5423 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005425 output += length;
5426 output_length -= length;
5427 tls12_prf->left_in_block -= length;
5428 }
5429
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005431}
John Durkop07cc04a2020-11-16 22:08:34 -08005432#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5433 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005434
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005435#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5436static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5437 psa_tls12_ecjpake_to_pms_t *ecjpake,
5438 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005440{
5441 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005442 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 if (output_length != 32) {
5445 return PSA_ERROR_INVALID_ARGUMENT;
5446 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5449 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5450 &output_size);
5451 if (status != PSA_SUCCESS) {
5452 return status;
5453 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 if (output_size != output_length) {
5456 return PSA_ERROR_GENERIC_ERROR;
5457 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005458
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005460}
5461#endif
5462
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005463psa_status_t psa_key_derivation_output_bytes(
5464 psa_key_derivation_operation_t *operation,
5465 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005467{
5468 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005470
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005472 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005474 }
5475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005477 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005478 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005479 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005480 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005481 goto exit;
5482 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005484 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005485 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005486 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5487 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005489 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005491 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005492 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005493
Przemek Stekiel69c46792022-06-10 12:59:51 +02005494#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5496 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5497 output, output_length);
5498 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005499#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005500#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5501 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5503 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5504 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5505 kdf_alg, output,
5506 output_length);
5507 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005508#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5509 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005510#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005512 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5514 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005515#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5516
Gilles Peskineeab56e42018-07-12 17:12:33 +02005517 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005518 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005520 }
5521
5522exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005524 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 * This allows us to differentiate between exhausted operations and
5526 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5527 * operations. */
5528 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005530 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005532 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005534}
5535
David Brown7807bf72021-02-09 16:28:23 -07005536#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005537static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005538{
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 if (data_size >= 8) {
5540 mbedtls_des_key_set_parity(data);
5541 }
5542 if (data_size >= 16) {
5543 mbedtls_des_key_set_parity(data + 8);
5544 }
5545 if (data_size >= 24) {
5546 mbedtls_des_key_set_parity(data + 16);
5547 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005548}
David Brown7807bf72021-02-09 16:28:23 -07005549#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005550
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005551/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 * ECC keys on a Weierstrass elliptic curve require the generation
5553 * of a private key which is an integer
5554 * in the range [1, N - 1], where N is the boundary of the private key domain:
5555 * N is the prime p for Diffie-Hellman, or the order of the
5556 * curve’s base point for ECC.
5557 *
5558 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5559 * This function generates the private key using the following process:
5560 *
5561 * 1. Draw a byte string of length ceiling(m/8) bytes.
5562 * 2. If m is not a multiple of 8, set the most significant
5563 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5564 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5565 * 4. If k > N - 2, discard the result and return to step 1.
5566 * 5. Output k + 1 as the private key.
5567 *
5568 * This method allows compliance to NIST standards, specifically the methods titled
5569 * Key-Pair Generation by Testing Candidates in the following publications:
5570 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5571 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5572 * Diffie-Hellman keys.
5573 *
5574 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5575 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5576 *
5577 * Note: Function allocates memory for *data buffer, so given *data should be
5578 * always NULL.
5579 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005580#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5581 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005582 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5583 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5584 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005585static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005586 psa_key_slot_t *slot,
5587 size_t bits,
5588 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005589 uint8_t **data
5590 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005591{
Valerio Setti52789862023-04-07 12:13:11 +02005592#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005593 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005594 mbedtls_mpi k;
5595 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005597 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005598
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 mbedtls_mpi_init(&k);
5600 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005601
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005602 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005604 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005608 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005609 goto cleanup;
5610 }
5611
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005612 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005616
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005617 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005618 /* Let m be the bit size of N. */
5619 size_t m = ecp_group.nbits;
5620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005622
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005623 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005625
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005626 /* Note: This function is always called with *data == NULL and it
5627 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 *data = mbedtls_calloc(1, m_bytes);
5629 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005630 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005631 goto cleanup;
5632 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005633
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005635 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005637 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005639
5640 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005642 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005643 * (8 * ceiling(m/8) - m) bits of the first byte in
5644 * the string to zero.
5645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005647 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005648 }
5649
5650 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 * big-endian byte string.
5652 */
5653 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005654
5655 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 * Result of comparison is returned. When it indicates error
5657 * then this function is called again.
5658 */
5659 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005660 }
5661
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005662 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5664 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005665cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 if (ret != 0) {
5667 status = mbedtls_to_psa_error(ret);
5668 }
5669 if (status != PSA_SUCCESS) {
5670 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005671 *data = NULL;
5672 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005673 mbedtls_mpi_free(&k);
5674 mbedtls_mpi_free(&diff_N_2);
5675 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005676#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005677 (void) slot;
5678 (void) bits;
5679 (void) operation;
5680 (void) data;
5681 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005682#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005683}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005684
5685/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5686 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5687 *
5688 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5689 * draw a 32-byte string and process it as specified in
5690 * Elliptic Curves for Security [RFC7748] §5.
5691 *
5692 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5693 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5694 *
5695 * Note: Function allocates memory for *data buffer, so given *data should be
5696 * always NULL.
5697 */
5698
5699static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5700 size_t bits,
5701 psa_key_derivation_operation_t *operation,
5702 uint8_t **data
5703 )
5704{
5705 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005709 case 255:
5710 output_length = 32;
5711 break;
5712 case 448:
5713 output_length = 56;
5714 break;
5715 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005717 break;
5718 }
5719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 if (*data == NULL) {
5723 return PSA_ERROR_INSUFFICIENT_MEMORY;
5724 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005729 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005733 case 255:
5734 (*data)[0] &= 248;
5735 (*data)[31] &= 127;
5736 (*data)[31] |= 64;
5737 break;
5738 case 448:
5739 (*data)[0] &= 252;
5740 (*data)[55] |= 128;
5741 break;
5742 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005744 break;
5745 }
5746
5747 return status;
5748}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005749#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5750 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005751 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5752 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5753 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005754
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005755static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005756 psa_key_slot_t *slot,
5757 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005759{
5760 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305762 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005763 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005764
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5766 return PSA_ERROR_INVALID_ARGUMENT;
5767 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005768
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005769#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5770 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005771 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5772 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5773 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5775 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5776 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005777 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5779 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005780 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 }
5782 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005783 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5785 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005786 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005788 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005789 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005790#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5791 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005792 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5793 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5794 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 if (key_type_is_raw_bytes(slot->attr.type)) {
5796 if (bits % 8 != 0) {
5797 return PSA_ERROR_INVALID_ARGUMENT;
5798 }
5799 data = mbedtls_calloc(1, bytes);
5800 if (data == NULL) {
5801 return PSA_ERROR_INSUFFICIENT_MEMORY;
5802 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005803
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 status = psa_key_derivation_output_bytes(operation, data, bytes);
5805 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005806 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 }
David Brown12ca5032021-02-11 11:02:00 -07005808#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5810 psa_des_set_key_parity(data, bytes);
5811 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005812#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 } else {
5814 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005815 }
Ronald Crondd04d422020-11-28 15:14:42 +01005816
Ronald Cronbf33c932020-11-28 18:06:53 +01005817 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005818 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005820 };
5821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5823 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5824 &storage_size);
5825 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305826 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 }
Archanad8a83dc2021-06-14 10:04:16 +05305828 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 status = psa_allocate_buffer_to_slot(slot, 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_driver_wrapper_import_key(&attributes,
5835 data, bytes,
5836 slot->key.data,
5837 slot->key.bytes,
5838 &slot->key.bytes, &bits);
5839 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005840 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005842
5843exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 mbedtls_free(data);
5845 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005846}
5847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5849 psa_key_derivation_operation_t *operation,
5850 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005851{
5852 psa_status_t status;
5853 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005854 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005855
Ronald Cron81709fc2020-11-14 12:10:32 +01005856 *key = MBEDTLS_SVC_KEY_ID_INIT;
5857
Gilles Peskine0f84d622019-09-12 19:03:13 +02005858 /* Reject any attempt to create a zero-length key so that we don't
5859 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 if (psa_get_key_bits(attributes) == 0) {
5861 return PSA_ERROR_INVALID_ARGUMENT;
5862 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 if (operation->alg == PSA_ALG_NONE) {
5865 return PSA_ERROR_BAD_STATE;
5866 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 if (!operation->can_output_key) {
5869 return PSA_ERROR_NOT_PERMITTED;
5870 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005871
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5873 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005874#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005876 /* Deriving a key in a secure element is not implemented yet. */
5877 status = PSA_ERROR_NOT_SUPPORTED;
5878 }
5879#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 if (status == PSA_SUCCESS) {
5881 status = psa_generate_derived_key_internal(slot,
5882 attributes->core.bits,
5883 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005884 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (status == PSA_SUCCESS) {
5886 status = psa_finish_key_creation(slot, driver, key);
5887 }
5888 if (status != PSA_SUCCESS) {
5889 psa_fail_key_creation(slot, driver);
5890 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005893}
5894
Gilles Peskineeab56e42018-07-12 17:12:33 +02005895
5896
5897/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005898/* Key derivation */
5899/****************************************************************/
5900
Steven Cooreman932ffb72021-02-15 12:14:32 +01005901#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005902static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005903{
5904#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5906 return 1;
5907 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005908#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005909#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5911 return 1;
5912 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005913#endif
5914#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5916 return 1;
5917 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005918#endif
5919#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5921 return 1;
5922 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005923#endif
5924#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5926 return 1;
5927 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005928#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005929#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5931 return 1;
5932 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005933#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005935}
5936
Gilles Peskine449bd832023-01-11 14:50:10 +01005937static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005938{
5939 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 psa_status_t status = psa_hash_setup(&operation, alg);
5941 psa_hash_abort(&operation);
5942 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005943}
5944
Gilles Peskine969c5d62019-01-16 15:53:06 +01005945static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005946 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005948{
Janos Follath5fe19732019-06-20 15:09:30 +01005949 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5950 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005952
Gilles Peskine969c5d62019-01-16 15:53:06 +01005953 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if (!is_kdf_alg_supported(kdf_alg)) {
5955 return PSA_ERROR_NOT_SUPPORTED;
5956 }
John Durkop07cc04a2020-11-16 22:08:34 -08005957
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005958 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005959 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5961 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5962 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5963 if (hash_size == 0) {
5964 return PSA_ERROR_NOT_SUPPORTED;
5965 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005966
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005967 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5968 * we might fail later, which is somewhat unfriendly and potentially
5969 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 psa_status_t status = psa_hash_try_support(hash_alg);
5971 if (status != PSA_SUCCESS) {
5972 return status;
5973 }
5974 } else {
5975 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005976 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5979 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5980 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5981 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005982 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005983#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005984 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5986 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005987 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005989#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5990 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 operation->capacity = 255 * hash_size;
5992 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005993}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005996{
5997#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 if (alg == PSA_ALG_ECDH) {
5999 return PSA_SUCCESS;
6000 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006001#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006002#if defined(PSA_WANT_ALG_FFDH)
6003 if (alg == PSA_ALG_FFDH) {
6004 return PSA_SUCCESS;
6005 }
6006#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006007 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006009}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006010
6011static int psa_key_derivation_allows_free_form_secret_input(
6012 psa_algorithm_t kdf_alg)
6013{
6014#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6015 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6016 return 0;
6017 }
6018#endif
6019 (void) kdf_alg;
6020 return 1;
6021}
John Durkop07cc04a2020-11-16 22:08:34 -08006022#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6025 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006026{
6027 psa_status_t status;
6028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 if (operation->alg != 0) {
6030 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006031 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6034 return PSA_ERROR_INVALID_ARGUMENT;
6035 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6036#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6037 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6038 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6039 status = psa_key_agreement_try_support(ka_alg);
6040 if (status != PSA_SUCCESS) {
6041 return status;
6042 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006043 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6044 return PSA_ERROR_INVALID_ARGUMENT;
6045 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6047#else
6048 return PSA_ERROR_NOT_SUPPORTED;
6049#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6050 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6051#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6052 status = psa_key_derivation_setup_kdf(operation, alg);
6053#else
6054 return PSA_ERROR_NOT_SUPPORTED;
6055#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6056 } else {
6057 return PSA_ERROR_INVALID_ARGUMENT;
6058 }
6059
6060 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006061 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 }
6063 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006064}
6065
Przemek Stekiel69c46792022-06-10 12:59:51 +02006066#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006067static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6068 psa_algorithm_t kdf_alg,
6069 psa_key_derivation_step_t step,
6070 const uint8_t *data,
6071 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006072{
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006074 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006076 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006077#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6079 return PSA_ERROR_INVALID_ARGUMENT;
6080 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006081#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 if (hkdf->state != HKDF_STATE_INIT) {
6083 return PSA_ERROR_BAD_STATE;
6084 } else {
6085 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6086 hash_alg,
6087 data, data_length);
6088 if (status != PSA_SUCCESS) {
6089 return status;
6090 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006091 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006093 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006094 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006095#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006097 /* We shouldn't be in different state as HKDF_EXPAND only allows
6098 * two inputs: SECRET (this case) and INFO which does not modify
6099 * the state. It could happen only if the hkdf
6100 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 if (hkdf->state != HKDF_STATE_INIT) {
6102 return PSA_ERROR_BAD_STATE;
6103 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006104
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006105 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6107 return PSA_ERROR_INVALID_ARGUMENT;
6108 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 memcpy(hkdf->prk, data, data_length);
6111 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006112#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006113 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006114 /* HKDF: If no salt was provided, use an empty salt.
6115 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006117#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6119 return PSA_ERROR_BAD_STATE;
6120 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006121#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6123 hash_alg,
6124 NULL, 0);
6125 if (status != PSA_SUCCESS) {
6126 return status;
6127 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006128 hkdf->state = HKDF_STATE_STARTED;
6129 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 if (hkdf->state != HKDF_STATE_STARTED) {
6131 return PSA_ERROR_BAD_STATE;
6132 }
6133 status = psa_mac_update(&hkdf->hmac,
6134 data, data_length);
6135 if (status != PSA_SUCCESS) {
6136 return status;
6137 }
6138 status = psa_mac_sign_finish(&hkdf->hmac,
6139 hkdf->prk,
6140 sizeof(hkdf->prk),
6141 &data_length);
6142 if (status != PSA_SUCCESS) {
6143 return status;
6144 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006145 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006146
Gilles Peskine2b522db2019-04-12 15:11:49 +02006147 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006148 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006149#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006151 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006153 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006155#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006156 {
6157 /* Block 0 is empty, and the next block will be
6158 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006160 }
6161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006163 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006164#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6166 return PSA_ERROR_INVALID_ARGUMENT;
6167 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006168#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006169#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6171 hkdf->state == HKDF_STATE_INIT) {
6172 return PSA_ERROR_BAD_STATE;
6173 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006174#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 if (hkdf->state == HKDF_STATE_OUTPUT) {
6176 return PSA_ERROR_BAD_STATE;
6177 }
6178 if (hkdf->info_set) {
6179 return PSA_ERROR_BAD_STATE;
6180 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006181 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 if (data_length != 0) {
6183 hkdf->info = mbedtls_calloc(1, data_length);
6184 if (hkdf->info == NULL) {
6185 return PSA_ERROR_INSUFFICIENT_MEMORY;
6186 }
6187 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006188 }
6189 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006191 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006193 }
6194}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006195#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006196
John Durkop07cc04a2020-11-16 22:08:34 -08006197#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6198 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006199static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6200 const uint8_t *data,
6201 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006202{
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6204 return PSA_ERROR_BAD_STATE;
6205 }
Janos Follathf08e2652019-06-13 09:05:41 +01006206
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 if (data_length != 0) {
6208 prf->seed = mbedtls_calloc(1, data_length);
6209 if (prf->seed == NULL) {
6210 return PSA_ERROR_INSUFFICIENT_MEMORY;
6211 }
Janos Follathf08e2652019-06-13 09:05:41 +01006212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006214 prf->seed_length = data_length;
6215 }
Janos Follathf08e2652019-06-13 09:05:41 +01006216
Gilles Peskine43f958b2020-12-13 14:55:14 +01006217 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006220}
6221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6223 const uint8_t *data,
6224 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006225{
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6227 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6228 return PSA_ERROR_BAD_STATE;
6229 }
Janos Follath81550542019-06-13 14:26:34 +01006230
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 if (data_length != 0) {
6232 prf->secret = mbedtls_calloc(1, data_length);
6233 if (prf->secret == NULL) {
6234 return PSA_ERROR_INSUFFICIENT_MEMORY;
6235 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006238 prf->secret_length = data_length;
6239 }
Janos Follath81550542019-06-13 14:26:34 +01006240
Gilles Peskine43f958b2020-12-13 14:55:14 +01006241 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006242
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006244}
6245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6247 const uint8_t *data,
6248 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006249{
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6251 return PSA_ERROR_BAD_STATE;
6252 }
Janos Follath63028dd2019-06-13 09:15:47 +01006253
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 if (data_length != 0) {
6255 prf->label = mbedtls_calloc(1, data_length);
6256 if (prf->label == NULL) {
6257 return PSA_ERROR_INSUFFICIENT_MEMORY;
6258 }
Janos Follath63028dd2019-06-13 09:15:47 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006261 prf->label_length = data_length;
6262 }
Janos Follath63028dd2019-06-13 09:15:47 +01006263
Gilles Peskine43f958b2020-12-13 14:55:14 +01006264 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006267}
6268
Gilles Peskine449bd832023-01-11 14:50:10 +01006269static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6270 psa_key_derivation_step_t step,
6271 const uint8_t *data,
6272 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006273{
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006275 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006277 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006279 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006281 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006283 }
6284}
John Durkop07cc04a2020-11-16 22:08:34 -08006285#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6286 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6287
6288#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6289static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6290 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006291 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006293{
6294 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6296 4 + data_length + prf->other_secret_length :
6297 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6300 return PSA_ERROR_INVALID_ARGUMENT;
6301 }
John Durkop07cc04a2020-11-16 22:08:34 -08006302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 uint8_t *pms = mbedtls_calloc(1, pms_len);
6304 if (pms == NULL) {
6305 return PSA_ERROR_INSUFFICIENT_MEMORY;
6306 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006307 uint8_t *cur = pms;
6308
6309 /* pure-PSK:
6310 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006311 *
6312 * The premaster secret is formed as follows: if the PSK is N octets
6313 * long, concatenate a uint16 with the value N, N zero octets, a second
6314 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006315 *
6316 * mixed-PSK:
6317 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6318 * follows: concatenate a uint16 with the length of the other secret,
6319 * the other secret itself, uint16 with the length of PSK, and the
6320 * PSK itself.
6321 * For details please check:
6322 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6323 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6324 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006325 */
6326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6328 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6329 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6330 if (prf->other_secret_length != 0) {
6331 memcpy(cur, prf->other_secret, prf->other_secret_length);
6332 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006333 cur += prf->other_secret_length;
6334 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 } else {
6336 *cur++ = MBEDTLS_BYTE_1(data_length);
6337 *cur++ = MBEDTLS_BYTE_0(data_length);
6338 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006339 cur += data_length;
6340 }
6341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 *cur++ = MBEDTLS_BYTE_1(data_length);
6343 *cur++ = MBEDTLS_BYTE_0(data_length);
6344 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006345 cur += data_length;
6346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 mbedtls_platform_zeroize(pms, pms_len);
6350 mbedtls_free(pms);
6351 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006352}
Janos Follath6660f0e2019-06-17 08:44:03 +01006353
Przemek Stekiele47201b2022-04-19 13:53:28 +02006354static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6355 psa_tls12_prf_key_derivation_t *prf,
6356 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006358{
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6360 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006361 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006362
6363 if (data_length != 0) {
6364 prf->other_secret = mbedtls_calloc(1, data_length);
6365 if (prf->other_secret == NULL) {
6366 return PSA_ERROR_INSUFFICIENT_MEMORY;
6367 }
6368
6369 memcpy(prf->other_secret, data, data_length);
6370 prf->other_secret_length = data_length;
6371 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006372 prf->other_secret_length = 0;
6373 }
6374
6375 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006378}
6379
Janos Follath6660f0e2019-06-17 08:44:03 +01006380static psa_status_t psa_tls12_prf_psk_to_ms_input(
6381 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006382 psa_key_derivation_step_t step,
6383 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006385{
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006387 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 return psa_tls12_prf_psk_to_ms_set_key(prf,
6389 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006390 break;
6391 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6393 data,
6394 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006395 break;
6396 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006398 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006399
Przemek Stekiele47201b2022-04-19 13:53:28 +02006400 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006401}
John Durkop07cc04a2020-11-16 22:08:34 -08006402#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006403
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006404#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6405static psa_status_t psa_tls12_ecjpake_to_pms_input(
6406 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006407 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006408 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006410{
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6412 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6413 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006414 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006415
6416 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 if (data[0] != 0x04) {
6418 return PSA_ERROR_INVALID_ARGUMENT;
6419 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006420
6421 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006423
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006425}
6426#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006427/** Check whether the given key type is acceptable for the given
6428 * input step of a key derivation.
6429 *
6430 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6431 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6432 * Both secret and non-secret inputs can alternatively have the type
6433 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6434 * that the input was passed as a buffer rather than via a key object.
6435 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006436static int psa_key_derivation_check_input_type(
6437 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006439{
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006441 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 if (key_type == PSA_KEY_TYPE_DERIVE) {
6443 return PSA_SUCCESS;
6444 }
6445 if (key_type == PSA_KEY_TYPE_NONE) {
6446 return PSA_SUCCESS;
6447 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006448 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006449 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 if (key_type == PSA_KEY_TYPE_DERIVE) {
6451 return PSA_SUCCESS;
6452 }
6453 if (key_type == PSA_KEY_TYPE_NONE) {
6454 return PSA_SUCCESS;
6455 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006456 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006457 case PSA_KEY_DERIVATION_INPUT_LABEL:
6458 case PSA_KEY_DERIVATION_INPUT_SALT:
6459 case PSA_KEY_DERIVATION_INPUT_INFO:
6460 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
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;
6468 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006470}
6471
Janos Follathb80a94e2019-06-12 15:54:46 +01006472static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006473 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006474 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006475 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006476 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006478{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006479 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006481
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 status = psa_key_derivation_check_input_type(step, key_type);
6483 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006484 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006486
Przemek Stekiel69c46792022-06-10 12:59:51 +02006487#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6489 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6490 step, data, data_length);
6491 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006492#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006493#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6495 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6496 step, data, data_length);
6497 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006498#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6499#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6501 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6502 step, data, data_length);
6503 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006504#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006505#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006507 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6509 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006510#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006511 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006512 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006513 (void) data;
6514 (void) data_length;
6515 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006517 }
6518
Gilles Peskine224b0d62019-09-23 18:13:17 +02006519exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 if (status != PSA_SUCCESS) {
6521 psa_key_derivation_abort(operation);
6522 }
6523 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006524}
6525
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306526static psa_status_t psa_key_derivation_input_integer_internal(
6527 psa_key_derivation_operation_t *operation,
6528 psa_key_derivation_step_t step,
6529 uint64_t value)
6530{
6531 psa_status_t status;
6532 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6533
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306534 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306535 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306536 (void) value;
6537 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306538 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306539 }
6540
6541 if (status != PSA_SUCCESS) {
6542 psa_key_derivation_abort(operation);
6543 }
6544 return status;
6545}
6546
Janos Follath51f4a0f2019-06-14 11:35:55 +01006547psa_status_t psa_key_derivation_input_bytes(
6548 psa_key_derivation_operation_t *operation,
6549 psa_key_derivation_step_t step,
6550 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006552{
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 return psa_key_derivation_input_internal(operation, step,
6554 PSA_KEY_TYPE_NONE,
6555 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006556}
6557
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306558psa_status_t psa_key_derivation_input_integer(
6559 psa_key_derivation_operation_t *operation,
6560 psa_key_derivation_step_t step,
6561 uint64_t value)
6562{
6563 return psa_key_derivation_input_integer_internal(operation, step, value);
6564}
6565
Janos Follath51f4a0f2019-06-14 11:35:55 +01006566psa_status_t psa_key_derivation_input_key(
6567 psa_key_derivation_operation_t *operation,
6568 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006570{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006571 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006572 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006573 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006574
Ronald Cron5c522922020-11-14 16:35:34 +01006575 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6577 if (status != PSA_SUCCESS) {
6578 psa_key_derivation_abort(operation);
6579 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006580 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006581
6582 /* Passing a key object as a SECRET input unlocks the permission
6583 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006585 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 status = psa_key_derivation_input_internal(operation,
6589 step, slot->attr.type,
6590 slot->key.data,
6591 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006592
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006594
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006596}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006597
6598
6599
6600/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006601/* Key agreement */
6602/****************************************************************/
6603
Gilles Peskine449bd832023-01-11 14:50:10 +01006604psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6605 const uint8_t *key_buffer,
6606 size_t key_buffer_size,
6607 psa_algorithm_t alg,
6608 const uint8_t *peer_key,
6609 size_t peer_key_length,
6610 uint8_t *shared_secret,
6611 size_t shared_secret_size,
6612 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006613{
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006615#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006616 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6618 key_buffer_size, alg,
6619 peer_key, peer_key_length,
6620 shared_secret,
6621 shared_secret_size,
6622 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006623#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006624
6625#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6626 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006627 return mbedtls_psa_key_agreement_ffdh(attributes,
6628 peer_key,
6629 peer_key_length,
6630 key_buffer,
6631 key_buffer_size,
6632 shared_secret,
6633 shared_secret_size,
6634 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006635#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6636
Gilles Peskine01d718c2018-09-18 12:01:02 +02006637 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006638 (void) attributes;
6639 (void) key_buffer;
6640 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006641 (void) peer_key;
6642 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006643 (void) shared_secret;
6644 (void) shared_secret_size;
6645 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006647 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006648}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006649
Aditya Deshpande17845b82022-10-13 17:21:01 +01006650/** Internal function for raw key agreement
6651 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006652 * to the driver's implementation if a driver is present.
6653 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006654 * (psa_key_agreement_raw_builtin).
6655 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006656static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6657 psa_key_slot_t *private_key,
6658 const uint8_t *peer_key,
6659 size_t peer_key_length,
6660 uint8_t *shared_secret,
6661 size_t shared_secret_size,
6662 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006663{
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6665 return PSA_ERROR_NOT_SUPPORTED;
6666 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006667
6668 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006670 };
6671
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 return psa_driver_wrapper_key_agreement(&attributes,
6673 private_key->key.data,
6674 private_key->key.bytes, alg,
6675 peer_key, peer_key_length,
6676 shared_secret,
6677 shared_secret_size,
6678 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006679}
6680
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006681/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006682 * to potentially free embedded data structures and wipe confidential data.
6683 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006684static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6685 psa_key_derivation_step_t step,
6686 psa_key_slot_t *private_key,
6687 const uint8_t *peer_key,
6688 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006689{
6690 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006691 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006692 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006694
6695 /* Step 1: run the secret agreement algorithm to generate the shared
6696 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 status = psa_key_agreement_raw_internal(ka_alg,
6698 private_key,
6699 peer_key, peer_key_length,
6700 shared_secret,
6701 sizeof(shared_secret),
6702 &shared_secret_length);
6703 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006704 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006706
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006707 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006708 * the shared secret. A shared secret is permitted wherever a key
6709 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 status = psa_key_derivation_input_internal(operation, step,
6711 PSA_KEY_TYPE_DERIVE,
6712 shared_secret,
6713 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006714exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6716 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006717}
6718
Gilles Peskine449bd832023-01-11 14:50:10 +01006719psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6720 psa_key_derivation_step_t step,
6721 mbedtls_svc_key_id_t private_key,
6722 const uint8_t *peer_key,
6723 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006724{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006726 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006727 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006728
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6730 return PSA_ERROR_INVALID_ARGUMENT;
6731 }
Ronald Cron5c522922020-11-14 16:35:34 +01006732 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006733 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6734 if (status != PSA_SUCCESS) {
6735 return status;
6736 }
6737 status = psa_key_agreement_internal(operation, step,
6738 slot,
6739 peer_key, peer_key_length);
6740 if (status != PSA_SUCCESS) {
6741 psa_key_derivation_abort(operation);
6742 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006743 /* If a private key has been added as SECRET, we allow the derived
6744 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006746 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006748 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006749
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006751
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006753}
6754
Gilles Peskine449bd832023-01-11 14:50:10 +01006755psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6756 mbedtls_svc_key_id_t private_key,
6757 const uint8_t *peer_key,
6758 size_t peer_key_length,
6759 uint8_t *output,
6760 size_t output_size,
6761 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006762{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006763 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006764 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006765 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006766
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006768 status = PSA_ERROR_INVALID_ARGUMENT;
6769 goto exit;
6770 }
Ronald Cron5c522922020-11-14 16:35:34 +01006771 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006772 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6773 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006774 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006776
Gilles Peskine3e561302022-04-14 00:17:15 +02006777 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6778 * for the output size. The PSA specification only guarantees that this
6779 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6780 * but it might be nice to allow smaller buffers if the output fits.
6781 * At the time of writing this comment, with only ECDH implemented,
6782 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6783 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6784 * be exact for it as well. */
6785 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6787 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006788 status = PSA_ERROR_BUFFER_TOO_SMALL;
6789 goto exit;
6790 }
6791
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 status = psa_key_agreement_raw_internal(alg, slot,
6793 peer_key, peer_key_length,
6794 output, output_size,
6795 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006796
6797exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006799 /* If an error happens and is not handled properly, the output
6800 * may be used as a key to protect sensitive data. Arrange for such
6801 * a key to be random, which is likely to result in decryption or
6802 * verification errors. This is better than filling the buffer with
6803 * some constant data such as zeros, which would result in the data
6804 * being protected with a reproducible, easily knowable key.
6805 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006807 *output_length = output_size;
6808 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006809
Gilles Peskine449bd832023-01-11 14:50:10 +01006810 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006811
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006813}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006814
6815
Gilles Peskine30524eb2020-11-13 17:02:26 +01006816
Gilles Peskine86a440b2018-11-12 18:39:40 +01006817/****************************************************************/
6818/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006819/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006820
Gilles Peskine30524eb2020-11-13 17:02:26 +01006821/** Initialize the PSA random generator.
6822 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006823static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006824{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006825#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006827#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6828
Gilles Peskine30524eb2020-11-13 17:02:26 +01006829 /* Set default configuration if
6830 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006832 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006833 }
6834 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006835 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006837
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006839#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6840 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6841 /* The PSA entropy injection feature depends on using NV seed as an entropy
6842 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 mbedtls_entropy_add_source(&rng->entropy,
6844 mbedtls_nv_seed_poll, NULL,
6845 MBEDTLS_ENTROPY_BLOCK_SIZE,
6846 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006847#endif
6848
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006850#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006851}
6852
6853/** Deinitialize the PSA random generator.
6854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006855static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006856{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006857#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006859#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6861 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006862#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006863}
6864
6865/** Seed the PSA random generator.
6866 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006867static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006868{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006869#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6870 /* Do nothing: the external RNG seeds itself. */
6871 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006873#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006874 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6876 drbg_seed, sizeof(drbg_seed) - 1);
6877 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006878#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006879}
6880
Gilles Peskine449bd832023-01-11 14:50:10 +01006881psa_status_t psa_generate_random(uint8_t *output,
6882 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006883{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006884 GUARD_MODULE_INITIALIZED;
6885
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006886#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6887
6888 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6890 output, output_size,
6891 &output_length);
6892 if (status != PSA_SUCCESS) {
6893 return status;
6894 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006895 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006896 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 if (output_length != output_size) {
6898 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6899 }
6900 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006901
6902#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6903
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006905 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6907 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6908 output_size);
6909 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6910 output, request_size);
6911 if (ret != 0) {
6912 return mbedtls_to_psa_error(ret);
6913 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006914 output_size -= request_size;
6915 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006916 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006917 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006918#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006919}
6920
Gilles Peskine8814fc42020-12-14 15:33:44 +01006921/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006922 *
6923 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6924 * `psa_generate_random(...)`. The state parameter is ignored since the
6925 * PSA API doesn't support passing an explicit state.
6926 *
6927 * In the non-external case, psa_generate_random() calls an
6928 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6929 * and semantics as mbedtls_psa_get_random(). As an optimization,
6930 * instead of doing this back-and-forth between the PSA API and the
6931 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6932 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006933 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006934#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6935int mbedtls_psa_get_random(void *p_rng,
6936 unsigned char *output,
6937 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006938{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006939 /* This function takes a pointer to the RNG state because that's what
6940 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6941 * its own state internally and doesn't let the caller access that state.
6942 * So we just ignore the state parameter, and in practice we'll pass
6943 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006944 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 psa_status_t status = psa_generate_random(output, output_size);
6946 if (status == PSA_SUCCESS) {
6947 return 0;
6948 } else {
6949 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6950 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006951}
6952#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6953
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006954#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006955#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006956
Gilles Peskine449bd832023-01-11 14:50:10 +01006957psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6958 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006959{
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 if (global_data.initialized) {
6961 return PSA_ERROR_NOT_PERMITTED;
6962 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006963
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6965 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6966 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6967 return PSA_ERROR_INVALID_ARGUMENT;
6968 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006969
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006971}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006972#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006973
Ronald Cron3772afe2021-02-08 16:10:05 +01006974/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006975 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006976 * \param type The key type
6977 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006978 *
6979 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006980 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006981 * \retval #PSA_ERROR_INVALID_ARGUMENT
6982 * The size in bits of the key is not valid.
6983 * \retval #PSA_ERROR_NOT_SUPPORTED
6984 * The type and/or the size in bits of the key or the combination of
6985 * the two is not supported.
6986 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006987static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006989{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006990 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006991
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 if (key_type_is_raw_bytes(type)) {
6993 status = psa_validate_unstructured_key_bit_size(type, bits);
6994 if (status != PSA_SUCCESS) {
6995 return status;
6996 }
6997 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006998#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7000 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7001 return PSA_ERROR_NOT_SUPPORTED;
7002 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007003
Ronald Cron01b2aba2020-10-05 09:42:02 +02007004 /* Accept only byte-aligned keys, for the same reasons as
7005 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007006 if (bits % 8 != 0) {
7007 return PSA_ERROR_NOT_SUPPORTED;
7008 }
7009 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007010#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007011
Ronald Cron5c4d3862020-12-07 11:07:24 +01007012#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007014 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 return PSA_SUCCESS;
7016 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007017#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007018
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007019#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007020 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007021 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007022 return PSA_ERROR_NOT_SUPPORTED;
7023 }
7024 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007025#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007026 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007028 }
7029
Gilles Peskine449bd832023-01-11 14:50:10 +01007030 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007031}
7032
Ronald Cron55ed0592020-10-05 10:30:40 +02007033psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007034 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007036{
7037 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007038 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007039
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 if ((attributes->domain_parameters == NULL) &&
7041 (attributes->domain_parameters_size != 0)) {
7042 return PSA_ERROR_INVALID_ARGUMENT;
7043 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007044
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 if (key_type_is_raw_bytes(type)) {
7046 status = psa_generate_random(key_buffer, key_buffer_size);
7047 if (status != PSA_SUCCESS) {
7048 return status;
7049 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007050
David Brown12ca5032021-02-11 11:02:00 -07007051#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 if (type == PSA_KEY_TYPE_DES) {
7053 psa_des_set_key_parity(key_buffer, key_buffer_size);
7054 }
David Brown8107e312021-02-12 08:08:46 -07007055#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007057
Jaeden Amero424fa932021-05-14 08:34:32 +01007058#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7059 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7061 return mbedtls_psa_rsa_generate_key(attributes,
7062 key_buffer,
7063 key_buffer_size,
7064 key_buffer_length);
7065 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007066#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7067 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007068
John Durkop9814fa22020-11-04 12:28:15 -08007069#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7071 return mbedtls_psa_ecp_generate_key(attributes,
7072 key_buffer,
7073 key_buffer_size,
7074 key_buffer_length);
7075 } else
John Durkop9814fa22020-11-04 12:28:15 -08007076#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007077
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007078#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007079 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7080 return mbedtls_psa_ffdh_generate_key(attributes,
7081 key_buffer,
7082 key_buffer_size,
7083 key_buffer_length);
7084 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007085#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007086 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 (void) key_buffer_length;
7088 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007089 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007090
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007092}
Darryl Green0c6575a2018-11-07 16:05:30 +00007093
Gilles Peskine449bd832023-01-11 14:50:10 +01007094psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7095 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007096{
7097 psa_status_t status;
7098 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007099 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007100 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007101
Ronald Cron81709fc2020-11-14 12:10:32 +01007102 *key = MBEDTLS_SVC_KEY_ID_INIT;
7103
Gilles Peskine0f84d622019-09-12 19:03:13 +02007104 /* Reject any attempt to create a zero-length key so that we don't
7105 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 if (psa_get_key_bits(attributes) == 0) {
7107 return PSA_ERROR_INVALID_ARGUMENT;
7108 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007109
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007110 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7112 return PSA_ERROR_INVALID_ARGUMENT;
7113 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007114
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7116 &slot, &driver);
7117 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 }
Gilles Peskine11792082019-08-06 18:36:36 +02007120
Ronald Cron977c2472020-10-13 08:32:21 +02007121 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307122 * storage ( thus not in the case of generating a key in a secure element
7123 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7124 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 if (slot->key.data == NULL) {
7126 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7127 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007128 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 attributes->core.type, attributes->core.bits);
7130 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007131 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007133
7134 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 attributes->core.type,
7136 attributes->core.bits);
7137 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007138 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 attributes, &key_buffer_size);
7140 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007142 }
Ronald Cron977c2472020-10-13 08:32:21 +02007143 }
Ronald Cron977c2472020-10-13 08:32:21 +02007144
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7146 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 }
Ronald Cron977c2472020-10-13 08:32:21 +02007149 }
7150
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 status = psa_driver_wrapper_generate_key(attributes,
7152 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007153
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 if (status != PSA_SUCCESS) {
7155 psa_remove_key_data_from_memory(slot);
7156 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007157
Gilles Peskine11792082019-08-06 18:36:36 +02007158exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007159 if (status == PSA_SUCCESS) {
7160 status = psa_finish_key_creation(slot, driver, key);
7161 }
7162 if (status != PSA_SUCCESS) {
7163 psa_fail_key_creation(slot, driver);
7164 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007165
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007167}
7168
Gilles Peskinee59236f2018-01-27 23:32:46 +01007169/****************************************************************/
7170/* Module setup */
7171/****************************************************************/
7172
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007173#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007174psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 void (* entropy_init)(mbedtls_entropy_context *ctx),
7176 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007177{
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7179 return PSA_ERROR_BAD_STATE;
7180 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007181 global_data.rng.entropy_init = entropy_init;
7182 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007184}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007185#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007186
Gilles Peskine449bd832023-01-11 14:50:10 +01007187void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007188{
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 psa_wipe_all_key_slots();
7190 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7191 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007192 }
7193 /* Wipe all remaining data, including configuration.
7194 * In particular, this sets all state indicator to the value
7195 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007197
7198 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007199 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007200}
7201
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007202#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7203/** Recover a transaction that was interrupted by a power failure.
7204 *
7205 * This function is called during initialization, before psa_crypto_init()
7206 * returns. If this function returns a failure status, the initialization
7207 * fails.
7208 */
7209static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007210 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007211{
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007213 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7214 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 /* TODO - fall through to the failure case until this
7216 * is implemented.
7217 * https://github.com/ARMmbed/mbed-crypto/issues/218
7218 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007219 default:
7220 /* We found an unsupported transaction in the storage.
7221 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007222 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007223 }
7224}
7225#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7226
Gilles Peskine449bd832023-01-11 14:50:10 +01007227psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007228{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007229 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007230
Gilles Peskinec6b69072018-11-20 21:42:52 +01007231 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 if (global_data.initialized != 0) {
7233 return PSA_SUCCESS;
7234 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007235
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007236 /* Init drivers */
7237 status = psa_driver_wrapper_init();
7238 if (status != PSA_SUCCESS) {
7239 goto exit;
7240 }
7241 global_data.drivers_initialized = 1;
7242
Gilles Peskine30524eb2020-11-13 17:02:26 +01007243 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007245 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 status = mbedtls_psa_random_seed(&global_data.rng);
7247 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007248 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007250 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007251
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 status = psa_initialize_key_slots();
7253 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007254 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007256
Gilles Peskine4b734222019-07-24 15:56:31 +02007257#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 status = psa_crypto_load_transaction();
7259 if (status == PSA_SUCCESS) {
7260 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7261 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007262 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 }
7264 status = psa_crypto_stop_transaction();
7265 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007266 /* There's no transaction to complete. It's all good. */
7267 status = PSA_SUCCESS;
7268 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007269#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007270
Gilles Peskinec6b69072018-11-20 21:42:52 +01007271 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007272 global_data.initialized = 1;
7273
7274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 if (status != PSA_SUCCESS) {
7276 mbedtls_psa_crypto_free();
7277 }
7278 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007279}
7280
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007281psa_status_t psa_crypto_driver_pake_get_password_len(
7282 const psa_crypto_driver_pake_inputs_t *inputs,
7283 size_t *password_len)
7284{
7285 if (inputs->password_len == 0) {
7286 return PSA_ERROR_BAD_STATE;
7287 }
7288
7289 *password_len = inputs->password_len;
7290
7291 return PSA_SUCCESS;
7292}
7293
7294psa_status_t psa_crypto_driver_pake_get_password(
7295 const psa_crypto_driver_pake_inputs_t *inputs,
7296 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7297{
7298 if (inputs->password_len == 0) {
7299 return PSA_ERROR_BAD_STATE;
7300 }
7301
7302 if (buffer_size < inputs->password_len) {
7303 return PSA_ERROR_BUFFER_TOO_SMALL;
7304 }
7305
7306 memcpy(buffer, inputs->password, inputs->password_len);
7307 *buffer_length = inputs->password_len;
7308
7309 return PSA_SUCCESS;
7310}
7311
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007312psa_status_t psa_crypto_driver_pake_get_user_len(
7313 const psa_crypto_driver_pake_inputs_t *inputs,
7314 size_t *user_len)
7315{
7316 if (inputs->user_len == 0) {
7317 return PSA_ERROR_BAD_STATE;
7318 }
7319
7320 *user_len = inputs->user_len;
7321
7322 return PSA_SUCCESS;
7323}
7324
7325psa_status_t psa_crypto_driver_pake_get_user(
7326 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007327 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007328{
7329 if (inputs->user_len == 0) {
7330 return PSA_ERROR_BAD_STATE;
7331 }
7332
Przemek Stekielc0e62502023-03-14 11:49:36 +01007333 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007334 return PSA_ERROR_BUFFER_TOO_SMALL;
7335 }
7336
Przemek Stekielc0e62502023-03-14 11:49:36 +01007337 memcpy(user_id, inputs->user, inputs->user_len);
7338 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007339
7340 return PSA_SUCCESS;
7341}
7342
7343psa_status_t psa_crypto_driver_pake_get_peer_len(
7344 const psa_crypto_driver_pake_inputs_t *inputs,
7345 size_t *peer_len)
7346{
7347 if (inputs->peer_len == 0) {
7348 return PSA_ERROR_BAD_STATE;
7349 }
7350
7351 *peer_len = inputs->peer_len;
7352
7353 return PSA_SUCCESS;
7354}
7355
7356psa_status_t psa_crypto_driver_pake_get_peer(
7357 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007358 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007359{
7360 if (inputs->peer_len == 0) {
7361 return PSA_ERROR_BAD_STATE;
7362 }
7363
Przemek Stekielc0e62502023-03-14 11:49:36 +01007364 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007365 return PSA_ERROR_BUFFER_TOO_SMALL;
7366 }
7367
Przemek Stekielc0e62502023-03-14 11:49:36 +01007368 memcpy(peer_id, inputs->peer, inputs->peer_len);
7369 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007370
7371 return PSA_SUCCESS;
7372}
7373
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007374psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7375 const psa_crypto_driver_pake_inputs_t *inputs,
7376 psa_pake_cipher_suite_t *cipher_suite)
7377{
7378 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7379 return PSA_ERROR_BAD_STATE;
7380 }
7381
7382 *cipher_suite = inputs->cipher_suite;
7383
7384 return PSA_SUCCESS;
7385}
7386
Neil Armstronga7d08c32022-06-01 18:21:20 +02007387psa_status_t psa_pake_setup(
7388 psa_pake_operation_t *operation,
7389 const psa_pake_cipher_suite_t *cipher_suite)
7390{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007392
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007393 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007394 status = PSA_ERROR_BAD_STATE;
7395 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007396 }
7397
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007398 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007399 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007400 status = PSA_ERROR_INVALID_ARGUMENT;
7401 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007402 }
7403
Przemek Stekiel51eac532022-12-07 11:04:51 +01007404 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7405
Przemek Stekiele12ed362022-12-21 12:54:46 +01007406 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007407 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7408 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007409 operation->data.inputs.cipher_suite = *cipher_suite;
7410
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007411#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007412 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007413 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007414 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007415
Przemek Stekiele12ed362022-12-21 12:54:46 +01007416 computation_stage->state = PSA_PAKE_STATE_SETUP;
7417 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7418 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7419 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007420 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007421#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007422 {
7423 status = PSA_ERROR_NOT_SUPPORTED;
7424 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007425 }
7426
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007427 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7428
Przemek Stekiel51eac532022-12-07 11:04:51 +01007429 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007430exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007431 psa_pake_abort(operation);
7432 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007433}
7434
7435psa_status_t psa_pake_set_password_key(
7436 psa_pake_operation_t *operation,
7437 mbedtls_svc_key_id_t password)
7438{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007439 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007440 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7441 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007442
Przemek Stekiel51eac532022-12-07 11:04:51 +01007443 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007444 status = PSA_ERROR_BAD_STATE;
7445 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007446 }
7447
Przemek Stekiel6c764412022-11-22 14:05:12 +01007448 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7449 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007450 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007451 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007452 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007453 }
7454
Przemek Stekiel6c764412022-11-22 14:05:12 +01007455 psa_key_attributes_t attributes = {
7456 .core = slot->attr
7457 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007458
Przemek Stekiel51eac532022-12-07 11:04:51 +01007459 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007460
Przemek Stekiel51eac532022-12-07 11:04:51 +01007461 if (type != PSA_KEY_TYPE_PASSWORD &&
7462 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7463 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007464 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007465 }
7466
Przemek Stekiel51eac532022-12-07 11:04:51 +01007467 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7468 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007469 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007470 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007471 }
7472
7473 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7474 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007475 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007476exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007477 if (status != PSA_SUCCESS) {
7478 psa_pake_abort(operation);
7479 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007480 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007481 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007482}
7483
7484psa_status_t psa_pake_set_user(
7485 psa_pake_operation_t *operation,
7486 const uint8_t *user_id,
7487 size_t user_id_len)
7488{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007489 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007490
7491 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007492 status = PSA_ERROR_BAD_STATE;
7493 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007494 }
7495
Przemek Stekiel51eac532022-12-07 11:04:51 +01007496 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007497 status = PSA_ERROR_INVALID_ARGUMENT;
7498 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007499 }
7500
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007501 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007502 status = PSA_ERROR_BAD_STATE;
7503 goto exit;
7504 }
7505
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007506 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7507 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007508 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7509 goto exit;
7510 }
7511
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007512 memcpy(operation->data.inputs.user, user_id, user_id_len);
7513 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007514
7515 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007516exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007517 psa_pake_abort(operation);
7518 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007519}
7520
7521psa_status_t psa_pake_set_peer(
7522 psa_pake_operation_t *operation,
7523 const uint8_t *peer_id,
7524 size_t peer_id_len)
7525{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007527
7528 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007529 status = PSA_ERROR_BAD_STATE;
7530 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007531 }
7532
Przemek Stekiel51eac532022-12-07 11:04:51 +01007533 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007534 status = PSA_ERROR_INVALID_ARGUMENT;
7535 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007536 }
7537
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007538 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007539 status = PSA_ERROR_BAD_STATE;
7540 goto exit;
7541 }
7542
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007543 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7544 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007545 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7546 goto exit;
7547 }
7548
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007549 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7550 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007551
7552 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007553exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007554 psa_pake_abort(operation);
7555 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007556}
7557
7558psa_status_t psa_pake_set_role(
7559 psa_pake_operation_t *operation,
7560 psa_pake_role_t role)
7561{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007563
Przemek Stekiel51eac532022-12-07 11:04:51 +01007564 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007565 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007566 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007567 }
7568
Przemek Stekiel09104b82023-03-06 14:11:51 +01007569 switch (operation->alg) {
7570#if defined(PSA_WANT_ALG_JPAKE)
7571 case PSA_ALG_JPAKE:
7572 if (role == PSA_PAKE_ROLE_NONE) {
7573 return PSA_SUCCESS;
7574 }
7575 status = PSA_ERROR_INVALID_ARGUMENT;
7576 break;
7577#endif
7578 default:
7579 (void) role;
7580 status = PSA_ERROR_NOT_SUPPORTED;
7581 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007582 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007583exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007584 psa_pake_abort(operation);
7585 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007586}
7587
Przemek Stekielb09c4872023-01-17 12:05:38 +01007588/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007589#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007590static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007591 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007592{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007593 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007594 case PSA_PAKE_OUTPUT_X1_X2:
7595 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007596 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007597 case PSA_PAKE_X1_STEP_KEY_SHARE:
7598 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007599 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7600 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007601 case PSA_PAKE_X1_STEP_ZK_PROOF:
7602 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007603 case PSA_PAKE_X2_STEP_KEY_SHARE:
7604 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007605 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7606 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007607 case PSA_PAKE_X2_STEP_ZK_PROOF:
7608 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007609 default:
7610 return PSA_JPAKE_STEP_INVALID;
7611 }
7612 break;
7613 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007614 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007615 case PSA_PAKE_X1_STEP_KEY_SHARE:
7616 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007617 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7618 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007619 case PSA_PAKE_X1_STEP_ZK_PROOF:
7620 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007621 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007622 return PSA_JPAKE_STEP_INVALID;
7623 }
7624 break;
7625 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007626 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007627 case PSA_PAKE_X1_STEP_KEY_SHARE:
7628 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007629 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7630 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007631 case PSA_PAKE_X1_STEP_ZK_PROOF:
7632 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007633 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007634 return PSA_JPAKE_STEP_INVALID;
7635 }
7636 break;
7637 default:
7638 return PSA_JPAKE_STEP_INVALID;
7639 }
7640 return PSA_JPAKE_STEP_INVALID;
7641}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007642#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007643
Przemek Stekiel2797d372022-12-22 11:19:22 +01007644static psa_status_t psa_pake_complete_inputs(
7645 psa_pake_operation_t *operation)
7646{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007647 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007648 /* Create copy of the inputs on stack as inputs share memory
7649 with the driver context which will be setup by the driver. */
7650 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007651
Przemek Stekielfde11282023-03-13 16:06:09 +01007652 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007653 return PSA_ERROR_BAD_STATE;
7654 }
7655
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007656 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007657 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7658 return PSA_ERROR_BAD_STATE;
7659 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007660 }
7661
Przemek Stekiel18620a32023-01-17 16:34:52 +01007662 /* Clear driver context */
7663 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7664
7665 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007666
7667 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007668 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7669 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007670
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007671 /* User and peer are translated to role. */
7672 mbedtls_free(inputs.user);
7673 mbedtls_free(inputs.peer);
7674
Przemek Stekiel2797d372022-12-22 11:19:22 +01007675 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007676#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007677 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007678 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007679 psa_jpake_computation_stage_t *computation_stage =
7680 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007681 computation_stage->state = PSA_PAKE_STATE_READY;
7682 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7683 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7684 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007685 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007686#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007687 {
7688 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007689 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007690 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007691 return status;
7692}
7693
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007694#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007695static psa_status_t psa_jpake_output_prologue(
7696 psa_pake_operation_t *operation,
7697 psa_pake_step_t step)
7698{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007699 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7700 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7701 step != PSA_PAKE_STEP_ZK_PROOF) {
7702 return PSA_ERROR_INVALID_ARGUMENT;
7703 }
7704
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007705 psa_jpake_computation_stage_t *computation_stage =
7706 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007707
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007708 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7709 return PSA_ERROR_BAD_STATE;
7710 }
7711
7712 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7713 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7714 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7715 return PSA_ERROR_BAD_STATE;
7716 }
7717
7718 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7719 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007720 return PSA_ERROR_BAD_STATE;
7721 }
7722
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007723 switch (computation_stage->output_step) {
7724 case PSA_PAKE_STEP_X1_X2:
7725 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007726 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007727 case PSA_PAKE_STEP_X2S:
7728 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007729 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007730 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007731 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007732 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007733
7734 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7735 }
7736
7737 /* Check if step matches current sequence */
7738 switch (computation_stage->sequence) {
7739 case PSA_PAKE_X1_STEP_KEY_SHARE:
7740 case PSA_PAKE_X2_STEP_KEY_SHARE:
7741 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7742 return PSA_ERROR_BAD_STATE;
7743 }
7744 break;
7745
7746 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7747 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7748 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7749 return PSA_ERROR_BAD_STATE;
7750 }
7751 break;
7752
7753 case PSA_PAKE_X1_STEP_ZK_PROOF:
7754 case PSA_PAKE_X2_STEP_ZK_PROOF:
7755 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7756 return PSA_ERROR_BAD_STATE;
7757 }
7758 break;
7759
7760 default:
7761 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007762 }
7763
7764 return PSA_SUCCESS;
7765}
7766
7767static psa_status_t psa_jpake_output_epilogue(
7768 psa_pake_operation_t *operation)
7769{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007770 psa_jpake_computation_stage_t *computation_stage =
7771 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007772
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007773 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007774 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007775 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007776 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007777 computation_stage->state = PSA_PAKE_STATE_READY;
7778 computation_stage->output_step++;
7779 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7780 } else {
7781 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007782 }
7783
7784 return PSA_SUCCESS;
7785}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007786#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007787
Neil Armstronga7d08c32022-06-01 18:21:20 +02007788psa_status_t psa_pake_output(
7789 psa_pake_operation_t *operation,
7790 psa_pake_step_t step,
7791 uint8_t *output,
7792 size_t output_size,
7793 size_t *output_length)
7794{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007795 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007796 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007797 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007798
7799 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007800 status = psa_pake_complete_inputs(operation);
7801 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007802 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007803 }
7804 }
7805
7806 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007807 status = PSA_ERROR_BAD_STATE;
7808 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007809 }
7810
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007811 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007812 status = PSA_ERROR_INVALID_ARGUMENT;
7813 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007814 }
7815
Przemek Stekiele12ed362022-12-21 12:54:46 +01007816 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007817#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007818 case PSA_ALG_JPAKE:
7819 status = psa_jpake_output_prologue(operation, step);
7820 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007821 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007822 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007823 driver_step = convert_jpake_computation_stage_to_driver_step(
7824 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007825 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007826#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007827 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007828 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007829 status = PSA_ERROR_NOT_SUPPORTED;
7830 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007831 }
7832
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007833 status = psa_driver_wrapper_pake_output(operation, driver_step,
7834 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007835
7836 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007837 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007838 }
7839
7840 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007841#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007842 case PSA_ALG_JPAKE:
7843 status = psa_jpake_output_epilogue(operation);
7844 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007845 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007846 }
7847 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007848#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007849 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007850 status = PSA_ERROR_NOT_SUPPORTED;
7851 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007852 }
7853
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007854 return PSA_SUCCESS;
7855exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007856 psa_pake_abort(operation);
7857 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007858}
7859
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007860#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007861static psa_status_t psa_jpake_input_prologue(
7862 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007863 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007864{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007865 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7866 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7867 step != PSA_PAKE_STEP_ZK_PROOF) {
7868 return PSA_ERROR_INVALID_ARGUMENT;
7869 }
7870
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007871 psa_jpake_computation_stage_t *computation_stage =
7872 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007873
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007874 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7875 return PSA_ERROR_BAD_STATE;
7876 }
7877
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007878 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7879 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7880 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7881 return PSA_ERROR_BAD_STATE;
7882 }
7883
7884 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7885 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007886 return PSA_ERROR_BAD_STATE;
7887 }
7888
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007889 switch (computation_stage->input_step) {
7890 case PSA_PAKE_STEP_X1_X2:
7891 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007892 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007893 case PSA_PAKE_STEP_X2S:
7894 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007895 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007896 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007897 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007898 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007899
7900 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7901 }
7902
7903 /* Check if step matches current sequence */
7904 switch (computation_stage->sequence) {
7905 case PSA_PAKE_X1_STEP_KEY_SHARE:
7906 case PSA_PAKE_X2_STEP_KEY_SHARE:
7907 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7908 return PSA_ERROR_BAD_STATE;
7909 }
7910 break;
7911
7912 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7913 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7914 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7915 return PSA_ERROR_BAD_STATE;
7916 }
7917 break;
7918
7919 case PSA_PAKE_X1_STEP_ZK_PROOF:
7920 case PSA_PAKE_X2_STEP_ZK_PROOF:
7921 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7922 return PSA_ERROR_BAD_STATE;
7923 }
7924 break;
7925
7926 default:
7927 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007928 }
7929
7930 return PSA_SUCCESS;
7931}
7932
Przemek Stekiele12ed362022-12-21 12:54:46 +01007933static psa_status_t psa_jpake_input_epilogue(
7934 psa_pake_operation_t *operation)
7935{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007936 psa_jpake_computation_stage_t *computation_stage =
7937 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007938
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007939 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007940 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007941 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007942 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007943 computation_stage->state = PSA_PAKE_STATE_READY;
7944 computation_stage->input_step++;
7945 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7946 } else {
7947 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007948 }
7949
7950 return PSA_SUCCESS;
7951}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007952#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007953
Neil Armstronga7d08c32022-06-01 18:21:20 +02007954psa_status_t psa_pake_input(
7955 psa_pake_operation_t *operation,
7956 psa_pake_step_t step,
7957 const uint8_t *input,
7958 size_t input_length)
7959{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007960 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007961 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007962 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
7963 operation->primitive,
7964 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007965
7966 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007967 status = psa_pake_complete_inputs(operation);
7968 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007969 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007970 }
7971 }
7972
7973 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007974 status = PSA_ERROR_BAD_STATE;
7975 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007976 }
7977
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007978 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007979 status = PSA_ERROR_INVALID_ARGUMENT;
7980 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007981 }
7982
Przemek Stekiele12ed362022-12-21 12:54:46 +01007983 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007984#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007985 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007986 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007987 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007988 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007989 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007990 driver_step = convert_jpake_computation_stage_to_driver_step(
7991 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007992 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007993#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007994 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007995 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007996 status = PSA_ERROR_NOT_SUPPORTED;
7997 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007998 }
7999
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008000 status = psa_driver_wrapper_pake_input(operation, driver_step,
8001 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008002
8003 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008004 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008005 }
8006
8007 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008008#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008009 case PSA_ALG_JPAKE:
8010 status = psa_jpake_input_epilogue(operation);
8011 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008012 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008013 }
8014 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008015#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008016 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008017 status = PSA_ERROR_NOT_SUPPORTED;
8018 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008019 }
8020
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008021 return PSA_SUCCESS;
8022exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008023 psa_pake_abort(operation);
8024 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008025}
8026
8027psa_status_t psa_pake_get_implicit_key(
8028 psa_pake_operation_t *operation,
8029 psa_key_derivation_operation_t *output)
8030{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008031 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008032 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008033 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008034 size_t shared_key_len = 0;
8035
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008036 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008037 status = PSA_ERROR_BAD_STATE;
8038 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008039 }
8040
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008041#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008042 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008043 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008044 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008045 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8046 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008047 status = PSA_ERROR_BAD_STATE;
8048 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008049 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008050 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008051#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008052 {
8053 status = PSA_ERROR_NOT_SUPPORTED;
8054 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008055 }
8056
Przemek Stekiel0c781802022-11-29 14:53:13 +01008057 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8058 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008059 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008060 &shared_key_len);
8061
8062 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008063 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008064 }
8065
8066 status = psa_key_derivation_input_bytes(output,
8067 PSA_KEY_DERIVATION_INPUT_SECRET,
8068 shared_key,
8069 shared_key_len);
8070
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008071 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008072exit:
8073 abort_status = psa_pake_abort(operation);
8074 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008075}
8076
8077psa_status_t psa_pake_abort(
8078 psa_pake_operation_t *operation)
8079{
Przemek Stekield69dca92023-01-31 19:59:20 +01008080 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008081
Przemek Stekield69dca92023-01-31 19:59:20 +01008082 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008083 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008084 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008085
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008086 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8087 if (operation->data.inputs.password != NULL) {
8088 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008089 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008090 mbedtls_free(operation->data.inputs.password);
8091 }
8092 if (operation->data.inputs.user != NULL) {
8093 mbedtls_free(operation->data.inputs.user);
8094 }
8095 if (operation->data.inputs.peer != NULL) {
8096 mbedtls_free(operation->data.inputs.peer);
8097 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008098 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008099 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008100
Przemek Stekield69dca92023-01-31 19:59:20 +01008101 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008102}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008103
Gilles Peskinee59236f2018-01-27 23:32:46 +01008104#endif /* MBEDTLS_PSA_CRYPTO_C */