blob: dc383bca31ac93b3afbd7dbc5b6985270fa5e7b6 [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 Stekiel134cc2e2023-05-05 10:13:37 +0200132static int psa_is_dh_key_size_valid(size_t bits)
133{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200134 if (bits != 2048 && bits != 3072 && bits != 4096 &&
135 bits != 6144 && bits != 8192) {
136 return 0;
137 }
138
139 return 1;
140}
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100144 /* Mbed TLS error codes can combine a high-level error code and a
145 * low-level error code. The low-level error usually reflects the
146 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 int low_level_ret = -(-ret & 0x007f);
148 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100149 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100151
152 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
153 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200155 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
156 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
157 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
158 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
159 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200161 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200163 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200165
Jaeden Amero93e21112019-02-20 13:57:28 +0000166#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000167 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100171
172 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100176
Gilles Peskine26869f22019-05-06 15:25:00 +0200177 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200179
180 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200182 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200184
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100187 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
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_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
201 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100202 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
203 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
207 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100211#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100212
213 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215
Gilles Peskinee59236f2018-01-27 23:32:46 +0100216 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
217 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
218 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
221 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200223 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100227
Gilles Peskine82e57d12020-11-13 21:31:17 +0100228#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100230 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
231 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
235 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100237 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100239#endif
240
Gilles Peskinea5905292018-02-07 20:59:33 +0100241 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100243 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249
Gilles Peskinef76aa772018-10-29 19:24:33 +0100250 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100252 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
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_DIVISION_BY_ZERO:
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_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100264 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100266
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100269 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
270 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
275 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
280 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100284 case MBEDTLS_ERR_PK_INVALID_ALG:
285 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
286 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200290 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292
Gilles Peskineff2d2002019-05-06 15:26:23 +0200293 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200295 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100300 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
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_PUBLIC_FAILED:
307 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100309 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100311 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200315
itayzafrir5c753392018-05-08 11:18:38 +0300316 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300317 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300319 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300321 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
324 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300326 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100328 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100330
Paul Elliott588f8ed2022-12-02 18:10:26 +0000331 case MBEDTLS_ERR_ECP_IN_PROGRESS:
332 return PSA_OPERATION_INCOMPLETE;
333
Janos Follatha13b9052019-11-22 12:48:59 +0000334 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300336
Gilles Peskinee59236f2018-01-27 23:32:46 +0100337 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 }
340}
341
Paul Elliottdc42ca82023-02-24 18:11:59 +0000342/**
343 * \brief For output buffers which contain "tags"
344 * (outputs that may be checked for validity like
345 * hashes, MACs and signatures), fill the unused
346 * part of the output buffer (the whole buffer on
347 * error, the trailing part on success) with
348 * something that isn't a valid tag (barring an
349 * attack on the tag and deliberately-crafted
350 * input), in case the caller doesn't check the
351 * return status properly.
352 *
353 * \param output_buffer Pointer to buffer to wipe. May not be NULL
354 * unless \p output_buffer_size is zero.
355 * \param status Status of function called to generate
356 * output_buffer originally
357 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
358 * could be NULL.
359 * \param output_buffer_length Length of data written to output_buffer, must be
360 * less than \p output_buffer_size
361 */
362static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000363 size_t output_buffer_size, size_t output_buffer_length)
364{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000365 size_t offset = 0;
366
367 if (output_buffer_size == 0) {
368 /* If output_buffer_size is 0 then we have nothing to do. We must not
369 call memset because output_buffer may be NULL in this case */
370 return;
371 }
372
373 if (status == PSA_SUCCESS) {
374 offset = output_buffer_length;
375 }
376
377 memset(output_buffer + offset, '!', output_buffer_size - offset);
378}
379
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200380
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200381
382
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383/****************************************************************/
384/* Key management */
385/****************************************************************/
386
Valerio Settid4a5d462023-04-05 18:19:01 +0200387#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100388mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
389 size_t bits,
390 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200391{
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100393 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100395#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100396 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100398#endif
399#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100400 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100402#endif
403#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100404 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100406#endif
407#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100408 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100410#endif
411#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100412 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100414 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (bits_is_sloppy) {
416 return MBEDTLS_ECP_DP_SECP521R1;
417 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100418 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100419#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100420 }
421 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100422
Paul Elliott8ff510a2020-06-02 17:19:28 +0100423 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100425#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100426 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100428#endif
429#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100430 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100432#endif
433#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100434 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100436#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100437 }
438 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100439
Paul Elliott8ff510a2020-06-02 17:19:28 +0100440 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100442#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100445 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (bits_is_sloppy) {
447 return MBEDTLS_ECP_DP_CURVE25519;
448 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#endif
451#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100452 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100454#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100455 }
456 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100457
Paul Elliott8ff510a2020-06-02 17:19:28 +0100458 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100460#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100461 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100463#endif
464#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100467#endif
468#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100471#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100472 }
473 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200474 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100475
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100476 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200478}
Valerio Settid4a5d462023-04-05 18:19:01 +0200479#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
482 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200483{
484 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200486 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200488 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200489 case PSA_KEY_TYPE_PASSWORD:
490 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200491 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100492#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200493 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (bits != 128 && bits != 192 && bits != 256) {
495 return PSA_ERROR_INVALID_ARGUMENT;
496 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497 break;
498#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200499#if defined(PSA_WANT_KEY_TYPE_ARIA)
500 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if (bits != 128 && bits != 192 && bits != 256) {
502 return PSA_ERROR_INVALID_ARGUMENT;
503 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200504 break;
505#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100506#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200507 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (bits != 128 && bits != 192 && bits != 256) {
509 return PSA_ERROR_INVALID_ARGUMENT;
510 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511 break;
512#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100513#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200514 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (bits != 64 && bits != 128 && bits != 192) {
516 return PSA_ERROR_INVALID_ARGUMENT;
517 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200518 break;
519#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100520#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200521 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (bits != 256) {
523 return PSA_ERROR_INVALID_ARGUMENT;
524 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200525 break;
526#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200529 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if (bits % 8 != 0) {
531 return PSA_ERROR_INVALID_ARGUMENT;
532 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200535}
536
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100537/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100538 *
Steven Cooremancd640932021-03-08 12:00:27 +0100539 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
540 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100541 *
542 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100543 * \param[in] key_type The key type of the key to be used with the
544 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100545 *
546 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100547 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100548 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100549 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100550 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100551MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100552 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554{
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (PSA_ALG_IS_HMAC(algorithm)) {
556 if (key_type == PSA_KEY_TYPE_HMAC) {
557 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100558 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100559 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
562 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
563 * key. */
564 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
565 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
566 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
567 * the block length (larger than 1) for block ciphers. */
568 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
569 return PSA_SUCCESS;
570 }
571 }
572 }
573
574 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100575}
576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
578 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200579{
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (slot->key.data != NULL) {
581 return PSA_ERROR_ALREADY_EXISTS;
582 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 slot->key.data = mbedtls_calloc(1, buffer_length);
585 if (slot->key.data == NULL) {
586 return PSA_ERROR_INSUFFICIENT_MEMORY;
587 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200588
Ronald Cronea0f8a62020-11-25 17:52:23 +0100589 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200591}
592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
594 const uint8_t *data,
595 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200596{
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 psa_status_t status = psa_allocate_buffer_to_slot(slot,
598 data_length);
599 if (status != PSA_SUCCESS) {
600 return status;
601 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 memcpy(slot->key.data, data, data_length);
604 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200605}
606
Ronald Crona0fe59f2020-11-29 11:14:53 +0100607psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100608 const psa_key_attributes_t *attributes,
609 const uint8_t *data, size_t data_length,
610 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100612{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100613 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
614 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100615
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200616 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (data_length == 0) {
618 return PSA_ERROR_NOT_SUPPORTED;
619 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (key_type_is_raw_bytes(type)) {
622 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
625 *bits);
626 if (status != PSA_SUCCESS) {
627 return status;
628 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200629
Ronald Crondd04d422020-11-28 15:14:42 +0100630 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100632 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return PSA_SUCCESS;
636 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Przemek Stekiel055ffed2023-05-30 12:51:49 +0200637#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
638 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
639 if (PSA_KEY_TYPE_IS_DH(type)) {
640 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
641 return PSA_ERROR_INVALID_ARGUMENT;
642 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200643 return mbedtls_psa_ffdh_import_key(attributes,
644 data, data_length,
645 key_buffer, key_buffer_size,
646 key_buffer_length,
647 bits);
Przemek Stekiel055ffed2023-05-30 12:51:49 +0200648 }
649#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
650 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
John Durkop9814fa22020-11-04 12:28:15 -0800651#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
653 if (PSA_KEY_TYPE_IS_ECC(type)) {
654 return mbedtls_psa_ecp_import_key(attributes,
655 data, data_length,
656 key_buffer, key_buffer_size,
657 key_buffer_length,
658 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100659 }
John Durkop9814fa22020-11-04 12:28:15 -0800660#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
661 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700662#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
664 if (PSA_KEY_TYPE_IS_RSA(type)) {
665 return mbedtls_psa_rsa_import_key(attributes,
666 data, data_length,
667 key_buffer, key_buffer_size,
668 key_buffer_length,
669 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200670 }
John Durkop9814fa22020-11-04 12:28:15 -0800671#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
672 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100673 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100676}
677
Gilles Peskinef603c712019-01-19 13:40:11 +0100678/** Calculate the intersection of two algorithm usage policies.
679 *
680 * Return 0 (which allows no operation) on incompatibility.
681 */
682static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100683 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100684 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100686{
Gilles Peskine549ea862019-05-22 11:45:59 +0200687 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (alg1 == alg2) {
689 return alg1;
690 }
Steven Cooreman03488022021-02-18 12:07:20 +0100691 /* If the policies are from the same hash-and-sign family, check
692 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
694 PSA_ALG_IS_SIGN_HASH(alg2) &&
695 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
696 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
697 return alg2;
698 }
699 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
700 return alg1;
701 }
Steven Cooreman03488022021-02-18 12:07:20 +0100702 }
703 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100704 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100705 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
707 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
708 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
709 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
710 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100711 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100712
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100713 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
715 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
716 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
717 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100718 }
719 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
721 (alg1_len <= alg2_len)) {
722 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100723 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
725 (alg2_len <= alg1_len)) {
726 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100727 }
728 }
729 /* If the policies are from the same MAC family, check whether one
730 * of them is a minimum-MAC-length policy. Calculate the most
731 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
733 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
734 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100735 /* Validate the combination of key type and algorithm. Since the base
736 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
738 return 0;
739 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100740
Steven Cooreman31a876d2021-03-03 20:47:40 +0100741 /* Get the (exact or at-least) output lengths for both sides of the
742 * requested intersection. None of the currently supported algorithms
743 * have an output length dependent on the actual key size, so setting it
744 * to a bogus value of 0 is currently OK.
745 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100746 * Note that for at-least-this-length wildcard algorithms, the output
747 * length is set to the shortest allowed length, which allows us to
748 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
750 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100751 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100752
Steven Cooremana1d83222021-02-25 10:20:29 +0100753 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
755 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
756 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100757 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100758
759 /* If only one is an at-least-this-length policy, the intersection would
760 * be the other (fixed-length) policy as long as said fixed length is
761 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
763 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100764 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
766 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100767 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100768
Steven Cooremancd640932021-03-08 12:00:27 +0100769 /* If none of them are wildcards, check whether they define the same tag
770 * length. This is still possible here when one is default-length and
771 * the other specific-length. Ensure to always return the
772 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (alg1_len == alg2_len) {
774 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
775 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100776 }
777 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100779}
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781static int psa_key_algorithm_permits(psa_key_type_t key_type,
782 psa_algorithm_t policy_alg,
783 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200784{
Gilles Peskine549ea862019-05-22 11:45:59 +0200785 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (requested_alg == policy_alg) {
787 return 1;
788 }
Steven Cooreman03488022021-02-18 12:07:20 +0100789 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
790 * and requested_alg is the same hash-and-sign family with any hash,
791 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
793 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
794 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
795 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100796 }
797 /* If policy_alg is a wildcard AEAD algorithm of the same base as
798 * the requested algorithm, check the requested tag length to be
799 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (PSA_ALG_IS_AEAD(policy_alg) &&
801 PSA_ALG_IS_AEAD(requested_alg) &&
802 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
803 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
804 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
805 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
806 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100807 }
Steven Cooremancd640932021-03-08 12:00:27 +0100808 /* If policy_alg is a MAC algorithm of the same base as the requested
809 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (PSA_ALG_IS_MAC(policy_alg) &&
811 PSA_ALG_IS_MAC(requested_alg) &&
812 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
813 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100814 /* Validate the combination of key type and algorithm. Since the policy
815 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
817 return 0;
818 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100819
Steven Cooreman31a876d2021-03-03 20:47:40 +0100820 /* Get both the requested output length for the algorithm which is to be
821 * verified, and the default output length for the base algorithm.
822 * Note that none of the currently supported algorithms have an output
823 * length dependent on actual key size, so setting it to a bogus value
824 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100825 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100827 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 key_type, 0,
829 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100830
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100831 /* If the policy is default-length, only allow an algorithm with
832 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
834 return requested_output_length == default_output_length;
835 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100836
837 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100838 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
840 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
841 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100842 }
843
Steven Cooremancd640932021-03-08 12:00:27 +0100844 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
845 * check for the requested MAC length to be equal to or longer than the
846 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
848 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
849 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100850 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200851 }
Steven Cooremance48e852020-10-05 16:02:45 +0200852 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200853 * a key derivation with that key agreement should also be allowed. This
854 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
856 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
857 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
858 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200859 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100860 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200862}
863
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100864/** Test whether a policy permits an algorithm.
865 *
866 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100867 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100868 * \note This function requires providing the key type for which the policy is
869 * being validated, since some algorithm policy definitions (e.g. MAC)
870 * have different properties depending on what kind of cipher it is
871 * combined with.
872 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100873 * \retval PSA_SUCCESS When \p alg is a specific algorithm
874 * allowed by the \p policy.
875 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
876 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
877 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100878 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100879static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
880 psa_key_type_t key_type,
881 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100882{
Steven Cooremand788fab2021-02-25 11:29:17 +0100883 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (alg == 0) {
885 return PSA_ERROR_INVALID_ARGUMENT;
886 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100887
Steven Cooreman7e39f052021-02-23 14:18:32 +0100888 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (PSA_ALG_IS_WILDCARD(alg)) {
890 return PSA_ERROR_INVALID_ARGUMENT;
891 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
894 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
895 return PSA_SUCCESS;
896 } else {
897 return PSA_ERROR_NOT_PERMITTED;
898 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100899}
900
Gilles Peskinef603c712019-01-19 13:40:11 +0100901/** Restrict a key policy based on a constraint.
902 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100903 * \note This function requires providing the key type for which the policy is
904 * being restricted, since some algorithm policy definitions (e.g. MAC)
905 * have different properties depending on what kind of cipher it is
906 * combined with.
907 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100908 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100909 * \param[in,out] policy The policy to restrict.
910 * \param[in] constraint The policy constraint to apply.
911 *
912 * \retval #PSA_SUCCESS
913 * \c *policy contains the intersection of the original value of
914 * \c *policy and \c *constraint.
915 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100916 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100917 * \c *policy is unchanged.
918 */
919static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100920 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100921 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100923{
924 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 psa_key_policy_algorithm_intersection(key_type, policy->alg,
926 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200927 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
929 constraint->alg2);
930 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
931 return PSA_ERROR_INVALID_ARGUMENT;
932 }
933 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
934 return PSA_ERROR_INVALID_ARGUMENT;
935 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100936 policy->usage &= constraint->usage;
937 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200938 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100940}
941
Przemek Stekiel061f6942022-11-30 10:51:35 +0100942/** Get the description of a key given its identifier and policy constraints
943 * and lock it.
944 *
945 * The key must have allow all the usage flags set in \p usage. If \p alg is
946 * nonzero, the key must allow operations with this algorithm. If \p alg is
947 * zero, the algorithm is not checked.
948 *
949 * In case of a persistent key, the function loads the description of the key
950 * into a key slot if not already done.
951 *
952 * On success, the returned key slot is locked. It is the responsibility of
953 * the caller to unlock the key slot when it does not access it anymore.
954 */
955static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100956 mbedtls_svc_key_id_t key,
957 psa_key_slot_t **p_slot,
958 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100960{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100962 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 status = psa_get_and_lock_key_slot(key, p_slot);
965 if (status != PSA_SUCCESS) {
966 return status;
967 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200968 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100969
970 /* Enforce that usage policy for the key slot contains all the flags
971 * required by the usage parameter. There is one exception: public
972 * keys can always be exported, so we treat public key objects as
973 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100975 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100979 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200980 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100981 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100982
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800983 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if (alg != 0) {
985 status = psa_key_policy_permits(&slot->attr.policy,
986 slot->attr.type,
987 alg);
988 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100989 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100991 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200994
995error:
996 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001000}
Darryl Green940d72c2018-07-13 13:18:51 +01001001
Ronald Cron5c522922020-11-14 16:35:34 +01001002/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001003 *
1004 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001005 * available, as opposed to a key in a secure element and/or to be used
1006 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001007 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001008 * This is a temporary function that may be used instead of
1009 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1010 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001011 *
Ronald Cron5c522922020-11-14 16:35:34 +01001012 * On success, the returned key slot is locked. It is the responsibility of the
1013 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014 */
Ronald Cron5c522922020-11-14 16:35:34 +01001015static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1016 mbedtls_svc_key_id_t key,
1017 psa_key_slot_t **p_slot,
1018 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001020{
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1022 usage, alg);
1023 if (status != PSA_SUCCESS) {
1024 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001025 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1028 psa_unlock_key_slot(*p_slot);
1029 *p_slot = NULL;
1030 return PSA_ERROR_NOT_SUPPORTED;
1031 }
1032
1033 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001034}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001037{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001038 /* Data pointer will always be either a valid pointer or NULL in an
1039 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (slot->key.data != NULL) {
1041 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1042 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001045 slot->key.data = NULL;
1046 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001049}
1050
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001051/** Completely wipe a slot in memory, including its policy.
1052 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001054{
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 /*
1058 * As the return error code may not be handled in case of multiple errors,
1059 * do our best to report an unexpected lock counter. Assert with
1060 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1061 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1062 * function is called as part of the execution of a test suite, the
1063 * execution of the test suite is stopped in error if the assertion fails.
1064 */
1065 if (slot->lock_count != 1) {
1066 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001067 status = PSA_ERROR_CORRUPTION_DETECTED;
1068 }
1069
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001070 /* Multipart operations may still be using the key. This is safe
1071 * because all multipart operation objects are independent from
1072 * the key slot: if they need to access the key after the setup
1073 * phase, they have a copy of the key. Note that this means that
1074 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001075 /* At this point, key material and other type-specific content has
1076 * been wiped. Clear remaining metadata. We can call memset and not
1077 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 memset(slot, 0, sizeof(*slot));
1079 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001080}
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001083{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001084 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001085 psa_status_t status; /* status of the last operation */
1086 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001087#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1088 psa_se_drv_table_entry_t *driver;
1089#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (mbedtls_svc_key_id_is_null(key)) {
1092 return PSA_SUCCESS;
1093 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001094
Ronald Cronf2911112020-10-29 17:51:10 +01001095 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001096 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001097 * key, this will load the key description from persistent memory if not
1098 * done yet. We cannot avoid this loading as without it we don't know if
1099 * the key is operated by an SE or not and this information is needed by
1100 * the current implementation.
1101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 status = psa_get_and_lock_key_slot(key, &slot);
1103 if (status != PSA_SUCCESS) {
1104 return status;
1105 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001106
Ronald Cronf2911112020-10-29 17:51:10 +01001107 /*
1108 * If the key slot containing the key description is under access by the
1109 * library (apart from the present access), the key cannot be destroyed
1110 * yet. For the time being, just return in error. Eventually (to be
1111 * implemented), the key should be destroyed when all accesses have
1112 * stopped.
1113 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (slot->lock_count > 1) {
1115 psa_unlock_key_slot(slot);
1116 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001117 }
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001120 /* Refuse the destruction of a read-only key (which may or may not work
1121 * if we attempt it, depending on whether the key is merely read-only
1122 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001123 * Just do the best we can, which is to wipe the copy in memory
1124 * (done in this function's cleanup code). */
1125 overall_status = PSA_ERROR_NOT_PERMITTED;
1126 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001127 }
1128
Gilles Peskine354f7672019-07-12 23:46:38 +02001129#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1131 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001132 /* For a key in a secure element, we need to do three things:
1133 * remove the key file in internal storage, destroy the
1134 * key inside the secure element, and update the driver's
1135 * persistent data. Start a transaction that will encompass these
1136 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001138 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001140 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 status = psa_crypto_save_transaction();
1142 if (status != PSA_SUCCESS) {
1143 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001144 /* We should still try to destroy the key in the secure
1145 * element and the key metadata in storage. This is especially
1146 * important if the error is that the storage is full.
1147 * But how to do it exactly without risking an inconsistent
1148 * state after a reset?
1149 * https://github.com/ARMmbed/mbed-crypto/issues/215
1150 */
1151 overall_status = status;
1152 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001153 }
1154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 status = psa_destroy_se_key(driver,
1156 psa_key_slot_get_slot_number(slot));
1157 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001158 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001160 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001161#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1162
Darryl Greend49a4992018-06-18 17:27:26 +01001163#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1165 status = psa_destroy_persistent_key(slot->attr.id);
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 Peskine4b7f3402019-08-13 15:58:36 +02001169
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001170 /* TODO: other slots may have a copy of the same key. We should
1171 * invalidate them.
1172 * https://github.com/ARMmbed/mbed-crypto/issues/214
1173 */
Darryl Greend49a4992018-06-18 17:27:26 +01001174 }
1175#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001176
Gilles Peskinefc762652019-07-22 19:30:34 +02001177#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (driver != NULL) {
1179 status = psa_save_se_persistent_data(driver);
1180 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001181 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 }
1183 status = psa_crypto_stop_transaction();
1184 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001185 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001187 }
1188#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1189
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001192 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001194 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 }
1196 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001197}
1198
John Durkop07cc04a2020-11-16 22:08:34 -08001199#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001200 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001201static psa_status_t psa_get_rsa_public_exponent(
1202 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001204{
1205 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001207 uint8_t *buffer = NULL;
1208 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1212 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 }
1215 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001216 /* It's the default value, which is reported as an empty string,
1217 * so there's nothing to do. */
1218 goto exit;
1219 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 buflen = mbedtls_mpi_size(&mpi);
1222 buffer = mbedtls_calloc(1, buflen);
1223 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001224 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1225 goto exit;
1226 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1228 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 attributes->domain_parameters = buffer;
1232 attributes->domain_parameters_size = buflen;
1233
1234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_mpi_free(&mpi);
1236 if (ret != 0) {
1237 mbedtls_free(buffer);
1238 }
1239 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001240}
John Durkop07cc04a2020-11-16 22:08:34 -08001241#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001242 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001243
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001244/** Retrieve all the publicly-accessible attributes of a key.
1245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1247 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001248{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001249 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001250 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001251 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1256 if (status != PSA_SUCCESS) {
1257 return status;
1258 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001259
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001260 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1262 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001263
1264#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1266 psa_set_key_slot_number(attributes,
1267 psa_key_slot_get_slot_number(slot));
1268 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001269#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001272#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001273 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001274 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001275 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001276 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001277 * is not yet implemented.
1278 * https://github.com/ARMmbed/mbed-crypto/issues/216
1279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001281 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001282
Ronald Cron90857082020-11-25 14:58:33 +01001283 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 slot->attr.type,
1285 slot->key.data,
1286 slot->key.bytes,
1287 &rsa);
1288 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001289 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 status = psa_get_rsa_public_exponent(rsa,
1293 attributes);
1294 mbedtls_rsa_free(rsa);
1295 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001296 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001297 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001298#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001299 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001300 default:
1301 /* Nothing else to do. */
1302 break;
1303 }
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (status != PSA_SUCCESS) {
1306 psa_reset_key_attributes(attributes);
1307 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312}
1313
Gilles Peskinec8000c02019-08-02 20:15:51 +02001314#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1315psa_status_t psa_get_key_slot_number(
1316 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318{
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001320 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 return PSA_SUCCESS;
1322 } else {
1323 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001324 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001325}
1326#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1329 size_t key_buffer_size,
1330 uint8_t *data,
1331 size_t data_size,
1332 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001333{
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if (key_buffer_size > data_size) {
1335 return PSA_ERROR_BUFFER_TOO_SMALL;
1336 }
1337 memcpy(data, key_buffer, key_buffer_size);
1338 memset(data + key_buffer_size, 0,
1339 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001340 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001342}
1343
Ronald Cron7285cda2020-11-26 14:46:37 +01001344psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001345 const psa_key_attributes_t *attributes,
1346 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001348{
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 psa_key_type_t type = attributes->core.type;
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if (key_type_is_raw_bytes(type) ||
1352 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001353 PSA_KEY_TYPE_IS_ECC(type) ||
1354 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 return psa_export_key_buffer_internal(
1356 key_buffer, key_buffer_size,
1357 data, data_size, data_length);
1358 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001359 /* This shouldn't happen in the reference implementation, but
1360 it is valid for a special-purpose implementation to omit
1361 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001363 }
1364}
1365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1367 uint8_t *data,
1368 size_t data_size,
1369 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001370{
1371 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1372 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1373 psa_key_slot_t *slot;
1374
Ronald Crone907e552021-01-18 13:22:38 +01001375 /* Reject a zero-length output buffer now, since this can never be a
1376 * valid key representation. This way we know that data must be a valid
1377 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if (data_size == 0) {
1379 return PSA_ERROR_BUFFER_TOO_SMALL;
1380 }
Ronald Crone907e552021-01-18 13:22:38 +01001381
Ronald Cron9486f9d2020-11-26 13:36:23 +01001382 /* Set the key to empty now, so that even when there are errors, we always
1383 * set data_length to a value between 0 and data_size. On error, setting
1384 * the key to empty is a good choice because an empty key representation is
1385 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001386 *data_length = 0;
1387
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 /* Export requires the EXPORT flag. There is an exception for public keys,
1389 * which don't require any flag, but
1390 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1393 PSA_KEY_USAGE_EXPORT, 0);
1394 if (status != PSA_SUCCESS) {
1395 return status;
1396 }
mohammad160306e79202018-03-28 13:17:44 +03001397
Ronald Crond18b5f82020-11-25 16:46:05 +01001398 psa_key_attributes_t attributes = {
1399 .core = slot->attr
1400 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 status = psa_driver_wrapper_export_key(&attributes,
1402 slot->key.data, slot->key.bytes,
1403 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408}
1409
Ronald Cron7285cda2020-11-26 14:46:37 +01001410psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001411 const psa_key_attributes_t *attributes,
1412 const uint8_t *key_buffer,
1413 size_t key_buffer_size,
1414 uint8_t *data,
1415 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001417{
Ronald Crond18b5f82020-11-25 16:46:05 +01001418 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001419
Przemek Stekiel055ffed2023-05-30 12:51:49 +02001420 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
1421 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1422 PSA_KEY_TYPE_IS_DH(type))) {
1423 /* Exporting public -> public */
1424 return psa_export_key_buffer_internal(
1425 key_buffer, key_buffer_size,
1426 data, data_size, data_length);
1427 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001428#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001429 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1430 return mbedtls_psa_rsa_export_public_key(attributes,
1431 key_buffer,
1432 key_buffer_size,
1433 data,
1434 data_size,
1435 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001436#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001437 /* We don't know how to convert a private RSA key to public. */
1438 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001439#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1440 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001441 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001442#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001443 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1444 return mbedtls_psa_ecp_export_public_key(attributes,
1445 key_buffer,
1446 key_buffer_size,
1447 data,
1448 data_size,
1449 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001450#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001451 /* We don't know how to convert a private ECC key to public */
1452 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001453#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1454 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001455 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001456#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
1457 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001458 return mbedtls_psa_export_ffdh_public_key(attributes,
1459 key_buffer,
1460 key_buffer_size,
1461 data, data_size,
1462 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001463#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001464 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001465#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
1466 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001468 (void) key_buffer;
1469 (void) key_buffer_size;
1470 (void) data;
1471 (void) data_size;
1472 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001474 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001475}
Ronald Crond18b5f82020-11-25 16:46:05 +01001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1478 uint8_t *data,
1479 size_t data_size,
1480 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001481{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001482 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001483 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001484 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001485
Ronald Crone907e552021-01-18 13:22:38 +01001486 /* Reject a zero-length output buffer now, since this can never be a
1487 * valid key representation. This way we know that data must be a valid
1488 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (data_size == 0) {
1490 return PSA_ERROR_BUFFER_TOO_SMALL;
1491 }
Ronald Crone907e552021-01-18 13:22:38 +01001492
Darryl Greendd8fb772018-11-07 16:00:44 +00001493 /* Set the key to empty now, so that even when there are errors, we always
1494 * set data_length to a value between 0 and data_size. On error, setting
1495 * the key to empty is a good choice because an empty key representation is
1496 * unlikely to be accepted anywhere. */
1497 *data_length = 0;
1498
1499 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1501 if (status != PSA_SUCCESS) {
1502 return status;
1503 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1506 status = PSA_ERROR_INVALID_ARGUMENT;
1507 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508 }
1509
Ronald Crond18b5f82020-11-25 16:46:05 +01001510 psa_key_attributes_t attributes = {
1511 .core = slot->attr
1512 };
Przemek Stekiel055ffed2023-05-30 12:51:49 +02001513 status = psa_driver_wrapper_export_public_key(
1514 &attributes, slot->key.data, slot->key.bytes,
1515 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001516
1517exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001521}
1522
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001523MBEDTLS_STATIC_ASSERT(
1524 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1525 "One or more key attribute flag is listed as both external-only and dual-use")
1526MBEDTLS_STATIC_ASSERT(
1527 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1528 "One or more key attribute flag is listed as both internal-only and dual-use")
1529MBEDTLS_STATIC_ASSERT(
1530 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1531 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001532
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001533/** Validate that a key policy is internally well-formed.
1534 *
1535 * This function only rejects invalid policies. It does not validate the
1536 * consistency of the policy with respect to other attributes of the key
1537 * such as the key type.
1538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001539static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001540{
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1542 PSA_KEY_USAGE_COPY |
1543 PSA_KEY_USAGE_ENCRYPT |
1544 PSA_KEY_USAGE_DECRYPT |
1545 PSA_KEY_USAGE_SIGN_MESSAGE |
1546 PSA_KEY_USAGE_VERIFY_MESSAGE |
1547 PSA_KEY_USAGE_SIGN_HASH |
1548 PSA_KEY_USAGE_VERIFY_HASH |
1549 PSA_KEY_USAGE_VERIFY_DERIVATION |
1550 PSA_KEY_USAGE_DERIVE)) != 0) {
1551 return PSA_ERROR_INVALID_ARGUMENT;
1552 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001555}
1556
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001557/** Validate the internal consistency of key attributes.
1558 *
1559 * This function only rejects invalid attribute values. If does not
1560 * validate the consistency of the attributes with any key data that may
1561 * be involved in the creation of the key.
1562 *
1563 * Call this function early in the key creation process.
1564 *
1565 * \param[in] attributes Key attributes for the new key.
1566 * \param[out] p_drv On any return, the driver for the key, if any.
1567 * NULL for a transparent key.
1568 *
1569 */
1570static psa_status_t psa_validate_key_attributes(
1571 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001573{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001574 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1576 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 status = psa_validate_key_location(lifetime, p_drv);
1579 if (status != PSA_SUCCESS) {
1580 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001581 }
1582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 status = psa_validate_key_persistence(lifetime);
1584 if (status != PSA_SUCCESS) {
1585 return status;
1586 }
1587
1588 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1589 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1590 return PSA_ERROR_INVALID_ARGUMENT;
1591 }
1592 } else {
1593 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1594 return PSA_ERROR_INVALID_ARGUMENT;
1595 }
1596 }
1597
1598 status = psa_validate_key_policy(&attributes->core.policy);
1599 if (status != PSA_SUCCESS) {
1600 return status;
1601 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001602
1603 /* Refuse to create overly large keys.
1604 * Note that this doesn't trigger on import if the attributes don't
1605 * explicitly specify a size (so psa_get_key_bits returns 0), so
1606 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1608 return PSA_ERROR_NOT_SUPPORTED;
1609 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001610
Gilles Peskine91e8c332019-08-02 19:19:39 +02001611 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1613 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1614 return PSA_ERROR_INVALID_ARGUMENT;
1615 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001618}
1619
Gilles Peskine4747d192019-04-17 15:05:45 +02001620/** Prepare a key slot to receive key material.
1621 *
1622 * This function allocates a key slot and sets its metadata.
1623 *
1624 * If this function fails, call psa_fail_key_creation().
1625 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001626 * This function is intended to be used as follows:
1627 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001628 * it with the specified attributes, and in case of a volatile key assign it
1629 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001630 * -# Populate the slot with the key material.
1631 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1632 * In case of failure at any step, stop the sequence and call
1633 * psa_fail_key_creation().
1634 *
Ronald Cron5c522922020-11-14 16:35:34 +01001635 * On success, the key slot is locked. It is the responsibility of the caller
1636 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001637 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001638 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001639 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001640 * \param[out] p_slot On success, a pointer to the prepared slot.
1641 * \param[out] p_drv On any return, the driver for the key, if any.
1642 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001643 *
1644 * \retval #PSA_SUCCESS
1645 * The key slot is ready to receive key material.
1646 * \return If this function fails, the key slot is an invalid state.
1647 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001648 */
1649static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001650 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001651 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001652 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001654{
1655 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001656 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001657 psa_key_slot_t *slot;
1658
Gilles Peskinedf179142019-07-15 22:02:14 +02001659 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001660 *p_drv = NULL;
1661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 status = psa_validate_key_attributes(attributes, p_drv);
1663 if (status != PSA_SUCCESS) {
1664 return status;
1665 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1668 if (status != PSA_SUCCESS) {
1669 return status;
1670 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001671 slot = *p_slot;
1672
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001673 /* We're storing the declared bit-size of the key. It's up to each
1674 * creation mechanism to verify that this information is correct.
1675 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001676 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001677 * is optional (import, copy). In case of a volatile key, assign it the
1678 * volatile key identifier associated to the slot returned to contain its
1679 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001680
1681 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001683#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1684 slot->attr.id = volatile_key_id;
1685#else
1686 slot->attr.id.key_id = volatile_key_id;
1687#endif
1688 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001689
Gilles Peskine91e8c332019-08-02 19:19:39 +02001690 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001691 * external-only flags, query `attributes`. Thanks to the check
1692 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001693 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001694 * may have set. */
1695 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001696
Gilles Peskinecbaff462019-07-12 23:46:04 +02001697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001698 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001699 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001700 * create the key file in internal storage, create the
1701 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001702 * persistent data. This is done by starting a transaction that will
1703 * encompass these three actions.
1704 * For registering a volatile key, we just need to find an appropriate
1705 * slot number inside the SE. Since the key is designated volatile, creating
1706 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001707 /* The first thing to do is to find a slot number for the new key.
1708 * We save the slot number in persistent storage as part of the
1709 * transaction data. It will be needed to recover if the power
1710 * fails during the key creation process, to clean up on the secure
1711 * element side after restarting. Obtaining a slot number from the
1712 * secure element driver updates its persistent state, but we do not yet
1713 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001714 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001716 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1718 &slot_number);
1719 if (status != PSA_SUCCESS) {
1720 return status;
1721 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001722
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1724 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001725 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001726 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001727 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 status = psa_crypto_save_transaction();
1729 if (status != PSA_SUCCESS) {
1730 (void) psa_crypto_stop_transaction();
1731 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001732 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001733 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001734
1735 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001737 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001740 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001742 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001743#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001746}
Gilles Peskine4747d192019-04-17 15:05:45 +02001747
1748/** Finalize the creation of a key once its key material has been set.
1749 *
1750 * This entails writing the key to persistent storage.
1751 *
1752 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001753 * See the documentation of psa_start_key_creation() for the intended use
1754 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001755 *
Ronald Cron5c522922020-11-14 16:35:34 +01001756 * If the finalization succeeds, the function unlocks the key slot (it was
1757 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1758 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001759 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001760 * \param[in,out] slot Pointer to the slot with key material.
1761 * \param[in] driver The secure element driver for the key,
1762 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001763 * \param[out] key On success, identifier of the key. Note that the
1764 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001765 *
1766 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001767 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001768 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1769 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1770 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1771 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1772 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1773 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001774 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001775 * \return If this function fails, the key slot is an invalid state.
1776 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001777 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001778static psa_status_t psa_finish_key_creation(
1779 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001780 psa_se_drv_table_entry_t *driver,
1781 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001782{
1783 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001784 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001785 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001786
1787#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001789#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001791 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001792 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001794
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001795 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1796 sizeof(data.slot_number),
1797 "Slot number size does not match psa_se_key_data_storage_t");
1798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1800 status = psa_save_persistent_key(&slot->attr,
1801 (uint8_t *) &data,
1802 sizeof(data));
1803 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001804#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1805 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001806 /* Key material is saved in export representation in the slot, so
1807 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 status = psa_save_persistent_key(&slot->attr,
1809 slot->key.data,
1810 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001811 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001812 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001813#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1814
Gilles Peskinecbaff462019-07-12 23:46:04 +02001815#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001816 /* Finish the transaction for a key creation. This does not
1817 * happen when registering an existing key. Detect this case
1818 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001819 * creation of a persistent key in a secure element requires a transaction,
1820 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 if (driver != NULL &&
1822 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1823 status = psa_save_se_persistent_data(driver);
1824 if (status != PSA_SUCCESS) {
1825 psa_destroy_persistent_key(slot->attr.id);
1826 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001827 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001829 }
1830#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001833 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 status = psa_unlock_key_slot(slot);
1835 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001836 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001838 }
Ronald Cron50972942020-11-14 11:28:25 +01001839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001841}
1842
1843/** Abort the creation of a key.
1844 *
1845 * You may call this function after calling psa_start_key_creation(),
1846 * or after psa_finish_key_creation() fails. In other circumstances, this
1847 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001848 * See the documentation of psa_start_key_creation() for the intended use
1849 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001850 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001851 * \param[in,out] slot Pointer to the slot with key material.
1852 * \param[in] driver The secure element driver for the key,
1853 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001855static void psa_fail_key_creation(psa_key_slot_t *slot,
1856 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001857{
Gilles Peskine011e4282019-06-26 18:34:38 +02001858 (void) driver;
1859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001861 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001863
1864#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001865 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001866 * element, and the failure happened later (when saving metadata
1867 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001868 * element.
1869 * https://github.com/ARMmbed/mbed-crypto/issues/217
1870 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001871
Gilles Peskined7729582019-08-05 15:55:54 +02001872 /* Abort the ongoing transaction if any (there may not be one if
1873 * the creation process failed before starting one, or if the
1874 * key creation is a registration of a key in a secure element).
1875 * Earlier functions must already have done what it takes to undo any
1876 * partial creation. All that's left is to update the transaction data
1877 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001879#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001882}
1883
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001884/** Validate optional attributes during key creation.
1885 *
1886 * Some key attributes are optional during key creation. If they are
1887 * specified in the attributes structure, check that they are consistent
1888 * with the data in the slot.
1889 *
1890 * This function should be called near the end of key creation, after
1891 * the slot in memory is fully populated but before saving persistent data.
1892 */
1893static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001894 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896{
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 if (attributes->core.type != 0) {
1898 if (attributes->core.type != slot->attr.type) {
1899 return PSA_ERROR_INVALID_ARGUMENT;
1900 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001901 }
1902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001904#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1906 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001907 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001908 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001910
Ronald Cron90857082020-11-25 14:58:33 +01001911 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 slot->attr.type,
1913 slot->key.data,
1914 slot->key.bytes,
1915 &rsa);
1916 if (status != PSA_SUCCESS) {
1917 return status;
1918 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 mbedtls_mpi_init(&actual);
1921 mbedtls_mpi_init(&required);
1922 ret = mbedtls_rsa_export(rsa,
1923 NULL, NULL, NULL, NULL, &actual);
1924 mbedtls_rsa_free(rsa);
1925 mbedtls_free(rsa);
1926 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001927 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 }
1929 ret = mbedtls_mpi_read_binary(&required,
1930 attributes->domain_parameters,
1931 attributes->domain_parameters_size);
1932 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001933 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 }
1935 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001936 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 }
1938rsa_exit:
1939 mbedtls_mpi_free(&actual);
1940 mbedtls_mpi_free(&required);
1941 if (ret != 0) {
1942 return mbedtls_to_psa_error(ret);
1943 }
1944 } else
John Durkop9814fa22020-11-04 12:28:15 -08001945#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1946 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949 }
1950 }
1951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 if (attributes->core.bits != 0) {
1953 if (attributes->core.bits != slot->attr.bits) {
1954 return PSA_ERROR_INVALID_ARGUMENT;
1955 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001956 }
1957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001959}
1960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1962 const uint8_t *data,
1963 size_t data_length,
1964 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001965{
1966 psa_status_t status;
1967 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001968 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001969 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301970 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001971
Ronald Cron81709fc2020-11-14 12:10:32 +01001972 *key = MBEDTLS_SVC_KEY_ID_INIT;
1973
Gilles Peskine0f84d622019-09-12 19:03:13 +02001974 /* Reject zero-length symmetric keys (including raw data key objects).
1975 * This also rejects any key which might be encoded as an empty string,
1976 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 if (data_length == 0) {
1978 return PSA_ERROR_INVALID_ARGUMENT;
1979 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001980
Archana449608b2021-09-08 15:36:05 +05301981 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 if (data_length > SIZE_MAX / 8) {
1983 return PSA_ERROR_NOT_SUPPORTED;
1984 }
Archana6ed4bda2021-08-04 10:47:15 +05301985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1987 &slot, &driver);
1988 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001992 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301993 * storage ( thus not in the case of importing a key in a secure element
1994 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1995 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 if (slot->key.data == NULL) {
1997 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301998 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 attributes, data, data_length, &storage_size);
2000 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302001 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 }
Archanad8a83dc2021-06-14 10:04:16 +05302003 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 status = psa_allocate_buffer_to_slot(slot, storage_size);
2005 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002006 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002008 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002009
Przemek Stekiel055ffed2023-05-30 12:51:49 +02002010 bits = slot->attr.bits;
2011 status = psa_driver_wrapper_import_key(attributes,
2012 data, data_length,
2013 slot->key.data,
2014 slot->key.bytes,
2015 &slot->key.bytes, &bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002017 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002021 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002023 status = PSA_ERROR_INVALID_ARGUMENT;
2024 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002025 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002026
Archana6ed4bda2021-08-04 10:47:15 +05302027 /* Enforce a size limit, and in particular ensure that the bit
2028 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302030 status = PSA_ERROR_NOT_SUPPORTED;
2031 goto exit;
2032 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 status = psa_validate_optional_attributes(slot, attributes);
2034 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002035 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002039exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 if (status != PSA_SUCCESS) {
2041 psa_fail_key_creation(slot, driver);
2042 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002045}
2046
Gilles Peskined7729582019-08-05 15:55:54 +02002047#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2048psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002050{
2051 psa_status_t status;
2052 psa_key_slot_t *slot = NULL;
2053 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002054 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002055
2056 /* Leaving attributes unspecified is not currently supported.
2057 * It could make sense to query the key type and size from the
2058 * secure element, but not all secure elements support this
2059 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2061 return PSA_ERROR_NOT_SUPPORTED;
2062 }
2063 if (psa_get_key_bits(attributes) == 0) {
2064 return PSA_ERROR_NOT_SUPPORTED;
2065 }
Gilles Peskined7729582019-08-05 15:55:54 +02002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2068 &slot, &driver);
2069 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002070 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 }
Gilles Peskined7729582019-08-05 15:55:54 +02002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002074
2075exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 if (status != PSA_SUCCESS) {
2077 psa_fail_key_creation(slot, driver);
2078 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002079
Gilles Peskined7729582019-08-05 15:55:54 +02002080 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 psa_close_key(key);
2082 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002083}
2084#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2087 const psa_key_attributes_t *specified_attributes,
2088 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002089{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002090 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002091 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002092 psa_key_slot_t *source_slot = NULL;
2093 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002095 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302096 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002097
Ronald Cron81709fc2020-11-14 12:10:32 +01002098 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2099
Archana8a180362021-07-05 02:18:48 +05302100 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2102 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 status = psa_validate_optional_attributes(source_slot,
2107 specified_attributes);
2108 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002111
Archana9d17bf42021-09-10 06:22:44 +05302112 /* The target key type and number of bits have been validated by
2113 * psa_validate_optional_attributes() to be either equal to zero or
2114 * equal to the ones of the source key. So it is safe to inherit
2115 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302116 * */
Archana9d17bf42021-09-10 06:22:44 +05302117 actual_attributes.core.bits = source_slot->attr.bits;
2118 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302119
2120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 status = psa_restrict_key_policy(source_slot->attr.type,
2122 &actual_attributes.core.policy,
2123 &source_slot->attr.policy);
2124 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2129 &target_slot, &driver);
2130 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 }
2133 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2134 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302135 /*
Archana9d17bf42021-09-10 06:22:44 +05302136 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302137 * the source key would need to be exported as plaintext and re-imported
2138 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302139 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302140 * appropriate API invocations from the application, if needed.
2141 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002142 status = PSA_ERROR_NOT_SUPPORTED;
2143 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002144 }
Archana8a180362021-07-05 02:18:48 +05302145 /*
2146 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302147 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302148 * - For opaque keys this translates to an invocation of the drivers'
2149 * copy_key entry point through the dispatch layer.
2150 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2152 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2153 &storage_size);
2154 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 }
Archana374fe5b2021-09-08 15:50:28 +05302157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2159 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 }
Archana374fe5b2021-09-08 15:50:28 +05302162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 status = psa_driver_wrapper_copy_key(&actual_attributes,
2164 source_slot->key.data,
2165 source_slot->key.bytes,
2166 target_slot->key.data,
2167 target_slot->key.bytes,
2168 &target_slot->key.bytes);
2169 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302170 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 }
2172 } else {
2173 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302174 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 source_slot->key.bytes);
2176 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302177 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 }
Archana8a180362021-07-05 02:18:48 +05302179 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002181exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if (status != PSA_SUCCESS) {
2183 psa_fail_key_creation(target_slot, driver);
2184 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002185
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002189}
2190
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002191
2192
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002193/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002194/* Message digests */
2195/****************************************************************/
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002198{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002199 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (operation->id == 0) {
2201 return PSA_SUCCESS;
2202 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002205 operation->id = 0;
2206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002208}
2209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2211 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002214
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002215 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002217 status = PSA_ERROR_BAD_STATE;
2218 goto exit;
2219 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002222 status = PSA_ERROR_INVALID_ARGUMENT;
2223 goto exit;
2224 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002225
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002226 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2227 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002231
2232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if (status != PSA_SUCCESS) {
2234 psa_hash_abort(operation);
2235 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002236
2237 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238}
2239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2241 const uint8_t *input,
2242 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002243{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002244 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002247 status = PSA_ERROR_BAD_STATE;
2248 goto exit;
2249 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002250
Steven Cooremanc8288352021-03-04 14:02:19 +01002251 /* Don't require hash implementations to behave correctly on a
2252 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if (input_length == 0) {
2254 return PSA_SUCCESS;
2255 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002258
2259exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 if (status != PSA_SUCCESS) {
2261 psa_hash_abort(operation);
2262 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265}
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2268 uint8_t *hash,
2269 size_t hash_size,
2270 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002272 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (operation->id == 0) {
2274 return PSA_ERROR_BAD_STATE;
2275 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002276
Steven Cooreman1e582352021-02-18 17:24:37 +01002277 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 operation, hash, hash_size, hash_length);
2279 psa_hash_abort(operation);
2280 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281}
2282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2284 const uint8_t *hash,
2285 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286{
Ronald Cron69a63422021-10-18 09:47:58 +02002287 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002289 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 operation,
2291 actual_hash, sizeof(actual_hash),
2292 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002295 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002299 status = PSA_ERROR_INVALID_SIGNATURE;
2300 goto exit;
2301 }
2302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002304 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002306
2307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2309 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002310 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314}
2315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316psa_status_t psa_hash_compute(psa_algorithm_t alg,
2317 const uint8_t *input, size_t input_length,
2318 uint8_t *hash, size_t hash_size,
2319 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002320{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002321 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (!PSA_ALG_IS_HASH(alg)) {
2323 return PSA_ERROR_INVALID_ARGUMENT;
2324 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2327 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002328}
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330psa_status_t psa_hash_compare(psa_algorithm_t alg,
2331 const uint8_t *input, size_t input_length,
2332 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002333{
Ronald Cron69a63422021-10-18 09:47:58 +02002334 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002335 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (!PSA_ALG_IS_HASH(alg)) {
2338 return PSA_ERROR_INVALID_ARGUMENT;
2339 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002340
Steven Cooreman1e582352021-02-18 17:24:37 +01002341 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 alg, input, input_length,
2343 actual_hash, sizeof(actual_hash),
2344 &actual_hash_length);
2345 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002346 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 }
2348 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002349 status = PSA_ERROR_INVALID_SIGNATURE;
2350 goto exit;
2351 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002353 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002355
2356exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2358 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002359}
2360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2362 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002363{
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (source_operation->id == 0 ||
2365 target_operation->id != 0) {
2366 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002367 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2370 target_operation);
2371 if (status != PSA_SUCCESS) {
2372 psa_hash_abort(target_operation);
2373 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002376}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002377
2378
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002379/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002380/* MAC */
2381/****************************************************************/
2382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002384{
Steven Cooremane6804192021-03-19 18:28:56 +01002385 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (operation->id == 0) {
2387 return PSA_SUCCESS;
2388 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002391 operation->mac_size = 0;
2392 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002393 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002396}
2397
Ronald Cron2dff3b22021-06-17 16:33:22 +02002398static psa_status_t psa_mac_finalize_alg_and_key_validation(
2399 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002400 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002402{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 psa_key_type_t key_type = psa_get_key_type(attributes);
2405 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (!PSA_ALG_IS_MAC(alg)) {
2408 return PSA_ERROR_INVALID_ARGUMENT;
2409 }
Steven Cooremane6804192021-03-19 18:28:56 +01002410
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002411 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 status = psa_mac_key_can_do(alg, key_type);
2413 if (status != PSA_SUCCESS) {
2414 return status;
2415 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002416
Steven Cooremand1a68f12021-05-10 11:13:41 +02002417 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002421 /* A very short MAC is too short for security since it can be
2422 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2423 * so we make this our minimum, even though 32 bits is still
2424 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002426 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2429 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002430 /* It's impossible to "truncate" to a larger length than the full length
2431 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002433 }
2434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002436 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2437 * that is disabled in the compile-time configuration. The result can
2438 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2439 * configuration into account. In this case, force a return of
2440 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2441 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2442 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2443 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2444 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002446 }
2447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002449}
2450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2452 mbedtls_svc_key_id_t key,
2453 psa_algorithm_t alg,
2454 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002455{
2456 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2457 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002458 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459
2460 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002462 status = PSA_ERROR_BAD_STATE;
2463 goto exit;
2464 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002465
2466 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 key,
2468 &slot,
2469 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2470 alg);
2471 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002472 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002474
2475 psa_key_attributes_t attributes = {
2476 .core = slot->attr
2477 };
2478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2480 &operation->mac_size);
2481 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002482 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484
2485 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002486 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 if (is_sign) {
2488 status = psa_driver_wrapper_mac_sign_setup(operation,
2489 &attributes,
2490 slot->key.data,
2491 slot->key.bytes,
2492 alg);
2493 } else {
2494 status = psa_driver_wrapper_mac_verify_setup(operation,
2495 &attributes,
2496 slot->key.data,
2497 slot->key.bytes,
2498 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002500
Gilles Peskinefbfac682018-07-08 20:51:54 +02002501exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if (status != PSA_SUCCESS) {
2503 psa_mac_abort(operation);
2504 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509}
2510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2512 mbedtls_svc_key_id_t key,
2513 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002514{
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002516}
2517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2519 mbedtls_svc_key_id_t key,
2520 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002521{
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002523}
2524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2526 const uint8_t *input,
2527 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528{
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (operation->id == 0) {
2530 return PSA_ERROR_BAD_STATE;
2531 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002533 /* Don't require hash implementations to behave correctly on a
2534 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (input_length == 0) {
2536 return PSA_SUCCESS;
2537 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2540 input, input_length);
2541 if (status != PSA_SUCCESS) {
2542 psa_mac_abort(operation);
2543 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546}
2547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2549 uint8_t *mac,
2550 size_t mac_size,
2551 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002552{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2554 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002557 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002558 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002559 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002562 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002563 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002564 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002565
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002566 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2567 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002569 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002570 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002571 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002574 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002575 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002576 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 status = psa_driver_wrapper_mac_sign_finish(operation,
2579 mac, operation->mac_size,
2580 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002581
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002582exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002583 /* In case of success, set the potential excess room in the output buffer
2584 * to an invalid value, to avoid potentially leaking a longer MAC.
2585 * In case of error, set the output length and content to a safe default,
2586 * such that in case the caller misses an error check, the output would be
2587 * an unachievable MAC.
2588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002590 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002591 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002592 }
mohammad16036df908f2018-04-02 08:34:15 -07002593
Paul Elliottdc42ca82023-02-24 18:11:59 +00002594 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002599}
2600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2602 const uint8_t *mac,
2603 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002604{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002605 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2606 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002609 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002610 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002611 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002614 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002615 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002616 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002619 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002620 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002621 }
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 status = psa_driver_wrapper_mac_verify_finish(operation,
2624 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002625
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002626exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002630}
2631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2633 psa_algorithm_t alg,
2634 const uint8_t *input,
2635 size_t input_length,
2636 uint8_t *mac,
2637 size_t mac_size,
2638 size_t *mac_length,
2639 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002640{
2641 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002642 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2643 psa_key_slot_t *slot;
2644 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002645
Ronald Cron51131b52021-06-17 17:17:20 +02002646 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 key,
2648 &slot,
2649 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2650 alg);
2651 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002652 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002654
Ronald Cron51131b52021-06-17 17:17:20 +02002655 psa_key_attributes_t attributes = {
2656 .core = slot->attr
2657 };
2658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2660 &operation_mac_size);
2661 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002662 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002666 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002667 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002668 }
2669
2670 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 &attributes,
2672 slot->key.data, slot->key.bytes,
2673 alg,
2674 input, input_length,
2675 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002676
2677exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002678 /* In case of success, set the potential excess room in the output buffer
2679 * to an invalid value, to avoid potentially leaking a longer MAC.
2680 * In case of error, set the output length and content to a safe default,
2681 * such that in case the caller misses an error check, the output would be
2682 * an unachievable MAC.
2683 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002685 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002686 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002687 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002688
2689 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002694}
2695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002697 psa_algorithm_t alg,
2698 const uint8_t *input,
2699 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 uint8_t *mac,
2701 size_t mac_size,
2702 size_t *mac_length)
2703{
2704 return psa_mac_compute_internal(key, alg,
2705 input, input_length,
2706 mac, mac_size, mac_length, 1);
2707}
2708
2709psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2710 psa_algorithm_t alg,
2711 const uint8_t *input,
2712 size_t input_length,
2713 const uint8_t *mac,
2714 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002715{
2716 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002717 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2718 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 status = psa_mac_compute_internal(key, alg,
2721 input, input_length,
2722 actual_mac, sizeof(actual_mac),
2723 &actual_mac_length, 0);
2724 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002725 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002729 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002730 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002731 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
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 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002736
2737exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002741}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002742
Gilles Peskinee59236f2018-01-27 23:32:46 +01002743/****************************************************************/
2744/* Asymmetric cryptography */
2745/****************************************************************/
2746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2748 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002749{
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 if (input_is_message) {
2751 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2752 return PSA_ERROR_INVALID_ARGUMENT;
2753 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2756 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2757 return PSA_ERROR_INVALID_ARGUMENT;
2758 }
2759 }
2760 } else {
2761 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2762 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002763 }
2764 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002765
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002767}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2770 int input_is_message,
2771 psa_algorithm_t alg,
2772 const uint8_t *input,
2773 size_t input_length,
2774 uint8_t *signature,
2775 size_t signature_size,
2776 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002777{
2778 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2779 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2780 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002781
2782 *signature_length = 0;
2783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 status = psa_sign_verify_check_alg(input_is_message, alg);
2785 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002786 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002788
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002789 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002790 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002791 * buffer can in principle be empty since it doesn't actually have
2792 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 if (signature_size == 0) {
2794 return PSA_ERROR_BUFFER_TOO_SMALL;
2795 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002796
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002797 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 key, &slot,
2799 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2800 PSA_KEY_USAGE_SIGN_HASH,
2801 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002804 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002808 status = PSA_ERROR_INVALID_ARGUMENT;
2809 goto exit;
2810 }
2811
2812 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002814 };
2815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002817 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002818 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002819 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 signature, signature_size, signature_length);
2821 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002822
2823 status = psa_driver_wrapper_sign_hash(
2824 &attributes, slot->key.data, slot->key.bytes,
2825 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002827 }
2828
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002829
2830exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002831 psa_wipe_tag_output_buffer(signature, status, signature_size,
2832 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002837}
2838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2840 int input_is_message,
2841 psa_algorithm_t alg,
2842 const uint8_t *input,
2843 size_t input_length,
2844 const uint8_t *signature,
2845 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002846{
2847 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2848 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2849 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 status = psa_sign_verify_check_alg(input_is_message, alg);
2852 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002853 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002855
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002856 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 key, &slot,
2858 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2859 PSA_KEY_USAGE_VERIFY_HASH,
2860 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 if (status != PSA_SUCCESS) {
2863 return status;
2864 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002865
2866 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002868 };
2869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002871 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002872 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002873 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 signature, signature_length);
2875 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002876 status = psa_driver_wrapper_verify_hash(
2877 &attributes, slot->key.data, slot->key.bytes,
2878 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002880 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002883
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002885
2886}
2887
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002888psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002889 const psa_key_attributes_t *attributes,
2890 const uint8_t *key_buffer,
2891 size_t key_buffer_size,
2892 psa_algorithm_t alg,
2893 const uint8_t *input,
2894 size_t input_length,
2895 uint8_t *signature,
2896 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002898{
2899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002902 size_t hash_length;
2903 uint8_t hash[PSA_HASH_MAX_SIZE];
2904
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002905 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 PSA_ALG_SIGN_GET_HASH(alg),
2907 input, input_length,
2908 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002911 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002913
2914 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 attributes, key_buffer, key_buffer_size,
2916 alg, hash, hash_length,
2917 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002918 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002921}
2922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2924 psa_algorithm_t alg,
2925 const uint8_t *input,
2926 size_t input_length,
2927 uint8_t *signature,
2928 size_t signature_size,
2929 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002930{
2931 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002932 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002934}
2935
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002936psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002937 const psa_key_attributes_t *attributes,
2938 const uint8_t *key_buffer,
2939 size_t key_buffer_size,
2940 psa_algorithm_t alg,
2941 const uint8_t *input,
2942 size_t input_length,
2943 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002945{
2946 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002949 size_t hash_length;
2950 uint8_t hash[PSA_HASH_MAX_SIZE];
2951
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002952 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 PSA_ALG_SIGN_GET_HASH(alg),
2954 input, input_length,
2955 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002958 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002960
2961 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 attributes, key_buffer, key_buffer_size,
2963 alg, hash, hash_length,
2964 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002965 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002968}
2969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2971 psa_algorithm_t alg,
2972 const uint8_t *input,
2973 size_t input_length,
2974 const uint8_t *signature,
2975 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002976{
2977 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002978 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002980}
2981
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002982psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002983 const psa_key_attributes_t *attributes,
2984 const uint8_t *key_buffer, size_t key_buffer_size,
2985 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002987{
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2989 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2990 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002991#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2993 return mbedtls_psa_rsa_sign_hash(
2994 attributes,
2995 key_buffer, key_buffer_size,
2996 alg, hash, hash_length,
2997 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002998#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2999 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 } else {
3001 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003002 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3004 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003005#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3007 return mbedtls_psa_ecdsa_sign_hash(
3008 attributes,
3009 key_buffer, key_buffer_size,
3010 alg, hash, hash_length,
3011 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003012#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3013 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 } else {
3015 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003016 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003017 }
Ronald Cron4501c982021-03-19 20:09:32 +01003018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 (void) key_buffer;
3020 (void) key_buffer_size;
3021 (void) hash;
3022 (void) hash_length;
3023 (void) signature;
3024 (void) signature_size;
3025 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003026
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003028}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3031 psa_algorithm_t alg,
3032 const uint8_t *hash,
3033 size_t hash_length,
3034 uint8_t *signature,
3035 size_t signature_size,
3036 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003037{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003038 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003039 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003041}
3042
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003043psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003044 const psa_key_attributes_t *attributes,
3045 const uint8_t *key_buffer, size_t key_buffer_size,
3046 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003048{
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3050 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3051 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003052#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3054 return mbedtls_psa_rsa_verify_hash(
3055 attributes,
3056 key_buffer, key_buffer_size,
3057 alg, hash, hash_length,
3058 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003059#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3060 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 } else {
3062 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003063 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3065 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003066#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3068 return mbedtls_psa_ecdsa_verify_hash(
3069 attributes,
3070 key_buffer, key_buffer_size,
3071 alg, hash, hash_length,
3072 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003073#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3074 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 } else {
3076 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003077 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003078 }
Ronald Cron4501c982021-03-19 20:09:32 +01003079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 (void) key_buffer;
3081 (void) key_buffer_size;
3082 (void) hash;
3083 (void) hash_length;
3084 (void) signature;
3085 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003088}
3089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3091 psa_algorithm_t alg,
3092 const uint8_t *hash,
3093 size_t hash_length,
3094 const uint8_t *signature,
3095 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003096{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003097 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003098 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003100}
3101
Gilles Peskine449bd832023-01-11 14:50:10 +01003102psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3103 psa_algorithm_t alg,
3104 const uint8_t *input,
3105 size_t input_length,
3106 const uint8_t *salt,
3107 size_t salt_length,
3108 uint8_t *output,
3109 size_t output_size,
3110 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003111{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003113 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003114 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003115
Darryl Green5cc689a2018-07-24 15:34:10 +01003116 (void) input;
3117 (void) input_length;
3118 (void) salt;
3119 (void) output;
3120 (void) output_size;
3121
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003122 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003123
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3125 return PSA_ERROR_INVALID_ARGUMENT;
3126 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003127
Ronald Cron5c522922020-11-14 16:35:34 +01003128 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3130 if (status != PSA_SUCCESS) {
3131 return status;
3132 }
3133 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3134 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003135 status = PSA_ERROR_INVALID_ARGUMENT;
3136 goto exit;
3137 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003138
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003139 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003141 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003142
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003143 status = psa_driver_wrapper_asymmetric_encrypt(
3144 &attributes, slot->key.data, slot->key.bytes,
3145 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003151}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3154 psa_algorithm_t alg,
3155 const uint8_t *input,
3156 size_t input_length,
3157 const uint8_t *salt,
3158 size_t salt_length,
3159 uint8_t *output,
3160 size_t output_size,
3161 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003162{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003163 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003164 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003165 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003166
Darryl Green5cc689a2018-07-24 15:34:10 +01003167 (void) input;
3168 (void) input_length;
3169 (void) salt;
3170 (void) output;
3171 (void) output_size;
3172
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003173 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003174
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3176 return PSA_ERROR_INVALID_ARGUMENT;
3177 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003178
Ronald Cron5c522922020-11-14 16:35:34 +01003179 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3181 if (status != PSA_SUCCESS) {
3182 return status;
3183 }
3184 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003185 status = PSA_ERROR_INVALID_ARGUMENT;
3186 goto exit;
3187 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003188
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003189 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003191 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003193 status = psa_driver_wrapper_asymmetric_decrypt(
3194 &attributes, slot->key.data, slot->key.bytes,
3195 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003197
3198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003202}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003203
Paul Elliott9fe12f62022-11-30 19:16:02 +00003204/****************************************************************/
3205/* Asymmetric interruptible cryptography */
3206/****************************************************************/
3207
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003208static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3209
Paul Elliott9fe12f62022-11-30 19:16:02 +00003210void psa_interruptible_set_max_ops(uint32_t max_ops)
3211{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003212 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003213}
3214
3215uint32_t psa_interruptible_get_max_ops(void)
3216{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003217 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003218}
3219
Paul Elliott9fe12f62022-11-30 19:16:02 +00003220uint32_t psa_sign_hash_get_num_ops(
3221 const psa_sign_hash_interruptible_operation_t *operation)
3222{
Paul Elliott296ede92022-12-15 17:00:30 +00003223 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003224}
3225
3226uint32_t psa_verify_hash_get_num_ops(
3227 const psa_verify_hash_interruptible_operation_t *operation)
3228{
Paul Elliott296ede92022-12-15 17:00:30 +00003229 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003230}
3231
Paul Elliott59ad9452022-12-18 15:09:02 +00003232static psa_status_t psa_sign_hash_abort_internal(
3233 psa_sign_hash_interruptible_operation_t *operation)
3234{
3235 if (operation->id == 0) {
3236 /* The object has (apparently) been initialized but it is not (yet)
3237 * in use. It's ok to call abort on such an object, and there's
3238 * nothing to do. */
3239 return PSA_SUCCESS;
3240 }
3241
3242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3243
3244 status = psa_driver_wrapper_sign_hash_abort(operation);
3245
3246 operation->id = 0;
3247
Paul Elliotte6145dc2023-02-07 12:51:21 +00003248 /* Do not clear either the error_occurred or num_ops elements here as they
3249 * only want to be cleared by the application calling abort, not by abort
3250 * being called at completion of an operation. */
3251
Paul Elliott59ad9452022-12-18 15:09:02 +00003252 return status;
3253}
3254
Paul Elliott9fe12f62022-11-30 19:16:02 +00003255psa_status_t psa_sign_hash_start(
3256 psa_sign_hash_interruptible_operation_t *operation,
3257 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3258 const uint8_t *hash, size_t hash_length)
3259{
3260 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3261 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3262 psa_key_slot_t *slot;
3263
Paul Elliottc9774412023-02-06 15:14:07 +00003264 /* Check that start has not been previously called, or operation has not
3265 * previously errored. */
3266 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003267 return PSA_ERROR_BAD_STATE;
3268 }
3269
Paul Elliott9fe12f62022-11-30 19:16:02 +00003270 status = psa_sign_verify_check_alg(0, alg);
3271 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003272 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003273 return status;
3274 }
3275
3276 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3277 PSA_KEY_USAGE_SIGN_HASH,
3278 alg);
3279
3280 if (status != PSA_SUCCESS) {
3281 goto exit;
3282 }
3283
3284 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3285 status = PSA_ERROR_INVALID_ARGUMENT;
3286 goto exit;
3287 }
3288
3289 psa_key_attributes_t attributes = {
3290 .core = slot->attr
3291 };
3292
Paul Elliott296ede92022-12-15 17:00:30 +00003293 /* Ensure ops count gets reset, in case of operation re-use. */
3294 operation->num_ops = 0;
3295
Paul Elliott9fe12f62022-11-30 19:16:02 +00003296 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3297 slot->key.data,
3298 slot->key.bytes, alg,
3299 hash, hash_length);
3300exit:
3301
3302 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003303 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003304 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003305 }
3306
3307 unlock_status = psa_unlock_key_slot(slot);
3308
Paul Elliottc9774412023-02-06 15:14:07 +00003309 if (unlock_status != PSA_SUCCESS) {
3310 operation->error_occurred = 1;
3311 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003312
Paul Elliottc9774412023-02-06 15:14:07 +00003313 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003314}
3315
3316
3317psa_status_t psa_sign_hash_complete(
3318 psa_sign_hash_interruptible_operation_t *operation,
3319 uint8_t *signature, size_t signature_size,
3320 size_t *signature_length)
3321{
3322 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3323
3324 *signature_length = 0;
3325
Paul Elliottc9774412023-02-06 15:14:07 +00003326 /* Check that start has been called first, and that operation has not
3327 * previously errored. */
3328 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003329 status = PSA_ERROR_BAD_STATE;
3330 goto exit;
3331 }
3332
Paul Elliott096abc42023-02-03 18:33:23 +00003333 /* Immediately reject a zero-length signature buffer. This guarantees that
3334 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335 if (signature_size == 0) {
3336 status = PSA_ERROR_BUFFER_TOO_SMALL;
3337 goto exit;
3338 }
3339
3340 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3341 signature_size,
3342 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003343
Paul Elliott296ede92022-12-15 17:00:30 +00003344 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003345 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003346
Paul Elliottebe225c2023-02-10 14:32:53 +00003347exit:
3348
Paul Elliott59200a22023-02-21 15:07:40 +00003349 psa_wipe_tag_output_buffer(signature, status, signature_size,
3350 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003351
Paul Elliott53bb3122023-02-10 14:22:22 +00003352 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003353 if (status != PSA_SUCCESS) {
3354 operation->error_occurred = 1;
3355 }
3356
Paul Elliott59ad9452022-12-18 15:09:02 +00003357 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003358 }
3359
3360 return status;
3361}
3362
3363psa_status_t psa_sign_hash_abort(
3364 psa_sign_hash_interruptible_operation_t *operation)
3365{
Paul Elliott59ad9452022-12-18 15:09:02 +00003366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3367
3368 status = psa_sign_hash_abort_internal(operation);
3369
3370 /* We clear the number of ops done here, so that it is not cleared when
3371 * the operation fails or succeeds, only on manual abort. */
3372 operation->num_ops = 0;
3373
Paul Elliottc9774412023-02-06 15:14:07 +00003374 /* Likewise, failure state. */
3375 operation->error_occurred = 0;
3376
Paul Elliott59ad9452022-12-18 15:09:02 +00003377 return status;
3378}
3379
3380static psa_status_t psa_verify_hash_abort_internal(
3381 psa_verify_hash_interruptible_operation_t *operation)
3382{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003383 if (operation->id == 0) {
3384 /* The object has (apparently) been initialized but it is not (yet)
3385 * in use. It's ok to call abort on such an object, and there's
3386 * nothing to do. */
3387 return PSA_SUCCESS;
3388 }
3389
Paul Elliott59ad9452022-12-18 15:09:02 +00003390 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3391
3392 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003393
3394 operation->id = 0;
3395
Paul Elliotte6145dc2023-02-07 12:51:21 +00003396 /* Do not clear either the error_occurred or num_ops elements here as they
3397 * only want to be cleared by the application calling abort, not by abort
3398 * being called at completion of an operation. */
3399
Paul Elliott59ad9452022-12-18 15:09:02 +00003400 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003401}
3402
3403psa_status_t psa_verify_hash_start(
3404 psa_verify_hash_interruptible_operation_t *operation,
3405 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3406 const uint8_t *hash, size_t hash_length,
3407 const uint8_t *signature, size_t signature_length)
3408{
3409 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3410 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3411 psa_key_slot_t *slot;
3412
Paul Elliottc9774412023-02-06 15:14:07 +00003413 /* Check that start has not been previously called, or operation has not
3414 * previously errored. */
3415 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003416 return PSA_ERROR_BAD_STATE;
3417 }
3418
3419 status = psa_sign_verify_check_alg(0, alg);
3420 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003421 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003422 return status;
3423 }
3424
3425 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3426 PSA_KEY_USAGE_VERIFY_HASH,
3427 alg);
3428
3429 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003430 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003431 return status;
3432 }
3433
3434 psa_key_attributes_t attributes = {
3435 .core = slot->attr
3436 };
3437
Paul Elliott296ede92022-12-15 17:00:30 +00003438 /* Ensure ops count gets reset, in case of operation re-use. */
3439 operation->num_ops = 0;
3440
Paul Elliott9fe12f62022-11-30 19:16:02 +00003441 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3442 slot->key.data,
3443 slot->key.bytes,
3444 alg, hash, hash_length,
3445 signature, signature_length);
3446
3447 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003448 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003449 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003450 }
3451
3452 unlock_status = psa_unlock_key_slot(slot);
3453
Paul Elliottc9774412023-02-06 15:14:07 +00003454 if (unlock_status != PSA_SUCCESS) {
3455 operation->error_occurred = 1;
3456 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003457
Paul Elliottc9774412023-02-06 15:14:07 +00003458 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459}
3460
3461psa_status_t psa_verify_hash_complete(
3462 psa_verify_hash_interruptible_operation_t *operation)
3463{
3464 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3465
Paul Elliottc9774412023-02-06 15:14:07 +00003466 /* Check that start has been called first, and that operation has not
3467 * previously errored. */
3468 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003469 status = PSA_ERROR_BAD_STATE;
3470 goto exit;
3471 }
3472
3473 status = psa_driver_wrapper_verify_hash_complete(operation);
3474
Paul Elliott296ede92022-12-15 17:00:30 +00003475 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003476 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003477 operation);
3478
Paul Elliottebe225c2023-02-10 14:32:53 +00003479exit:
3480
Paul Elliott9fe12f62022-11-30 19:16:02 +00003481 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003482 if (status != PSA_SUCCESS) {
3483 operation->error_occurred = 1;
3484 }
3485
Paul Elliott59ad9452022-12-18 15:09:02 +00003486 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003487 }
3488
3489 return status;
3490}
3491
3492psa_status_t psa_verify_hash_abort(
3493 psa_verify_hash_interruptible_operation_t *operation)
3494{
Paul Elliott59ad9452022-12-18 15:09:02 +00003495 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003496
Paul Elliott59ad9452022-12-18 15:09:02 +00003497 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003498
Paul Elliott59ad9452022-12-18 15:09:02 +00003499 /* We clear the number of ops done here, so that it is not cleared when
3500 * the operation fails or succeeds, only on manual abort. */
3501 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003502
Paul Elliottc9774412023-02-06 15:14:07 +00003503 /* Likewise, failure state. */
3504 operation->error_occurred = 0;
3505
Paul Elliott59ad9452022-12-18 15:09:02 +00003506 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003507}
3508
Paul Elliott588f8ed2022-12-02 18:10:26 +00003509/****************************************************************/
3510/* Asymmetric interruptible cryptography internal */
3511/* implementations */
3512/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003513
Paul Elliott588f8ed2022-12-02 18:10:26 +00003514void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3515{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003516
Paul Elliott588f8ed2022-12-02 18:10:26 +00003517#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3518 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3519 defined(MBEDTLS_ECP_RESTARTABLE)
3520
3521 /* Internal implementation uses zero to indicate infinite number max ops,
3522 * therefore avoid this value, and set to minimum possible. */
3523 if (max_ops == 0) {
3524 max_ops = 1;
3525 }
3526
Paul Elliott588f8ed2022-12-02 18:10:26 +00003527 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003528#else
3529 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003530#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3531 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3532 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3533}
3534
Paul Elliott588f8ed2022-12-02 18:10:26 +00003535uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003536 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003537{
3538#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3539 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3540 defined(MBEDTLS_ECP_RESTARTABLE)
3541
Paul Elliott93d9ca82023-02-15 18:14:21 +00003542 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003543#else
3544 (void) operation;
3545 return 0;
3546#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3547 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3548 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3549}
3550
3551uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003552 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003553{
3554 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3555 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3556 defined(MBEDTLS_ECP_RESTARTABLE)
3557
Paul Elliott93d9ca82023-02-15 18:14:21 +00003558 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003559#else
3560 (void) operation;
3561 return 0;
3562#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3563 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3564 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3565}
3566
3567psa_status_t mbedtls_psa_sign_hash_start(
3568 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3569 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3570 size_t key_buffer_size, psa_algorithm_t alg,
3571 const uint8_t *hash, size_t hash_length)
3572{
3573 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003574 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003575
Paul Elliott068fe072023-01-16 13:59:15 +00003576 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3577 return PSA_ERROR_NOT_SUPPORTED;
3578 }
3579
3580 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003581 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003582 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003583
3584#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003585 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3586 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587
Paul Elliotta1c94092023-02-15 16:38:04 +00003588 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3589
Paul Elliott93d9ca82023-02-15 18:14:21 +00003590 /* Ensure num_ops is zero'ed in case of context re-use. */
3591 operation->num_ops = 0;
3592
Paul Elliott068fe072023-01-16 13:59:15 +00003593 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3594 attributes->core.bits,
3595 key_buffer,
3596 key_buffer_size,
3597 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003598
Paul Elliott068fe072023-01-16 13:59:15 +00003599 if (status != PSA_SUCCESS) {
3600 return status;
3601 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602
Paul Elliott1bc59df2023-02-05 13:41:57 +00003603 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003604 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003605
Paul Elliott068fe072023-01-16 13:59:15 +00003606 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3607 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3608 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609
Paul Elliott0290a762023-02-09 14:30:24 +00003610 /* We only need to store the same length of hash as the private key size
3611 * here, it would be truncated by the internal implementation anyway. */
3612 required_hash_length = (hash_length < operation->coordinate_bytes ?
3613 hash_length : operation->coordinate_bytes);
3614
Paul Elliottba70ad42023-02-15 18:23:53 +00003615 if (required_hash_length > sizeof(operation->hash)) {
3616 /* Shouldn't happen, but better safe than sorry. */
3617 return PSA_ERROR_CORRUPTION_DETECTED;
3618 }
3619
Paul Elliott0290a762023-02-09 14:30:24 +00003620 memcpy(operation->hash, hash, required_hash_length);
3621 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003622
3623 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003624
3625#else
Paul Elliott068fe072023-01-16 13:59:15 +00003626 (void) operation;
3627 (void) key_buffer;
3628 (void) key_buffer_size;
3629 (void) alg;
3630 (void) hash;
3631 (void) hash_length;
3632 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003633 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003634
Paul Elliott068fe072023-01-16 13:59:15 +00003635 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003636#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3637 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3638 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003639}
3640
3641psa_status_t mbedtls_psa_sign_hash_complete(
3642 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3643 uint8_t *signature, size_t signature_size,
3644 size_t *signature_length)
3645{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3647 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3648 defined(MBEDTLS_ECP_RESTARTABLE)
3649
Paul Elliotta1c94092023-02-15 16:38:04 +00003650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003651 mbedtls_mpi r;
3652 mbedtls_mpi s;
3653
Paul Elliott46845252023-02-03 14:59:11 +00003654 mbedtls_mpi_init(&r);
3655 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003656
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003657 /* Ensure max_ops is set to the current value (or default). */
3658 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3659
Paul Elliotta1c94092023-02-15 16:38:04 +00003660 if (signature_size < 2 * operation->coordinate_bytes) {
3661 status = PSA_ERROR_BUFFER_TOO_SMALL;
3662 goto exit;
3663 }
3664
Paul Elliott588f8ed2022-12-02 18:10:26 +00003665 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003666
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3668 status = mbedtls_to_psa_error(
3669 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003670 &r,
3671 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672 &operation->ctx->d,
3673 operation->hash,
3674 operation->hash_length,
3675 operation->md_alg,
3676 mbedtls_psa_get_random,
3677 MBEDTLS_PSA_RANDOM_STATE,
3678 &operation->restart_ctx));
3679#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003680 status = PSA_ERROR_NOT_SUPPORTED;
3681 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3683 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003684 status = mbedtls_to_psa_error(
3685 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003686 &r,
3687 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688 &operation->ctx->d,
3689 operation->hash,
3690 operation->hash_length,
3691 mbedtls_psa_get_random,
3692 MBEDTLS_PSA_RANDOM_STATE,
3693 mbedtls_psa_get_random,
3694 MBEDTLS_PSA_RANDOM_STATE,
3695 &operation->restart_ctx));
3696 }
3697
Paul Elliottf8e5b562023-02-19 18:43:45 +00003698 /* Hide the fact that the restart context only holds a delta of number of
3699 * ops done during the last operation, not an absolute value. */
3700 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3701
Paul Elliott724bd252023-02-08 12:35:08 +00003702 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003703 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003704 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003705 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003706 operation->coordinate_bytes)
3707 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003708
3709 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003710 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711 }
3712
3713 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003714 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003716 operation->coordinate_bytes,
3717 operation->coordinate_bytes)
3718 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719
3720 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003721 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722 }
3723
Paul Elliott1bc59df2023-02-05 13:41:57 +00003724 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725
Paul Elliott724bd252023-02-08 12:35:08 +00003726 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727 }
Paul Elliott724bd252023-02-08 12:35:08 +00003728
3729exit:
3730
3731 mbedtls_mpi_free(&r);
3732 mbedtls_mpi_free(&s);
3733 return status;
3734
Paul Elliott588f8ed2022-12-02 18:10:26 +00003735 #else
3736
3737 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738 (void) signature;
3739 (void) signature_size;
3740 (void) signature_length;
3741
3742 return PSA_ERROR_NOT_SUPPORTED;
3743
3744#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3745 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3746 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3747}
3748
3749psa_status_t mbedtls_psa_sign_hash_abort(
3750 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3751{
3752
3753#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3754 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3755 defined(MBEDTLS_ECP_RESTARTABLE)
3756
3757 if (operation->ctx) {
3758 mbedtls_ecdsa_free(operation->ctx);
3759 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003760 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003761 }
3762
3763 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3764
Paul Elliott93d9ca82023-02-15 18:14:21 +00003765 operation->num_ops = 0;
3766
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767 return PSA_SUCCESS;
3768
3769#else
3770
3771 (void) operation;
3772
3773 return PSA_ERROR_NOT_SUPPORTED;
3774
3775#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3776 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3777 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3778}
3779
3780psa_status_t mbedtls_psa_verify_hash_start(
3781 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3782 const psa_key_attributes_t *attributes,
3783 const uint8_t *key_buffer, size_t key_buffer_size,
3784 psa_algorithm_t alg,
3785 const uint8_t *hash, size_t hash_length,
3786 const uint8_t *signature, size_t signature_length)
3787{
3788 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003789 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003790 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003791
Paul Elliott068fe072023-01-16 13:59:15 +00003792 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3793 return PSA_ERROR_NOT_SUPPORTED;
3794 }
3795
3796 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003797 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003798 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799
3800#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003801 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3802 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803
Paul Elliotta1c94092023-02-15 16:38:04 +00003804 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3805 mbedtls_mpi_init(&operation->r);
3806 mbedtls_mpi_init(&operation->s);
3807
Paul Elliott93d9ca82023-02-15 18:14:21 +00003808 /* Ensure num_ops is zero'ed in case of context re-use. */
3809 operation->num_ops = 0;
3810
Paul Elliott068fe072023-01-16 13:59:15 +00003811 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3812 attributes->core.bits,
3813 key_buffer,
3814 key_buffer_size,
3815 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003816
Paul Elliott068fe072023-01-16 13:59:15 +00003817 if (status != PSA_SUCCESS) {
3818 return status;
3819 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820
Paul Elliottc569fc22023-02-10 13:02:54 +00003821 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003822
Paul Elliott1bc59df2023-02-05 13:41:57 +00003823 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003824 return PSA_ERROR_INVALID_SIGNATURE;
3825 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826
Paul Elliott068fe072023-01-16 13:59:15 +00003827 status = mbedtls_to_psa_error(
3828 mbedtls_mpi_read_binary(&operation->r,
3829 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003830 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831
Paul Elliott068fe072023-01-16 13:59:15 +00003832 if (status != PSA_SUCCESS) {
3833 return status;
3834 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835
Paul Elliott068fe072023-01-16 13:59:15 +00003836 status = mbedtls_to_psa_error(
3837 mbedtls_mpi_read_binary(&operation->s,
3838 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003839 coordinate_bytes,
3840 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841
Paul Elliott068fe072023-01-16 13:59:15 +00003842 if (status != PSA_SUCCESS) {
3843 return status;
3844 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845
Paul Elliott2c9843f2023-02-15 17:32:42 +00003846 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003847
Paul Elliott2c9843f2023-02-15 17:32:42 +00003848 if (status != PSA_SUCCESS) {
3849 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003850 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851
Paul Elliott0290a762023-02-09 14:30:24 +00003852 /* We only need to store the same length of hash as the private key size
3853 * here, it would be truncated by the internal implementation anyway. */
3854 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3855 coordinate_bytes);
3856
Paul Elliottba70ad42023-02-15 18:23:53 +00003857 if (required_hash_length > sizeof(operation->hash)) {
3858 /* Shouldn't happen, but better safe than sorry. */
3859 return PSA_ERROR_CORRUPTION_DETECTED;
3860 }
3861
Paul Elliott0290a762023-02-09 14:30:24 +00003862 memcpy(operation->hash, hash, required_hash_length);
3863 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003864
3865 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003866#else
Paul Elliott068fe072023-01-16 13:59:15 +00003867 (void) operation;
3868 (void) key_buffer;
3869 (void) key_buffer_size;
3870 (void) alg;
3871 (void) hash;
3872 (void) hash_length;
3873 (void) signature;
3874 (void) signature_length;
3875 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003876 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003877 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003878
Paul Elliott068fe072023-01-16 13:59:15 +00003879 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003880#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3881 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3882 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003883}
3884
3885psa_status_t mbedtls_psa_verify_hash_complete(
3886 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3887{
3888
3889#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3890 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3891 defined(MBEDTLS_ECP_RESTARTABLE)
3892
Paul Elliottf8e5b562023-02-19 18:43:45 +00003893 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3894
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003895 /* Ensure max_ops is set to the current value (or default). */
3896 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3897
Paul Elliottf8e5b562023-02-19 18:43:45 +00003898 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003899 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3900 operation->hash,
3901 operation->hash_length,
3902 &operation->ctx->Q,
3903 &operation->r,
3904 &operation->s,
3905 &operation->restart_ctx));
3906
Paul Elliottf8e5b562023-02-19 18:43:45 +00003907 /* Hide the fact that the restart context only holds a delta of number of
3908 * ops done during the last operation, not an absolute value. */
3909 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3910
3911 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003912#else
3913 (void) operation;
3914
3915 return PSA_ERROR_NOT_SUPPORTED;
3916
3917#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3918 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3919 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3920}
3921
3922psa_status_t mbedtls_psa_verify_hash_abort(
3923 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3924{
3925
3926#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3927 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3928 defined(MBEDTLS_ECP_RESTARTABLE)
3929
3930 if (operation->ctx) {
3931 mbedtls_ecdsa_free(operation->ctx);
3932 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003933 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003934 }
3935
3936 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3937
Paul Elliott93d9ca82023-02-15 18:14:21 +00003938 operation->num_ops = 0;
3939
Paul Elliott588f8ed2022-12-02 18:10:26 +00003940 mbedtls_mpi_free(&operation->r);
3941 mbedtls_mpi_free(&operation->s);
3942
3943 return PSA_SUCCESS;
3944
3945#else
3946 (void) operation;
3947
3948 return PSA_ERROR_NOT_SUPPORTED;
3949
3950#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3951 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3952 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3953}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003954
mohammad1603503973b2018-03-12 15:59:30 +02003955/****************************************************************/
3956/* Symmetric cryptography */
3957/****************************************************************/
3958
Gilles Peskine449bd832023-01-11 14:50:10 +01003959static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3960 mbedtls_svc_key_id_t key,
3961 psa_algorithm_t alg,
3962 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003963{
3964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3965 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003966 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3968 PSA_KEY_USAGE_ENCRYPT :
3969 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003970
3971 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003973 status = PSA_ERROR_BAD_STATE;
3974 goto exit;
3975 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003976
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003978 status = PSA_ERROR_INVALID_ARGUMENT;
3979 goto exit;
3980 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003981
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3983 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003984 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003986
Ronald Cron49fafa92021-03-10 08:34:23 +01003987 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003988 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003989 * so we only set it (in the driver wrapper) after resources have been
3990 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003991 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003993 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003995 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 }
3997 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003998
Ronald Crona4af55f2020-12-14 14:36:06 +01003999 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004001 };
4002
Ronald Cronab99ac22020-10-01 16:38:28 +02004003 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 if (cipher_operation == MBEDTLS_ENCRYPT) {
4005 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4006 &attributes,
4007 slot->key.data,
4008 slot->key.bytes,
4009 alg);
4010 } else {
4011 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4012 &attributes,
4013 slot->key.data,
4014 slot->key.bytes,
4015 alg);
4016 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004017
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004018exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004019 if (status != PSA_SUCCESS) {
4020 psa_cipher_abort(operation);
4021 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004022
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004024
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004026}
4027
Gilles Peskine449bd832023-01-11 14:50:10 +01004028psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4029 mbedtls_svc_key_id_t key,
4030 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004031{
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004033}
4034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4036 mbedtls_svc_key_id_t key,
4037 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004038{
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004040}
4041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4043 uint8_t *iv,
4044 size_t iv_size,
4045 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004046{
Ronald Cron6d051732020-10-01 14:10:20 +02004047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004048 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4049 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004050
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004052 status = PSA_ERROR_BAD_STATE;
4053 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004054 }
4055
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004057 status = PSA_ERROR_BAD_STATE;
4058 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004059 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004060
Ronald Cron2fb90522021-07-05 12:31:44 +02004061 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004063 status = PSA_ERROR_BUFFER_TOO_SMALL;
4064 goto exit;
4065 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004066
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004068 status = PSA_ERROR_GENERIC_ERROR;
4069 goto exit;
4070 }
4071
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 status = psa_generate_random(local_iv, default_iv_length);
4073 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004074 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 }
Ronald Cron5618a392021-03-26 09:52:26 +01004076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 status = psa_driver_wrapper_cipher_set_iv(operation,
4078 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004079
4080exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 if (status == PSA_SUCCESS) {
4082 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004083 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004084 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004086 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004088 }
Ronald Cron6d051732020-10-01 14:10:20 +02004089
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004091}
4092
Gilles Peskine449bd832023-01-11 14:50:10 +01004093psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4094 const uint8_t *iv,
4095 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004096{
Ronald Cron6d051732020-10-01 14:10:20 +02004097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004098
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004100 status = PSA_ERROR_BAD_STATE;
4101 goto exit;
4102 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004105 status = PSA_ERROR_BAD_STATE;
4106 goto exit;
4107 }
Ronald Crona0d68172021-03-26 10:15:08 +01004108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004110 status = PSA_ERROR_INVALID_ARGUMENT;
4111 goto exit;
4112 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 status = psa_driver_wrapper_cipher_set_iv(operation,
4115 iv,
4116 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004117
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004118exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004120 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 } else {
4122 psa_cipher_abort(operation);
4123 }
4124 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004125}
4126
Gilles Peskine449bd832023-01-11 14:50:10 +01004127psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4128 const uint8_t *input,
4129 size_t input_length,
4130 uint8_t *output,
4131 size_t output_size,
4132 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004133{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004134 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004137 status = PSA_ERROR_BAD_STATE;
4138 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004139 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004142 status = PSA_ERROR_BAD_STATE;
4143 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004144 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004145
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 status = psa_driver_wrapper_cipher_update(operation,
4147 input,
4148 input_length,
4149 output,
4150 output_size,
4151 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004152
4153exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 if (status != PSA_SUCCESS) {
4155 psa_cipher_abort(operation);
4156 }
Ronald Cron6d051732020-10-01 14:10:20 +02004157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004159}
4160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4162 uint8_t *output,
4163 size_t output_size,
4164 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004165{
David Saadab4ecc272019-02-14 13:48:10 +02004166 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004169 status = PSA_ERROR_BAD_STATE;
4170 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004171 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004174 status = PSA_ERROR_BAD_STATE;
4175 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004176 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 status = psa_driver_wrapper_cipher_finish(operation,
4179 output,
4180 output_size,
4181 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004182
4183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 if (status == PSA_SUCCESS) {
4185 return psa_cipher_abort(operation);
4186 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004187 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004189
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004191 }
mohammad1603503973b2018-03-12 15:59:30 +02004192}
4193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004195{
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004197 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004198 * in use. It's ok to call abort on such an object, and there's
4199 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004201 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004202
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004204
Ronald Cron49fafa92021-03-10 08:34:23 +01004205 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004206 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004207 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004210}
4211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4213 psa_algorithm_t alg,
4214 const uint8_t *input,
4215 size_t input_length,
4216 uint8_t *output,
4217 size_t output_size,
4218 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004219{
4220 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004221 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004222 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004223 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4224 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004227 status = PSA_ERROR_INVALID_ARGUMENT;
4228 goto exit;
4229 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004230
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4232 PSA_KEY_USAGE_ENCRYPT,
4233 alg);
4234 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004235 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004237
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004238 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004240 };
4241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4243 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004244 status = PSA_ERROR_GENERIC_ERROR;
4245 goto exit;
4246 }
4247
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 if (default_iv_length > 0) {
4249 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004250 status = PSA_ERROR_BUFFER_TOO_SMALL;
4251 goto exit;
4252 }
4253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 status = psa_generate_random(local_iv, default_iv_length);
4255 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004256 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004258 }
4259
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004260 status = psa_driver_wrapper_cipher_encrypt(
4261 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004262 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004263 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004265
4266exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 unlock_status = psa_unlock_key_slot(slot);
4268 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004269 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004270 }
Ronald Cron23919522021-07-08 19:00:07 +02004271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 if (status == PSA_SUCCESS) {
4273 if (default_iv_length > 0) {
4274 memcpy(output, local_iv, default_iv_length);
4275 }
4276 *output_length += default_iv_length;
4277 } else {
4278 *output_length = 0;
4279 }
4280
4281 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004282}
4283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4285 psa_algorithm_t alg,
4286 const uint8_t *input,
4287 size_t input_length,
4288 uint8_t *output,
4289 size_t output_size,
4290 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004291{
4292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004293 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004294 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004297 status = PSA_ERROR_INVALID_ARGUMENT;
4298 goto exit;
4299 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004300
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4302 PSA_KEY_USAGE_DECRYPT,
4303 alg);
4304 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004305 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004307
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004308 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004310 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004311
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4313 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004314 status = PSA_ERROR_INVALID_ARGUMENT;
4315 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004317 status = PSA_ERROR_INVALID_ARGUMENT;
4318 goto exit;
4319 }
4320
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004321 status = psa_driver_wrapper_cipher_decrypt(
4322 &attributes, slot->key.data, slot->key.bytes,
4323 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004325
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 unlock_status = psa_unlock_key_slot(slot);
4328 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004329 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004331
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004333 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 }
Ronald Cron23919522021-07-08 19:00:07 +02004335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004337}
4338
4339
mohammad16035955c982018-04-26 00:53:03 +03004340/****************************************************************/
4341/* AEAD */
4342/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004343
Paul Elliottbaff51c2021-09-28 17:44:45 +01004344/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004345static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004346{
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004348}
4349
4350/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004351static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4352 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004353{
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004355
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004357#if defined(PSA_WANT_ALG_GCM)
4358 case PSA_ALG_GCM:
4359 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 * arbitrarily large nonces. Please note that we do not generally
4361 * recommend the usage of nonces of greater length than
4362 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4363 * size, which can then lead to collisions if you encrypt a very
4364 * large number of messages.*/
4365 if (nonce_length != 0) {
4366 return PSA_SUCCESS;
4367 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004368 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004369#endif /* PSA_WANT_ALG_GCM */
4370#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004371 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 if (nonce_length >= 7 && nonce_length <= 13) {
4373 return PSA_SUCCESS;
4374 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004375 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004376#endif /* PSA_WANT_ALG_CCM */
4377#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004378 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 if (nonce_length == 12) {
4380 return PSA_SUCCESS;
4381 } else if (nonce_length == 8) {
4382 return PSA_ERROR_NOT_SUPPORTED;
4383 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004384 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004385#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004386 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004387 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004389 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004390
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004392}
4393
Gilles Peskine449bd832023-01-11 14:50:10 +01004394static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004395{
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4397 return PSA_ERROR_INVALID_ARGUMENT;
4398 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004401}
4402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4404 psa_algorithm_t alg,
4405 const uint8_t *nonce,
4406 size_t nonce_length,
4407 const uint8_t *additional_data,
4408 size_t additional_data_length,
4409 const uint8_t *plaintext,
4410 size_t plaintext_length,
4411 uint8_t *ciphertext,
4412 size_t ciphertext_size,
4413 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004414{
Ronald Cron215633c2021-03-16 17:15:37 +01004415 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4416 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004417
mohammad1603f08a5502018-06-03 15:05:47 +03004418 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004419
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 status = psa_aead_check_algorithm(alg);
4421 if (status != PSA_SUCCESS) {
4422 return status;
4423 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004424
Ronald Cron9a986162021-03-26 12:40:07 +01004425 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4427 if (status != PSA_SUCCESS) {
4428 return status;
4429 }
mohammad16035955c982018-04-26 00:53:03 +03004430
Ronald Cron215633c2021-03-16 17:15:37 +01004431 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004433 };
mohammad16035955c982018-04-26 00:53:03 +03004434
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 status = psa_aead_check_nonce_length(alg, nonce_length);
4436 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004437 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004439
Ronald Cronde822812021-03-17 16:08:20 +01004440 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004441 &attributes, slot->key.data, slot->key.bytes,
4442 alg,
4443 nonce, nonce_length,
4444 additional_data, additional_data_length,
4445 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004447
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4449 memset(ciphertext, 0, ciphertext_size);
4450 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004451
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004452exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004456}
4457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4459 psa_algorithm_t alg,
4460 const uint8_t *nonce,
4461 size_t nonce_length,
4462 const uint8_t *additional_data,
4463 size_t additional_data_length,
4464 const uint8_t *ciphertext,
4465 size_t ciphertext_length,
4466 uint8_t *plaintext,
4467 size_t plaintext_size,
4468 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004469{
Ronald Cron215633c2021-03-16 17:15:37 +01004470 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4471 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004472
Gilles Peskineee652a32018-06-01 19:23:52 +02004473 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004474
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 status = psa_aead_check_algorithm(alg);
4476 if (status != PSA_SUCCESS) {
4477 return status;
4478 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004479
Ronald Cron9a986162021-03-26 12:40:07 +01004480 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4482 if (status != PSA_SUCCESS) {
4483 return status;
4484 }
mohammad16035955c982018-04-26 00:53:03 +03004485
Ronald Cron215633c2021-03-16 17:15:37 +01004486 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004488 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 status = psa_aead_check_nonce_length(alg, nonce_length);
4491 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004492 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004494
Ronald Cronde822812021-03-17 16:08:20 +01004495 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004496 &attributes, slot->key.data, slot->key.bytes,
4497 alg,
4498 nonce, nonce_length,
4499 additional_data, additional_data_length,
4500 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 if (status != PSA_SUCCESS && plaintext_size != 0) {
4504 memset(plaintext, 0, plaintext_size);
4505 }
mohammad160360a64d02018-06-03 17:20:42 +03004506
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004507exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 return status;
mohammad16035955c982018-04-26 00:53:03 +03004511}
4512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4514{
4515 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004518#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004520 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4522 return PSA_ERROR_INVALID_ARGUMENT;
4523 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004524 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004525#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004526
Przemek Stekiel86679c72022-10-06 17:06:56 +02004527#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004529 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4531 return PSA_ERROR_INVALID_ARGUMENT;
4532 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004533 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004534#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004535
Przemek Stekiel86679c72022-10-06 17:06:56 +02004536#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004538 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if (tag_len != 16) {
4540 return PSA_ERROR_INVALID_ARGUMENT;
4541 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004542 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004543#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004544
4545 default:
4546 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004548 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004550}
4551
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004552/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004553static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4554 int is_encrypt,
4555 mbedtls_svc_key_id_t key,
4556 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004557{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004558 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4559 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4560 psa_key_slot_t *slot = NULL;
4561 psa_key_usage_t key_usage = 0;
4562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 status = psa_aead_check_algorithm(alg);
4564 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004565 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004567
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004569 status = PSA_ERROR_BAD_STATE;
4570 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004571 }
4572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 if (operation->nonce_set || operation->lengths_set ||
4574 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004575 status = PSA_ERROR_BAD_STATE;
4576 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004577 }
4578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004580 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004582 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4586 alg);
4587 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004588 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004590
4591 psa_key_attributes_t attributes = {
4592 .core = slot->attr
4593 };
4594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004596 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 if (is_encrypt) {
4600 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4601 &attributes,
4602 slot->key.data,
4603 slot->key.bytes,
4604 alg);
4605 } else {
4606 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4607 &attributes,
4608 slot->key.data,
4609 slot->key.bytes,
4610 alg);
4611 }
4612 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004613 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004615
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004617
4618exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004622 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004624 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 } else {
4626 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004627 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004630}
4631
Paul Elliott302ff6b2021-04-20 18:10:30 +01004632/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004633psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4634 mbedtls_svc_key_id_t key,
4635 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004636{
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004638}
4639
4640/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004641psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4642 mbedtls_svc_key_id_t key,
4643 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004644{
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004646}
4647
4648/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004649psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4650 uint8_t *nonce,
4651 size_t nonce_size,
4652 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004653{
4654 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004655 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004656 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004657
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004658 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004659
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004661 status = PSA_ERROR_BAD_STATE;
4662 goto exit;
4663 }
4664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004666 status = PSA_ERROR_BAD_STATE;
4667 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004668 }
4669
Paul Elliotte193ea82021-10-01 13:00:16 +01004670 /* For CCM, this size may not be correct according to the PSA
4671 * specification. The PSA Crypto 1.0.1 specification states:
4672 *
4673 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4674 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4675 *
4676 * However this restriction that L has to be the smallest integer is not
4677 * applied in practice, and it is not implementable here since the
4678 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4680 operation->alg);
4681 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004682 status = PSA_ERROR_BUFFER_TOO_SMALL;
4683 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004684 }
4685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 status = psa_generate_random(local_nonce, required_nonce_size);
4687 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004688 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004692
Paul Elliott39dc6b82021-05-11 19:16:09 +01004693exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 if (status == PSA_SUCCESS) {
4695 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004696 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 } else {
4698 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004699 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004702}
4703
4704/* Set the nonce for a multipart authenticated encryption or decryption
4705 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004706psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4707 const uint8_t *nonce,
4708 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004709{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004710 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004713 status = PSA_ERROR_BAD_STATE;
4714 goto exit;
4715 }
4716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004718 status = PSA_ERROR_BAD_STATE;
4719 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004720 }
4721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4723 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004724 status = PSA_ERROR_INVALID_ARGUMENT;
4725 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004726 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004727
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4729 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004730
Paul Elliott39dc6b82021-05-11 19:16:09 +01004731exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004733 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 } else {
4735 psa_aead_abort(operation);
4736 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004739}
4740
4741/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004742psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4743 size_t ad_length,
4744 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004745{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004746 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4747
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004749 status = PSA_ERROR_BAD_STATE;
4750 goto exit;
4751 }
4752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if (operation->lengths_set || operation->ad_started ||
4754 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004755 status = PSA_ERROR_BAD_STATE;
4756 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004757 }
4758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004760#if defined(PSA_WANT_ALG_GCM)
4761 case PSA_ALG_GCM:
4762 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 * bits. Without the guard this code will generate warnings on 32bit
4764 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004765#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 if (((uint64_t) ad_length) >> 61 != 0 ||
4767 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004768 status = PSA_ERROR_INVALID_ARGUMENT;
4769 goto exit;
4770 }
Paul Elliott325d3742021-09-27 17:56:28 +01004771#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004772 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004773#endif /* PSA_WANT_ALG_GCM */
4774#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004775 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004777 status = PSA_ERROR_INVALID_ARGUMENT;
4778 goto exit;
4779 }
4780 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004781#endif /* PSA_WANT_ALG_CCM */
4782#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004783 case PSA_ALG_CHACHA20_POLY1305:
4784 /* No length restrictions for ChaChaPoly. */
4785 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004786#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004787 default:
4788 break;
4789 }
Paul Elliott325d3742021-09-27 17:56:28 +01004790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4792 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004793
Paul Elliott39dc6b82021-05-11 19:16:09 +01004794exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004796 operation->ad_remaining = ad_length;
4797 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004798 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 } else {
4800 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004801 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004804}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004805
4806/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004807psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4808 const uint8_t *input,
4809 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004810{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004811 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004814 status = PSA_ERROR_BAD_STATE;
4815 goto exit;
4816 }
4817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004819 status = PSA_ERROR_BAD_STATE;
4820 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004821 }
4822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 if (operation->lengths_set) {
4824 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004825 status = PSA_ERROR_INVALID_ARGUMENT;
4826 goto exit;
4827 }
4828
4829 operation->ad_remaining -= input_length;
4830 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004831#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004833 status = PSA_ERROR_BAD_STATE;
4834 goto exit;
4835 }
4836#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 status = psa_driver_wrapper_aead_update_ad(operation, input,
4839 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004840
Paul Elliott39dc6b82021-05-11 19:16:09 +01004841exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004843 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 } else {
4845 psa_aead_abort(operation);
4846 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004849}
4850
4851/* Encrypt or decrypt a message fragment in an active multipart AEAD
4852 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004853psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4854 const uint8_t *input,
4855 size_t input_length,
4856 uint8_t *output,
4857 size_t output_size,
4858 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004859{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004860 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004861
4862 *output_length = 0;
4863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004865 status = PSA_ERROR_BAD_STATE;
4866 goto exit;
4867 }
4868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004870 status = PSA_ERROR_BAD_STATE;
4871 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004872 }
4873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004875 /* Additional data length was supplied, but not all the additional
4876 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004878 status = PSA_ERROR_INVALID_ARGUMENT;
4879 goto exit;
4880 }
4881
4882 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004884 status = PSA_ERROR_INVALID_ARGUMENT;
4885 goto exit;
4886 }
4887
4888 operation->body_remaining -= input_length;
4889 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004890#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004892 status = PSA_ERROR_BAD_STATE;
4893 goto exit;
4894 }
4895#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004896
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4898 output, output_size,
4899 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004900
Paul Elliott39dc6b82021-05-11 19:16:09 +01004901exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004903 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 } else {
4905 psa_aead_abort(operation);
4906 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004907
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004909}
4910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004912{
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 if (operation->id == 0 || !operation->nonce_set) {
4914 return PSA_ERROR_BAD_STATE;
4915 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4918 operation->body_remaining != 0)) {
4919 return PSA_ERROR_INVALID_ARGUMENT;
4920 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004921
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004923}
4924
Paul Elliott302ff6b2021-04-20 18:10:30 +01004925/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004926psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4927 uint8_t *ciphertext,
4928 size_t ciphertext_size,
4929 size_t *ciphertext_length,
4930 uint8_t *tag,
4931 size_t tag_size,
4932 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004933{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004934 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4935
Paul Elliott302ff6b2021-04-20 18:10:30 +01004936 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004937 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004938
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 status = psa_aead_final_checks(operation);
4940 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004941 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004943
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004945 status = PSA_ERROR_BAD_STATE;
4946 goto exit;
4947 }
4948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4950 ciphertext_size,
4951 ciphertext_length,
4952 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004953
Paul Elliott39dc6b82021-05-11 19:16:09 +01004954exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004955
4956
Paul Elliottf47b0952021-05-21 18:02:33 +01004957 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004958 * the zero tag size, make sure the tag is set to something implausible.
4959 * Even if the operation succeeds, make sure we clear the rest of the
4960 * buffer to prevent potential leakage of anything previously placed in
4961 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004962 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004963
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004965
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004967}
4968
4969/* Finish authenticating and decrypting a message in a multipart AEAD
4970 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004971psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4972 uint8_t *plaintext,
4973 size_t plaintext_size,
4974 size_t *plaintext_length,
4975 const uint8_t *tag,
4976 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004977{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004978 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4979
Paul Elliott302ff6b2021-04-20 18:10:30 +01004980 *plaintext_length = 0;
4981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 status = psa_aead_final_checks(operation);
4983 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004984 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004988 status = PSA_ERROR_BAD_STATE;
4989 goto exit;
4990 }
4991
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4993 plaintext_size,
4994 plaintext_length,
4995 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004996
4997exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005001}
5002
5003/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005004psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005005{
5006 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5007
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005009 /* The object has (apparently) been initialized but it is not (yet)
5010 * in use. It's ok to call abort on such an object, and there's
5011 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005013 }
5014
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005016
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005020}
5021
Gilles Peskinee59236f2018-01-27 23:32:46 +01005022/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005023/* Generators */
5024/****************************************************************/
5025
Przemek Stekiel69c46792022-06-10 12:59:51 +02005026#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005027 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005028 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5029 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005030#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005031#endif /* At least one builtin KDF */
5032
Przemek Stekiel69c46792022-06-10 12:59:51 +02005033#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005034 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5035 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5036static psa_status_t psa_key_derivation_start_hmac(
5037 psa_mac_operation_t *operation,
5038 psa_algorithm_t hash_alg,
5039 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005041{
5042 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5045 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5046 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005047
Steven Cooreman72f736a2021-05-07 14:14:37 +02005048 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005050
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 status = psa_driver_wrapper_mac_sign_setup(operation,
5052 &attributes,
5053 hmac_key, hmac_key_length,
5054 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 psa_reset_key_attributes(&attributes);
5057 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005058}
5059#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005060
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005061#define HKDF_STATE_INIT 0 /* no input yet */
5062#define HKDF_STATE_STARTED 1 /* got salt */
5063#define HKDF_STATE_KEYED 2 /* got key */
5064#define HKDF_STATE_OUTPUT 3 /* output started */
5065
Gilles Peskinecbe66502019-05-16 16:59:18 +02005066static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005068{
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5070 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5071 } else {
5072 return operation->alg;
5073 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005074}
5075
Gilles Peskine449bd832023-01-11 14:50:10 +01005076psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005077{
5078 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5080 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005081 /* The object has (apparently) been initialized but it is not
5082 * in use. It's ok to call abort on such an object, and there's
5083 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005085#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5087 mbedtls_free(operation->ctx.hkdf.info);
5088 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5089 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005090#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005091#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5092 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5094 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5095 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5096 if (operation->ctx.tls12_prf.secret != NULL) {
5097 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5098 operation->ctx.tls12_prf.secret_length);
5099 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005100 }
5101
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 if (operation->ctx.tls12_prf.seed != NULL) {
5103 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5104 operation->ctx.tls12_prf.seed_length);
5105 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005106 }
5107
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 if (operation->ctx.tls12_prf.label != NULL) {
5109 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5110 operation->ctx.tls12_prf.label_length);
5111 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005112 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005113#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 if (operation->ctx.tls12_prf.other_secret != NULL) {
5115 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5116 operation->ctx.tls12_prf.other_secret_length);
5117 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005118 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005119#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005120 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005121
5122 /* We leave the fields Ai and output_block to be erased safely by the
5123 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005125#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5126 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005127#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5129 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5130 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5131 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005132#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005133 {
5134 status = PSA_ERROR_BAD_STATE;
5135 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 mbedtls_platform_zeroize(operation, sizeof(*operation));
5137 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005138}
5139
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005142{
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005146 }
5147
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005150}
5151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5153 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005154{
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 if (operation->alg == 0) {
5156 return PSA_ERROR_BAD_STATE;
5157 }
5158 if (capacity > operation->capacity) {
5159 return PSA_ERROR_INVALID_ARGUMENT;
5160 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005161 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005163}
5164
Przemek Stekiel69c46792022-06-10 12:59:51 +02005165#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005166/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005167static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5168 psa_algorithm_t kdf_alg,
5169 uint8_t *output,
5170 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005171{
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5173 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005174 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005175 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005176#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005178#else
5179 const uint8_t last_block = 0xff;
5180#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 if (hkdf->state < HKDF_STATE_KEYED ||
5183 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005184#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005186#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 )) {
5188 return PSA_ERROR_BAD_STATE;
5189 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005190 hkdf->state = HKDF_STATE_OUTPUT;
5191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005193 /* Copy what remains of the current block */
5194 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005196 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 }
5198 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005199 output += n;
5200 output_length -= n;
5201 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005203 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005205 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005206 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005208 * object was corrupted or if this function is called directly
5209 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 if (hkdf->block_number == last_block) {
5211 return PSA_ERROR_BAD_STATE;
5212 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005213
5214 /* We need a new block */
5215 ++hkdf->block_number;
5216 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005217
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5219 hash_alg,
5220 hkdf->prk,
5221 hash_length);
5222 if (status != PSA_SUCCESS) {
5223 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005224 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005225
5226 if (hkdf->block_number != 1) {
5227 status = psa_mac_update(&hkdf->hmac,
5228 hkdf->output_block,
5229 hash_length);
5230 if (status != PSA_SUCCESS) {
5231 return status;
5232 }
5233 }
5234 status = psa_mac_update(&hkdf->hmac,
5235 hkdf->info,
5236 hkdf->info_length);
5237 if (status != PSA_SUCCESS) {
5238 return status;
5239 }
5240 status = psa_mac_update(&hkdf->hmac,
5241 &hkdf->block_number, 1);
5242 if (status != PSA_SUCCESS) {
5243 return status;
5244 }
5245 status = psa_mac_sign_finish(&hkdf->hmac,
5246 hkdf->output_block,
5247 sizeof(hkdf->output_block),
5248 &hmac_output_length);
5249 if (status != PSA_SUCCESS) {
5250 return status;
5251 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005252 }
5253
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005255}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005256#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005257
John Durkop07cc04a2020-11-16 22:08:34 -08005258#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5259 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005260static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5261 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005263{
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5265 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005266 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5267 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005268 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005269
Janos Follath7742fee2019-06-17 12:58:10 +01005270 /* We can't be wanting more output after block 0xff, otherwise
5271 * the capacity check in psa_key_derivation_output_bytes() would have
5272 * prevented this call. It could happen only if the operation
5273 * object was corrupted or if this function is called directly
5274 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 if (tls12_prf->block_number == 0xff) {
5276 return PSA_ERROR_CORRUPTION_DETECTED;
5277 }
Janos Follath7742fee2019-06-17 12:58:10 +01005278
5279 /* We need a new block */
5280 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005281 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005282
5283 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5284 *
5285 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5286 *
5287 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5288 * HMAC_hash(secret, A(2) + seed) +
5289 * HMAC_hash(secret, A(3) + seed) + ...
5290 *
5291 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005292 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005293 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005294 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005295 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005296 * is currently extracted as `output_block` and where i is
5297 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005298 */
5299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 status = psa_key_derivation_start_hmac(&hmac,
5301 hash_alg,
5302 tls12_prf->secret,
5303 tls12_prf->secret_length);
5304 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005305 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005307
5308 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005310 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5311 * the variable seed and in this instance means it in the context of the
5312 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 status = psa_mac_update(&hmac,
5314 tls12_prf->label,
5315 tls12_prf->label_length);
5316 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005317 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 }
5319 status = psa_mac_update(&hmac,
5320 tls12_prf->seed,
5321 tls12_prf->seed_length);
5322 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005323 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 }
5325 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005326 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5328 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005329 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005331 }
5332
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 status = psa_mac_sign_finish(&hmac,
5334 tls12_prf->Ai, hash_length,
5335 &hmac_output_length);
5336 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005337 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 }
5339 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005340 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005342
5343 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 status = psa_key_derivation_start_hmac(&hmac,
5345 hash_alg,
5346 tls12_prf->secret,
5347 tls12_prf->secret_length);
5348 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005349 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 }
5351 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_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->label, tls12_prf->label_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->seed, tls12_prf->seed_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_sign_finish(&hmac,
5364 tls12_prf->output_block, hash_length,
5365 &hmac_output_length);
5366 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005367 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005369
Janos Follath7742fee2019-06-17 12:58:10 +01005370
5371cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 cleanup_status = psa_mac_abort(&hmac);
5373 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005374 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005378}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005379
Janos Follath844eb0e2019-06-19 12:10:49 +01005380static psa_status_t psa_key_derivation_tls12_prf_read(
5381 psa_tls12_prf_key_derivation_t *tls12_prf,
5382 psa_algorithm_t alg,
5383 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005385{
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5387 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005388 psa_status_t status;
5389 uint8_t offset, length;
5390
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005392 case PSA_TLS12_PRF_STATE_LABEL_SET:
5393 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5394 break;
5395 case PSA_TLS12_PRF_STATE_OUTPUT:
5396 break;
5397 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005399 }
5400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005402 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 if (tls12_prf->left_in_block == 0) {
5404 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5405 alg);
5406 if (status != PSA_SUCCESS) {
5407 return status;
5408 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005409
5410 continue;
5411 }
5412
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005414 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005416 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005418
5419 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005421 output += length;
5422 output_length -= length;
5423 tls12_prf->left_in_block -= length;
5424 }
5425
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005427}
John Durkop07cc04a2020-11-16 22:08:34 -08005428#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5429 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005430
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005431#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5432static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5433 psa_tls12_ecjpake_to_pms_t *ecjpake,
5434 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005436{
5437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005438 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005439
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 if (output_length != 32) {
5441 return PSA_ERROR_INVALID_ARGUMENT;
5442 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5445 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5446 &output_size);
5447 if (status != PSA_SUCCESS) {
5448 return status;
5449 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 if (output_size != output_length) {
5452 return PSA_ERROR_GENERIC_ERROR;
5453 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005456}
5457#endif
5458
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005459psa_status_t psa_key_derivation_output_bytes(
5460 psa_key_derivation_operation_t *operation,
5461 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005463{
5464 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005470 }
5471
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005473 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005474 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005475 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005476 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005477 goto exit;
5478 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005480 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005481 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005482 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5483 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005484 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005485 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005487 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005489
Przemek Stekiel69c46792022-06-10 12:59:51 +02005490#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5492 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5493 output, output_length);
5494 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005495#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005496#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5497 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5499 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5500 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5501 kdf_alg, output,
5502 output_length);
5503 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005504#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5505 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005506#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005508 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5510 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005511#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5512
Gilles Peskineeab56e42018-07-12 17:12:33 +02005513 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005514 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005516 }
5517
5518exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005520 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005521 * This allows us to differentiate between exhausted operations and
5522 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5523 * operations. */
5524 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005526 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005528 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005530}
5531
David Brown7807bf72021-02-09 16:28:23 -07005532#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005533static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005534{
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 if (data_size >= 8) {
5536 mbedtls_des_key_set_parity(data);
5537 }
5538 if (data_size >= 16) {
5539 mbedtls_des_key_set_parity(data + 8);
5540 }
5541 if (data_size >= 24) {
5542 mbedtls_des_key_set_parity(data + 16);
5543 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005544}
David Brown7807bf72021-02-09 16:28:23 -07005545#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005546
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005547/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 * ECC keys on a Weierstrass elliptic curve require the generation
5549 * of a private key which is an integer
5550 * in the range [1, N - 1], where N is the boundary of the private key domain:
5551 * N is the prime p for Diffie-Hellman, or the order of the
5552 * curve’s base point for ECC.
5553 *
5554 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5555 * This function generates the private key using the following process:
5556 *
5557 * 1. Draw a byte string of length ceiling(m/8) bytes.
5558 * 2. If m is not a multiple of 8, set the most significant
5559 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5560 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5561 * 4. If k > N - 2, discard the result and return to step 1.
5562 * 5. Output k + 1 as the private key.
5563 *
5564 * This method allows compliance to NIST standards, specifically the methods titled
5565 * Key-Pair Generation by Testing Candidates in the following publications:
5566 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5567 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5568 * Diffie-Hellman keys.
5569 *
5570 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5571 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5572 *
5573 * Note: Function allocates memory for *data buffer, so given *data should be
5574 * always NULL.
5575 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005576#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5577 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005578 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5579 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5580 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005581static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005582 psa_key_slot_t *slot,
5583 size_t bits,
5584 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005585 uint8_t **data
5586 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005587{
Valerio Setti52789862023-04-07 12:13:11 +02005588#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005589 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005590 mbedtls_mpi k;
5591 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005594
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 mbedtls_mpi_init(&k);
5596 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005597
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005598 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005600 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005602
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005604 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005605 goto cleanup;
5606 }
5607
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005608 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005612
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005613 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005614 /* Let m be the bit size of N. */
5615 size_t m = ecp_group.nbits;
5616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005618
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005619 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005621
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005622 /* Note: This function is always called with *data == NULL and it
5623 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 *data = mbedtls_calloc(1, m_bytes);
5625 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005626 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005627 goto cleanup;
5628 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005629
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005631 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005633 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005635
5636 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005638 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005639 * (8 * ceiling(m/8) - m) bits of the first byte in
5640 * the string to zero.
5641 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005643 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005644 }
5645
5646 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 * big-endian byte string.
5648 */
5649 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005650
5651 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005652 * Result of comparison is returned. When it indicates error
5653 * then this function is called again.
5654 */
5655 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005656 }
5657
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005658 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5660 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005661cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 if (ret != 0) {
5663 status = mbedtls_to_psa_error(ret);
5664 }
5665 if (status != PSA_SUCCESS) {
5666 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005667 *data = NULL;
5668 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 mbedtls_mpi_free(&k);
5670 mbedtls_mpi_free(&diff_N_2);
5671 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005672#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005673 (void) slot;
5674 (void) bits;
5675 (void) operation;
5676 (void) data;
5677 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005678#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005679}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005680
5681/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5682 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5683 *
5684 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5685 * draw a 32-byte string and process it as specified in
5686 * Elliptic Curves for Security [RFC7748] §5.
5687 *
5688 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5689 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5690 *
5691 * Note: Function allocates memory for *data buffer, so given *data should be
5692 * always NULL.
5693 */
5694
5695static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5696 size_t bits,
5697 psa_key_derivation_operation_t *operation,
5698 uint8_t **data
5699 )
5700{
5701 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005705 case 255:
5706 output_length = 32;
5707 break;
5708 case 448:
5709 output_length = 56;
5710 break;
5711 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005712 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005713 break;
5714 }
5715
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 if (*data == NULL) {
5719 return PSA_ERROR_INSUFFICIENT_MEMORY;
5720 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005725 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005729 case 255:
5730 (*data)[0] &= 248;
5731 (*data)[31] &= 127;
5732 (*data)[31] |= 64;
5733 break;
5734 case 448:
5735 (*data)[0] &= 252;
5736 (*data)[55] |= 128;
5737 break;
5738 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005740 break;
5741 }
5742
5743 return status;
5744}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005745#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5746 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005747 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5748 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5749 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005750
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005751static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005752 psa_key_slot_t *slot,
5753 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005755{
5756 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305758 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005759 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5762 return PSA_ERROR_INVALID_ARGUMENT;
5763 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005764
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005765#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5766 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005767 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5768 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5769 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5771 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5772 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005773 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5775 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005776 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 }
5778 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005779 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5781 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005782 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005784 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005785 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005786#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5787 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005788 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5789 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5790 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 if (key_type_is_raw_bytes(slot->attr.type)) {
5792 if (bits % 8 != 0) {
5793 return PSA_ERROR_INVALID_ARGUMENT;
5794 }
5795 data = mbedtls_calloc(1, bytes);
5796 if (data == NULL) {
5797 return PSA_ERROR_INSUFFICIENT_MEMORY;
5798 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 status = psa_key_derivation_output_bytes(operation, data, bytes);
5801 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005802 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 }
David Brown12ca5032021-02-11 11:02:00 -07005804#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5806 psa_des_set_key_parity(data, bytes);
5807 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005808#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 } else {
5810 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005811 }
Ronald Crondd04d422020-11-28 15:14:42 +01005812
Ronald Cronbf33c932020-11-28 18:06:53 +01005813 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005814 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005816 };
5817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5819 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5820 &storage_size);
5821 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305822 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 }
Archanad8a83dc2021-06-14 10:04:16 +05305824 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 status = psa_allocate_buffer_to_slot(slot, storage_size);
5826 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305827 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 }
Archanad8a83dc2021-06-14 10:04:16 +05305829
Przemek Stekiel055ffed2023-05-30 12:51:49 +02005830 status = psa_driver_wrapper_import_key(&attributes,
5831 data, bytes,
5832 slot->key.data,
5833 slot->key.bytes,
5834 &slot->key.bytes, &bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005836 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005838
5839exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 mbedtls_free(data);
5841 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005842}
5843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5845 psa_key_derivation_operation_t *operation,
5846 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005847{
5848 psa_status_t status;
5849 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005850 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005851
Ronald Cron81709fc2020-11-14 12:10:32 +01005852 *key = MBEDTLS_SVC_KEY_ID_INIT;
5853
Gilles Peskine0f84d622019-09-12 19:03:13 +02005854 /* Reject any attempt to create a zero-length key so that we don't
5855 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 if (psa_get_key_bits(attributes) == 0) {
5857 return PSA_ERROR_INVALID_ARGUMENT;
5858 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005859
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 if (operation->alg == PSA_ALG_NONE) {
5861 return PSA_ERROR_BAD_STATE;
5862 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 if (!operation->can_output_key) {
5865 return PSA_ERROR_NOT_PERMITTED;
5866 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5869 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005870#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005872 /* Deriving a key in a secure element is not implemented yet. */
5873 status = PSA_ERROR_NOT_SUPPORTED;
5874 }
5875#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 if (status == PSA_SUCCESS) {
5877 status = psa_generate_derived_key_internal(slot,
5878 attributes->core.bits,
5879 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005880 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 if (status == PSA_SUCCESS) {
5882 status = psa_finish_key_creation(slot, driver, key);
5883 }
5884 if (status != PSA_SUCCESS) {
5885 psa_fail_key_creation(slot, driver);
5886 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005889}
5890
Gilles Peskineeab56e42018-07-12 17:12:33 +02005891
5892
5893/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005894/* Key derivation */
5895/****************************************************************/
5896
Steven Cooreman932ffb72021-02-15 12:14:32 +01005897#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005898static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005899{
5900#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5902 return 1;
5903 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005904#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005905#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5907 return 1;
5908 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005909#endif
5910#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5912 return 1;
5913 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005914#endif
5915#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5917 return 1;
5918 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005919#endif
5920#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5922 return 1;
5923 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005924#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005925#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5927 return 1;
5928 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005929#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005931}
5932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005934{
5935 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 psa_status_t status = psa_hash_setup(&operation, alg);
5937 psa_hash_abort(&operation);
5938 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005939}
5940
Gilles Peskine969c5d62019-01-16 15:53:06 +01005941static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005942 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005944{
Janos Follath5fe19732019-06-20 15:09:30 +01005945 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5946 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005948
Gilles Peskine969c5d62019-01-16 15:53:06 +01005949 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 if (!is_kdf_alg_supported(kdf_alg)) {
5951 return PSA_ERROR_NOT_SUPPORTED;
5952 }
John Durkop07cc04a2020-11-16 22:08:34 -08005953
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005954 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005955 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5957 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5958 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5959 if (hash_size == 0) {
5960 return PSA_ERROR_NOT_SUPPORTED;
5961 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005962
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005963 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5964 * we might fail later, which is somewhat unfriendly and potentially
5965 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 psa_status_t status = psa_hash_try_support(hash_alg);
5967 if (status != PSA_SUCCESS) {
5968 return status;
5969 }
5970 } else {
5971 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005972 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5975 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5976 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5977 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005978 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005979#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005980 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5982 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005983 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005985#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5986 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 operation->capacity = 255 * hash_size;
5988 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005989}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005992{
5993#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 if (alg == PSA_ALG_ECDH) {
5995 return PSA_SUCCESS;
5996 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005997#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01005998#if defined(PSA_WANT_ALG_FFDH)
5999 if (alg == PSA_ALG_FFDH) {
6000 return PSA_SUCCESS;
6001 }
6002#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006003 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006005}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006006
6007static int psa_key_derivation_allows_free_form_secret_input(
6008 psa_algorithm_t kdf_alg)
6009{
6010#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6011 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6012 return 0;
6013 }
6014#endif
6015 (void) kdf_alg;
6016 return 1;
6017}
John Durkop07cc04a2020-11-16 22:08:34 -08006018#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6021 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006022{
6023 psa_status_t status;
6024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 if (operation->alg != 0) {
6026 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006027 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6030 return PSA_ERROR_INVALID_ARGUMENT;
6031 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6032#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6033 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6034 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6035 status = psa_key_agreement_try_support(ka_alg);
6036 if (status != PSA_SUCCESS) {
6037 return status;
6038 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006039 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6040 return PSA_ERROR_INVALID_ARGUMENT;
6041 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6043#else
6044 return PSA_ERROR_NOT_SUPPORTED;
6045#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6046 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6047#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6048 status = psa_key_derivation_setup_kdf(operation, alg);
6049#else
6050 return PSA_ERROR_NOT_SUPPORTED;
6051#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6052 } else {
6053 return PSA_ERROR_INVALID_ARGUMENT;
6054 }
6055
6056 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006057 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 }
6059 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006060}
6061
Przemek Stekiel69c46792022-06-10 12:59:51 +02006062#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006063static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6064 psa_algorithm_t kdf_alg,
6065 psa_key_derivation_step_t step,
6066 const uint8_t *data,
6067 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006068{
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006070 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006072 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006073#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6075 return PSA_ERROR_INVALID_ARGUMENT;
6076 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006077#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (hkdf->state != HKDF_STATE_INIT) {
6079 return PSA_ERROR_BAD_STATE;
6080 } else {
6081 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6082 hash_alg,
6083 data, data_length);
6084 if (status != PSA_SUCCESS) {
6085 return status;
6086 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006087 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006089 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006090 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006091#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006093 /* We shouldn't be in different state as HKDF_EXPAND only allows
6094 * two inputs: SECRET (this case) and INFO which does not modify
6095 * the state. It could happen only if the hkdf
6096 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (hkdf->state != HKDF_STATE_INIT) {
6098 return PSA_ERROR_BAD_STATE;
6099 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006100
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006101 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6103 return PSA_ERROR_INVALID_ARGUMENT;
6104 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 memcpy(hkdf->prk, data, data_length);
6107 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006108#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006109 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006110 /* HKDF: If no salt was provided, use an empty salt.
6111 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006113#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6115 return PSA_ERROR_BAD_STATE;
6116 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006117#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6119 hash_alg,
6120 NULL, 0);
6121 if (status != PSA_SUCCESS) {
6122 return status;
6123 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006124 hkdf->state = HKDF_STATE_STARTED;
6125 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 if (hkdf->state != HKDF_STATE_STARTED) {
6127 return PSA_ERROR_BAD_STATE;
6128 }
6129 status = psa_mac_update(&hkdf->hmac,
6130 data, data_length);
6131 if (status != PSA_SUCCESS) {
6132 return status;
6133 }
6134 status = psa_mac_sign_finish(&hkdf->hmac,
6135 hkdf->prk,
6136 sizeof(hkdf->prk),
6137 &data_length);
6138 if (status != PSA_SUCCESS) {
6139 return status;
6140 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006141 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006142
Gilles Peskine2b522db2019-04-12 15:11:49 +02006143 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006144 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006145#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006147 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006149 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006151#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006152 {
6153 /* Block 0 is empty, and the next block will be
6154 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006156 }
6157
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006159 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006160#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6162 return PSA_ERROR_INVALID_ARGUMENT;
6163 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006164#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006165#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6167 hkdf->state == HKDF_STATE_INIT) {
6168 return PSA_ERROR_BAD_STATE;
6169 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006170#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 if (hkdf->state == HKDF_STATE_OUTPUT) {
6172 return PSA_ERROR_BAD_STATE;
6173 }
6174 if (hkdf->info_set) {
6175 return PSA_ERROR_BAD_STATE;
6176 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006177 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 if (data_length != 0) {
6179 hkdf->info = mbedtls_calloc(1, data_length);
6180 if (hkdf->info == NULL) {
6181 return PSA_ERROR_INSUFFICIENT_MEMORY;
6182 }
6183 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006184 }
6185 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006187 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006189 }
6190}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006191#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006192
John Durkop07cc04a2020-11-16 22:08:34 -08006193#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6194 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006195static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6196 const uint8_t *data,
6197 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006198{
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6200 return PSA_ERROR_BAD_STATE;
6201 }
Janos Follathf08e2652019-06-13 09:05:41 +01006202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (data_length != 0) {
6204 prf->seed = mbedtls_calloc(1, data_length);
6205 if (prf->seed == NULL) {
6206 return PSA_ERROR_INSUFFICIENT_MEMORY;
6207 }
Janos Follathf08e2652019-06-13 09:05:41 +01006208
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006210 prf->seed_length = data_length;
6211 }
Janos Follathf08e2652019-06-13 09:05:41 +01006212
Gilles Peskine43f958b2020-12-13 14:55:14 +01006213 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006216}
6217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6219 const uint8_t *data,
6220 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006221{
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6223 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6224 return PSA_ERROR_BAD_STATE;
6225 }
Janos Follath81550542019-06-13 14:26:34 +01006226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 if (data_length != 0) {
6228 prf->secret = mbedtls_calloc(1, data_length);
6229 if (prf->secret == NULL) {
6230 return PSA_ERROR_INSUFFICIENT_MEMORY;
6231 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006232
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006234 prf->secret_length = data_length;
6235 }
Janos Follath81550542019-06-13 14:26:34 +01006236
Gilles Peskine43f958b2020-12-13 14:55:14 +01006237 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006240}
6241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6243 const uint8_t *data,
6244 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006245{
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6247 return PSA_ERROR_BAD_STATE;
6248 }
Janos Follath63028dd2019-06-13 09:15:47 +01006249
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 if (data_length != 0) {
6251 prf->label = mbedtls_calloc(1, data_length);
6252 if (prf->label == NULL) {
6253 return PSA_ERROR_INSUFFICIENT_MEMORY;
6254 }
Janos Follath63028dd2019-06-13 09:15:47 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006257 prf->label_length = data_length;
6258 }
Janos Follath63028dd2019-06-13 09:15:47 +01006259
Gilles Peskine43f958b2020-12-13 14:55:14 +01006260 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006261
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006263}
6264
Gilles Peskine449bd832023-01-11 14:50:10 +01006265static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6266 psa_key_derivation_step_t step,
6267 const uint8_t *data,
6268 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006269{
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006271 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006273 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006275 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006277 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006279 }
6280}
John Durkop07cc04a2020-11-16 22:08:34 -08006281#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6282 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6283
6284#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6285static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6286 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006287 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006289{
6290 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6292 4 + data_length + prf->other_secret_length :
6293 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6296 return PSA_ERROR_INVALID_ARGUMENT;
6297 }
John Durkop07cc04a2020-11-16 22:08:34 -08006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 uint8_t *pms = mbedtls_calloc(1, pms_len);
6300 if (pms == NULL) {
6301 return PSA_ERROR_INSUFFICIENT_MEMORY;
6302 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006303 uint8_t *cur = pms;
6304
6305 /* pure-PSK:
6306 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006307 *
6308 * The premaster secret is formed as follows: if the PSK is N octets
6309 * long, concatenate a uint16 with the value N, N zero octets, a second
6310 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006311 *
6312 * mixed-PSK:
6313 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6314 * follows: concatenate a uint16 with the length of the other secret,
6315 * the other secret itself, uint16 with the length of PSK, and the
6316 * PSK itself.
6317 * For details please check:
6318 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6319 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6320 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006321 */
6322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6324 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6325 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6326 if (prf->other_secret_length != 0) {
6327 memcpy(cur, prf->other_secret, prf->other_secret_length);
6328 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006329 cur += prf->other_secret_length;
6330 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 } else {
6332 *cur++ = MBEDTLS_BYTE_1(data_length);
6333 *cur++ = MBEDTLS_BYTE_0(data_length);
6334 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006335 cur += data_length;
6336 }
6337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 *cur++ = MBEDTLS_BYTE_1(data_length);
6339 *cur++ = MBEDTLS_BYTE_0(data_length);
6340 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006341 cur += data_length;
6342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 mbedtls_platform_zeroize(pms, pms_len);
6346 mbedtls_free(pms);
6347 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006348}
Janos Follath6660f0e2019-06-17 08:44:03 +01006349
Przemek Stekiele47201b2022-04-19 13:53:28 +02006350static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6351 psa_tls12_prf_key_derivation_t *prf,
6352 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006354{
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6356 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006357 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006358
6359 if (data_length != 0) {
6360 prf->other_secret = mbedtls_calloc(1, data_length);
6361 if (prf->other_secret == NULL) {
6362 return PSA_ERROR_INSUFFICIENT_MEMORY;
6363 }
6364
6365 memcpy(prf->other_secret, data, data_length);
6366 prf->other_secret_length = data_length;
6367 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006368 prf->other_secret_length = 0;
6369 }
6370
6371 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006374}
6375
Janos Follath6660f0e2019-06-17 08:44:03 +01006376static psa_status_t psa_tls12_prf_psk_to_ms_input(
6377 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006378 psa_key_derivation_step_t step,
6379 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006381{
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006383 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 return psa_tls12_prf_psk_to_ms_set_key(prf,
6385 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006386 break;
6387 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6389 data,
6390 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006391 break;
6392 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006394 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006395
Przemek Stekiele47201b2022-04-19 13:53:28 +02006396 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006397}
John Durkop07cc04a2020-11-16 22:08:34 -08006398#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006399
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006400#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6401static psa_status_t psa_tls12_ecjpake_to_pms_input(
6402 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006403 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006404 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006406{
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6408 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6409 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006410 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006411
6412 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 if (data[0] != 0x04) {
6414 return PSA_ERROR_INVALID_ARGUMENT;
6415 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006416
6417 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006419
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006421}
6422#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006423/** Check whether the given key type is acceptable for the given
6424 * input step of a key derivation.
6425 *
6426 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6427 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6428 * Both secret and non-secret inputs can alternatively have the type
6429 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6430 * that the input was passed as a buffer rather than via a key object.
6431 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006432static int psa_key_derivation_check_input_type(
6433 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006435{
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006437 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 if (key_type == PSA_KEY_TYPE_DERIVE) {
6439 return PSA_SUCCESS;
6440 }
6441 if (key_type == PSA_KEY_TYPE_NONE) {
6442 return PSA_SUCCESS;
6443 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006444 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006445 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 if (key_type == PSA_KEY_TYPE_DERIVE) {
6447 return PSA_SUCCESS;
6448 }
6449 if (key_type == PSA_KEY_TYPE_NONE) {
6450 return PSA_SUCCESS;
6451 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006452 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006453 case PSA_KEY_DERIVATION_INPUT_LABEL:
6454 case PSA_KEY_DERIVATION_INPUT_SALT:
6455 case PSA_KEY_DERIVATION_INPUT_INFO:
6456 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6458 return PSA_SUCCESS;
6459 }
6460 if (key_type == PSA_KEY_TYPE_NONE) {
6461 return PSA_SUCCESS;
6462 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006463 break;
6464 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006466}
6467
Janos Follathb80a94e2019-06-12 15:54:46 +01006468static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006469 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006470 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006471 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006472 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006474{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006475 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006477
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 status = psa_key_derivation_check_input_type(step, key_type);
6479 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006480 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006482
Przemek Stekiel69c46792022-06-10 12:59:51 +02006483#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6485 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6486 step, data, data_length);
6487 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006488#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006489#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6491 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6492 step, data, data_length);
6493 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006494#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6495#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6497 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6498 step, data, data_length);
6499 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006500#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006501#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006503 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6505 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006506#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006507 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006508 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006509 (void) data;
6510 (void) data_length;
6511 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006513 }
6514
Gilles Peskine224b0d62019-09-23 18:13:17 +02006515exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 if (status != PSA_SUCCESS) {
6517 psa_key_derivation_abort(operation);
6518 }
6519 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006520}
6521
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306522static psa_status_t psa_key_derivation_input_integer_internal(
6523 psa_key_derivation_operation_t *operation,
6524 psa_key_derivation_step_t step,
6525 uint64_t value)
6526{
6527 psa_status_t status;
6528 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6529
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306530 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306531 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306532 (void) value;
6533 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306534 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306535 }
6536
6537 if (status != PSA_SUCCESS) {
6538 psa_key_derivation_abort(operation);
6539 }
6540 return status;
6541}
6542
Janos Follath51f4a0f2019-06-14 11:35:55 +01006543psa_status_t psa_key_derivation_input_bytes(
6544 psa_key_derivation_operation_t *operation,
6545 psa_key_derivation_step_t step,
6546 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006548{
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 return psa_key_derivation_input_internal(operation, step,
6550 PSA_KEY_TYPE_NONE,
6551 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006552}
6553
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306554psa_status_t psa_key_derivation_input_integer(
6555 psa_key_derivation_operation_t *operation,
6556 psa_key_derivation_step_t step,
6557 uint64_t value)
6558{
6559 return psa_key_derivation_input_integer_internal(operation, step, value);
6560}
6561
Janos Follath51f4a0f2019-06-14 11:35:55 +01006562psa_status_t psa_key_derivation_input_key(
6563 psa_key_derivation_operation_t *operation,
6564 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006566{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006568 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006569 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006570
Ronald Cron5c522922020-11-14 16:35:34 +01006571 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6573 if (status != PSA_SUCCESS) {
6574 psa_key_derivation_abort(operation);
6575 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006576 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006577
6578 /* Passing a key object as a SECRET input unlocks the permission
6579 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006581 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006583
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 status = psa_key_derivation_input_internal(operation,
6585 step, slot->attr.type,
6586 slot->key.data,
6587 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006588
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006590
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006592}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006593
6594
6595
6596/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006597/* Key agreement */
6598/****************************************************************/
6599
Gilles Peskine449bd832023-01-11 14:50:10 +01006600psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6601 const uint8_t *key_buffer,
6602 size_t key_buffer_size,
6603 psa_algorithm_t alg,
6604 const uint8_t *peer_key,
6605 size_t peer_key_length,
6606 uint8_t *shared_secret,
6607 size_t shared_secret_size,
6608 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006609{
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006611#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006612 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6614 key_buffer_size, alg,
6615 peer_key, peer_key_length,
6616 shared_secret,
6617 shared_secret_size,
6618 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006619#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006620
6621#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6622 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006623 return mbedtls_psa_key_agreement_ffdh(attributes,
6624 peer_key,
6625 peer_key_length,
6626 key_buffer,
6627 key_buffer_size,
6628 shared_secret,
6629 shared_secret_size,
6630 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006631#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6632
Gilles Peskine01d718c2018-09-18 12:01:02 +02006633 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006634 (void) attributes;
6635 (void) key_buffer;
6636 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006637 (void) peer_key;
6638 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006639 (void) shared_secret;
6640 (void) shared_secret_size;
6641 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006643 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006644}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006645
Aditya Deshpande17845b82022-10-13 17:21:01 +01006646/** Internal function for raw key agreement
6647 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006648 * to the driver's implementation if a driver is present.
6649 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006650 * (psa_key_agreement_raw_builtin).
6651 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006652static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6653 psa_key_slot_t *private_key,
6654 const uint8_t *peer_key,
6655 size_t peer_key_length,
6656 uint8_t *shared_secret,
6657 size_t shared_secret_size,
6658 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006659{
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6661 return PSA_ERROR_NOT_SUPPORTED;
6662 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006663
6664 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006666 };
6667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 return psa_driver_wrapper_key_agreement(&attributes,
6669 private_key->key.data,
6670 private_key->key.bytes, alg,
6671 peer_key, peer_key_length,
6672 shared_secret,
6673 shared_secret_size,
6674 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006675}
6676
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006677/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006678 * to potentially free embedded data structures and wipe confidential data.
6679 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006680static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6681 psa_key_derivation_step_t step,
6682 psa_key_slot_t *private_key,
6683 const uint8_t *peer_key,
6684 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006685{
6686 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006687 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006688 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006690
6691 /* Step 1: run the secret agreement algorithm to generate the shared
6692 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 status = psa_key_agreement_raw_internal(ka_alg,
6694 private_key,
6695 peer_key, peer_key_length,
6696 shared_secret,
6697 sizeof(shared_secret),
6698 &shared_secret_length);
6699 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006700 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006702
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006703 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006704 * the shared secret. A shared secret is permitted wherever a key
6705 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006706 status = psa_key_derivation_input_internal(operation, step,
6707 PSA_KEY_TYPE_DERIVE,
6708 shared_secret,
6709 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006710exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6712 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006713}
6714
Gilles Peskine449bd832023-01-11 14:50:10 +01006715psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6716 psa_key_derivation_step_t step,
6717 mbedtls_svc_key_id_t private_key,
6718 const uint8_t *peer_key,
6719 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006720{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006721 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006722 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006723 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006724
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6726 return PSA_ERROR_INVALID_ARGUMENT;
6727 }
Ronald Cron5c522922020-11-14 16:35:34 +01006728 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6730 if (status != PSA_SUCCESS) {
6731 return status;
6732 }
6733 status = psa_key_agreement_internal(operation, step,
6734 slot,
6735 peer_key, peer_key_length);
6736 if (status != PSA_SUCCESS) {
6737 psa_key_derivation_abort(operation);
6738 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006739 /* If a private key has been added as SECRET, we allow the derived
6740 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006742 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006744 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006745
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006747
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006749}
6750
Gilles Peskine449bd832023-01-11 14:50:10 +01006751psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6752 mbedtls_svc_key_id_t private_key,
6753 const uint8_t *peer_key,
6754 size_t peer_key_length,
6755 uint8_t *output,
6756 size_t output_size,
6757 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006758{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006759 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006760 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006761 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006762
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006764 status = PSA_ERROR_INVALID_ARGUMENT;
6765 goto exit;
6766 }
Ronald Cron5c522922020-11-14 16:35:34 +01006767 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6769 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006770 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006772
Gilles Peskine3e561302022-04-14 00:17:15 +02006773 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6774 * for the output size. The PSA specification only guarantees that this
6775 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6776 * but it might be nice to allow smaller buffers if the output fits.
6777 * At the time of writing this comment, with only ECDH implemented,
6778 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6779 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6780 * be exact for it as well. */
6781 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6783 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006784 status = PSA_ERROR_BUFFER_TOO_SMALL;
6785 goto exit;
6786 }
6787
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 status = psa_key_agreement_raw_internal(alg, slot,
6789 peer_key, peer_key_length,
6790 output, output_size,
6791 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006792
6793exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006795 /* If an error happens and is not handled properly, the output
6796 * may be used as a key to protect sensitive data. Arrange for such
6797 * a key to be random, which is likely to result in decryption or
6798 * verification errors. This is better than filling the buffer with
6799 * some constant data such as zeros, which would result in the data
6800 * being protected with a reproducible, easily knowable key.
6801 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006803 *output_length = output_size;
6804 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006805
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006807
Gilles Peskine449bd832023-01-11 14:50:10 +01006808 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006809}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006810
6811
Gilles Peskine30524eb2020-11-13 17:02:26 +01006812
Gilles Peskine86a440b2018-11-12 18:39:40 +01006813/****************************************************************/
6814/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006815/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006816
Gilles Peskine30524eb2020-11-13 17:02:26 +01006817/** Initialize the PSA random generator.
6818 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006819static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006820{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006821#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006823#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6824
Gilles Peskine30524eb2020-11-13 17:02:26 +01006825 /* Set default configuration if
6826 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006828 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 }
6830 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006831 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006833
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006835#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6836 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6837 /* The PSA entropy injection feature depends on using NV seed as an entropy
6838 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 mbedtls_entropy_add_source(&rng->entropy,
6840 mbedtls_nv_seed_poll, NULL,
6841 MBEDTLS_ENTROPY_BLOCK_SIZE,
6842 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006843#endif
6844
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006846#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006847}
6848
6849/** Deinitialize the PSA random generator.
6850 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006851static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006852{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006853#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006855#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6857 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006858#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006859}
6860
6861/** Seed the PSA random generator.
6862 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006863static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006864{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006865#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6866 /* Do nothing: the external RNG seeds itself. */
6867 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006869#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006870 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6872 drbg_seed, sizeof(drbg_seed) - 1);
6873 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006874#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006875}
6876
Gilles Peskine449bd832023-01-11 14:50:10 +01006877psa_status_t psa_generate_random(uint8_t *output,
6878 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006879{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006880 GUARD_MODULE_INITIALIZED;
6881
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006882#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6883
6884 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6886 output, output_size,
6887 &output_length);
6888 if (status != PSA_SUCCESS) {
6889 return status;
6890 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006891 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006892 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 if (output_length != output_size) {
6894 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6895 }
6896 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006897
6898#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6899
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006901 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6903 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6904 output_size);
6905 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6906 output, request_size);
6907 if (ret != 0) {
6908 return mbedtls_to_psa_error(ret);
6909 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006910 output_size -= request_size;
6911 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006912 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006914#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006915}
6916
Gilles Peskine8814fc42020-12-14 15:33:44 +01006917/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006918 *
6919 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6920 * `psa_generate_random(...)`. The state parameter is ignored since the
6921 * PSA API doesn't support passing an explicit state.
6922 *
6923 * In the non-external case, psa_generate_random() calls an
6924 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6925 * and semantics as mbedtls_psa_get_random(). As an optimization,
6926 * instead of doing this back-and-forth between the PSA API and the
6927 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6928 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006929 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006930#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6931int mbedtls_psa_get_random(void *p_rng,
6932 unsigned char *output,
6933 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006934{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006935 /* This function takes a pointer to the RNG state because that's what
6936 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6937 * its own state internally and doesn't let the caller access that state.
6938 * So we just ignore the state parameter, and in practice we'll pass
6939 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006940 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 psa_status_t status = psa_generate_random(output, output_size);
6942 if (status == PSA_SUCCESS) {
6943 return 0;
6944 } else {
6945 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6946 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006947}
6948#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6949
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006950#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006951#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006952
Gilles Peskine449bd832023-01-11 14:50:10 +01006953psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6954 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006955{
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 if (global_data.initialized) {
6957 return PSA_ERROR_NOT_PERMITTED;
6958 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006959
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6961 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6962 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6963 return PSA_ERROR_INVALID_ARGUMENT;
6964 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006967}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006968#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006969
Ronald Cron3772afe2021-02-08 16:10:05 +01006970/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006971 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006972 * \param type The key type
6973 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006974 *
6975 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006976 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006977 * \retval #PSA_ERROR_INVALID_ARGUMENT
6978 * The size in bits of the key is not valid.
6979 * \retval #PSA_ERROR_NOT_SUPPORTED
6980 * The type and/or the size in bits of the key or the combination of
6981 * the two is not supported.
6982 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006983static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006985{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006986 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006987
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 if (key_type_is_raw_bytes(type)) {
6989 status = psa_validate_unstructured_key_bit_size(type, bits);
6990 if (status != PSA_SUCCESS) {
6991 return status;
6992 }
6993 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006994#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006995 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6996 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6997 return PSA_ERROR_NOT_SUPPORTED;
6998 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006999
Ronald Cron01b2aba2020-10-05 09:42:02 +02007000 /* Accept only byte-aligned keys, for the same reasons as
7001 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007002 if (bits % 8 != 0) {
7003 return PSA_ERROR_NOT_SUPPORTED;
7004 }
7005 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007006#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007007
Ronald Cron5c4d3862020-12-07 11:07:24 +01007008#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007010 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 return PSA_SUCCESS;
7012 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007013#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007014
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007015#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007016 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007017 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007018 return PSA_ERROR_NOT_SUPPORTED;
7019 }
7020 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007021#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007022 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007024 }
7025
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007027}
7028
Ronald Cron55ed0592020-10-05 10:30:40 +02007029psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007030 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007032{
7033 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007034 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007035
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 if ((attributes->domain_parameters == NULL) &&
7037 (attributes->domain_parameters_size != 0)) {
7038 return PSA_ERROR_INVALID_ARGUMENT;
7039 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007040
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 if (key_type_is_raw_bytes(type)) {
7042 status = psa_generate_random(key_buffer, key_buffer_size);
7043 if (status != PSA_SUCCESS) {
7044 return status;
7045 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007046
David Brown12ca5032021-02-11 11:02:00 -07007047#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007048 if (type == PSA_KEY_TYPE_DES) {
7049 psa_des_set_key_parity(key_buffer, key_buffer_size);
7050 }
David Brown8107e312021-02-12 08:08:46 -07007051#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007053
Jaeden Amero424fa932021-05-14 08:34:32 +01007054#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7055 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7057 return mbedtls_psa_rsa_generate_key(attributes,
7058 key_buffer,
7059 key_buffer_size,
7060 key_buffer_length);
7061 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007062#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7063 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007064
John Durkop9814fa22020-11-04 12:28:15 -08007065#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7067 return mbedtls_psa_ecp_generate_key(attributes,
7068 key_buffer,
7069 key_buffer_size,
7070 key_buffer_length);
7071 } else
John Durkop9814fa22020-11-04 12:28:15 -08007072#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007073
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007074#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007075 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7076 return mbedtls_psa_ffdh_generate_key(attributes,
7077 key_buffer,
7078 key_buffer_size,
7079 key_buffer_length);
7080 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007081#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007082 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 (void) key_buffer_length;
7084 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007085 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007086
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007088}
Darryl Green0c6575a2018-11-07 16:05:30 +00007089
Gilles Peskine449bd832023-01-11 14:50:10 +01007090psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7091 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007092{
7093 psa_status_t status;
7094 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007095 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007096 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007097
Ronald Cron81709fc2020-11-14 12:10:32 +01007098 *key = MBEDTLS_SVC_KEY_ID_INIT;
7099
Gilles Peskine0f84d622019-09-12 19:03:13 +02007100 /* Reject any attempt to create a zero-length key so that we don't
7101 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 if (psa_get_key_bits(attributes) == 0) {
7103 return PSA_ERROR_INVALID_ARGUMENT;
7104 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007105
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007106 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7108 return PSA_ERROR_INVALID_ARGUMENT;
7109 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007110
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7112 &slot, &driver);
7113 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 }
Gilles Peskine11792082019-08-06 18:36:36 +02007116
Ronald Cron977c2472020-10-13 08:32:21 +02007117 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307118 * storage ( thus not in the case of generating a key in a secure element
7119 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7120 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 if (slot->key.data == NULL) {
7122 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7123 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007124 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 attributes->core.type, attributes->core.bits);
7126 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007129
7130 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 attributes->core.type,
7132 attributes->core.bits);
7133 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007134 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 attributes, &key_buffer_size);
7136 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 }
Ronald Cron977c2472020-10-13 08:32:21 +02007139 }
Ronald Cron977c2472020-10-13 08:32:21 +02007140
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7142 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007143 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 }
Ronald Cron977c2472020-10-13 08:32:21 +02007145 }
7146
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 status = psa_driver_wrapper_generate_key(attributes,
7148 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007149
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 if (status != PSA_SUCCESS) {
7151 psa_remove_key_data_from_memory(slot);
7152 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007153
Gilles Peskine11792082019-08-06 18:36:36 +02007154exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 if (status == PSA_SUCCESS) {
7156 status = psa_finish_key_creation(slot, driver, key);
7157 }
7158 if (status != PSA_SUCCESS) {
7159 psa_fail_key_creation(slot, driver);
7160 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007161
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007163}
7164
Gilles Peskinee59236f2018-01-27 23:32:46 +01007165/****************************************************************/
7166/* Module setup */
7167/****************************************************************/
7168
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007169#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007170psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 void (* entropy_init)(mbedtls_entropy_context *ctx),
7172 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007173{
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7175 return PSA_ERROR_BAD_STATE;
7176 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007177 global_data.rng.entropy_init = entropy_init;
7178 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007180}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007181#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007182
Gilles Peskine449bd832023-01-11 14:50:10 +01007183void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007184{
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 psa_wipe_all_key_slots();
7186 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7187 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007188 }
7189 /* Wipe all remaining data, including configuration.
7190 * In particular, this sets all state indicator to the value
7191 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007192 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007193
7194 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007196}
7197
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007198#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7199/** Recover a transaction that was interrupted by a power failure.
7200 *
7201 * This function is called during initialization, before psa_crypto_init()
7202 * returns. If this function returns a failure status, the initialization
7203 * fails.
7204 */
7205static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007206 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007207{
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007209 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7210 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 /* TODO - fall through to the failure case until this
7212 * is implemented.
7213 * https://github.com/ARMmbed/mbed-crypto/issues/218
7214 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007215 default:
7216 /* We found an unsupported transaction in the storage.
7217 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007219 }
7220}
7221#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7222
Gilles Peskine449bd832023-01-11 14:50:10 +01007223psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007224{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007225 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007226
Gilles Peskinec6b69072018-11-20 21:42:52 +01007227 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007228 if (global_data.initialized != 0) {
7229 return PSA_SUCCESS;
7230 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007231
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007232 /* Init drivers */
7233 status = psa_driver_wrapper_init();
7234 if (status != PSA_SUCCESS) {
7235 goto exit;
7236 }
7237 global_data.drivers_initialized = 1;
7238
Gilles Peskine30524eb2020-11-13 17:02:26 +01007239 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007240 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007241 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 status = mbedtls_psa_random_seed(&global_data.rng);
7243 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007244 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007245 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007246 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007247
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 status = psa_initialize_key_slots();
7249 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007250 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007252
Gilles Peskine4b734222019-07-24 15:56:31 +02007253#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 status = psa_crypto_load_transaction();
7255 if (status == PSA_SUCCESS) {
7256 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7257 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007258 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 }
7260 status = psa_crypto_stop_transaction();
7261 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007262 /* There's no transaction to complete. It's all good. */
7263 status = PSA_SUCCESS;
7264 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007265#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007266
Gilles Peskinec6b69072018-11-20 21:42:52 +01007267 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007268 global_data.initialized = 1;
7269
7270exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 if (status != PSA_SUCCESS) {
7272 mbedtls_psa_crypto_free();
7273 }
7274 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007275}
7276
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007277psa_status_t psa_crypto_driver_pake_get_password_len(
7278 const psa_crypto_driver_pake_inputs_t *inputs,
7279 size_t *password_len)
7280{
7281 if (inputs->password_len == 0) {
7282 return PSA_ERROR_BAD_STATE;
7283 }
7284
7285 *password_len = inputs->password_len;
7286
7287 return PSA_SUCCESS;
7288}
7289
7290psa_status_t psa_crypto_driver_pake_get_password(
7291 const psa_crypto_driver_pake_inputs_t *inputs,
7292 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7293{
7294 if (inputs->password_len == 0) {
7295 return PSA_ERROR_BAD_STATE;
7296 }
7297
7298 if (buffer_size < inputs->password_len) {
7299 return PSA_ERROR_BUFFER_TOO_SMALL;
7300 }
7301
7302 memcpy(buffer, inputs->password, inputs->password_len);
7303 *buffer_length = inputs->password_len;
7304
7305 return PSA_SUCCESS;
7306}
7307
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007308psa_status_t psa_crypto_driver_pake_get_user_len(
7309 const psa_crypto_driver_pake_inputs_t *inputs,
7310 size_t *user_len)
7311{
7312 if (inputs->user_len == 0) {
7313 return PSA_ERROR_BAD_STATE;
7314 }
7315
7316 *user_len = inputs->user_len;
7317
7318 return PSA_SUCCESS;
7319}
7320
7321psa_status_t psa_crypto_driver_pake_get_user(
7322 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007323 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007324{
7325 if (inputs->user_len == 0) {
7326 return PSA_ERROR_BAD_STATE;
7327 }
7328
Przemek Stekielc0e62502023-03-14 11:49:36 +01007329 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007330 return PSA_ERROR_BUFFER_TOO_SMALL;
7331 }
7332
Przemek Stekielc0e62502023-03-14 11:49:36 +01007333 memcpy(user_id, inputs->user, inputs->user_len);
7334 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007335
7336 return PSA_SUCCESS;
7337}
7338
7339psa_status_t psa_crypto_driver_pake_get_peer_len(
7340 const psa_crypto_driver_pake_inputs_t *inputs,
7341 size_t *peer_len)
7342{
7343 if (inputs->peer_len == 0) {
7344 return PSA_ERROR_BAD_STATE;
7345 }
7346
7347 *peer_len = inputs->peer_len;
7348
7349 return PSA_SUCCESS;
7350}
7351
7352psa_status_t psa_crypto_driver_pake_get_peer(
7353 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007354 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007355{
7356 if (inputs->peer_len == 0) {
7357 return PSA_ERROR_BAD_STATE;
7358 }
7359
Przemek Stekielc0e62502023-03-14 11:49:36 +01007360 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007361 return PSA_ERROR_BUFFER_TOO_SMALL;
7362 }
7363
Przemek Stekielc0e62502023-03-14 11:49:36 +01007364 memcpy(peer_id, inputs->peer, inputs->peer_len);
7365 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007366
7367 return PSA_SUCCESS;
7368}
7369
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007370psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7371 const psa_crypto_driver_pake_inputs_t *inputs,
7372 psa_pake_cipher_suite_t *cipher_suite)
7373{
7374 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7375 return PSA_ERROR_BAD_STATE;
7376 }
7377
7378 *cipher_suite = inputs->cipher_suite;
7379
7380 return PSA_SUCCESS;
7381}
7382
Neil Armstronga7d08c32022-06-01 18:21:20 +02007383psa_status_t psa_pake_setup(
7384 psa_pake_operation_t *operation,
7385 const psa_pake_cipher_suite_t *cipher_suite)
7386{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007388
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007389 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007390 status = PSA_ERROR_BAD_STATE;
7391 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007392 }
7393
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007394 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007395 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007396 status = PSA_ERROR_INVALID_ARGUMENT;
7397 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007398 }
7399
Przemek Stekiel51eac532022-12-07 11:04:51 +01007400 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7401
Przemek Stekiele12ed362022-12-21 12:54:46 +01007402 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007403 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7404 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007405 operation->data.inputs.cipher_suite = *cipher_suite;
7406
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007407#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007408 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007409 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007410 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007411
Przemek Stekiele12ed362022-12-21 12:54:46 +01007412 computation_stage->state = PSA_PAKE_STATE_SETUP;
7413 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7414 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7415 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007416 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007417#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007418 {
7419 status = PSA_ERROR_NOT_SUPPORTED;
7420 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007421 }
7422
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007423 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7424
Przemek Stekiel51eac532022-12-07 11:04:51 +01007425 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007426exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007427 psa_pake_abort(operation);
7428 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007429}
7430
7431psa_status_t psa_pake_set_password_key(
7432 psa_pake_operation_t *operation,
7433 mbedtls_svc_key_id_t password)
7434{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007435 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007436 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7437 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007438
Przemek Stekiel51eac532022-12-07 11:04:51 +01007439 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007440 status = PSA_ERROR_BAD_STATE;
7441 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007442 }
7443
Przemek Stekiel6c764412022-11-22 14:05:12 +01007444 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7445 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007446 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007447 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007448 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007449 }
7450
Przemek Stekiel6c764412022-11-22 14:05:12 +01007451 psa_key_attributes_t attributes = {
7452 .core = slot->attr
7453 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007454
Przemek Stekiel51eac532022-12-07 11:04:51 +01007455 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007456
Przemek Stekiel51eac532022-12-07 11:04:51 +01007457 if (type != PSA_KEY_TYPE_PASSWORD &&
7458 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7459 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007460 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007461 }
7462
Przemek Stekiel51eac532022-12-07 11:04:51 +01007463 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7464 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007465 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007466 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007467 }
7468
7469 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7470 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007471 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007472exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007473 if (status != PSA_SUCCESS) {
7474 psa_pake_abort(operation);
7475 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007476 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007477 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007478}
7479
7480psa_status_t psa_pake_set_user(
7481 psa_pake_operation_t *operation,
7482 const uint8_t *user_id,
7483 size_t user_id_len)
7484{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007485 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007486
7487 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007488 status = PSA_ERROR_BAD_STATE;
7489 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007490 }
7491
Przemek Stekiel51eac532022-12-07 11:04:51 +01007492 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007493 status = PSA_ERROR_INVALID_ARGUMENT;
7494 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007495 }
7496
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007497 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007498 status = PSA_ERROR_BAD_STATE;
7499 goto exit;
7500 }
7501
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007502 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7503 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007504 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7505 goto exit;
7506 }
7507
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007508 memcpy(operation->data.inputs.user, user_id, user_id_len);
7509 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007510
7511 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007512exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007513 psa_pake_abort(operation);
7514 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007515}
7516
7517psa_status_t psa_pake_set_peer(
7518 psa_pake_operation_t *operation,
7519 const uint8_t *peer_id,
7520 size_t peer_id_len)
7521{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007523
7524 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007525 status = PSA_ERROR_BAD_STATE;
7526 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007527 }
7528
Przemek Stekiel51eac532022-12-07 11:04:51 +01007529 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007530 status = PSA_ERROR_INVALID_ARGUMENT;
7531 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007532 }
7533
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007534 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007535 status = PSA_ERROR_BAD_STATE;
7536 goto exit;
7537 }
7538
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007539 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7540 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007541 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7542 goto exit;
7543 }
7544
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007545 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7546 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007547
7548 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007549exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007550 psa_pake_abort(operation);
7551 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007552}
7553
7554psa_status_t psa_pake_set_role(
7555 psa_pake_operation_t *operation,
7556 psa_pake_role_t role)
7557{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007558 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007559
Przemek Stekiel51eac532022-12-07 11:04:51 +01007560 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007561 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007562 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007563 }
7564
Przemek Stekiel09104b82023-03-06 14:11:51 +01007565 switch (operation->alg) {
7566#if defined(PSA_WANT_ALG_JPAKE)
7567 case PSA_ALG_JPAKE:
7568 if (role == PSA_PAKE_ROLE_NONE) {
7569 return PSA_SUCCESS;
7570 }
7571 status = PSA_ERROR_INVALID_ARGUMENT;
7572 break;
7573#endif
7574 default:
7575 (void) role;
7576 status = PSA_ERROR_NOT_SUPPORTED;
7577 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007578 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007579exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007580 psa_pake_abort(operation);
7581 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007582}
7583
Przemek Stekielb09c4872023-01-17 12:05:38 +01007584/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007585#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007586static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007587 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007588{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007589 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007590 case PSA_PAKE_OUTPUT_X1_X2:
7591 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007592 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007593 case PSA_PAKE_X1_STEP_KEY_SHARE:
7594 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007595 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7596 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007597 case PSA_PAKE_X1_STEP_ZK_PROOF:
7598 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007599 case PSA_PAKE_X2_STEP_KEY_SHARE:
7600 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007601 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7602 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007603 case PSA_PAKE_X2_STEP_ZK_PROOF:
7604 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007605 default:
7606 return PSA_JPAKE_STEP_INVALID;
7607 }
7608 break;
7609 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007610 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007611 case PSA_PAKE_X1_STEP_KEY_SHARE:
7612 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007613 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7614 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007615 case PSA_PAKE_X1_STEP_ZK_PROOF:
7616 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007617 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007618 return PSA_JPAKE_STEP_INVALID;
7619 }
7620 break;
7621 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007622 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007623 case PSA_PAKE_X1_STEP_KEY_SHARE:
7624 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007625 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7626 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007627 case PSA_PAKE_X1_STEP_ZK_PROOF:
7628 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007629 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007630 return PSA_JPAKE_STEP_INVALID;
7631 }
7632 break;
7633 default:
7634 return PSA_JPAKE_STEP_INVALID;
7635 }
7636 return PSA_JPAKE_STEP_INVALID;
7637}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007638#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007639
Przemek Stekiel2797d372022-12-22 11:19:22 +01007640static psa_status_t psa_pake_complete_inputs(
7641 psa_pake_operation_t *operation)
7642{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007643 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007644 /* Create copy of the inputs on stack as inputs share memory
7645 with the driver context which will be setup by the driver. */
7646 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007647
Przemek Stekielfde11282023-03-13 16:06:09 +01007648 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007649 return PSA_ERROR_BAD_STATE;
7650 }
7651
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007652 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007653 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7654 return PSA_ERROR_BAD_STATE;
7655 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007656 }
7657
Przemek Stekiel18620a32023-01-17 16:34:52 +01007658 /* Clear driver context */
7659 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7660
7661 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007662
7663 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007664 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7665 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007666
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007667 /* User and peer are translated to role. */
7668 mbedtls_free(inputs.user);
7669 mbedtls_free(inputs.peer);
7670
Przemek Stekiel2797d372022-12-22 11:19:22 +01007671 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007672#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007673 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007674 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007675 psa_jpake_computation_stage_t *computation_stage =
7676 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007677 computation_stage->state = PSA_PAKE_STATE_READY;
7678 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7679 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7680 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007681 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007682#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007683 {
7684 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007685 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007686 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007687 return status;
7688}
7689
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007690#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007691static psa_status_t psa_jpake_output_prologue(
7692 psa_pake_operation_t *operation,
7693 psa_pake_step_t step)
7694{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007695 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7696 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7697 step != PSA_PAKE_STEP_ZK_PROOF) {
7698 return PSA_ERROR_INVALID_ARGUMENT;
7699 }
7700
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007701 psa_jpake_computation_stage_t *computation_stage =
7702 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007703
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007704 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7705 return PSA_ERROR_BAD_STATE;
7706 }
7707
7708 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7709 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7710 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7711 return PSA_ERROR_BAD_STATE;
7712 }
7713
7714 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7715 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007716 return PSA_ERROR_BAD_STATE;
7717 }
7718
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007719 switch (computation_stage->output_step) {
7720 case PSA_PAKE_STEP_X1_X2:
7721 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007722 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007723 case PSA_PAKE_STEP_X2S:
7724 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007725 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007726 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007727 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007728 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007729
7730 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7731 }
7732
7733 /* Check if step matches current sequence */
7734 switch (computation_stage->sequence) {
7735 case PSA_PAKE_X1_STEP_KEY_SHARE:
7736 case PSA_PAKE_X2_STEP_KEY_SHARE:
7737 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7738 return PSA_ERROR_BAD_STATE;
7739 }
7740 break;
7741
7742 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7743 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7744 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7745 return PSA_ERROR_BAD_STATE;
7746 }
7747 break;
7748
7749 case PSA_PAKE_X1_STEP_ZK_PROOF:
7750 case PSA_PAKE_X2_STEP_ZK_PROOF:
7751 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7752 return PSA_ERROR_BAD_STATE;
7753 }
7754 break;
7755
7756 default:
7757 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007758 }
7759
7760 return PSA_SUCCESS;
7761}
7762
7763static psa_status_t psa_jpake_output_epilogue(
7764 psa_pake_operation_t *operation)
7765{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007766 psa_jpake_computation_stage_t *computation_stage =
7767 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007768
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007769 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007770 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007771 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007772 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007773 computation_stage->state = PSA_PAKE_STATE_READY;
7774 computation_stage->output_step++;
7775 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7776 } else {
7777 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007778 }
7779
7780 return PSA_SUCCESS;
7781}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007782#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007783
Neil Armstronga7d08c32022-06-01 18:21:20 +02007784psa_status_t psa_pake_output(
7785 psa_pake_operation_t *operation,
7786 psa_pake_step_t step,
7787 uint8_t *output,
7788 size_t output_size,
7789 size_t *output_length)
7790{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007791 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007792 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007793 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007794
7795 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007796 status = psa_pake_complete_inputs(operation);
7797 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007798 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007799 }
7800 }
7801
7802 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007803 status = PSA_ERROR_BAD_STATE;
7804 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007805 }
7806
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007807 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007808 status = PSA_ERROR_INVALID_ARGUMENT;
7809 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007810 }
7811
Przemek Stekiele12ed362022-12-21 12:54:46 +01007812 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007813#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007814 case PSA_ALG_JPAKE:
7815 status = psa_jpake_output_prologue(operation, step);
7816 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007817 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007818 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007819 driver_step = convert_jpake_computation_stage_to_driver_step(
7820 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007821 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007822#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007823 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007824 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007825 status = PSA_ERROR_NOT_SUPPORTED;
7826 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007827 }
7828
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007829 status = psa_driver_wrapper_pake_output(operation, driver_step,
7830 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007831
7832 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007833 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007834 }
7835
7836 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007837#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007838 case PSA_ALG_JPAKE:
7839 status = psa_jpake_output_epilogue(operation);
7840 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007841 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007842 }
7843 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007844#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007845 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007846 status = PSA_ERROR_NOT_SUPPORTED;
7847 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007848 }
7849
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007850 return PSA_SUCCESS;
7851exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007852 psa_pake_abort(operation);
7853 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007854}
7855
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007856#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007857static psa_status_t psa_jpake_input_prologue(
7858 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007859 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007860{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007861 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7862 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7863 step != PSA_PAKE_STEP_ZK_PROOF) {
7864 return PSA_ERROR_INVALID_ARGUMENT;
7865 }
7866
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007867 psa_jpake_computation_stage_t *computation_stage =
7868 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007869
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007870 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7871 return PSA_ERROR_BAD_STATE;
7872 }
7873
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007874 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7875 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7876 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7877 return PSA_ERROR_BAD_STATE;
7878 }
7879
7880 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7881 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007882 return PSA_ERROR_BAD_STATE;
7883 }
7884
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007885 switch (computation_stage->input_step) {
7886 case PSA_PAKE_STEP_X1_X2:
7887 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007888 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007889 case PSA_PAKE_STEP_X2S:
7890 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007891 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007892 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007893 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007894 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007895
7896 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7897 }
7898
7899 /* Check if step matches current sequence */
7900 switch (computation_stage->sequence) {
7901 case PSA_PAKE_X1_STEP_KEY_SHARE:
7902 case PSA_PAKE_X2_STEP_KEY_SHARE:
7903 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7904 return PSA_ERROR_BAD_STATE;
7905 }
7906 break;
7907
7908 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7909 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7910 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7911 return PSA_ERROR_BAD_STATE;
7912 }
7913 break;
7914
7915 case PSA_PAKE_X1_STEP_ZK_PROOF:
7916 case PSA_PAKE_X2_STEP_ZK_PROOF:
7917 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7918 return PSA_ERROR_BAD_STATE;
7919 }
7920 break;
7921
7922 default:
7923 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007924 }
7925
7926 return PSA_SUCCESS;
7927}
7928
Przemek Stekiele12ed362022-12-21 12:54:46 +01007929static psa_status_t psa_jpake_input_epilogue(
7930 psa_pake_operation_t *operation)
7931{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007932 psa_jpake_computation_stage_t *computation_stage =
7933 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007934
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007935 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007936 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007937 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007938 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007939 computation_stage->state = PSA_PAKE_STATE_READY;
7940 computation_stage->input_step++;
7941 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7942 } else {
7943 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007944 }
7945
7946 return PSA_SUCCESS;
7947}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007948#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007949
Neil Armstronga7d08c32022-06-01 18:21:20 +02007950psa_status_t psa_pake_input(
7951 psa_pake_operation_t *operation,
7952 psa_pake_step_t step,
7953 const uint8_t *input,
7954 size_t input_length)
7955{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007956 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007957 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007958 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
7959 operation->primitive,
7960 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007961
7962 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007963 status = psa_pake_complete_inputs(operation);
7964 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007965 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007966 }
7967 }
7968
7969 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007970 status = PSA_ERROR_BAD_STATE;
7971 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007972 }
7973
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007974 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007975 status = PSA_ERROR_INVALID_ARGUMENT;
7976 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007977 }
7978
Przemek Stekiele12ed362022-12-21 12:54:46 +01007979 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007980#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007981 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007982 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007983 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007984 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007985 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007986 driver_step = convert_jpake_computation_stage_to_driver_step(
7987 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007988 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007989#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007990 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007991 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007992 status = PSA_ERROR_NOT_SUPPORTED;
7993 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007994 }
7995
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007996 status = psa_driver_wrapper_pake_input(operation, driver_step,
7997 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007998
7999 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008000 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008001 }
8002
8003 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008004#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008005 case PSA_ALG_JPAKE:
8006 status = psa_jpake_input_epilogue(operation);
8007 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008008 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008009 }
8010 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008011#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008012 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008013 status = PSA_ERROR_NOT_SUPPORTED;
8014 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008015 }
8016
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008017 return PSA_SUCCESS;
8018exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008019 psa_pake_abort(operation);
8020 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008021}
8022
8023psa_status_t psa_pake_get_implicit_key(
8024 psa_pake_operation_t *operation,
8025 psa_key_derivation_operation_t *output)
8026{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008027 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008028 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008029 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008030 size_t shared_key_len = 0;
8031
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008032 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008033 status = PSA_ERROR_BAD_STATE;
8034 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008035 }
8036
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008037#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008038 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008039 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008040 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008041 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8042 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008043 status = PSA_ERROR_BAD_STATE;
8044 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008045 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008046 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008047#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008048 {
8049 status = PSA_ERROR_NOT_SUPPORTED;
8050 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008051 }
8052
Przemek Stekiel0c781802022-11-29 14:53:13 +01008053 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8054 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008055 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008056 &shared_key_len);
8057
8058 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008059 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008060 }
8061
8062 status = psa_key_derivation_input_bytes(output,
8063 PSA_KEY_DERIVATION_INPUT_SECRET,
8064 shared_key,
8065 shared_key_len);
8066
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008067 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008068exit:
8069 abort_status = psa_pake_abort(operation);
8070 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008071}
8072
8073psa_status_t psa_pake_abort(
8074 psa_pake_operation_t *operation)
8075{
Przemek Stekield69dca92023-01-31 19:59:20 +01008076 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008077
Przemek Stekield69dca92023-01-31 19:59:20 +01008078 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008079 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008080 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008081
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008082 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8083 if (operation->data.inputs.password != NULL) {
8084 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008085 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008086 mbedtls_free(operation->data.inputs.password);
8087 }
8088 if (operation->data.inputs.user != NULL) {
8089 mbedtls_free(operation->data.inputs.user);
8090 }
8091 if (operation->data.inputs.peer != NULL) {
8092 mbedtls_free(operation->data.inputs.peer);
8093 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008094 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008095 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008096
Przemek Stekield69dca92023-01-31 19:59:20 +01008097 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008098}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008099
Gilles Peskinee59236f2018-01-27 23:32:46 +01008100#endif /* MBEDTLS_PSA_CRYPTO_C */