blob: acb39a1bcffe7cf06206f93db3f3c135f7a98471 [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 }
643
644 /* Copy the key material. */
645 memcpy(key_buffer, data, data_length);
646 *key_buffer_length = data_length;
647 *bits = PSA_BYTES_TO_BITS(data_length);
648 (void) key_buffer_size;
649
650 return PSA_SUCCESS;
651 }
652#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
653 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
John Durkop9814fa22020-11-04 12:28:15 -0800654#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
656 if (PSA_KEY_TYPE_IS_ECC(type)) {
657 return mbedtls_psa_ecp_import_key(attributes,
658 data, data_length,
659 key_buffer, key_buffer_size,
660 key_buffer_length,
661 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100662 }
John Durkop9814fa22020-11-04 12:28:15 -0800663#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
664 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700665#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
667 if (PSA_KEY_TYPE_IS_RSA(type)) {
668 return mbedtls_psa_rsa_import_key(attributes,
669 data, data_length,
670 key_buffer, key_buffer_size,
671 key_buffer_length,
672 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200673 }
John Durkop9814fa22020-11-04 12:28:15 -0800674#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
675 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100676 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100679}
680
Gilles Peskinef603c712019-01-19 13:40:11 +0100681/** Calculate the intersection of two algorithm usage policies.
682 *
683 * Return 0 (which allows no operation) on incompatibility.
684 */
685static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100686 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100687 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100689{
Gilles Peskine549ea862019-05-22 11:45:59 +0200690 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (alg1 == alg2) {
692 return alg1;
693 }
Steven Cooreman03488022021-02-18 12:07:20 +0100694 /* If the policies are from the same hash-and-sign family, check
695 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
697 PSA_ALG_IS_SIGN_HASH(alg2) &&
698 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
699 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
700 return alg2;
701 }
702 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
703 return alg1;
704 }
Steven Cooreman03488022021-02-18 12:07:20 +0100705 }
706 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100707 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100708 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
710 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
711 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
712 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
713 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100714 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100715
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100716 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
718 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
719 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
720 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100721 }
722 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
724 (alg1_len <= alg2_len)) {
725 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100726 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
728 (alg2_len <= alg1_len)) {
729 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100730 }
731 }
732 /* If the policies are from the same MAC family, check whether one
733 * of them is a minimum-MAC-length policy. Calculate the most
734 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
736 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
737 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100738 /* Validate the combination of key type and algorithm. Since the base
739 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
741 return 0;
742 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100743
Steven Cooreman31a876d2021-03-03 20:47:40 +0100744 /* Get the (exact or at-least) output lengths for both sides of the
745 * requested intersection. None of the currently supported algorithms
746 * have an output length dependent on the actual key size, so setting it
747 * to a bogus value of 0 is currently OK.
748 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100749 * Note that for at-least-this-length wildcard algorithms, the output
750 * length is set to the shortest allowed length, which allows us to
751 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
753 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100754 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100755
Steven Cooremana1d83222021-02-25 10:20:29 +0100756 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
758 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
759 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100760 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100761
762 /* If only one is an at-least-this-length policy, the intersection would
763 * be the other (fixed-length) policy as long as said fixed length is
764 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
766 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100767 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
769 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100770 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100771
Steven Cooremancd640932021-03-08 12:00:27 +0100772 /* If none of them are wildcards, check whether they define the same tag
773 * length. This is still possible here when one is default-length and
774 * the other specific-length. Ensure to always return the
775 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (alg1_len == alg2_len) {
777 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
778 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100779 }
780 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100782}
783
Gilles Peskine449bd832023-01-11 14:50:10 +0100784static int psa_key_algorithm_permits(psa_key_type_t key_type,
785 psa_algorithm_t policy_alg,
786 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200787{
Gilles Peskine549ea862019-05-22 11:45:59 +0200788 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if (requested_alg == policy_alg) {
790 return 1;
791 }
Steven Cooreman03488022021-02-18 12:07:20 +0100792 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
793 * and requested_alg is the same hash-and-sign family with any hash,
794 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
796 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
797 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
798 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100799 }
800 /* If policy_alg is a wildcard AEAD algorithm of the same base as
801 * the requested algorithm, check the requested tag length to be
802 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (PSA_ALG_IS_AEAD(policy_alg) &&
804 PSA_ALG_IS_AEAD(requested_alg) &&
805 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
806 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
807 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
808 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
809 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100810 }
Steven Cooremancd640932021-03-08 12:00:27 +0100811 /* If policy_alg is a MAC algorithm of the same base as the requested
812 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (PSA_ALG_IS_MAC(policy_alg) &&
814 PSA_ALG_IS_MAC(requested_alg) &&
815 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
816 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100817 /* Validate the combination of key type and algorithm. Since the policy
818 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
820 return 0;
821 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100822
Steven Cooreman31a876d2021-03-03 20:47:40 +0100823 /* Get both the requested output length for the algorithm which is to be
824 * verified, and the default output length for the base algorithm.
825 * Note that none of the currently supported algorithms have an output
826 * length dependent on actual key size, so setting it to a bogus value
827 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100828 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100830 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 key_type, 0,
832 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100833
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100834 /* If the policy is default-length, only allow an algorithm with
835 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
837 return requested_output_length == default_output_length;
838 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100839
840 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100841 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
843 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
844 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100845 }
846
Steven Cooremancd640932021-03-08 12:00:27 +0100847 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
848 * check for the requested MAC length to be equal to or longer than the
849 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
851 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
852 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100853 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200854 }
Steven Cooremance48e852020-10-05 16:02:45 +0200855 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200856 * a key derivation with that key agreement should also be allowed. This
857 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
859 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
860 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
861 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200862 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100863 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200865}
866
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100867/** Test whether a policy permits an algorithm.
868 *
869 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100870 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100871 * \note This function requires providing the key type for which the policy is
872 * being validated, since some algorithm policy definitions (e.g. MAC)
873 * have different properties depending on what kind of cipher it is
874 * combined with.
875 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100876 * \retval PSA_SUCCESS When \p alg is a specific algorithm
877 * allowed by the \p policy.
878 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
879 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
880 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100881 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
883 psa_key_type_t key_type,
884 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100885{
Steven Cooremand788fab2021-02-25 11:29:17 +0100886 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (alg == 0) {
888 return PSA_ERROR_INVALID_ARGUMENT;
889 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100890
Steven Cooreman7e39f052021-02-23 14:18:32 +0100891 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if (PSA_ALG_IS_WILDCARD(alg)) {
893 return PSA_ERROR_INVALID_ARGUMENT;
894 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
897 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
898 return PSA_SUCCESS;
899 } else {
900 return PSA_ERROR_NOT_PERMITTED;
901 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100902}
903
Gilles Peskinef603c712019-01-19 13:40:11 +0100904/** Restrict a key policy based on a constraint.
905 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100906 * \note This function requires providing the key type for which the policy is
907 * being restricted, since some algorithm policy definitions (e.g. MAC)
908 * have different properties depending on what kind of cipher it is
909 * combined with.
910 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100911 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100912 * \param[in,out] policy The policy to restrict.
913 * \param[in] constraint The policy constraint to apply.
914 *
915 * \retval #PSA_SUCCESS
916 * \c *policy contains the intersection of the original value of
917 * \c *policy and \c *constraint.
918 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100919 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100920 * \c *policy is unchanged.
921 */
922static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100923 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100924 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100926{
927 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 psa_key_policy_algorithm_intersection(key_type, policy->alg,
929 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200930 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
932 constraint->alg2);
933 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
934 return PSA_ERROR_INVALID_ARGUMENT;
935 }
936 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
937 return PSA_ERROR_INVALID_ARGUMENT;
938 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100939 policy->usage &= constraint->usage;
940 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200941 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100943}
944
Przemek Stekiel061f6942022-11-30 10:51:35 +0100945/** Get the description of a key given its identifier and policy constraints
946 * and lock it.
947 *
948 * The key must have allow all the usage flags set in \p usage. If \p alg is
949 * nonzero, the key must allow operations with this algorithm. If \p alg is
950 * zero, the algorithm is not checked.
951 *
952 * In case of a persistent key, the function loads the description of the key
953 * into a key slot if not already done.
954 *
955 * On success, the returned key slot is locked. It is the responsibility of
956 * the caller to unlock the key slot when it does not access it anymore.
957 */
958static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100959 mbedtls_svc_key_id_t key,
960 psa_key_slot_t **p_slot,
961 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100963{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100965 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 status = psa_get_and_lock_key_slot(key, p_slot);
968 if (status != PSA_SUCCESS) {
969 return status;
970 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200971 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100972
973 /* Enforce that usage policy for the key slot contains all the flags
974 * required by the usage parameter. There is one exception: public
975 * keys can always be exported, so we treat public key objects as
976 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100978 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100982 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200983 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100984 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100985
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800986 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if (alg != 0) {
988 status = psa_key_policy_permits(&slot->attr.policy,
989 slot->attr.type,
990 alg);
991 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100992 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100994 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200997
998error:
999 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001003}
Darryl Green940d72c2018-07-13 13:18:51 +01001004
Ronald Cron5c522922020-11-14 16:35:34 +01001005/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001006 *
1007 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001008 * available, as opposed to a key in a secure element and/or to be used
1009 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001010 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001011 * This is a temporary function that may be used instead of
1012 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1013 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001014 *
Ronald Cron5c522922020-11-14 16:35:34 +01001015 * On success, the returned key slot is locked. It is the responsibility of the
1016 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001017 */
Ronald Cron5c522922020-11-14 16:35:34 +01001018static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1019 mbedtls_svc_key_id_t key,
1020 psa_key_slot_t **p_slot,
1021 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001023{
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1025 usage, alg);
1026 if (status != PSA_SUCCESS) {
1027 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001028 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1031 psa_unlock_key_slot(*p_slot);
1032 *p_slot = NULL;
1033 return PSA_ERROR_NOT_SUPPORTED;
1034 }
1035
1036 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001037}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001040{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001041 /* Data pointer will always be either a valid pointer or NULL in an
1042 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if (slot->key.data != NULL) {
1044 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1045 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001048 slot->key.data = NULL;
1049 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001052}
1053
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001054/** Completely wipe a slot in memory, including its policy.
1055 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001056psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001057{
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 /*
1061 * As the return error code may not be handled in case of multiple errors,
1062 * do our best to report an unexpected lock counter. Assert with
1063 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1064 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1065 * function is called as part of the execution of a test suite, the
1066 * execution of the test suite is stopped in error if the assertion fails.
1067 */
1068 if (slot->lock_count != 1) {
1069 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001070 status = PSA_ERROR_CORRUPTION_DETECTED;
1071 }
1072
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001073 /* Multipart operations may still be using the key. This is safe
1074 * because all multipart operation objects are independent from
1075 * the key slot: if they need to access the key after the setup
1076 * phase, they have a copy of the key. Note that this means that
1077 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001078 /* At this point, key material and other type-specific content has
1079 * been wiped. Clear remaining metadata. We can call memset and not
1080 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 memset(slot, 0, sizeof(*slot));
1082 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001083}
1084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001086{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001087 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001088 psa_status_t status; /* status of the last operation */
1089 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001090#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1091 psa_se_drv_table_entry_t *driver;
1092#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if (mbedtls_svc_key_id_is_null(key)) {
1095 return PSA_SUCCESS;
1096 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001097
Ronald Cronf2911112020-10-29 17:51:10 +01001098 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001099 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001100 * key, this will load the key description from persistent memory if not
1101 * done yet. We cannot avoid this loading as without it we don't know if
1102 * the key is operated by an SE or not and this information is needed by
1103 * the current implementation.
1104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 status = psa_get_and_lock_key_slot(key, &slot);
1106 if (status != PSA_SUCCESS) {
1107 return status;
1108 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001109
Ronald Cronf2911112020-10-29 17:51:10 +01001110 /*
1111 * If the key slot containing the key description is under access by the
1112 * library (apart from the present access), the key cannot be destroyed
1113 * yet. For the time being, just return in error. Eventually (to be
1114 * implemented), the key should be destroyed when all accesses have
1115 * stopped.
1116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if (slot->lock_count > 1) {
1118 psa_unlock_key_slot(slot);
1119 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001120 }
1121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001123 /* Refuse the destruction of a read-only key (which may or may not work
1124 * if we attempt it, depending on whether the key is merely read-only
1125 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001126 * Just do the best we can, which is to wipe the copy in memory
1127 * (done in this function's cleanup code). */
1128 overall_status = PSA_ERROR_NOT_PERMITTED;
1129 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001130 }
1131
Gilles Peskine354f7672019-07-12 23:46:38 +02001132#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1134 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001135 /* For a key in a secure element, we need to do three things:
1136 * remove the key file in internal storage, destroy the
1137 * key inside the secure element, and update the driver's
1138 * persistent data. Start a transaction that will encompass these
1139 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001141 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001143 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 status = psa_crypto_save_transaction();
1145 if (status != PSA_SUCCESS) {
1146 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001147 /* We should still try to destroy the key in the secure
1148 * element and the key metadata in storage. This is especially
1149 * important if the error is that the storage is full.
1150 * But how to do it exactly without risking an inconsistent
1151 * state after a reset?
1152 * https://github.com/ARMmbed/mbed-crypto/issues/215
1153 */
1154 overall_status = status;
1155 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001156 }
1157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 status = psa_destroy_se_key(driver,
1159 psa_key_slot_get_slot_number(slot));
1160 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001161 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001163 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001164#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1165
Darryl Greend49a4992018-06-18 17:27:26 +01001166#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1168 status = psa_destroy_persistent_key(slot->attr.id);
1169 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001170 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001172
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001173 /* TODO: other slots may have a copy of the same key. We should
1174 * invalidate them.
1175 * https://github.com/ARMmbed/mbed-crypto/issues/214
1176 */
Darryl Greend49a4992018-06-18 17:27:26 +01001177 }
1178#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001179
Gilles Peskinefc762652019-07-22 19:30:34 +02001180#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if (driver != NULL) {
1182 status = psa_save_se_persistent_data(driver);
1183 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001184 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 }
1186 status = psa_crypto_stop_transaction();
1187 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001188 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001190 }
1191#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1192
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001195 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001197 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 }
1199 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001200}
1201
John Durkop07cc04a2020-11-16 22:08:34 -08001202#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001203 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001204static psa_status_t psa_get_rsa_public_exponent(
1205 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001207{
1208 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001209 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001210 uint8_t *buffer = NULL;
1211 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1215 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001216 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 }
1218 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001219 /* It's the default value, which is reported as an empty string,
1220 * so there's nothing to do. */
1221 goto exit;
1222 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 buflen = mbedtls_mpi_size(&mpi);
1225 buffer = mbedtls_calloc(1, buflen);
1226 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001227 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1228 goto exit;
1229 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1231 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001232 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001234 attributes->domain_parameters = buffer;
1235 attributes->domain_parameters_size = buflen;
1236
1237exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 mbedtls_mpi_free(&mpi);
1239 if (ret != 0) {
1240 mbedtls_free(buffer);
1241 }
1242 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001243}
John Durkop07cc04a2020-11-16 22:08:34 -08001244#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001245 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001246
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001247/** Retrieve all the publicly-accessible attributes of a key.
1248 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001249psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1250 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001251{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001253 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001254 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1259 if (status != PSA_SUCCESS) {
1260 return status;
1261 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001262
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001263 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1265 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001266
1267#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1269 psa_set_key_slot_number(attributes,
1270 psa_key_slot_get_slot_number(slot));
1271 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001272#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001275#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001276 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001277 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001278 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001279 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001280 * is not yet implemented.
1281 * https://github.com/ARMmbed/mbed-crypto/issues/216
1282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001284 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001285
Ronald Cron90857082020-11-25 14:58:33 +01001286 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 slot->attr.type,
1288 slot->key.data,
1289 slot->key.bytes,
1290 &rsa);
1291 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001292 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 status = psa_get_rsa_public_exponent(rsa,
1296 attributes);
1297 mbedtls_rsa_free(rsa);
1298 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001299 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001300 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001301#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001302 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001303 default:
1304 /* Nothing else to do. */
1305 break;
1306 }
1307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (status != PSA_SUCCESS) {
1309 psa_reset_key_attributes(attributes);
1310 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315}
1316
Gilles Peskinec8000c02019-08-02 20:15:51 +02001317#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1318psa_status_t psa_get_key_slot_number(
1319 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001321{
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001323 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 return PSA_SUCCESS;
1325 } else {
1326 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001327 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001328}
1329#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1332 size_t key_buffer_size,
1333 uint8_t *data,
1334 size_t data_size,
1335 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001336{
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (key_buffer_size > data_size) {
1338 return PSA_ERROR_BUFFER_TOO_SMALL;
1339 }
1340 memcpy(data, key_buffer, key_buffer_size);
1341 memset(data + key_buffer_size, 0,
1342 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001343 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001345}
1346
Ronald Cron7285cda2020-11-26 14:46:37 +01001347psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001348 const psa_key_attributes_t *attributes,
1349 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001351{
Ronald Crond18b5f82020-11-25 16:46:05 +01001352 psa_key_type_t type = attributes->core.type;
1353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 if (key_type_is_raw_bytes(type) ||
1355 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001356 PSA_KEY_TYPE_IS_ECC(type) ||
1357 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 return psa_export_key_buffer_internal(
1359 key_buffer, key_buffer_size,
1360 data, data_size, data_length);
1361 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001362 /* This shouldn't happen in the reference implementation, but
1363 it is valid for a special-purpose implementation to omit
1364 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001366 }
1367}
1368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1370 uint8_t *data,
1371 size_t data_size,
1372 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001373{
1374 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1375 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1376 psa_key_slot_t *slot;
1377
Ronald Crone907e552021-01-18 13:22:38 +01001378 /* Reject a zero-length output buffer now, since this can never be a
1379 * valid key representation. This way we know that data must be a valid
1380 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if (data_size == 0) {
1382 return PSA_ERROR_BUFFER_TOO_SMALL;
1383 }
Ronald Crone907e552021-01-18 13:22:38 +01001384
Ronald Cron9486f9d2020-11-26 13:36:23 +01001385 /* Set the key to empty now, so that even when there are errors, we always
1386 * set data_length to a value between 0 and data_size. On error, setting
1387 * the key to empty is a good choice because an empty key representation is
1388 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001389 *data_length = 0;
1390
Ronald Cron9486f9d2020-11-26 13:36:23 +01001391 /* Export requires the EXPORT flag. There is an exception for public keys,
1392 * which don't require any flag, but
1393 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1396 PSA_KEY_USAGE_EXPORT, 0);
1397 if (status != PSA_SUCCESS) {
1398 return status;
1399 }
mohammad160306e79202018-03-28 13:17:44 +03001400
Ronald Crond18b5f82020-11-25 16:46:05 +01001401 psa_key_attributes_t attributes = {
1402 .core = slot->attr
1403 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 status = psa_driver_wrapper_export_key(&attributes,
1405 slot->key.data, slot->key.bytes,
1406 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001411}
1412
Ronald Cron7285cda2020-11-26 14:46:37 +01001413psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001414 const psa_key_attributes_t *attributes,
1415 const uint8_t *key_buffer,
1416 size_t key_buffer_size,
1417 uint8_t *data,
1418 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001420{
Ronald Crond18b5f82020-11-25 16:46:05 +01001421 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001422
Przemek Stekiel055ffed2023-05-30 12:51:49 +02001423 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
1424 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1425 PSA_KEY_TYPE_IS_DH(type))) {
1426 /* Exporting public -> public */
1427 return psa_export_key_buffer_internal(
1428 key_buffer, key_buffer_size,
1429 data, data_size, data_length);
1430 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001431#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001432 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1433 return mbedtls_psa_rsa_export_public_key(attributes,
1434 key_buffer,
1435 key_buffer_size,
1436 data,
1437 data_size,
1438 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001439#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001440 /* We don't know how to convert a private RSA key to public. */
1441 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001442#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1443 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001444 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001445#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001446 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1447 return mbedtls_psa_ecp_export_public_key(attributes,
1448 key_buffer,
1449 key_buffer_size,
1450 data,
1451 data_size,
1452 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001453#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001454 /* We don't know how to convert a private ECC key to public */
1455 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001456#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1457 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001458 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001459#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
1460 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001461 return mbedtls_psa_export_ffdh_public_key(attributes,
1462 key_buffer,
1463 key_buffer_size,
1464 data, data_size,
1465 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001466#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001467 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001468#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
1469 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001471 (void) key_buffer;
1472 (void) key_buffer_size;
1473 (void) data;
1474 (void) data_size;
1475 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001477 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001478}
Ronald Crond18b5f82020-11-25 16:46:05 +01001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1481 uint8_t *data,
1482 size_t data_size,
1483 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001484{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001485 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001486 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001487 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001488
Ronald Crone907e552021-01-18 13:22:38 +01001489 /* Reject a zero-length output buffer now, since this can never be a
1490 * valid key representation. This way we know that data must be a valid
1491 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if (data_size == 0) {
1493 return PSA_ERROR_BUFFER_TOO_SMALL;
1494 }
Ronald Crone907e552021-01-18 13:22:38 +01001495
Darryl Greendd8fb772018-11-07 16:00:44 +00001496 /* Set the key to empty now, so that even when there are errors, we always
1497 * set data_length to a value between 0 and data_size. On error, setting
1498 * the key to empty is a good choice because an empty key representation is
1499 * unlikely to be accepted anywhere. */
1500 *data_length = 0;
1501
1502 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1504 if (status != PSA_SUCCESS) {
1505 return status;
1506 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1509 status = PSA_ERROR_INVALID_ARGUMENT;
1510 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001511 }
1512
Ronald Crond18b5f82020-11-25 16:46:05 +01001513 psa_key_attributes_t attributes = {
1514 .core = slot->attr
1515 };
Przemek Stekiel055ffed2023-05-30 12:51:49 +02001516 status = psa_driver_wrapper_export_public_key(
1517 &attributes, slot->key.data, slot->key.bytes,
1518 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001519
1520exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001524}
1525
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001526MBEDTLS_STATIC_ASSERT(
1527 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1528 "One or more key attribute flag is listed as both external-only and dual-use")
1529MBEDTLS_STATIC_ASSERT(
1530 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1531 "One or more key attribute flag is listed as both internal-only and dual-use")
1532MBEDTLS_STATIC_ASSERT(
1533 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1534 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001535
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001536/** Validate that a key policy is internally well-formed.
1537 *
1538 * This function only rejects invalid policies. It does not validate the
1539 * consistency of the policy with respect to other attributes of the key
1540 * such as the key type.
1541 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001542static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001543{
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1545 PSA_KEY_USAGE_COPY |
1546 PSA_KEY_USAGE_ENCRYPT |
1547 PSA_KEY_USAGE_DECRYPT |
1548 PSA_KEY_USAGE_SIGN_MESSAGE |
1549 PSA_KEY_USAGE_VERIFY_MESSAGE |
1550 PSA_KEY_USAGE_SIGN_HASH |
1551 PSA_KEY_USAGE_VERIFY_HASH |
1552 PSA_KEY_USAGE_VERIFY_DERIVATION |
1553 PSA_KEY_USAGE_DERIVE)) != 0) {
1554 return PSA_ERROR_INVALID_ARGUMENT;
1555 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001558}
1559
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001560/** Validate the internal consistency of key attributes.
1561 *
1562 * This function only rejects invalid attribute values. If does not
1563 * validate the consistency of the attributes with any key data that may
1564 * be involved in the creation of the key.
1565 *
1566 * Call this function early in the key creation process.
1567 *
1568 * \param[in] attributes Key attributes for the new key.
1569 * \param[out] p_drv On any return, the driver for the key, if any.
1570 * NULL for a transparent key.
1571 *
1572 */
1573static psa_status_t psa_validate_key_attributes(
1574 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001576{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001577 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1579 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 status = psa_validate_key_location(lifetime, p_drv);
1582 if (status != PSA_SUCCESS) {
1583 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001584 }
1585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 status = psa_validate_key_persistence(lifetime);
1587 if (status != PSA_SUCCESS) {
1588 return status;
1589 }
1590
1591 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1592 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1593 return PSA_ERROR_INVALID_ARGUMENT;
1594 }
1595 } else {
1596 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1597 return PSA_ERROR_INVALID_ARGUMENT;
1598 }
1599 }
1600
1601 status = psa_validate_key_policy(&attributes->core.policy);
1602 if (status != PSA_SUCCESS) {
1603 return status;
1604 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001605
1606 /* Refuse to create overly large keys.
1607 * Note that this doesn't trigger on import if the attributes don't
1608 * explicitly specify a size (so psa_get_key_bits returns 0), so
1609 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1611 return PSA_ERROR_NOT_SUPPORTED;
1612 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001613
Gilles Peskine91e8c332019-08-02 19:19:39 +02001614 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1616 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1617 return PSA_ERROR_INVALID_ARGUMENT;
1618 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001621}
1622
Gilles Peskine4747d192019-04-17 15:05:45 +02001623/** Prepare a key slot to receive key material.
1624 *
1625 * This function allocates a key slot and sets its metadata.
1626 *
1627 * If this function fails, call psa_fail_key_creation().
1628 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001629 * This function is intended to be used as follows:
1630 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001631 * it with the specified attributes, and in case of a volatile key assign it
1632 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001633 * -# Populate the slot with the key material.
1634 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1635 * In case of failure at any step, stop the sequence and call
1636 * psa_fail_key_creation().
1637 *
Ronald Cron5c522922020-11-14 16:35:34 +01001638 * On success, the key slot is locked. It is the responsibility of the caller
1639 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001640 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001641 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001642 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001643 * \param[out] p_slot On success, a pointer to the prepared slot.
1644 * \param[out] p_drv On any return, the driver for the key, if any.
1645 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001646 *
1647 * \retval #PSA_SUCCESS
1648 * The key slot is ready to receive key material.
1649 * \return If this function fails, the key slot is an invalid state.
1650 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001651 */
1652static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001653 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001654 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001655 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001657{
1658 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001659 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001660 psa_key_slot_t *slot;
1661
Gilles Peskinedf179142019-07-15 22:02:14 +02001662 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001663 *p_drv = NULL;
1664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 status = psa_validate_key_attributes(attributes, p_drv);
1666 if (status != PSA_SUCCESS) {
1667 return status;
1668 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1671 if (status != PSA_SUCCESS) {
1672 return status;
1673 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001674 slot = *p_slot;
1675
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001676 /* We're storing the declared bit-size of the key. It's up to each
1677 * creation mechanism to verify that this information is correct.
1678 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001679 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001680 * is optional (import, copy). In case of a volatile key, assign it the
1681 * volatile key identifier associated to the slot returned to contain its
1682 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001683
1684 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001686#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1687 slot->attr.id = volatile_key_id;
1688#else
1689 slot->attr.id.key_id = volatile_key_id;
1690#endif
1691 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001692
Gilles Peskine91e8c332019-08-02 19:19:39 +02001693 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001694 * external-only flags, query `attributes`. Thanks to the check
1695 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001696 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001697 * may have set. */
1698 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001699
Gilles Peskinecbaff462019-07-12 23:46:04 +02001700#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001701 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001702 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001703 * create the key file in internal storage, create the
1704 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001705 * persistent data. This is done by starting a transaction that will
1706 * encompass these three actions.
1707 * For registering a volatile key, we just need to find an appropriate
1708 * slot number inside the SE. Since the key is designated volatile, creating
1709 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001710 /* The first thing to do is to find a slot number for the new key.
1711 * We save the slot number in persistent storage as part of the
1712 * transaction data. It will be needed to recover if the power
1713 * fails during the key creation process, to clean up on the secure
1714 * element side after restarting. Obtaining a slot number from the
1715 * secure element driver updates its persistent state, but we do not yet
1716 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001717 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001719 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1721 &slot_number);
1722 if (status != PSA_SUCCESS) {
1723 return status;
1724 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1727 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001728 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001729 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001730 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 status = psa_crypto_save_transaction();
1732 if (status != PSA_SUCCESS) {
1733 (void) psa_crypto_stop_transaction();
1734 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001735 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001736 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001737
1738 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001740 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001743 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001745 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001746#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001749}
Gilles Peskine4747d192019-04-17 15:05:45 +02001750
1751/** Finalize the creation of a key once its key material has been set.
1752 *
1753 * This entails writing the key to persistent storage.
1754 *
1755 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001756 * See the documentation of psa_start_key_creation() for the intended use
1757 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 *
Ronald Cron5c522922020-11-14 16:35:34 +01001759 * If the finalization succeeds, the function unlocks the key slot (it was
1760 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1761 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001762 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001763 * \param[in,out] slot Pointer to the slot with key material.
1764 * \param[in] driver The secure element driver for the key,
1765 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001766 * \param[out] key On success, identifier of the key. Note that the
1767 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001768 *
1769 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001770 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001771 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1772 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1773 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1774 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1775 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1776 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001777 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001778 * \return If this function fails, the key slot is an invalid state.
1779 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001780 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001781static psa_status_t psa_finish_key_creation(
1782 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001783 psa_se_drv_table_entry_t *driver,
1784 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001785{
1786 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001787 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001788 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001789
1790#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001792#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001794 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001795 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001797
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001798 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1799 sizeof(data.slot_number),
1800 "Slot number size does not match psa_se_key_data_storage_t");
1801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1803 status = psa_save_persistent_key(&slot->attr,
1804 (uint8_t *) &data,
1805 sizeof(data));
1806 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001807#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1808 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001809 /* Key material is saved in export representation in the slot, so
1810 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 status = psa_save_persistent_key(&slot->attr,
1812 slot->key.data,
1813 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001814 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001815 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001816#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1817
Gilles Peskinecbaff462019-07-12 23:46:04 +02001818#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001819 /* Finish the transaction for a key creation. This does not
1820 * happen when registering an existing key. Detect this case
1821 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001822 * creation of a persistent key in a secure element requires a transaction,
1823 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 if (driver != NULL &&
1825 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1826 status = psa_save_se_persistent_data(driver);
1827 if (status != PSA_SUCCESS) {
1828 psa_destroy_persistent_key(slot->attr.id);
1829 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001830 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001832 }
1833#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001836 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 status = psa_unlock_key_slot(slot);
1838 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001839 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001841 }
Ronald Cron50972942020-11-14 11:28:25 +01001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001844}
1845
1846/** Abort the creation of a key.
1847 *
1848 * You may call this function after calling psa_start_key_creation(),
1849 * or after psa_finish_key_creation() fails. In other circumstances, this
1850 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001851 * See the documentation of psa_start_key_creation() for the intended use
1852 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001853 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001854 * \param[in,out] slot Pointer to the slot with key material.
1855 * \param[in] driver The secure element driver for the key,
1856 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001857 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001858static void psa_fail_key_creation(psa_key_slot_t *slot,
1859 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001860{
Gilles Peskine011e4282019-06-26 18:34:38 +02001861 (void) driver;
1862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001864 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001866
1867#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001868 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001869 * element, and the failure happened later (when saving metadata
1870 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001871 * element.
1872 * https://github.com/ARMmbed/mbed-crypto/issues/217
1873 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001874
Gilles Peskined7729582019-08-05 15:55:54 +02001875 /* Abort the ongoing transaction if any (there may not be one if
1876 * the creation process failed before starting one, or if the
1877 * key creation is a registration of a key in a secure element).
1878 * Earlier functions must already have done what it takes to undo any
1879 * partial creation. All that's left is to update the transaction data
1880 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001882#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001885}
1886
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001887/** Validate optional attributes during key creation.
1888 *
1889 * Some key attributes are optional during key creation. If they are
1890 * specified in the attributes structure, check that they are consistent
1891 * with the data in the slot.
1892 *
1893 * This function should be called near the end of key creation, after
1894 * the slot in memory is fully populated but before saving persistent data.
1895 */
1896static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001897 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899{
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 if (attributes->core.type != 0) {
1901 if (attributes->core.type != slot->attr.type) {
1902 return PSA_ERROR_INVALID_ARGUMENT;
1903 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001904 }
1905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001907#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1909 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001910 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001911 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001913
Ronald Cron90857082020-11-25 14:58:33 +01001914 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 slot->attr.type,
1916 slot->key.data,
1917 slot->key.bytes,
1918 &rsa);
1919 if (status != PSA_SUCCESS) {
1920 return status;
1921 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 mbedtls_mpi_init(&actual);
1924 mbedtls_mpi_init(&required);
1925 ret = mbedtls_rsa_export(rsa,
1926 NULL, NULL, NULL, NULL, &actual);
1927 mbedtls_rsa_free(rsa);
1928 mbedtls_free(rsa);
1929 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001930 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 }
1932 ret = mbedtls_mpi_read_binary(&required,
1933 attributes->domain_parameters,
1934 attributes->domain_parameters_size);
1935 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001936 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 }
1938 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001939 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 }
1941rsa_exit:
1942 mbedtls_mpi_free(&actual);
1943 mbedtls_mpi_free(&required);
1944 if (ret != 0) {
1945 return mbedtls_to_psa_error(ret);
1946 }
1947 } else
John Durkop9814fa22020-11-04 12:28:15 -08001948#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1949 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001950 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001952 }
1953 }
1954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (attributes->core.bits != 0) {
1956 if (attributes->core.bits != slot->attr.bits) {
1957 return PSA_ERROR_INVALID_ARGUMENT;
1958 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001959 }
1960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001962}
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1965 const uint8_t *data,
1966 size_t data_length,
1967 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001968{
1969 psa_status_t status;
1970 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001971 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001972 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301973 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001974
Ronald Cron81709fc2020-11-14 12:10:32 +01001975 *key = MBEDTLS_SVC_KEY_ID_INIT;
1976
Gilles Peskine0f84d622019-09-12 19:03:13 +02001977 /* Reject zero-length symmetric keys (including raw data key objects).
1978 * This also rejects any key which might be encoded as an empty string,
1979 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (data_length == 0) {
1981 return PSA_ERROR_INVALID_ARGUMENT;
1982 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001983
Archana449608b2021-09-08 15:36:05 +05301984 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (data_length > SIZE_MAX / 8) {
1986 return PSA_ERROR_NOT_SUPPORTED;
1987 }
Archana6ed4bda2021-08-04 10:47:15 +05301988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1990 &slot, &driver);
1991 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001992 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001995 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301996 * storage ( thus not in the case of importing a key in a secure element
1997 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1998 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 if (slot->key.data == NULL) {
2000 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302001 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 attributes, data, data_length, &storage_size);
2003 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302004 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 }
Archanad8a83dc2021-06-14 10:04:16 +05302006 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 status = psa_allocate_buffer_to_slot(slot, storage_size);
2008 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002009 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002011 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002012
Przemek Stekiel055ffed2023-05-30 12:51:49 +02002013 bits = slot->attr.bits;
2014 status = psa_driver_wrapper_import_key(attributes,
2015 data, data_length,
2016 slot->key.data,
2017 slot->key.bytes,
2018 &slot->key.bytes, &bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002020 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002024 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002026 status = PSA_ERROR_INVALID_ARGUMENT;
2027 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002028 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002029
Archana6ed4bda2021-08-04 10:47:15 +05302030 /* Enforce a size limit, and in particular ensure that the bit
2031 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302033 status = PSA_ERROR_NOT_SUPPORTED;
2034 goto exit;
2035 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 status = psa_validate_optional_attributes(slot, attributes);
2037 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002038 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002042exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if (status != PSA_SUCCESS) {
2044 psa_fail_key_creation(slot, driver);
2045 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002048}
2049
Gilles Peskined7729582019-08-05 15:55:54 +02002050#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2051psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002053{
2054 psa_status_t status;
2055 psa_key_slot_t *slot = NULL;
2056 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002057 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002058
2059 /* Leaving attributes unspecified is not currently supported.
2060 * It could make sense to query the key type and size from the
2061 * secure element, but not all secure elements support this
2062 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2064 return PSA_ERROR_NOT_SUPPORTED;
2065 }
2066 if (psa_get_key_bits(attributes) == 0) {
2067 return PSA_ERROR_NOT_SUPPORTED;
2068 }
Gilles Peskined7729582019-08-05 15:55:54 +02002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2071 &slot, &driver);
2072 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002073 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 }
Gilles Peskined7729582019-08-05 15:55:54 +02002075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002077
2078exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (status != PSA_SUCCESS) {
2080 psa_fail_key_creation(slot, driver);
2081 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002082
Gilles Peskined7729582019-08-05 15:55:54 +02002083 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 psa_close_key(key);
2085 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002086}
2087#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2090 const psa_key_attributes_t *specified_attributes,
2091 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002092{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002094 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002095 psa_key_slot_t *source_slot = NULL;
2096 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002097 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002098 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302099 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002100
Ronald Cron81709fc2020-11-14 12:10:32 +01002101 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2102
Archana8a180362021-07-05 02:18:48 +05302103 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2105 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 status = psa_validate_optional_attributes(source_slot,
2110 specified_attributes);
2111 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002114
Archana9d17bf42021-09-10 06:22:44 +05302115 /* The target key type and number of bits have been validated by
2116 * psa_validate_optional_attributes() to be either equal to zero or
2117 * equal to the ones of the source key. So it is safe to inherit
2118 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302119 * */
Archana9d17bf42021-09-10 06:22:44 +05302120 actual_attributes.core.bits = source_slot->attr.bits;
2121 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302122
2123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 status = psa_restrict_key_policy(source_slot->attr.type,
2125 &actual_attributes.core.policy,
2126 &source_slot->attr.policy);
2127 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2132 &target_slot, &driver);
2133 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 }
2136 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2137 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302138 /*
Archana9d17bf42021-09-10 06:22:44 +05302139 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302140 * the source key would need to be exported as plaintext and re-imported
2141 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302142 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302143 * appropriate API invocations from the application, if needed.
2144 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002145 status = PSA_ERROR_NOT_SUPPORTED;
2146 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002147 }
Archana8a180362021-07-05 02:18:48 +05302148 /*
2149 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302150 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302151 * - For opaque keys this translates to an invocation of the drivers'
2152 * copy_key entry point through the dispatch layer.
2153 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2155 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2156 &storage_size);
2157 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302158 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 }
Archana374fe5b2021-09-08 15:50:28 +05302160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2162 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302163 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 }
Archana374fe5b2021-09-08 15:50:28 +05302165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 status = psa_driver_wrapper_copy_key(&actual_attributes,
2167 source_slot->key.data,
2168 source_slot->key.bytes,
2169 target_slot->key.data,
2170 target_slot->key.bytes,
2171 &target_slot->key.bytes);
2172 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 }
2175 } else {
2176 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302177 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 source_slot->key.bytes);
2179 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 }
Archana8a180362021-07-05 02:18:48 +05302182 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002184exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 if (status != PSA_SUCCESS) {
2186 psa_fail_key_creation(target_slot, driver);
2187 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002192}
2193
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002194
2195
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002196/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002197/* Message digests */
2198/****************************************************************/
2199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002201{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002202 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (operation->id == 0) {
2204 return PSA_SUCCESS;
2205 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002208 operation->id = 0;
2209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211}
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2214 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002215{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002217
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002218 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002220 status = PSA_ERROR_BAD_STATE;
2221 goto exit;
2222 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002225 status = PSA_ERROR_INVALID_ARGUMENT;
2226 goto exit;
2227 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002228
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002229 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2230 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002234
2235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 if (status != PSA_SUCCESS) {
2237 psa_hash_abort(operation);
2238 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002239
2240 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002241}
2242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2244 const uint8_t *input,
2245 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002246{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002250 status = PSA_ERROR_BAD_STATE;
2251 goto exit;
2252 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002253
Steven Cooremanc8288352021-03-04 14:02:19 +01002254 /* Don't require hash implementations to behave correctly on a
2255 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 if (input_length == 0) {
2257 return PSA_SUCCESS;
2258 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002261
2262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if (status != PSA_SUCCESS) {
2264 psa_hash_abort(operation);
2265 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002268}
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2271 uint8_t *hash,
2272 size_t hash_size,
2273 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002274{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002275 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (operation->id == 0) {
2277 return PSA_ERROR_BAD_STATE;
2278 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279
Steven Cooreman1e582352021-02-18 17:24:37 +01002280 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 operation, hash, hash_size, hash_length);
2282 psa_hash_abort(operation);
2283 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284}
2285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2287 const uint8_t *hash,
2288 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002289{
Ronald Cron69a63422021-10-18 09:47:58 +02002290 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002291 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002292 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 operation,
2294 actual_hash, sizeof(actual_hash),
2295 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002298 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002302 status = PSA_ERROR_INVALID_SIGNATURE;
2303 goto exit;
2304 }
2305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002307 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002309
2310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2312 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002313 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002317}
2318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319psa_status_t psa_hash_compute(psa_algorithm_t alg,
2320 const uint8_t *input, size_t input_length,
2321 uint8_t *hash, size_t hash_size,
2322 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002323{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002324 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if (!PSA_ALG_IS_HASH(alg)) {
2326 return PSA_ERROR_INVALID_ARGUMENT;
2327 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2330 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002331}
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333psa_status_t psa_hash_compare(psa_algorithm_t alg,
2334 const uint8_t *input, size_t input_length,
2335 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002336{
Ronald Cron69a63422021-10-18 09:47:58 +02002337 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002338 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (!PSA_ALG_IS_HASH(alg)) {
2341 return PSA_ERROR_INVALID_ARGUMENT;
2342 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002343
Steven Cooreman1e582352021-02-18 17:24:37 +01002344 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 alg, input, input_length,
2346 actual_hash, sizeof(actual_hash),
2347 &actual_hash_length);
2348 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002349 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 }
2351 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002352 status = PSA_ERROR_INVALID_SIGNATURE;
2353 goto exit;
2354 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002356 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002358
2359exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2361 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002362}
2363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2365 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002366{
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 if (source_operation->id == 0 ||
2368 target_operation->id != 0) {
2369 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002370 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2373 target_operation);
2374 if (status != PSA_SUCCESS) {
2375 psa_hash_abort(target_operation);
2376 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002379}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002380
2381
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002382/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002383/* MAC */
2384/****************************************************************/
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002387{
Steven Cooremane6804192021-03-19 18:28:56 +01002388 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 if (operation->id == 0) {
2390 return PSA_SUCCESS;
2391 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002394 operation->mac_size = 0;
2395 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002396 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002399}
2400
Ronald Cron2dff3b22021-06-17 16:33:22 +02002401static psa_status_t psa_mac_finalize_alg_and_key_validation(
2402 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002403 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002405{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002406 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 psa_key_type_t key_type = psa_get_key_type(attributes);
2408 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (!PSA_ALG_IS_MAC(alg)) {
2411 return PSA_ERROR_INVALID_ARGUMENT;
2412 }
Steven Cooremane6804192021-03-19 18:28:56 +01002413
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002414 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 status = psa_mac_key_can_do(alg, key_type);
2416 if (status != PSA_SUCCESS) {
2417 return status;
2418 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002419
Steven Cooremand1a68f12021-05-10 11:13:41 +02002420 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002424 /* A very short MAC is too short for security since it can be
2425 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2426 * so we make this our minimum, even though 32 bits is still
2427 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002429 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2432 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002433 /* It's impossible to "truncate" to a larger length than the full length
2434 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002436 }
2437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002439 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2440 * that is disabled in the compile-time configuration. The result can
2441 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2442 * configuration into account. In this case, force a return of
2443 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2444 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2445 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2446 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2447 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002449 }
2450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002452}
2453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2455 mbedtls_svc_key_id_t key,
2456 psa_algorithm_t alg,
2457 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002458{
2459 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2460 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002461 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002462
2463 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002465 status = PSA_ERROR_BAD_STATE;
2466 goto exit;
2467 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002468
2469 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 key,
2471 &slot,
2472 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2473 alg);
2474 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002475 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477
2478 psa_key_attributes_t attributes = {
2479 .core = slot->attr
2480 };
2481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2483 &operation->mac_size);
2484 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002487
2488 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002489 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 if (is_sign) {
2491 status = psa_driver_wrapper_mac_sign_setup(operation,
2492 &attributes,
2493 slot->key.data,
2494 slot->key.bytes,
2495 alg);
2496 } else {
2497 status = psa_driver_wrapper_mac_verify_setup(operation,
2498 &attributes,
2499 slot->key.data,
2500 slot->key.bytes,
2501 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002503
Gilles Peskinefbfac682018-07-08 20:51:54 +02002504exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 if (status != PSA_SUCCESS) {
2506 psa_mac_abort(operation);
2507 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512}
2513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2515 mbedtls_svc_key_id_t key,
2516 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002517{
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002519}
2520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2522 mbedtls_svc_key_id_t key,
2523 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002524{
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002526}
2527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2529 const uint8_t *input,
2530 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531{
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (operation->id == 0) {
2533 return PSA_ERROR_BAD_STATE;
2534 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002536 /* Don't require hash implementations to behave correctly on a
2537 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 if (input_length == 0) {
2539 return PSA_SUCCESS;
2540 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2543 input, input_length);
2544 if (status != PSA_SUCCESS) {
2545 psa_mac_abort(operation);
2546 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549}
2550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2552 uint8_t *mac,
2553 size_t mac_size,
2554 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002555{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002556 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2557 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002560 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002561 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002562 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002565 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002566 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002567 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002568
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002569 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2570 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002572 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002573 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002574 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002577 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002578 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002579 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 status = psa_driver_wrapper_mac_sign_finish(operation,
2582 mac, operation->mac_size,
2583 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002584
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002585exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002586 /* In case of success, set the potential excess room in the output buffer
2587 * to an invalid value, to avoid potentially leaking a longer MAC.
2588 * In case of error, set the output length and content to a safe default,
2589 * such that in case the caller misses an error check, the output would be
2590 * an unachievable MAC.
2591 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002593 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002594 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002595 }
mohammad16036df908f2018-04-02 08:34:15 -07002596
Paul Elliottdc42ca82023-02-24 18:11:59 +00002597 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002602}
2603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2605 const uint8_t *mac,
2606 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2609 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002612 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002613 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002614 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002617 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002618 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002619 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002622 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002623 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002624 }
2625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 status = psa_driver_wrapper_mac_verify_finish(operation,
2627 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002628
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002629exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633}
2634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2636 psa_algorithm_t alg,
2637 const uint8_t *input,
2638 size_t input_length,
2639 uint8_t *mac,
2640 size_t mac_size,
2641 size_t *mac_length,
2642 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002643{
2644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002645 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2646 psa_key_slot_t *slot;
2647 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648
Ronald Cron51131b52021-06-17 17:17:20 +02002649 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 key,
2651 &slot,
2652 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2653 alg);
2654 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002655 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002657
Ronald Cron51131b52021-06-17 17:17:20 +02002658 psa_key_attributes_t attributes = {
2659 .core = slot->attr
2660 };
2661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2663 &operation_mac_size);
2664 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002665 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002669 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002670 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002671 }
2672
2673 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 &attributes,
2675 slot->key.data, slot->key.bytes,
2676 alg,
2677 input, input_length,
2678 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002679
2680exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002681 /* In case of success, set the potential excess room in the output buffer
2682 * to an invalid value, to avoid potentially leaking a longer MAC.
2683 * In case of error, set the output length and content to a safe default,
2684 * such that in case the caller misses an error check, the output would be
2685 * an unachievable MAC.
2686 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002688 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002689 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002690 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002691
2692 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002697}
2698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002700 psa_algorithm_t alg,
2701 const uint8_t *input,
2702 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 uint8_t *mac,
2704 size_t mac_size,
2705 size_t *mac_length)
2706{
2707 return psa_mac_compute_internal(key, alg,
2708 input, input_length,
2709 mac, mac_size, mac_length, 1);
2710}
2711
2712psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2713 psa_algorithm_t alg,
2714 const uint8_t *input,
2715 size_t input_length,
2716 const uint8_t *mac,
2717 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002718{
2719 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002720 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2721 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 status = psa_mac_compute_internal(key, alg,
2724 input, input_length,
2725 actual_mac, sizeof(actual_mac),
2726 &actual_mac_length, 0);
2727 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002728 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002732 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002733 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002734 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002736 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002737 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002738 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002739
2740exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002744}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002745
Gilles Peskinee59236f2018-01-27 23:32:46 +01002746/****************************************************************/
2747/* Asymmetric cryptography */
2748/****************************************************************/
2749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2751 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002752{
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 if (input_is_message) {
2754 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2755 return PSA_ERROR_INVALID_ARGUMENT;
2756 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2759 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2760 return PSA_ERROR_INVALID_ARGUMENT;
2761 }
2762 }
2763 } else {
2764 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2765 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002766 }
2767 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002770}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002771
Gilles Peskine449bd832023-01-11 14:50:10 +01002772static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2773 int input_is_message,
2774 psa_algorithm_t alg,
2775 const uint8_t *input,
2776 size_t input_length,
2777 uint8_t *signature,
2778 size_t signature_size,
2779 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002780{
2781 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2782 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2783 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002784
2785 *signature_length = 0;
2786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 status = psa_sign_verify_check_alg(input_is_message, alg);
2788 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002789 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002791
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002792 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002793 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002794 * buffer can in principle be empty since it doesn't actually have
2795 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 if (signature_size == 0) {
2797 return PSA_ERROR_BUFFER_TOO_SMALL;
2798 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002799
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002800 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 key, &slot,
2802 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2803 PSA_KEY_USAGE_SIGN_HASH,
2804 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002805
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002807 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002811 status = PSA_ERROR_INVALID_ARGUMENT;
2812 goto exit;
2813 }
2814
2815 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002817 };
2818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002820 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002821 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002822 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 signature, signature_size, signature_length);
2824 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002825
2826 status = psa_driver_wrapper_sign_hash(
2827 &attributes, slot->key.data, slot->key.bytes,
2828 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002830 }
2831
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002832
2833exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002834 psa_wipe_tag_output_buffer(signature, status, signature_size,
2835 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002836
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002840}
2841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2843 int input_is_message,
2844 psa_algorithm_t alg,
2845 const uint8_t *input,
2846 size_t input_length,
2847 const uint8_t *signature,
2848 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002849{
2850 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2851 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2852 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002853
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 status = psa_sign_verify_check_alg(input_is_message, alg);
2855 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002856 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002858
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002859 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 key, &slot,
2861 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2862 PSA_KEY_USAGE_VERIFY_HASH,
2863 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 if (status != PSA_SUCCESS) {
2866 return status;
2867 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002868
2869 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002871 };
2872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002874 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002875 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002876 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 signature, signature_length);
2878 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002879 status = psa_driver_wrapper_verify_hash(
2880 &attributes, slot->key.data, slot->key.bytes,
2881 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002883 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002884
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002888
2889}
2890
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002891psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002892 const psa_key_attributes_t *attributes,
2893 const uint8_t *key_buffer,
2894 size_t key_buffer_size,
2895 psa_algorithm_t alg,
2896 const uint8_t *input,
2897 size_t input_length,
2898 uint8_t *signature,
2899 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002901{
2902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002905 size_t hash_length;
2906 uint8_t hash[PSA_HASH_MAX_SIZE];
2907
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002908 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 PSA_ALG_SIGN_GET_HASH(alg),
2910 input, input_length,
2911 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002914 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002916
2917 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 attributes, key_buffer, key_buffer_size,
2919 alg, hash, hash_length,
2920 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002921 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002924}
2925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2927 psa_algorithm_t alg,
2928 const uint8_t *input,
2929 size_t input_length,
2930 uint8_t *signature,
2931 size_t signature_size,
2932 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002933{
2934 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002935 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002937}
2938
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002939psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002940 const psa_key_attributes_t *attributes,
2941 const uint8_t *key_buffer,
2942 size_t key_buffer_size,
2943 psa_algorithm_t alg,
2944 const uint8_t *input,
2945 size_t input_length,
2946 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002948{
2949 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002952 size_t hash_length;
2953 uint8_t hash[PSA_HASH_MAX_SIZE];
2954
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002955 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 PSA_ALG_SIGN_GET_HASH(alg),
2957 input, input_length,
2958 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002961 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002963
2964 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 attributes, key_buffer, key_buffer_size,
2966 alg, hash, hash_length,
2967 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002968 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002971}
2972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2974 psa_algorithm_t alg,
2975 const uint8_t *input,
2976 size_t input_length,
2977 const uint8_t *signature,
2978 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002979{
2980 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002981 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002983}
2984
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002985psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002986 const psa_key_attributes_t *attributes,
2987 const uint8_t *key_buffer, size_t key_buffer_size,
2988 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002990{
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2992 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2993 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002994#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2996 return mbedtls_psa_rsa_sign_hash(
2997 attributes,
2998 key_buffer, key_buffer_size,
2999 alg, hash, hash_length,
3000 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003001#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3002 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 } else {
3004 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003005 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3007 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003008#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3010 return mbedtls_psa_ecdsa_sign_hash(
3011 attributes,
3012 key_buffer, key_buffer_size,
3013 alg, hash, hash_length,
3014 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003015#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3016 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 } else {
3018 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003019 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003020 }
Ronald Cron4501c982021-03-19 20:09:32 +01003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 (void) key_buffer;
3023 (void) key_buffer_size;
3024 (void) hash;
3025 (void) hash_length;
3026 (void) signature;
3027 (void) signature_size;
3028 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003031}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3034 psa_algorithm_t alg,
3035 const uint8_t *hash,
3036 size_t hash_length,
3037 uint8_t *signature,
3038 size_t signature_size,
3039 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003040{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003041 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003042 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003044}
3045
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003046psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003047 const psa_key_attributes_t *attributes,
3048 const uint8_t *key_buffer, size_t key_buffer_size,
3049 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003051{
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3053 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3054 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003055#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3057 return mbedtls_psa_rsa_verify_hash(
3058 attributes,
3059 key_buffer, key_buffer_size,
3060 alg, hash, hash_length,
3061 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003062#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3063 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 } else {
3065 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003066 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3068 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003069#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3071 return mbedtls_psa_ecdsa_verify_hash(
3072 attributes,
3073 key_buffer, key_buffer_size,
3074 alg, hash, hash_length,
3075 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003076#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3077 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 } else {
3079 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003080 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003081 }
Ronald Cron4501c982021-03-19 20:09:32 +01003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 (void) key_buffer;
3084 (void) key_buffer_size;
3085 (void) hash;
3086 (void) hash_length;
3087 (void) signature;
3088 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003091}
3092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3094 psa_algorithm_t alg,
3095 const uint8_t *hash,
3096 size_t hash_length,
3097 const uint8_t *signature,
3098 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003099{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003100 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003101 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003103}
3104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3106 psa_algorithm_t alg,
3107 const uint8_t *input,
3108 size_t input_length,
3109 const uint8_t *salt,
3110 size_t salt_length,
3111 uint8_t *output,
3112 size_t output_size,
3113 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003114{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003115 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003116 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003117 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003118
Darryl Green5cc689a2018-07-24 15:34:10 +01003119 (void) input;
3120 (void) input_length;
3121 (void) salt;
3122 (void) output;
3123 (void) output_size;
3124
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003125 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003126
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3128 return PSA_ERROR_INVALID_ARGUMENT;
3129 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003130
Ronald Cron5c522922020-11-14 16:35:34 +01003131 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3133 if (status != PSA_SUCCESS) {
3134 return status;
3135 }
3136 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3137 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003138 status = PSA_ERROR_INVALID_ARGUMENT;
3139 goto exit;
3140 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003141
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003142 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003144 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003145
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003146 status = psa_driver_wrapper_asymmetric_encrypt(
3147 &attributes, slot->key.data, slot->key.bytes,
3148 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003154}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003155
Gilles Peskine449bd832023-01-11 14:50:10 +01003156psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3157 psa_algorithm_t alg,
3158 const uint8_t *input,
3159 size_t input_length,
3160 const uint8_t *salt,
3161 size_t salt_length,
3162 uint8_t *output,
3163 size_t output_size,
3164 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003165{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003166 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003167 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003168 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003169
Darryl Green5cc689a2018-07-24 15:34:10 +01003170 (void) input;
3171 (void) input_length;
3172 (void) salt;
3173 (void) output;
3174 (void) output_size;
3175
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003176 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3179 return PSA_ERROR_INVALID_ARGUMENT;
3180 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003181
Ronald Cron5c522922020-11-14 16:35:34 +01003182 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3184 if (status != PSA_SUCCESS) {
3185 return status;
3186 }
3187 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003188 status = PSA_ERROR_INVALID_ARGUMENT;
3189 goto exit;
3190 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003192 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003194 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003196 status = psa_driver_wrapper_asymmetric_decrypt(
3197 &attributes, slot->key.data, slot->key.bytes,
3198 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003200
3201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003205}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003206
Paul Elliott9fe12f62022-11-30 19:16:02 +00003207/****************************************************************/
3208/* Asymmetric interruptible cryptography */
3209/****************************************************************/
3210
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003211static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3212
Paul Elliott9fe12f62022-11-30 19:16:02 +00003213void psa_interruptible_set_max_ops(uint32_t max_ops)
3214{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003215 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003216}
3217
3218uint32_t psa_interruptible_get_max_ops(void)
3219{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003220 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003221}
3222
Paul Elliott9fe12f62022-11-30 19:16:02 +00003223uint32_t psa_sign_hash_get_num_ops(
3224 const psa_sign_hash_interruptible_operation_t *operation)
3225{
Paul Elliott296ede92022-12-15 17:00:30 +00003226 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003227}
3228
3229uint32_t psa_verify_hash_get_num_ops(
3230 const psa_verify_hash_interruptible_operation_t *operation)
3231{
Paul Elliott296ede92022-12-15 17:00:30 +00003232 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003233}
3234
Paul Elliott59ad9452022-12-18 15:09:02 +00003235static psa_status_t psa_sign_hash_abort_internal(
3236 psa_sign_hash_interruptible_operation_t *operation)
3237{
3238 if (operation->id == 0) {
3239 /* The object has (apparently) been initialized but it is not (yet)
3240 * in use. It's ok to call abort on such an object, and there's
3241 * nothing to do. */
3242 return PSA_SUCCESS;
3243 }
3244
3245 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3246
3247 status = psa_driver_wrapper_sign_hash_abort(operation);
3248
3249 operation->id = 0;
3250
Paul Elliotte6145dc2023-02-07 12:51:21 +00003251 /* Do not clear either the error_occurred or num_ops elements here as they
3252 * only want to be cleared by the application calling abort, not by abort
3253 * being called at completion of an operation. */
3254
Paul Elliott59ad9452022-12-18 15:09:02 +00003255 return status;
3256}
3257
Paul Elliott9fe12f62022-11-30 19:16:02 +00003258psa_status_t psa_sign_hash_start(
3259 psa_sign_hash_interruptible_operation_t *operation,
3260 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3261 const uint8_t *hash, size_t hash_length)
3262{
3263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3264 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3265 psa_key_slot_t *slot;
3266
Paul Elliottc9774412023-02-06 15:14:07 +00003267 /* Check that start has not been previously called, or operation has not
3268 * previously errored. */
3269 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003270 return PSA_ERROR_BAD_STATE;
3271 }
3272
Paul Elliott9fe12f62022-11-30 19:16:02 +00003273 status = psa_sign_verify_check_alg(0, alg);
3274 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003275 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003276 return status;
3277 }
3278
3279 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3280 PSA_KEY_USAGE_SIGN_HASH,
3281 alg);
3282
3283 if (status != PSA_SUCCESS) {
3284 goto exit;
3285 }
3286
3287 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3288 status = PSA_ERROR_INVALID_ARGUMENT;
3289 goto exit;
3290 }
3291
3292 psa_key_attributes_t attributes = {
3293 .core = slot->attr
3294 };
3295
Paul Elliott296ede92022-12-15 17:00:30 +00003296 /* Ensure ops count gets reset, in case of operation re-use. */
3297 operation->num_ops = 0;
3298
Paul Elliott9fe12f62022-11-30 19:16:02 +00003299 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3300 slot->key.data,
3301 slot->key.bytes, alg,
3302 hash, hash_length);
3303exit:
3304
3305 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003306 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003307 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003308 }
3309
3310 unlock_status = psa_unlock_key_slot(slot);
3311
Paul Elliottc9774412023-02-06 15:14:07 +00003312 if (unlock_status != PSA_SUCCESS) {
3313 operation->error_occurred = 1;
3314 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003315
Paul Elliottc9774412023-02-06 15:14:07 +00003316 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003317}
3318
3319
3320psa_status_t psa_sign_hash_complete(
3321 psa_sign_hash_interruptible_operation_t *operation,
3322 uint8_t *signature, size_t signature_size,
3323 size_t *signature_length)
3324{
3325 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3326
3327 *signature_length = 0;
3328
Paul Elliottc9774412023-02-06 15:14:07 +00003329 /* Check that start has been called first, and that operation has not
3330 * previously errored. */
3331 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003332 status = PSA_ERROR_BAD_STATE;
3333 goto exit;
3334 }
3335
Paul Elliott096abc42023-02-03 18:33:23 +00003336 /* Immediately reject a zero-length signature buffer. This guarantees that
3337 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003338 if (signature_size == 0) {
3339 status = PSA_ERROR_BUFFER_TOO_SMALL;
3340 goto exit;
3341 }
3342
3343 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3344 signature_size,
3345 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346
Paul Elliott296ede92022-12-15 17:00:30 +00003347 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003348 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003349
Paul Elliottebe225c2023-02-10 14:32:53 +00003350exit:
3351
Paul Elliott59200a22023-02-21 15:07:40 +00003352 psa_wipe_tag_output_buffer(signature, status, signature_size,
3353 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003354
Paul Elliott53bb3122023-02-10 14:22:22 +00003355 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003356 if (status != PSA_SUCCESS) {
3357 operation->error_occurred = 1;
3358 }
3359
Paul Elliott59ad9452022-12-18 15:09:02 +00003360 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003361 }
3362
3363 return status;
3364}
3365
3366psa_status_t psa_sign_hash_abort(
3367 psa_sign_hash_interruptible_operation_t *operation)
3368{
Paul Elliott59ad9452022-12-18 15:09:02 +00003369 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3370
3371 status = psa_sign_hash_abort_internal(operation);
3372
3373 /* We clear the number of ops done here, so that it is not cleared when
3374 * the operation fails or succeeds, only on manual abort. */
3375 operation->num_ops = 0;
3376
Paul Elliottc9774412023-02-06 15:14:07 +00003377 /* Likewise, failure state. */
3378 operation->error_occurred = 0;
3379
Paul Elliott59ad9452022-12-18 15:09:02 +00003380 return status;
3381}
3382
3383static psa_status_t psa_verify_hash_abort_internal(
3384 psa_verify_hash_interruptible_operation_t *operation)
3385{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003386 if (operation->id == 0) {
3387 /* The object has (apparently) been initialized but it is not (yet)
3388 * in use. It's ok to call abort on such an object, and there's
3389 * nothing to do. */
3390 return PSA_SUCCESS;
3391 }
3392
Paul Elliott59ad9452022-12-18 15:09:02 +00003393 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3394
3395 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003396
3397 operation->id = 0;
3398
Paul Elliotte6145dc2023-02-07 12:51:21 +00003399 /* Do not clear either the error_occurred or num_ops elements here as they
3400 * only want to be cleared by the application calling abort, not by abort
3401 * being called at completion of an operation. */
3402
Paul Elliott59ad9452022-12-18 15:09:02 +00003403 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003404}
3405
3406psa_status_t psa_verify_hash_start(
3407 psa_verify_hash_interruptible_operation_t *operation,
3408 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3409 const uint8_t *hash, size_t hash_length,
3410 const uint8_t *signature, size_t signature_length)
3411{
3412 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3413 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3414 psa_key_slot_t *slot;
3415
Paul Elliottc9774412023-02-06 15:14:07 +00003416 /* Check that start has not been previously called, or operation has not
3417 * previously errored. */
3418 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003419 return PSA_ERROR_BAD_STATE;
3420 }
3421
3422 status = psa_sign_verify_check_alg(0, alg);
3423 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003424 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003425 return status;
3426 }
3427
3428 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3429 PSA_KEY_USAGE_VERIFY_HASH,
3430 alg);
3431
3432 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003433 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003434 return status;
3435 }
3436
3437 psa_key_attributes_t attributes = {
3438 .core = slot->attr
3439 };
3440
Paul Elliott296ede92022-12-15 17:00:30 +00003441 /* Ensure ops count gets reset, in case of operation re-use. */
3442 operation->num_ops = 0;
3443
Paul Elliott9fe12f62022-11-30 19:16:02 +00003444 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3445 slot->key.data,
3446 slot->key.bytes,
3447 alg, hash, hash_length,
3448 signature, signature_length);
3449
3450 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003451 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003452 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003453 }
3454
3455 unlock_status = psa_unlock_key_slot(slot);
3456
Paul Elliottc9774412023-02-06 15:14:07 +00003457 if (unlock_status != PSA_SUCCESS) {
3458 operation->error_occurred = 1;
3459 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003460
Paul Elliottc9774412023-02-06 15:14:07 +00003461 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003462}
3463
3464psa_status_t psa_verify_hash_complete(
3465 psa_verify_hash_interruptible_operation_t *operation)
3466{
3467 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3468
Paul Elliottc9774412023-02-06 15:14:07 +00003469 /* Check that start has been called first, and that operation has not
3470 * previously errored. */
3471 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003472 status = PSA_ERROR_BAD_STATE;
3473 goto exit;
3474 }
3475
3476 status = psa_driver_wrapper_verify_hash_complete(operation);
3477
Paul Elliott296ede92022-12-15 17:00:30 +00003478 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003479 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003480 operation);
3481
Paul Elliottebe225c2023-02-10 14:32:53 +00003482exit:
3483
Paul Elliott9fe12f62022-11-30 19:16:02 +00003484 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003485 if (status != PSA_SUCCESS) {
3486 operation->error_occurred = 1;
3487 }
3488
Paul Elliott59ad9452022-12-18 15:09:02 +00003489 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003490 }
3491
3492 return status;
3493}
3494
3495psa_status_t psa_verify_hash_abort(
3496 psa_verify_hash_interruptible_operation_t *operation)
3497{
Paul Elliott59ad9452022-12-18 15:09:02 +00003498 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003499
Paul Elliott59ad9452022-12-18 15:09:02 +00003500 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003501
Paul Elliott59ad9452022-12-18 15:09:02 +00003502 /* We clear the number of ops done here, so that it is not cleared when
3503 * the operation fails or succeeds, only on manual abort. */
3504 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003505
Paul Elliottc9774412023-02-06 15:14:07 +00003506 /* Likewise, failure state. */
3507 operation->error_occurred = 0;
3508
Paul Elliott59ad9452022-12-18 15:09:02 +00003509 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003510}
3511
Paul Elliott588f8ed2022-12-02 18:10:26 +00003512/****************************************************************/
3513/* Asymmetric interruptible cryptography internal */
3514/* implementations */
3515/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003516
Paul Elliott588f8ed2022-12-02 18:10:26 +00003517void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3518{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003519
Paul Elliott588f8ed2022-12-02 18:10:26 +00003520#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3521 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3522 defined(MBEDTLS_ECP_RESTARTABLE)
3523
3524 /* Internal implementation uses zero to indicate infinite number max ops,
3525 * therefore avoid this value, and set to minimum possible. */
3526 if (max_ops == 0) {
3527 max_ops = 1;
3528 }
3529
Paul Elliott588f8ed2022-12-02 18:10:26 +00003530 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003531#else
3532 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003533#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3534 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3535 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3536}
3537
Paul Elliott588f8ed2022-12-02 18:10:26 +00003538uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003539 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003540{
3541#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3542 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3543 defined(MBEDTLS_ECP_RESTARTABLE)
3544
Paul Elliott93d9ca82023-02-15 18:14:21 +00003545 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003546#else
3547 (void) operation;
3548 return 0;
3549#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3550 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3551 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3552}
3553
3554uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003555 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003556{
3557 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3558 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3559 defined(MBEDTLS_ECP_RESTARTABLE)
3560
Paul Elliott93d9ca82023-02-15 18:14:21 +00003561 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003562#else
3563 (void) operation;
3564 return 0;
3565#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3566 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3567 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3568}
3569
3570psa_status_t mbedtls_psa_sign_hash_start(
3571 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3572 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3573 size_t key_buffer_size, psa_algorithm_t alg,
3574 const uint8_t *hash, size_t hash_length)
3575{
3576 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003577 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003578
Paul Elliott068fe072023-01-16 13:59:15 +00003579 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3580 return PSA_ERROR_NOT_SUPPORTED;
3581 }
3582
3583 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003584 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003585 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003586
3587#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003588 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3589 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003590
Paul Elliotta1c94092023-02-15 16:38:04 +00003591 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3592
Paul Elliott93d9ca82023-02-15 18:14:21 +00003593 /* Ensure num_ops is zero'ed in case of context re-use. */
3594 operation->num_ops = 0;
3595
Paul Elliott068fe072023-01-16 13:59:15 +00003596 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3597 attributes->core.bits,
3598 key_buffer,
3599 key_buffer_size,
3600 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003601
Paul Elliott068fe072023-01-16 13:59:15 +00003602 if (status != PSA_SUCCESS) {
3603 return status;
3604 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003605
Paul Elliott1bc59df2023-02-05 13:41:57 +00003606 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003607 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003608
Paul Elliott068fe072023-01-16 13:59:15 +00003609 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3610 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3611 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003612
Paul Elliott0290a762023-02-09 14:30:24 +00003613 /* We only need to store the same length of hash as the private key size
3614 * here, it would be truncated by the internal implementation anyway. */
3615 required_hash_length = (hash_length < operation->coordinate_bytes ?
3616 hash_length : operation->coordinate_bytes);
3617
Paul Elliottba70ad42023-02-15 18:23:53 +00003618 if (required_hash_length > sizeof(operation->hash)) {
3619 /* Shouldn't happen, but better safe than sorry. */
3620 return PSA_ERROR_CORRUPTION_DETECTED;
3621 }
3622
Paul Elliott0290a762023-02-09 14:30:24 +00003623 memcpy(operation->hash, hash, required_hash_length);
3624 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003625
3626 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003627
3628#else
Paul Elliott068fe072023-01-16 13:59:15 +00003629 (void) operation;
3630 (void) key_buffer;
3631 (void) key_buffer_size;
3632 (void) alg;
3633 (void) hash;
3634 (void) hash_length;
3635 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003636 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003637
Paul Elliott068fe072023-01-16 13:59:15 +00003638 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003639#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3640 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3641 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003642}
3643
3644psa_status_t mbedtls_psa_sign_hash_complete(
3645 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3646 uint8_t *signature, size_t signature_size,
3647 size_t *signature_length)
3648{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003649#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3650 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3651 defined(MBEDTLS_ECP_RESTARTABLE)
3652
Paul Elliotta1c94092023-02-15 16:38:04 +00003653 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003654 mbedtls_mpi r;
3655 mbedtls_mpi s;
3656
Paul Elliott46845252023-02-03 14:59:11 +00003657 mbedtls_mpi_init(&r);
3658 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003659
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003660 /* Ensure max_ops is set to the current value (or default). */
3661 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3662
Paul Elliotta1c94092023-02-15 16:38:04 +00003663 if (signature_size < 2 * operation->coordinate_bytes) {
3664 status = PSA_ERROR_BUFFER_TOO_SMALL;
3665 goto exit;
3666 }
3667
Paul Elliott588f8ed2022-12-02 18:10:26 +00003668 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003669
Paul Elliott588f8ed2022-12-02 18:10:26 +00003670#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3671 status = mbedtls_to_psa_error(
3672 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003673 &r,
3674 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675 &operation->ctx->d,
3676 operation->hash,
3677 operation->hash_length,
3678 operation->md_alg,
3679 mbedtls_psa_get_random,
3680 MBEDTLS_PSA_RANDOM_STATE,
3681 &operation->restart_ctx));
3682#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003683 status = PSA_ERROR_NOT_SUPPORTED;
3684 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003685#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3686 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003687 status = mbedtls_to_psa_error(
3688 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003689 &r,
3690 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003691 &operation->ctx->d,
3692 operation->hash,
3693 operation->hash_length,
3694 mbedtls_psa_get_random,
3695 MBEDTLS_PSA_RANDOM_STATE,
3696 mbedtls_psa_get_random,
3697 MBEDTLS_PSA_RANDOM_STATE,
3698 &operation->restart_ctx));
3699 }
3700
Paul Elliottf8e5b562023-02-19 18:43:45 +00003701 /* Hide the fact that the restart context only holds a delta of number of
3702 * ops done during the last operation, not an absolute value. */
3703 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3704
Paul Elliott724bd252023-02-08 12:35:08 +00003705 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003706 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003707 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003708 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003709 operation->coordinate_bytes)
3710 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711
3712 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003713 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003714 }
3715
3716 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003717 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003719 operation->coordinate_bytes,
3720 operation->coordinate_bytes)
3721 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722
3723 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003724 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725 }
3726
Paul Elliott1bc59df2023-02-05 13:41:57 +00003727 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003728
Paul Elliott724bd252023-02-08 12:35:08 +00003729 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003730 }
Paul Elliott724bd252023-02-08 12:35:08 +00003731
3732exit:
3733
3734 mbedtls_mpi_free(&r);
3735 mbedtls_mpi_free(&s);
3736 return status;
3737
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738 #else
3739
3740 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741 (void) signature;
3742 (void) signature_size;
3743 (void) signature_length;
3744
3745 return PSA_ERROR_NOT_SUPPORTED;
3746
3747#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3748 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3749 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3750}
3751
3752psa_status_t mbedtls_psa_sign_hash_abort(
3753 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3754{
3755
3756#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3757 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3758 defined(MBEDTLS_ECP_RESTARTABLE)
3759
3760 if (operation->ctx) {
3761 mbedtls_ecdsa_free(operation->ctx);
3762 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003763 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003764 }
3765
3766 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3767
Paul Elliott93d9ca82023-02-15 18:14:21 +00003768 operation->num_ops = 0;
3769
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770 return PSA_SUCCESS;
3771
3772#else
3773
3774 (void) operation;
3775
3776 return PSA_ERROR_NOT_SUPPORTED;
3777
3778#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3779 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3780 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3781}
3782
3783psa_status_t mbedtls_psa_verify_hash_start(
3784 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3785 const psa_key_attributes_t *attributes,
3786 const uint8_t *key_buffer, size_t key_buffer_size,
3787 psa_algorithm_t alg,
3788 const uint8_t *hash, size_t hash_length,
3789 const uint8_t *signature, size_t signature_length)
3790{
3791 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003792 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003793 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003794
Paul Elliott068fe072023-01-16 13:59:15 +00003795 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3796 return PSA_ERROR_NOT_SUPPORTED;
3797 }
3798
3799 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003800 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003801 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802
3803#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003804 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3805 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003806
Paul Elliotta1c94092023-02-15 16:38:04 +00003807 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3808 mbedtls_mpi_init(&operation->r);
3809 mbedtls_mpi_init(&operation->s);
3810
Paul Elliott93d9ca82023-02-15 18:14:21 +00003811 /* Ensure num_ops is zero'ed in case of context re-use. */
3812 operation->num_ops = 0;
3813
Paul Elliott068fe072023-01-16 13:59:15 +00003814 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3815 attributes->core.bits,
3816 key_buffer,
3817 key_buffer_size,
3818 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003819
Paul Elliott068fe072023-01-16 13:59:15 +00003820 if (status != PSA_SUCCESS) {
3821 return status;
3822 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003823
Paul Elliottc569fc22023-02-10 13:02:54 +00003824 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003825
Paul Elliott1bc59df2023-02-05 13:41:57 +00003826 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003827 return PSA_ERROR_INVALID_SIGNATURE;
3828 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003829
Paul Elliott068fe072023-01-16 13:59:15 +00003830 status = mbedtls_to_psa_error(
3831 mbedtls_mpi_read_binary(&operation->r,
3832 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003833 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834
Paul Elliott068fe072023-01-16 13:59:15 +00003835 if (status != PSA_SUCCESS) {
3836 return status;
3837 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003838
Paul Elliott068fe072023-01-16 13:59:15 +00003839 status = mbedtls_to_psa_error(
3840 mbedtls_mpi_read_binary(&operation->s,
3841 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003842 coordinate_bytes,
3843 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844
Paul Elliott068fe072023-01-16 13:59:15 +00003845 if (status != PSA_SUCCESS) {
3846 return status;
3847 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003848
Paul Elliott2c9843f2023-02-15 17:32:42 +00003849 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850
Paul Elliott2c9843f2023-02-15 17:32:42 +00003851 if (status != PSA_SUCCESS) {
3852 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003853 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854
Paul Elliott0290a762023-02-09 14:30:24 +00003855 /* We only need to store the same length of hash as the private key size
3856 * here, it would be truncated by the internal implementation anyway. */
3857 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3858 coordinate_bytes);
3859
Paul Elliottba70ad42023-02-15 18:23:53 +00003860 if (required_hash_length > sizeof(operation->hash)) {
3861 /* Shouldn't happen, but better safe than sorry. */
3862 return PSA_ERROR_CORRUPTION_DETECTED;
3863 }
3864
Paul Elliott0290a762023-02-09 14:30:24 +00003865 memcpy(operation->hash, hash, required_hash_length);
3866 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003867
3868 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003869#else
Paul Elliott068fe072023-01-16 13:59:15 +00003870 (void) operation;
3871 (void) key_buffer;
3872 (void) key_buffer_size;
3873 (void) alg;
3874 (void) hash;
3875 (void) hash_length;
3876 (void) signature;
3877 (void) signature_length;
3878 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003879 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003880 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003881
Paul Elliott068fe072023-01-16 13:59:15 +00003882 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003883#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3884 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3885 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003886}
3887
3888psa_status_t mbedtls_psa_verify_hash_complete(
3889 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3890{
3891
3892#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3893 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3894 defined(MBEDTLS_ECP_RESTARTABLE)
3895
Paul Elliottf8e5b562023-02-19 18:43:45 +00003896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3897
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003898 /* Ensure max_ops is set to the current value (or default). */
3899 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3900
Paul Elliottf8e5b562023-02-19 18:43:45 +00003901 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003902 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3903 operation->hash,
3904 operation->hash_length,
3905 &operation->ctx->Q,
3906 &operation->r,
3907 &operation->s,
3908 &operation->restart_ctx));
3909
Paul Elliottf8e5b562023-02-19 18:43:45 +00003910 /* Hide the fact that the restart context only holds a delta of number of
3911 * ops done during the last operation, not an absolute value. */
3912 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3913
3914 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003915#else
3916 (void) operation;
3917
3918 return PSA_ERROR_NOT_SUPPORTED;
3919
3920#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3921 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3922 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3923}
3924
3925psa_status_t mbedtls_psa_verify_hash_abort(
3926 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3927{
3928
3929#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3930 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3931 defined(MBEDTLS_ECP_RESTARTABLE)
3932
3933 if (operation->ctx) {
3934 mbedtls_ecdsa_free(operation->ctx);
3935 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003936 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003937 }
3938
3939 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3940
Paul Elliott93d9ca82023-02-15 18:14:21 +00003941 operation->num_ops = 0;
3942
Paul Elliott588f8ed2022-12-02 18:10:26 +00003943 mbedtls_mpi_free(&operation->r);
3944 mbedtls_mpi_free(&operation->s);
3945
3946 return PSA_SUCCESS;
3947
3948#else
3949 (void) operation;
3950
3951 return PSA_ERROR_NOT_SUPPORTED;
3952
3953#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3954 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3955 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3956}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003957
mohammad1603503973b2018-03-12 15:59:30 +02003958/****************************************************************/
3959/* Symmetric cryptography */
3960/****************************************************************/
3961
Gilles Peskine449bd832023-01-11 14:50:10 +01003962static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3963 mbedtls_svc_key_id_t key,
3964 psa_algorithm_t alg,
3965 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003966{
3967 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3968 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003969 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003970 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3971 PSA_KEY_USAGE_ENCRYPT :
3972 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003973
3974 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003976 status = PSA_ERROR_BAD_STATE;
3977 goto exit;
3978 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003979
Gilles Peskine449bd832023-01-11 14:50:10 +01003980 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003981 status = PSA_ERROR_INVALID_ARGUMENT;
3982 goto exit;
3983 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003984
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3986 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003987 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003988 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003989
Ronald Cron49fafa92021-03-10 08:34:23 +01003990 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003991 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003992 * so we only set it (in the driver wrapper) after resources have been
3993 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003994 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003996 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003998 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 }
4000 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004001
Ronald Crona4af55f2020-12-14 14:36:06 +01004002 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004004 };
4005
Ronald Cronab99ac22020-10-01 16:38:28 +02004006 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 if (cipher_operation == MBEDTLS_ENCRYPT) {
4008 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4009 &attributes,
4010 slot->key.data,
4011 slot->key.bytes,
4012 alg);
4013 } else {
4014 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4015 &attributes,
4016 slot->key.data,
4017 slot->key.bytes,
4018 alg);
4019 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004020
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004021exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 if (status != PSA_SUCCESS) {
4023 psa_cipher_abort(operation);
4024 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004025
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004027
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004029}
4030
Gilles Peskine449bd832023-01-11 14:50:10 +01004031psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4032 mbedtls_svc_key_id_t key,
4033 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004034{
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004036}
4037
Gilles Peskine449bd832023-01-11 14:50:10 +01004038psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4039 mbedtls_svc_key_id_t key,
4040 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004041{
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004043}
4044
Gilles Peskine449bd832023-01-11 14:50:10 +01004045psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4046 uint8_t *iv,
4047 size_t iv_size,
4048 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004049{
Ronald Cron6d051732020-10-01 14:10:20 +02004050 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004051 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4052 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004053
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004055 status = PSA_ERROR_BAD_STATE;
4056 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004057 }
4058
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004060 status = PSA_ERROR_BAD_STATE;
4061 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004062 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004063
Ronald Cron2fb90522021-07-05 12:31:44 +02004064 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004065 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004066 status = PSA_ERROR_BUFFER_TOO_SMALL;
4067 goto exit;
4068 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004069
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004071 status = PSA_ERROR_GENERIC_ERROR;
4072 goto exit;
4073 }
4074
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 status = psa_generate_random(local_iv, default_iv_length);
4076 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 }
Ronald Cron5618a392021-03-26 09:52:26 +01004079
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 status = psa_driver_wrapper_cipher_set_iv(operation,
4081 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004082
4083exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 if (status == PSA_SUCCESS) {
4085 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004086 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004087 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004089 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004091 }
Ronald Cron6d051732020-10-01 14:10:20 +02004092
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004094}
4095
Gilles Peskine449bd832023-01-11 14:50:10 +01004096psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4097 const uint8_t *iv,
4098 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004099{
Ronald Cron6d051732020-10-01 14:10:20 +02004100 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004101
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004103 status = PSA_ERROR_BAD_STATE;
4104 goto exit;
4105 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004106
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004108 status = PSA_ERROR_BAD_STATE;
4109 goto exit;
4110 }
Ronald Crona0d68172021-03-26 10:15:08 +01004111
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004113 status = PSA_ERROR_INVALID_ARGUMENT;
4114 goto exit;
4115 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004116
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 status = psa_driver_wrapper_cipher_set_iv(operation,
4118 iv,
4119 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004120
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004123 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 } else {
4125 psa_cipher_abort(operation);
4126 }
4127 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004128}
4129
Gilles Peskine449bd832023-01-11 14:50:10 +01004130psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4131 const uint8_t *input,
4132 size_t input_length,
4133 uint8_t *output,
4134 size_t output_size,
4135 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004136{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004137 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004138
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004140 status = PSA_ERROR_BAD_STATE;
4141 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004142 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004143
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004145 status = PSA_ERROR_BAD_STATE;
4146 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004147 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004148
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 status = psa_driver_wrapper_cipher_update(operation,
4150 input,
4151 input_length,
4152 output,
4153 output_size,
4154 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004155
4156exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 if (status != PSA_SUCCESS) {
4158 psa_cipher_abort(operation);
4159 }
Ronald Cron6d051732020-10-01 14:10:20 +02004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004162}
4163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4165 uint8_t *output,
4166 size_t output_size,
4167 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004168{
David Saadab4ecc272019-02-14 13:48:10 +02004169 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004172 status = PSA_ERROR_BAD_STATE;
4173 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004174 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004177 status = PSA_ERROR_BAD_STATE;
4178 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004179 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 status = psa_driver_wrapper_cipher_finish(operation,
4182 output,
4183 output_size,
4184 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004185
4186exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 if (status == PSA_SUCCESS) {
4188 return psa_cipher_abort(operation);
4189 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004190 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004194 }
mohammad1603503973b2018-03-12 15:59:30 +02004195}
4196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004198{
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004200 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004201 * in use. It's ok to call abort on such an object, and there's
4202 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004204 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004205
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004207
Ronald Cron49fafa92021-03-10 08:34:23 +01004208 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004209 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004210 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004213}
4214
Gilles Peskine449bd832023-01-11 14:50:10 +01004215psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4216 psa_algorithm_t alg,
4217 const uint8_t *input,
4218 size_t input_length,
4219 uint8_t *output,
4220 size_t output_size,
4221 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004222{
4223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004224 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004225 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004226 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4227 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004228
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004230 status = PSA_ERROR_INVALID_ARGUMENT;
4231 goto exit;
4232 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4235 PSA_KEY_USAGE_ENCRYPT,
4236 alg);
4237 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004238 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004240
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004241 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004243 };
4244
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4246 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004247 status = PSA_ERROR_GENERIC_ERROR;
4248 goto exit;
4249 }
4250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 if (default_iv_length > 0) {
4252 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004253 status = PSA_ERROR_BUFFER_TOO_SMALL;
4254 goto exit;
4255 }
4256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 status = psa_generate_random(local_iv, default_iv_length);
4258 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004259 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004261 }
4262
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004263 status = psa_driver_wrapper_cipher_encrypt(
4264 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004265 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004266 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004268
4269exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 unlock_status = psa_unlock_key_slot(slot);
4271 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004272 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004273 }
Ronald Cron23919522021-07-08 19:00:07 +02004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 if (status == PSA_SUCCESS) {
4276 if (default_iv_length > 0) {
4277 memcpy(output, local_iv, default_iv_length);
4278 }
4279 *output_length += default_iv_length;
4280 } else {
4281 *output_length = 0;
4282 }
4283
4284 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004285}
4286
Gilles Peskine449bd832023-01-11 14:50:10 +01004287psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4288 psa_algorithm_t alg,
4289 const uint8_t *input,
4290 size_t input_length,
4291 uint8_t *output,
4292 size_t output_size,
4293 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004294{
4295 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004296 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004297 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004298
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004300 status = PSA_ERROR_INVALID_ARGUMENT;
4301 goto exit;
4302 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004303
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4305 PSA_KEY_USAGE_DECRYPT,
4306 alg);
4307 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004308 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004310
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004311 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004313 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004314
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4316 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004317 status = PSA_ERROR_INVALID_ARGUMENT;
4318 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004320 status = PSA_ERROR_INVALID_ARGUMENT;
4321 goto exit;
4322 }
4323
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004324 status = psa_driver_wrapper_cipher_decrypt(
4325 &attributes, slot->key.data, slot->key.bytes,
4326 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004328
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004329exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 unlock_status = psa_unlock_key_slot(slot);
4331 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004332 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004334
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004336 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 }
Ronald Cron23919522021-07-08 19:00:07 +02004338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004340}
4341
4342
mohammad16035955c982018-04-26 00:53:03 +03004343/****************************************************************/
4344/* AEAD */
4345/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004346
Paul Elliottbaff51c2021-09-28 17:44:45 +01004347/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004348static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004349{
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004351}
4352
4353/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004354static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4355 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004356{
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004360#if defined(PSA_WANT_ALG_GCM)
4361 case PSA_ALG_GCM:
4362 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 * arbitrarily large nonces. Please note that we do not generally
4364 * recommend the usage of nonces of greater length than
4365 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4366 * size, which can then lead to collisions if you encrypt a very
4367 * large number of messages.*/
4368 if (nonce_length != 0) {
4369 return PSA_SUCCESS;
4370 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004371 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004372#endif /* PSA_WANT_ALG_GCM */
4373#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004374 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 if (nonce_length >= 7 && nonce_length <= 13) {
4376 return PSA_SUCCESS;
4377 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004378 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004379#endif /* PSA_WANT_ALG_CCM */
4380#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004381 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 if (nonce_length == 12) {
4383 return PSA_SUCCESS;
4384 } else if (nonce_length == 8) {
4385 return PSA_ERROR_NOT_SUPPORTED;
4386 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004387 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004388#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004389 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004390 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004392 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004393
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004395}
4396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004398{
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4400 return PSA_ERROR_INVALID_ARGUMENT;
4401 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004404}
4405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4407 psa_algorithm_t alg,
4408 const uint8_t *nonce,
4409 size_t nonce_length,
4410 const uint8_t *additional_data,
4411 size_t additional_data_length,
4412 const uint8_t *plaintext,
4413 size_t plaintext_length,
4414 uint8_t *ciphertext,
4415 size_t ciphertext_size,
4416 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004417{
Ronald Cron215633c2021-03-16 17:15:37 +01004418 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4419 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004420
mohammad1603f08a5502018-06-03 15:05:47 +03004421 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004422
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 status = psa_aead_check_algorithm(alg);
4424 if (status != PSA_SUCCESS) {
4425 return status;
4426 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004427
Ronald Cron9a986162021-03-26 12:40:07 +01004428 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4430 if (status != PSA_SUCCESS) {
4431 return status;
4432 }
mohammad16035955c982018-04-26 00:53:03 +03004433
Ronald Cron215633c2021-03-16 17:15:37 +01004434 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004436 };
mohammad16035955c982018-04-26 00:53:03 +03004437
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 status = psa_aead_check_nonce_length(alg, nonce_length);
4439 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004440 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004442
Ronald Cronde822812021-03-17 16:08:20 +01004443 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004444 &attributes, slot->key.data, slot->key.bytes,
4445 alg,
4446 nonce, nonce_length,
4447 additional_data, additional_data_length,
4448 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4452 memset(ciphertext, 0, ciphertext_size);
4453 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004454
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004455exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004459}
4460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4462 psa_algorithm_t alg,
4463 const uint8_t *nonce,
4464 size_t nonce_length,
4465 const uint8_t *additional_data,
4466 size_t additional_data_length,
4467 const uint8_t *ciphertext,
4468 size_t ciphertext_length,
4469 uint8_t *plaintext,
4470 size_t plaintext_size,
4471 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004472{
Ronald Cron215633c2021-03-16 17:15:37 +01004473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4474 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004475
Gilles Peskineee652a32018-06-01 19:23:52 +02004476 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004477
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 status = psa_aead_check_algorithm(alg);
4479 if (status != PSA_SUCCESS) {
4480 return status;
4481 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004482
Ronald Cron9a986162021-03-26 12:40:07 +01004483 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4485 if (status != PSA_SUCCESS) {
4486 return status;
4487 }
mohammad16035955c982018-04-26 00:53:03 +03004488
Ronald Cron215633c2021-03-16 17:15:37 +01004489 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004491 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004492
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 status = psa_aead_check_nonce_length(alg, nonce_length);
4494 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004495 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004497
Ronald Cronde822812021-03-17 16:08:20 +01004498 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004499 &attributes, slot->key.data, slot->key.bytes,
4500 alg,
4501 nonce, nonce_length,
4502 additional_data, additional_data_length,
4503 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 if (status != PSA_SUCCESS && plaintext_size != 0) {
4507 memset(plaintext, 0, plaintext_size);
4508 }
mohammad160360a64d02018-06-03 17:20:42 +03004509
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004510exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 return status;
mohammad16035955c982018-04-26 00:53:03 +03004514}
4515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4517{
4518 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004521#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004523 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4525 return PSA_ERROR_INVALID_ARGUMENT;
4526 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004527 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004528#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004529
Przemek Stekiel86679c72022-10-06 17:06:56 +02004530#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004532 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4534 return PSA_ERROR_INVALID_ARGUMENT;
4535 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004536 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004537#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004538
Przemek Stekiel86679c72022-10-06 17:06:56 +02004539#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004541 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 if (tag_len != 16) {
4543 return PSA_ERROR_INVALID_ARGUMENT;
4544 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004545 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004546#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004547
4548 default:
4549 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004551 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004553}
4554
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004555/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004556static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4557 int is_encrypt,
4558 mbedtls_svc_key_id_t key,
4559 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004560{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004561 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4562 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4563 psa_key_slot_t *slot = NULL;
4564 psa_key_usage_t key_usage = 0;
4565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 status = psa_aead_check_algorithm(alg);
4567 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004568 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004572 status = PSA_ERROR_BAD_STATE;
4573 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004574 }
4575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 if (operation->nonce_set || operation->lengths_set ||
4577 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004578 status = PSA_ERROR_BAD_STATE;
4579 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004580 }
4581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004583 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004585 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4589 alg);
4590 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004591 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004593
4594 psa_key_attributes_t attributes = {
4595 .core = slot->attr
4596 };
4597
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004599 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004601
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 if (is_encrypt) {
4603 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4604 &attributes,
4605 slot->key.data,
4606 slot->key.bytes,
4607 alg);
4608 } else {
4609 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4610 &attributes,
4611 slot->key.data,
4612 slot->key.bytes,
4613 alg);
4614 }
4615 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004616 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004618
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004620
4621exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004625 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004627 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 } else {
4629 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004630 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004633}
4634
Paul Elliott302ff6b2021-04-20 18:10:30 +01004635/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004636psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4637 mbedtls_svc_key_id_t key,
4638 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004639{
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004641}
4642
4643/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004644psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4645 mbedtls_svc_key_id_t key,
4646 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004647{
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004649}
4650
4651/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004652psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4653 uint8_t *nonce,
4654 size_t nonce_size,
4655 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004656{
4657 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004658 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004659 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004660
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004661 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004662
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004664 status = PSA_ERROR_BAD_STATE;
4665 goto exit;
4666 }
4667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004669 status = PSA_ERROR_BAD_STATE;
4670 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004671 }
4672
Paul Elliotte193ea82021-10-01 13:00:16 +01004673 /* For CCM, this size may not be correct according to the PSA
4674 * specification. The PSA Crypto 1.0.1 specification states:
4675 *
4676 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4677 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4678 *
4679 * However this restriction that L has to be the smallest integer is not
4680 * applied in practice, and it is not implementable here since the
4681 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4683 operation->alg);
4684 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004685 status = PSA_ERROR_BUFFER_TOO_SMALL;
4686 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004687 }
4688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 status = psa_generate_random(local_nonce, required_nonce_size);
4690 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004691 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004693
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004695
Paul Elliott39dc6b82021-05-11 19:16:09 +01004696exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 if (status == PSA_SUCCESS) {
4698 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004699 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 } else {
4701 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004702 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004705}
4706
4707/* Set the nonce for a multipart authenticated encryption or decryption
4708 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004709psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4710 const uint8_t *nonce,
4711 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004712{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004713 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004716 status = PSA_ERROR_BAD_STATE;
4717 goto exit;
4718 }
4719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004721 status = PSA_ERROR_BAD_STATE;
4722 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004723 }
4724
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4726 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004727 status = PSA_ERROR_INVALID_ARGUMENT;
4728 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004729 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4732 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004733
Paul Elliott39dc6b82021-05-11 19:16:09 +01004734exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004736 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 } else {
4738 psa_aead_abort(operation);
4739 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004742}
4743
4744/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004745psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4746 size_t ad_length,
4747 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004748{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004749 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004752 status = PSA_ERROR_BAD_STATE;
4753 goto exit;
4754 }
4755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 if (operation->lengths_set || operation->ad_started ||
4757 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004758 status = PSA_ERROR_BAD_STATE;
4759 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004760 }
4761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004763#if defined(PSA_WANT_ALG_GCM)
4764 case PSA_ALG_GCM:
4765 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 * bits. Without the guard this code will generate warnings on 32bit
4767 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004768#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 if (((uint64_t) ad_length) >> 61 != 0 ||
4770 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004771 status = PSA_ERROR_INVALID_ARGUMENT;
4772 goto exit;
4773 }
Paul Elliott325d3742021-09-27 17:56:28 +01004774#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004775 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004776#endif /* PSA_WANT_ALG_GCM */
4777#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004778 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004780 status = PSA_ERROR_INVALID_ARGUMENT;
4781 goto exit;
4782 }
4783 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004784#endif /* PSA_WANT_ALG_CCM */
4785#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004786 case PSA_ALG_CHACHA20_POLY1305:
4787 /* No length restrictions for ChaChaPoly. */
4788 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004789#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004790 default:
4791 break;
4792 }
Paul Elliott325d3742021-09-27 17:56:28 +01004793
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4795 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004796
Paul Elliott39dc6b82021-05-11 19:16:09 +01004797exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004799 operation->ad_remaining = ad_length;
4800 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004801 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 } else {
4803 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004804 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004807}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004808
4809/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004810psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4811 const uint8_t *input,
4812 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004813{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004814 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4815
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004817 status = PSA_ERROR_BAD_STATE;
4818 goto exit;
4819 }
4820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004822 status = PSA_ERROR_BAD_STATE;
4823 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004824 }
4825
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 if (operation->lengths_set) {
4827 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004828 status = PSA_ERROR_INVALID_ARGUMENT;
4829 goto exit;
4830 }
4831
4832 operation->ad_remaining -= input_length;
4833 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004834#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004836 status = PSA_ERROR_BAD_STATE;
4837 goto exit;
4838 }
4839#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 status = psa_driver_wrapper_aead_update_ad(operation, input,
4842 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004843
Paul Elliott39dc6b82021-05-11 19:16:09 +01004844exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004846 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 } else {
4848 psa_aead_abort(operation);
4849 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004852}
4853
4854/* Encrypt or decrypt a message fragment in an active multipart AEAD
4855 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004856psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4857 const uint8_t *input,
4858 size_t input_length,
4859 uint8_t *output,
4860 size_t output_size,
4861 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004862{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004863 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004864
4865 *output_length = 0;
4866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004868 status = PSA_ERROR_BAD_STATE;
4869 goto exit;
4870 }
4871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004873 status = PSA_ERROR_BAD_STATE;
4874 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004875 }
4876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004878 /* Additional data length was supplied, but not all the additional
4879 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004881 status = PSA_ERROR_INVALID_ARGUMENT;
4882 goto exit;
4883 }
4884
4885 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004887 status = PSA_ERROR_INVALID_ARGUMENT;
4888 goto exit;
4889 }
4890
4891 operation->body_remaining -= input_length;
4892 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004893#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004895 status = PSA_ERROR_BAD_STATE;
4896 goto exit;
4897 }
4898#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4901 output, output_size,
4902 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004903
Paul Elliott39dc6b82021-05-11 19:16:09 +01004904exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004906 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 } else {
4908 psa_aead_abort(operation);
4909 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004912}
4913
Gilles Peskine449bd832023-01-11 14:50:10 +01004914static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004915{
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 if (operation->id == 0 || !operation->nonce_set) {
4917 return PSA_ERROR_BAD_STATE;
4918 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004919
Gilles Peskine449bd832023-01-11 14:50:10 +01004920 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4921 operation->body_remaining != 0)) {
4922 return PSA_ERROR_INVALID_ARGUMENT;
4923 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004924
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004926}
4927
Paul Elliott302ff6b2021-04-20 18:10:30 +01004928/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004929psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4930 uint8_t *ciphertext,
4931 size_t ciphertext_size,
4932 size_t *ciphertext_length,
4933 uint8_t *tag,
4934 size_t tag_size,
4935 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004936{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004937 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4938
Paul Elliott302ff6b2021-04-20 18:10:30 +01004939 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004940 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004941
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 status = psa_aead_final_checks(operation);
4943 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004944 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004946
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004948 status = PSA_ERROR_BAD_STATE;
4949 goto exit;
4950 }
4951
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4953 ciphertext_size,
4954 ciphertext_length,
4955 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004956
Paul Elliott39dc6b82021-05-11 19:16:09 +01004957exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004958
4959
Paul Elliottf47b0952021-05-21 18:02:33 +01004960 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004961 * the zero tag size, make sure the tag is set to something implausible.
4962 * Even if the operation succeeds, make sure we clear the rest of the
4963 * buffer to prevent potential leakage of anything previously placed in
4964 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004965 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004966
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004968
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004970}
4971
4972/* Finish authenticating and decrypting a message in a multipart AEAD
4973 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004974psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4975 uint8_t *plaintext,
4976 size_t plaintext_size,
4977 size_t *plaintext_length,
4978 const uint8_t *tag,
4979 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004980{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004981 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4982
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983 *plaintext_length = 0;
4984
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 status = psa_aead_final_checks(operation);
4986 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004987 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004989
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004991 status = PSA_ERROR_BAD_STATE;
4992 goto exit;
4993 }
4994
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4996 plaintext_size,
4997 plaintext_length,
4998 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004999
5000exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005004}
5005
5006/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005007psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005008{
5009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5010
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005012 /* The object has (apparently) been initialized but it is not (yet)
5013 * in use. It's ok to call abort on such an object, and there's
5014 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005016 }
5017
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005019
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005021
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005023}
5024
Gilles Peskinee59236f2018-01-27 23:32:46 +01005025/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005026/* Generators */
5027/****************************************************************/
5028
Przemek Stekiel69c46792022-06-10 12:59:51 +02005029#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005030 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005031 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5032 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005033#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005034#endif /* At least one builtin KDF */
5035
Przemek Stekiel69c46792022-06-10 12:59:51 +02005036#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005037 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5038 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5039static psa_status_t psa_key_derivation_start_hmac(
5040 psa_mac_operation_t *operation,
5041 psa_algorithm_t hash_alg,
5042 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005044{
5045 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5048 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5049 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005050
Steven Cooreman72f736a2021-05-07 14:14:37 +02005051 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005053
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 status = psa_driver_wrapper_mac_sign_setup(operation,
5055 &attributes,
5056 hmac_key, hmac_key_length,
5057 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005058
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 psa_reset_key_attributes(&attributes);
5060 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005061}
5062#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005063
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005064#define HKDF_STATE_INIT 0 /* no input yet */
5065#define HKDF_STATE_STARTED 1 /* got salt */
5066#define HKDF_STATE_KEYED 2 /* got key */
5067#define HKDF_STATE_OUTPUT 3 /* output started */
5068
Gilles Peskinecbe66502019-05-16 16:59:18 +02005069static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005071{
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5073 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5074 } else {
5075 return operation->alg;
5076 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005077}
5078
Gilles Peskine449bd832023-01-11 14:50:10 +01005079psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005080{
5081 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5083 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005084 /* The object has (apparently) been initialized but it is not
5085 * in use. It's ok to call abort on such an object, and there's
5086 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005088#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5090 mbedtls_free(operation->ctx.hkdf.info);
5091 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5092 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005093#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005094#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5095 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5097 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5098 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5099 if (operation->ctx.tls12_prf.secret != NULL) {
5100 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5101 operation->ctx.tls12_prf.secret_length);
5102 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005103 }
5104
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 if (operation->ctx.tls12_prf.seed != NULL) {
5106 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5107 operation->ctx.tls12_prf.seed_length);
5108 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005109 }
5110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 if (operation->ctx.tls12_prf.label != NULL) {
5112 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5113 operation->ctx.tls12_prf.label_length);
5114 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005115 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005116#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (operation->ctx.tls12_prf.other_secret != NULL) {
5118 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5119 operation->ctx.tls12_prf.other_secret_length);
5120 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005121 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005122#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005123 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005124
5125 /* We leave the fields Ai and output_block to be erased safely by the
5126 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005128#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5129 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005130#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5132 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5133 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5134 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005135#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005136 {
5137 status = PSA_ERROR_BAD_STATE;
5138 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 mbedtls_platform_zeroize(operation, sizeof(*operation));
5140 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005141}
5142
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005143psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005145{
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005147 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005149 }
5150
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005153}
5154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5156 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005157{
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 if (operation->alg == 0) {
5159 return PSA_ERROR_BAD_STATE;
5160 }
5161 if (capacity > operation->capacity) {
5162 return PSA_ERROR_INVALID_ARGUMENT;
5163 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005166}
5167
Przemek Stekiel69c46792022-06-10 12:59:51 +02005168#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005169/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005170static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5171 psa_algorithm_t kdf_alg,
5172 uint8_t *output,
5173 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005174{
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5176 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005177 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005178 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005179#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005181#else
5182 const uint8_t last_block = 0xff;
5183#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 if (hkdf->state < HKDF_STATE_KEYED ||
5186 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005187#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005189#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 )) {
5191 return PSA_ERROR_BAD_STATE;
5192 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005193 hkdf->state = HKDF_STATE_OUTPUT;
5194
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005196 /* Copy what remains of the current block */
5197 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005199 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 }
5201 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005202 output += n;
5203 output_length -= n;
5204 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005206 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005208 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005209 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005211 * object was corrupted or if this function is called directly
5212 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 if (hkdf->block_number == last_block) {
5214 return PSA_ERROR_BAD_STATE;
5215 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005216
5217 /* We need a new block */
5218 ++hkdf->block_number;
5219 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5222 hash_alg,
5223 hkdf->prk,
5224 hash_length);
5225 if (status != PSA_SUCCESS) {
5226 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005227 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005228
5229 if (hkdf->block_number != 1) {
5230 status = psa_mac_update(&hkdf->hmac,
5231 hkdf->output_block,
5232 hash_length);
5233 if (status != PSA_SUCCESS) {
5234 return status;
5235 }
5236 }
5237 status = psa_mac_update(&hkdf->hmac,
5238 hkdf->info,
5239 hkdf->info_length);
5240 if (status != PSA_SUCCESS) {
5241 return status;
5242 }
5243 status = psa_mac_update(&hkdf->hmac,
5244 &hkdf->block_number, 1);
5245 if (status != PSA_SUCCESS) {
5246 return status;
5247 }
5248 status = psa_mac_sign_finish(&hkdf->hmac,
5249 hkdf->output_block,
5250 sizeof(hkdf->output_block),
5251 &hmac_output_length);
5252 if (status != PSA_SUCCESS) {
5253 return status;
5254 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005255 }
5256
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005258}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005259#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005260
John Durkop07cc04a2020-11-16 22:08:34 -08005261#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5262 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005263static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5264 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005266{
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5268 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005269 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5270 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005271 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005272
Janos Follath7742fee2019-06-17 12:58:10 +01005273 /* We can't be wanting more output after block 0xff, otherwise
5274 * the capacity check in psa_key_derivation_output_bytes() would have
5275 * prevented this call. It could happen only if the operation
5276 * object was corrupted or if this function is called directly
5277 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 if (tls12_prf->block_number == 0xff) {
5279 return PSA_ERROR_CORRUPTION_DETECTED;
5280 }
Janos Follath7742fee2019-06-17 12:58:10 +01005281
5282 /* We need a new block */
5283 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005284 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005285
5286 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5287 *
5288 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5289 *
5290 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5291 * HMAC_hash(secret, A(2) + seed) +
5292 * HMAC_hash(secret, A(3) + seed) + ...
5293 *
5294 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005295 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005296 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005297 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005298 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005299 * is currently extracted as `output_block` and where i is
5300 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005301 */
5302
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 status = psa_key_derivation_start_hmac(&hmac,
5304 hash_alg,
5305 tls12_prf->secret,
5306 tls12_prf->secret_length);
5307 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005308 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005310
5311 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005313 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5314 * the variable seed and in this instance means it in the context of the
5315 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 status = psa_mac_update(&hmac,
5317 tls12_prf->label,
5318 tls12_prf->label_length);
5319 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005320 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 }
5322 status = psa_mac_update(&hmac,
5323 tls12_prf->seed,
5324 tls12_prf->seed_length);
5325 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005326 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 }
5328 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005329 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5331 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005332 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005334 }
5335
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 status = psa_mac_sign_finish(&hmac,
5337 tls12_prf->Ai, hash_length,
5338 &hmac_output_length);
5339 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005340 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 }
5342 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005343 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005345
5346 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 status = psa_key_derivation_start_hmac(&hmac,
5348 hash_alg,
5349 tls12_prf->secret,
5350 tls12_prf->secret_length);
5351 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005352 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 }
5354 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5355 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005356 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 }
5358 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5359 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005360 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 }
5362 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5363 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005364 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 }
5366 status = psa_mac_sign_finish(&hmac,
5367 tls12_prf->output_block, hash_length,
5368 &hmac_output_length);
5369 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005370 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005372
Janos Follath7742fee2019-06-17 12:58:10 +01005373
5374cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 cleanup_status = psa_mac_abort(&hmac);
5376 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005377 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005379
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005381}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005382
Janos Follath844eb0e2019-06-19 12:10:49 +01005383static psa_status_t psa_key_derivation_tls12_prf_read(
5384 psa_tls12_prf_key_derivation_t *tls12_prf,
5385 psa_algorithm_t alg,
5386 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005388{
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5390 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005391 psa_status_t status;
5392 uint8_t offset, length;
5393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005395 case PSA_TLS12_PRF_STATE_LABEL_SET:
5396 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5397 break;
5398 case PSA_TLS12_PRF_STATE_OUTPUT:
5399 break;
5400 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005402 }
5403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005405 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 if (tls12_prf->left_in_block == 0) {
5407 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5408 alg);
5409 if (status != PSA_SUCCESS) {
5410 return status;
5411 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005412
5413 continue;
5414 }
5415
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005417 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005419 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005421
5422 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005424 output += length;
5425 output_length -= length;
5426 tls12_prf->left_in_block -= length;
5427 }
5428
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005430}
John Durkop07cc04a2020-11-16 22:08:34 -08005431#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5432 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005433
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005434#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5435static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5436 psa_tls12_ecjpake_to_pms_t *ecjpake,
5437 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005439{
5440 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005441 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005442
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 if (output_length != 32) {
5444 return PSA_ERROR_INVALID_ARGUMENT;
5445 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5448 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5449 &output_size);
5450 if (status != PSA_SUCCESS) {
5451 return status;
5452 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 if (output_size != output_length) {
5455 return PSA_ERROR_GENERIC_ERROR;
5456 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005459}
5460#endif
5461
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005462psa_status_t psa_key_derivation_output_bytes(
5463 psa_key_derivation_operation_t *operation,
5464 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005466{
5467 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005471 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005473 }
5474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005476 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005477 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005478 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005479 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005480 goto exit;
5481 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005483 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005484 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005485 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5486 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005487 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005488 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005490 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005491 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005492
Przemek Stekiel69c46792022-06-10 12:59:51 +02005493#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5495 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5496 output, output_length);
5497 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005498#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005499#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5500 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5502 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5503 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5504 kdf_alg, output,
5505 output_length);
5506 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005507#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5508 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005509#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005511 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5513 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005514#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5515
Gilles Peskineeab56e42018-07-12 17:12:33 +02005516 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005517 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005519 }
5520
5521exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005523 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005524 * This allows us to differentiate between exhausted operations and
5525 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5526 * operations. */
5527 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005529 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005531 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005533}
5534
David Brown7807bf72021-02-09 16:28:23 -07005535#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005536static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005537{
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 if (data_size >= 8) {
5539 mbedtls_des_key_set_parity(data);
5540 }
5541 if (data_size >= 16) {
5542 mbedtls_des_key_set_parity(data + 8);
5543 }
5544 if (data_size >= 24) {
5545 mbedtls_des_key_set_parity(data + 16);
5546 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005547}
David Brown7807bf72021-02-09 16:28:23 -07005548#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005549
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005550/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 * ECC keys on a Weierstrass elliptic curve require the generation
5552 * of a private key which is an integer
5553 * in the range [1, N - 1], where N is the boundary of the private key domain:
5554 * N is the prime p for Diffie-Hellman, or the order of the
5555 * curve’s base point for ECC.
5556 *
5557 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5558 * This function generates the private key using the following process:
5559 *
5560 * 1. Draw a byte string of length ceiling(m/8) bytes.
5561 * 2. If m is not a multiple of 8, set the most significant
5562 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5563 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5564 * 4. If k > N - 2, discard the result and return to step 1.
5565 * 5. Output k + 1 as the private key.
5566 *
5567 * This method allows compliance to NIST standards, specifically the methods titled
5568 * Key-Pair Generation by Testing Candidates in the following publications:
5569 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5570 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5571 * Diffie-Hellman keys.
5572 *
5573 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5574 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5575 *
5576 * Note: Function allocates memory for *data buffer, so given *data should be
5577 * always NULL.
5578 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005579#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5580 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005581 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5582 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5583 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005584static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005585 psa_key_slot_t *slot,
5586 size_t bits,
5587 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005588 uint8_t **data
5589 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005590{
Valerio Setti52789862023-04-07 12:13:11 +02005591#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005592 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005593 mbedtls_mpi k;
5594 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005596 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005597
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 mbedtls_mpi_init(&k);
5599 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005600
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005601 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005603 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005607 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005608 goto cleanup;
5609 }
5610
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005611 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005613
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005615
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005616 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005617 /* Let m be the bit size of N. */
5618 size_t m = ecp_group.nbits;
5619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005621
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005622 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005624
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005625 /* Note: This function is always called with *data == NULL and it
5626 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 *data = mbedtls_calloc(1, m_bytes);
5628 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005629 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005630 goto cleanup;
5631 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005632
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005634 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005636 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005638
5639 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005641 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005642 * (8 * ceiling(m/8) - m) bits of the first byte in
5643 * the string to zero.
5644 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005646 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005647 }
5648
5649 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 * big-endian byte string.
5651 */
5652 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005653
5654 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 * Result of comparison is returned. When it indicates error
5656 * then this function is called again.
5657 */
5658 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005659 }
5660
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005661 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5663 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005664cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005665 if (ret != 0) {
5666 status = mbedtls_to_psa_error(ret);
5667 }
5668 if (status != PSA_SUCCESS) {
5669 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005670 *data = NULL;
5671 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 mbedtls_mpi_free(&k);
5673 mbedtls_mpi_free(&diff_N_2);
5674 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005675#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005676 (void) slot;
5677 (void) bits;
5678 (void) operation;
5679 (void) data;
5680 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005681#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005682}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005683
5684/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5685 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5686 *
5687 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5688 * draw a 32-byte string and process it as specified in
5689 * Elliptic Curves for Security [RFC7748] §5.
5690 *
5691 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5692 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5693 *
5694 * Note: Function allocates memory for *data buffer, so given *data should be
5695 * always NULL.
5696 */
5697
5698static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5699 size_t bits,
5700 psa_key_derivation_operation_t *operation,
5701 uint8_t **data
5702 )
5703{
5704 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005705 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005708 case 255:
5709 output_length = 32;
5710 break;
5711 case 448:
5712 output_length = 56;
5713 break;
5714 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005716 break;
5717 }
5718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 if (*data == NULL) {
5722 return PSA_ERROR_INSUFFICIENT_MEMORY;
5723 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005728 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005732 case 255:
5733 (*data)[0] &= 248;
5734 (*data)[31] &= 127;
5735 (*data)[31] |= 64;
5736 break;
5737 case 448:
5738 (*data)[0] &= 252;
5739 (*data)[55] |= 128;
5740 break;
5741 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005743 break;
5744 }
5745
5746 return status;
5747}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005748#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5749 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005750 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5751 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5752 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005753
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005754static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005755 psa_key_slot_t *slot,
5756 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005758{
5759 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305761 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005762 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5765 return PSA_ERROR_INVALID_ARGUMENT;
5766 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005767
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005768#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5769 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005770 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5771 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5772 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5774 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5775 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005776 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5778 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005779 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 }
5781 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005782 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5784 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005785 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005787 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005788 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005789#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5790 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005791 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5792 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5793 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 if (key_type_is_raw_bytes(slot->attr.type)) {
5795 if (bits % 8 != 0) {
5796 return PSA_ERROR_INVALID_ARGUMENT;
5797 }
5798 data = mbedtls_calloc(1, bytes);
5799 if (data == NULL) {
5800 return PSA_ERROR_INSUFFICIENT_MEMORY;
5801 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 status = psa_key_derivation_output_bytes(operation, data, bytes);
5804 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005805 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 }
David Brown12ca5032021-02-11 11:02:00 -07005807#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5809 psa_des_set_key_parity(data, bytes);
5810 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005811#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 } else {
5813 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005814 }
Ronald Crondd04d422020-11-28 15:14:42 +01005815
Ronald Cronbf33c932020-11-28 18:06:53 +01005816 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005817 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005819 };
5820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5822 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5823 &storage_size);
5824 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305825 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 }
Archanad8a83dc2021-06-14 10:04:16 +05305827 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 status = psa_allocate_buffer_to_slot(slot, storage_size);
5829 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305830 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 }
Archanad8a83dc2021-06-14 10:04:16 +05305832
Przemek Stekiel055ffed2023-05-30 12:51:49 +02005833 status = psa_driver_wrapper_import_key(&attributes,
5834 data, bytes,
5835 slot->key.data,
5836 slot->key.bytes,
5837 &slot->key.bytes, &bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005839 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005841
5842exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 mbedtls_free(data);
5844 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005845}
5846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5848 psa_key_derivation_operation_t *operation,
5849 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005850{
5851 psa_status_t status;
5852 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005853 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005854
Ronald Cron81709fc2020-11-14 12:10:32 +01005855 *key = MBEDTLS_SVC_KEY_ID_INIT;
5856
Gilles Peskine0f84d622019-09-12 19:03:13 +02005857 /* Reject any attempt to create a zero-length key so that we don't
5858 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 if (psa_get_key_bits(attributes) == 0) {
5860 return PSA_ERROR_INVALID_ARGUMENT;
5861 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 if (operation->alg == PSA_ALG_NONE) {
5864 return PSA_ERROR_BAD_STATE;
5865 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 if (!operation->can_output_key) {
5868 return PSA_ERROR_NOT_PERMITTED;
5869 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5872 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005873#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005875 /* Deriving a key in a secure element is not implemented yet. */
5876 status = PSA_ERROR_NOT_SUPPORTED;
5877 }
5878#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 if (status == PSA_SUCCESS) {
5880 status = psa_generate_derived_key_internal(slot,
5881 attributes->core.bits,
5882 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005883 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 if (status == PSA_SUCCESS) {
5885 status = psa_finish_key_creation(slot, driver, key);
5886 }
5887 if (status != PSA_SUCCESS) {
5888 psa_fail_key_creation(slot, driver);
5889 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005892}
5893
Gilles Peskineeab56e42018-07-12 17:12:33 +02005894
5895
5896/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005897/* Key derivation */
5898/****************************************************************/
5899
Steven Cooreman932ffb72021-02-15 12:14:32 +01005900#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005901static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005902{
5903#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5905 return 1;
5906 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005907#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005908#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5910 return 1;
5911 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005912#endif
5913#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5915 return 1;
5916 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005917#endif
5918#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5920 return 1;
5921 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005922#endif
5923#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5925 return 1;
5926 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005927#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005928#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5930 return 1;
5931 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005932#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005934}
5935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005937{
5938 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 psa_status_t status = psa_hash_setup(&operation, alg);
5940 psa_hash_abort(&operation);
5941 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005942}
5943
Gilles Peskine969c5d62019-01-16 15:53:06 +01005944static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005945 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005947{
Janos Follath5fe19732019-06-20 15:09:30 +01005948 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5949 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005951
Gilles Peskine969c5d62019-01-16 15:53:06 +01005952 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 if (!is_kdf_alg_supported(kdf_alg)) {
5954 return PSA_ERROR_NOT_SUPPORTED;
5955 }
John Durkop07cc04a2020-11-16 22:08:34 -08005956
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005957 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005958 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5960 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5961 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5962 if (hash_size == 0) {
5963 return PSA_ERROR_NOT_SUPPORTED;
5964 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005965
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005966 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5967 * we might fail later, which is somewhat unfriendly and potentially
5968 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 psa_status_t status = psa_hash_try_support(hash_alg);
5970 if (status != PSA_SUCCESS) {
5971 return status;
5972 }
5973 } else {
5974 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005975 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5978 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5979 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5980 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005981 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005982#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005983 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5985 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005986 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005988#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5989 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 operation->capacity = 255 * hash_size;
5991 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005992}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005995{
5996#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 if (alg == PSA_ALG_ECDH) {
5998 return PSA_SUCCESS;
5999 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006000#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006001#if defined(PSA_WANT_ALG_FFDH)
6002 if (alg == PSA_ALG_FFDH) {
6003 return PSA_SUCCESS;
6004 }
6005#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006006 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006008}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006009
6010static int psa_key_derivation_allows_free_form_secret_input(
6011 psa_algorithm_t kdf_alg)
6012{
6013#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6014 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6015 return 0;
6016 }
6017#endif
6018 (void) kdf_alg;
6019 return 1;
6020}
John Durkop07cc04a2020-11-16 22:08:34 -08006021#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006022
Gilles Peskine449bd832023-01-11 14:50:10 +01006023psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6024 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006025{
6026 psa_status_t status;
6027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 if (operation->alg != 0) {
6029 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006030 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006031
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6033 return PSA_ERROR_INVALID_ARGUMENT;
6034 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6035#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6036 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6037 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6038 status = psa_key_agreement_try_support(ka_alg);
6039 if (status != PSA_SUCCESS) {
6040 return status;
6041 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006042 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6043 return PSA_ERROR_INVALID_ARGUMENT;
6044 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6046#else
6047 return PSA_ERROR_NOT_SUPPORTED;
6048#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6049 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6050#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6051 status = psa_key_derivation_setup_kdf(operation, alg);
6052#else
6053 return PSA_ERROR_NOT_SUPPORTED;
6054#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6055 } else {
6056 return PSA_ERROR_INVALID_ARGUMENT;
6057 }
6058
6059 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006060 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 }
6062 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006063}
6064
Przemek Stekiel69c46792022-06-10 12:59:51 +02006065#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006066static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6067 psa_algorithm_t kdf_alg,
6068 psa_key_derivation_step_t step,
6069 const uint8_t *data,
6070 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006071{
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006073 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006075 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006076#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6078 return PSA_ERROR_INVALID_ARGUMENT;
6079 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006080#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 if (hkdf->state != HKDF_STATE_INIT) {
6082 return PSA_ERROR_BAD_STATE;
6083 } else {
6084 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6085 hash_alg,
6086 data, data_length);
6087 if (status != PSA_SUCCESS) {
6088 return status;
6089 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006090 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006092 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006093 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006094#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006096 /* We shouldn't be in different state as HKDF_EXPAND only allows
6097 * two inputs: SECRET (this case) and INFO which does not modify
6098 * the state. It could happen only if the hkdf
6099 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 if (hkdf->state != HKDF_STATE_INIT) {
6101 return PSA_ERROR_BAD_STATE;
6102 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006103
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006104 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6106 return PSA_ERROR_INVALID_ARGUMENT;
6107 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 memcpy(hkdf->prk, data, data_length);
6110 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006111#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006112 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006113 /* HKDF: If no salt was provided, use an empty salt.
6114 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006116#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6118 return PSA_ERROR_BAD_STATE;
6119 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006120#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6122 hash_alg,
6123 NULL, 0);
6124 if (status != PSA_SUCCESS) {
6125 return status;
6126 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006127 hkdf->state = HKDF_STATE_STARTED;
6128 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 if (hkdf->state != HKDF_STATE_STARTED) {
6130 return PSA_ERROR_BAD_STATE;
6131 }
6132 status = psa_mac_update(&hkdf->hmac,
6133 data, data_length);
6134 if (status != PSA_SUCCESS) {
6135 return status;
6136 }
6137 status = psa_mac_sign_finish(&hkdf->hmac,
6138 hkdf->prk,
6139 sizeof(hkdf->prk),
6140 &data_length);
6141 if (status != PSA_SUCCESS) {
6142 return status;
6143 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006144 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006145
Gilles Peskine2b522db2019-04-12 15:11:49 +02006146 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006147 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006148#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006150 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006152 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006154#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006155 {
6156 /* Block 0 is empty, and the next block will be
6157 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006159 }
6160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006162 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006163#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6165 return PSA_ERROR_INVALID_ARGUMENT;
6166 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006167#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006168#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6170 hkdf->state == HKDF_STATE_INIT) {
6171 return PSA_ERROR_BAD_STATE;
6172 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006173#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 if (hkdf->state == HKDF_STATE_OUTPUT) {
6175 return PSA_ERROR_BAD_STATE;
6176 }
6177 if (hkdf->info_set) {
6178 return PSA_ERROR_BAD_STATE;
6179 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006180 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 if (data_length != 0) {
6182 hkdf->info = mbedtls_calloc(1, data_length);
6183 if (hkdf->info == NULL) {
6184 return PSA_ERROR_INSUFFICIENT_MEMORY;
6185 }
6186 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006187 }
6188 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006190 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006192 }
6193}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006194#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006195
John Durkop07cc04a2020-11-16 22:08:34 -08006196#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6197 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006198static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6199 const uint8_t *data,
6200 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006201{
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6203 return PSA_ERROR_BAD_STATE;
6204 }
Janos Follathf08e2652019-06-13 09:05:41 +01006205
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 if (data_length != 0) {
6207 prf->seed = mbedtls_calloc(1, data_length);
6208 if (prf->seed == NULL) {
6209 return PSA_ERROR_INSUFFICIENT_MEMORY;
6210 }
Janos Follathf08e2652019-06-13 09:05:41 +01006211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006213 prf->seed_length = data_length;
6214 }
Janos Follathf08e2652019-06-13 09:05:41 +01006215
Gilles Peskine43f958b2020-12-13 14:55:14 +01006216 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006219}
6220
Gilles Peskine449bd832023-01-11 14:50:10 +01006221static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6222 const uint8_t *data,
6223 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006224{
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6226 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6227 return PSA_ERROR_BAD_STATE;
6228 }
Janos Follath81550542019-06-13 14:26:34 +01006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 if (data_length != 0) {
6231 prf->secret = mbedtls_calloc(1, data_length);
6232 if (prf->secret == NULL) {
6233 return PSA_ERROR_INSUFFICIENT_MEMORY;
6234 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006235
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006237 prf->secret_length = data_length;
6238 }
Janos Follath81550542019-06-13 14:26:34 +01006239
Gilles Peskine43f958b2020-12-13 14:55:14 +01006240 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006243}
6244
Gilles Peskine449bd832023-01-11 14:50:10 +01006245static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6246 const uint8_t *data,
6247 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006248{
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6250 return PSA_ERROR_BAD_STATE;
6251 }
Janos Follath63028dd2019-06-13 09:15:47 +01006252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 if (data_length != 0) {
6254 prf->label = mbedtls_calloc(1, data_length);
6255 if (prf->label == NULL) {
6256 return PSA_ERROR_INSUFFICIENT_MEMORY;
6257 }
Janos Follath63028dd2019-06-13 09:15:47 +01006258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006260 prf->label_length = data_length;
6261 }
Janos Follath63028dd2019-06-13 09:15:47 +01006262
Gilles Peskine43f958b2020-12-13 14:55:14 +01006263 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006264
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006266}
6267
Gilles Peskine449bd832023-01-11 14:50:10 +01006268static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6269 psa_key_derivation_step_t step,
6270 const uint8_t *data,
6271 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006272{
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006274 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006276 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006278 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006280 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006282 }
6283}
John Durkop07cc04a2020-11-16 22:08:34 -08006284#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6285 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6286
6287#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6288static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6289 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006290 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006292{
6293 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6295 4 + data_length + prf->other_secret_length :
6296 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6299 return PSA_ERROR_INVALID_ARGUMENT;
6300 }
John Durkop07cc04a2020-11-16 22:08:34 -08006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 uint8_t *pms = mbedtls_calloc(1, pms_len);
6303 if (pms == NULL) {
6304 return PSA_ERROR_INSUFFICIENT_MEMORY;
6305 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006306 uint8_t *cur = pms;
6307
6308 /* pure-PSK:
6309 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006310 *
6311 * The premaster secret is formed as follows: if the PSK is N octets
6312 * long, concatenate a uint16 with the value N, N zero octets, a second
6313 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006314 *
6315 * mixed-PSK:
6316 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6317 * follows: concatenate a uint16 with the length of the other secret,
6318 * the other secret itself, uint16 with the length of PSK, and the
6319 * PSK itself.
6320 * For details please check:
6321 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6322 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6323 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006324 */
6325
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6327 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6328 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6329 if (prf->other_secret_length != 0) {
6330 memcpy(cur, prf->other_secret, prf->other_secret_length);
6331 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006332 cur += prf->other_secret_length;
6333 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 } else {
6335 *cur++ = MBEDTLS_BYTE_1(data_length);
6336 *cur++ = MBEDTLS_BYTE_0(data_length);
6337 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006338 cur += data_length;
6339 }
6340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 *cur++ = MBEDTLS_BYTE_1(data_length);
6342 *cur++ = MBEDTLS_BYTE_0(data_length);
6343 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006344 cur += data_length;
6345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 mbedtls_platform_zeroize(pms, pms_len);
6349 mbedtls_free(pms);
6350 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006351}
Janos Follath6660f0e2019-06-17 08:44:03 +01006352
Przemek Stekiele47201b2022-04-19 13:53:28 +02006353static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6354 psa_tls12_prf_key_derivation_t *prf,
6355 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006357{
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6359 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006360 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006361
6362 if (data_length != 0) {
6363 prf->other_secret = mbedtls_calloc(1, data_length);
6364 if (prf->other_secret == NULL) {
6365 return PSA_ERROR_INSUFFICIENT_MEMORY;
6366 }
6367
6368 memcpy(prf->other_secret, data, data_length);
6369 prf->other_secret_length = data_length;
6370 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006371 prf->other_secret_length = 0;
6372 }
6373
6374 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006377}
6378
Janos Follath6660f0e2019-06-17 08:44:03 +01006379static psa_status_t psa_tls12_prf_psk_to_ms_input(
6380 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006381 psa_key_derivation_step_t step,
6382 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006384{
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006386 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 return psa_tls12_prf_psk_to_ms_set_key(prf,
6388 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006389 break;
6390 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6392 data,
6393 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006394 break;
6395 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006397 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006398
Przemek Stekiele47201b2022-04-19 13:53:28 +02006399 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006400}
John Durkop07cc04a2020-11-16 22:08:34 -08006401#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006402
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006403#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6404static psa_status_t psa_tls12_ecjpake_to_pms_input(
6405 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006406 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006407 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006409{
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6411 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6412 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006413 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006414
6415 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 if (data[0] != 0x04) {
6417 return PSA_ERROR_INVALID_ARGUMENT;
6418 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006419
6420 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006424}
6425#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006426/** Check whether the given key type is acceptable for the given
6427 * input step of a key derivation.
6428 *
6429 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6430 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6431 * Both secret and non-secret inputs can alternatively have the type
6432 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6433 * that the input was passed as a buffer rather than via a key object.
6434 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006435static int psa_key_derivation_check_input_type(
6436 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006438{
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006440 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 if (key_type == PSA_KEY_TYPE_DERIVE) {
6442 return PSA_SUCCESS;
6443 }
6444 if (key_type == PSA_KEY_TYPE_NONE) {
6445 return PSA_SUCCESS;
6446 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006447 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006448 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (key_type == PSA_KEY_TYPE_DERIVE) {
6450 return PSA_SUCCESS;
6451 }
6452 if (key_type == PSA_KEY_TYPE_NONE) {
6453 return PSA_SUCCESS;
6454 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006455 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006456 case PSA_KEY_DERIVATION_INPUT_LABEL:
6457 case PSA_KEY_DERIVATION_INPUT_SALT:
6458 case PSA_KEY_DERIVATION_INPUT_INFO:
6459 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6461 return PSA_SUCCESS;
6462 }
6463 if (key_type == PSA_KEY_TYPE_NONE) {
6464 return PSA_SUCCESS;
6465 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006466 break;
6467 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006469}
6470
Janos Follathb80a94e2019-06-12 15:54:46 +01006471static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006472 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006473 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006474 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006475 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006477{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006478 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006480
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 status = psa_key_derivation_check_input_type(step, key_type);
6482 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006483 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006485
Przemek Stekiel69c46792022-06-10 12:59:51 +02006486#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6488 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6489 step, data, data_length);
6490 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006491#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006492#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6494 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6495 step, data, data_length);
6496 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006497#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6498#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6500 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6501 step, data, data_length);
6502 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006503#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006504#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006506 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6508 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006509#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006510 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006511 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006512 (void) data;
6513 (void) data_length;
6514 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006516 }
6517
Gilles Peskine224b0d62019-09-23 18:13:17 +02006518exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 if (status != PSA_SUCCESS) {
6520 psa_key_derivation_abort(operation);
6521 }
6522 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006523}
6524
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306525static psa_status_t psa_key_derivation_input_integer_internal(
6526 psa_key_derivation_operation_t *operation,
6527 psa_key_derivation_step_t step,
6528 uint64_t value)
6529{
6530 psa_status_t status;
6531 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6532
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306533 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306534 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306535 (void) value;
6536 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306537 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306538 }
6539
6540 if (status != PSA_SUCCESS) {
6541 psa_key_derivation_abort(operation);
6542 }
6543 return status;
6544}
6545
Janos Follath51f4a0f2019-06-14 11:35:55 +01006546psa_status_t psa_key_derivation_input_bytes(
6547 psa_key_derivation_operation_t *operation,
6548 psa_key_derivation_step_t step,
6549 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006551{
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 return psa_key_derivation_input_internal(operation, step,
6553 PSA_KEY_TYPE_NONE,
6554 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006555}
6556
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306557psa_status_t psa_key_derivation_input_integer(
6558 psa_key_derivation_operation_t *operation,
6559 psa_key_derivation_step_t step,
6560 uint64_t value)
6561{
6562 return psa_key_derivation_input_integer_internal(operation, step, value);
6563}
6564
Janos Follath51f4a0f2019-06-14 11:35:55 +01006565psa_status_t psa_key_derivation_input_key(
6566 psa_key_derivation_operation_t *operation,
6567 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006569{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006570 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006571 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006572 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006573
Ronald Cron5c522922020-11-14 16:35:34 +01006574 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6576 if (status != PSA_SUCCESS) {
6577 psa_key_derivation_abort(operation);
6578 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006579 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006580
6581 /* Passing a key object as a SECRET input unlocks the permission
6582 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006584 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006586
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 status = psa_key_derivation_input_internal(operation,
6588 step, slot->attr.type,
6589 slot->key.data,
6590 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006591
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006593
Gilles Peskine449bd832023-01-11 14:50:10 +01006594 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006595}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006596
6597
6598
6599/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006600/* Key agreement */
6601/****************************************************************/
6602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6604 const uint8_t *key_buffer,
6605 size_t key_buffer_size,
6606 psa_algorithm_t alg,
6607 const uint8_t *peer_key,
6608 size_t peer_key_length,
6609 uint8_t *shared_secret,
6610 size_t shared_secret_size,
6611 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006612{
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006614#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006615 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6617 key_buffer_size, alg,
6618 peer_key, peer_key_length,
6619 shared_secret,
6620 shared_secret_size,
6621 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006622#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006623
6624#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6625 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006626 return mbedtls_psa_key_agreement_ffdh(attributes,
6627 peer_key,
6628 peer_key_length,
6629 key_buffer,
6630 key_buffer_size,
6631 shared_secret,
6632 shared_secret_size,
6633 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006634#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6635
Gilles Peskine01d718c2018-09-18 12:01:02 +02006636 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006637 (void) attributes;
6638 (void) key_buffer;
6639 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006640 (void) peer_key;
6641 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006642 (void) shared_secret;
6643 (void) shared_secret_size;
6644 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006646 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006647}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006648
Aditya Deshpande17845b82022-10-13 17:21:01 +01006649/** Internal function for raw key agreement
6650 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006651 * to the driver's implementation if a driver is present.
6652 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006653 * (psa_key_agreement_raw_builtin).
6654 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006655static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6656 psa_key_slot_t *private_key,
6657 const uint8_t *peer_key,
6658 size_t peer_key_length,
6659 uint8_t *shared_secret,
6660 size_t shared_secret_size,
6661 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006662{
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6664 return PSA_ERROR_NOT_SUPPORTED;
6665 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006666
6667 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006669 };
6670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 return psa_driver_wrapper_key_agreement(&attributes,
6672 private_key->key.data,
6673 private_key->key.bytes, alg,
6674 peer_key, peer_key_length,
6675 shared_secret,
6676 shared_secret_size,
6677 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006678}
6679
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006680/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006681 * to potentially free embedded data structures and wipe confidential data.
6682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006683static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6684 psa_key_derivation_step_t step,
6685 psa_key_slot_t *private_key,
6686 const uint8_t *peer_key,
6687 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006688{
6689 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006690 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006691 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006693
6694 /* Step 1: run the secret agreement algorithm to generate the shared
6695 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 status = psa_key_agreement_raw_internal(ka_alg,
6697 private_key,
6698 peer_key, peer_key_length,
6699 shared_secret,
6700 sizeof(shared_secret),
6701 &shared_secret_length);
6702 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006703 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006705
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006706 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006707 * the shared secret. A shared secret is permitted wherever a key
6708 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 status = psa_key_derivation_input_internal(operation, step,
6710 PSA_KEY_TYPE_DERIVE,
6711 shared_secret,
6712 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006713exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6715 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006716}
6717
Gilles Peskine449bd832023-01-11 14:50:10 +01006718psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6719 psa_key_derivation_step_t step,
6720 mbedtls_svc_key_id_t private_key,
6721 const uint8_t *peer_key,
6722 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006723{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006724 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006725 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006726 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006727
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6729 return PSA_ERROR_INVALID_ARGUMENT;
6730 }
Ronald Cron5c522922020-11-14 16:35:34 +01006731 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6733 if (status != PSA_SUCCESS) {
6734 return status;
6735 }
6736 status = psa_key_agreement_internal(operation, step,
6737 slot,
6738 peer_key, peer_key_length);
6739 if (status != PSA_SUCCESS) {
6740 psa_key_derivation_abort(operation);
6741 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006742 /* If a private key has been added as SECRET, we allow the derived
6743 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006745 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006747 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006748
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006750
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006752}
6753
Gilles Peskine449bd832023-01-11 14:50:10 +01006754psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6755 mbedtls_svc_key_id_t private_key,
6756 const uint8_t *peer_key,
6757 size_t peer_key_length,
6758 uint8_t *output,
6759 size_t output_size,
6760 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006761{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006762 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006763 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006764 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006765
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006767 status = PSA_ERROR_INVALID_ARGUMENT;
6768 goto exit;
6769 }
Ronald Cron5c522922020-11-14 16:35:34 +01006770 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6772 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006773 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006775
Gilles Peskine3e561302022-04-14 00:17:15 +02006776 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6777 * for the output size. The PSA specification only guarantees that this
6778 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6779 * but it might be nice to allow smaller buffers if the output fits.
6780 * At the time of writing this comment, with only ECDH implemented,
6781 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6782 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6783 * be exact for it as well. */
6784 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6786 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006787 status = PSA_ERROR_BUFFER_TOO_SMALL;
6788 goto exit;
6789 }
6790
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 status = psa_key_agreement_raw_internal(alg, slot,
6792 peer_key, peer_key_length,
6793 output, output_size,
6794 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006795
6796exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006798 /* If an error happens and is not handled properly, the output
6799 * may be used as a key to protect sensitive data. Arrange for such
6800 * a key to be random, which is likely to result in decryption or
6801 * verification errors. This is better than filling the buffer with
6802 * some constant data such as zeros, which would result in the data
6803 * being protected with a reproducible, easily knowable key.
6804 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006806 *output_length = output_size;
6807 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006808
Gilles Peskine449bd832023-01-11 14:50:10 +01006809 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006810
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006812}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006813
6814
Gilles Peskine30524eb2020-11-13 17:02:26 +01006815
Gilles Peskine86a440b2018-11-12 18:39:40 +01006816/****************************************************************/
6817/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006818/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006819
Gilles Peskine30524eb2020-11-13 17:02:26 +01006820/** Initialize the PSA random generator.
6821 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006822static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006823{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006824#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006826#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6827
Gilles Peskine30524eb2020-11-13 17:02:26 +01006828 /* Set default configuration if
6829 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006831 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 }
6833 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006834 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006835 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006836
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006838#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6839 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6840 /* The PSA entropy injection feature depends on using NV seed as an entropy
6841 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 mbedtls_entropy_add_source(&rng->entropy,
6843 mbedtls_nv_seed_poll, NULL,
6844 MBEDTLS_ENTROPY_BLOCK_SIZE,
6845 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006846#endif
6847
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006849#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006850}
6851
6852/** Deinitialize the PSA random generator.
6853 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006854static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006855{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006856#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006858#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6860 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006861#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006862}
6863
6864/** Seed the PSA random generator.
6865 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006866static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006867{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006868#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6869 /* Do nothing: the external RNG seeds itself. */
6870 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006872#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006873 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6875 drbg_seed, sizeof(drbg_seed) - 1);
6876 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006877#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006878}
6879
Gilles Peskine449bd832023-01-11 14:50:10 +01006880psa_status_t psa_generate_random(uint8_t *output,
6881 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006882{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006883 GUARD_MODULE_INITIALIZED;
6884
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006885#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6886
6887 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6889 output, output_size,
6890 &output_length);
6891 if (status != PSA_SUCCESS) {
6892 return status;
6893 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006894 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006895 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 if (output_length != output_size) {
6897 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6898 }
6899 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006900
6901#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6902
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006904 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6906 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6907 output_size);
6908 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6909 output, request_size);
6910 if (ret != 0) {
6911 return mbedtls_to_psa_error(ret);
6912 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006913 output_size -= request_size;
6914 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006915 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006916 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006917#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006918}
6919
Gilles Peskine8814fc42020-12-14 15:33:44 +01006920/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006921 *
6922 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6923 * `psa_generate_random(...)`. The state parameter is ignored since the
6924 * PSA API doesn't support passing an explicit state.
6925 *
6926 * In the non-external case, psa_generate_random() calls an
6927 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6928 * and semantics as mbedtls_psa_get_random(). As an optimization,
6929 * instead of doing this back-and-forth between the PSA API and the
6930 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6931 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006932 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006933#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6934int mbedtls_psa_get_random(void *p_rng,
6935 unsigned char *output,
6936 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006937{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006938 /* This function takes a pointer to the RNG state because that's what
6939 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6940 * its own state internally and doesn't let the caller access that state.
6941 * So we just ignore the state parameter, and in practice we'll pass
6942 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006943 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 psa_status_t status = psa_generate_random(output, output_size);
6945 if (status == PSA_SUCCESS) {
6946 return 0;
6947 } else {
6948 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6949 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006950}
6951#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6952
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006953#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006954#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006955
Gilles Peskine449bd832023-01-11 14:50:10 +01006956psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6957 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006958{
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 if (global_data.initialized) {
6960 return PSA_ERROR_NOT_PERMITTED;
6961 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006962
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6964 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6965 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6966 return PSA_ERROR_INVALID_ARGUMENT;
6967 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006968
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006970}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006971#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006972
Ronald Cron3772afe2021-02-08 16:10:05 +01006973/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006974 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006975 * \param type The key type
6976 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006977 *
6978 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006979 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006980 * \retval #PSA_ERROR_INVALID_ARGUMENT
6981 * The size in bits of the key is not valid.
6982 * \retval #PSA_ERROR_NOT_SUPPORTED
6983 * The type and/or the size in bits of the key or the combination of
6984 * the two is not supported.
6985 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006986static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006988{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006989 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006990
Gilles Peskine449bd832023-01-11 14:50:10 +01006991 if (key_type_is_raw_bytes(type)) {
6992 status = psa_validate_unstructured_key_bit_size(type, bits);
6993 if (status != PSA_SUCCESS) {
6994 return status;
6995 }
6996 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006997#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006998 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6999 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7000 return PSA_ERROR_NOT_SUPPORTED;
7001 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007002
Ronald Cron01b2aba2020-10-05 09:42:02 +02007003 /* Accept only byte-aligned keys, for the same reasons as
7004 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 if (bits % 8 != 0) {
7006 return PSA_ERROR_NOT_SUPPORTED;
7007 }
7008 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007009#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007010
Ronald Cron5c4d3862020-12-07 11:07:24 +01007011#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007013 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 return PSA_SUCCESS;
7015 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007016#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007017
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007018#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007019 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007020 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007021 return PSA_ERROR_NOT_SUPPORTED;
7022 }
7023 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007024#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007025 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007027 }
7028
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007030}
7031
Ronald Cron55ed0592020-10-05 10:30:40 +02007032psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007033 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007035{
7036 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007037 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007038
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 if ((attributes->domain_parameters == NULL) &&
7040 (attributes->domain_parameters_size != 0)) {
7041 return PSA_ERROR_INVALID_ARGUMENT;
7042 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007043
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 if (key_type_is_raw_bytes(type)) {
7045 status = psa_generate_random(key_buffer, key_buffer_size);
7046 if (status != PSA_SUCCESS) {
7047 return status;
7048 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007049
David Brown12ca5032021-02-11 11:02:00 -07007050#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 if (type == PSA_KEY_TYPE_DES) {
7052 psa_des_set_key_parity(key_buffer, key_buffer_size);
7053 }
David Brown8107e312021-02-12 08:08:46 -07007054#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007056
Jaeden Amero424fa932021-05-14 08:34:32 +01007057#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7058 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7060 return mbedtls_psa_rsa_generate_key(attributes,
7061 key_buffer,
7062 key_buffer_size,
7063 key_buffer_length);
7064 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007065#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7066 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007067
John Durkop9814fa22020-11-04 12:28:15 -08007068#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7070 return mbedtls_psa_ecp_generate_key(attributes,
7071 key_buffer,
7072 key_buffer_size,
7073 key_buffer_length);
7074 } else
John Durkop9814fa22020-11-04 12:28:15 -08007075#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007076
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007077#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007078 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7079 return mbedtls_psa_ffdh_generate_key(attributes,
7080 key_buffer,
7081 key_buffer_size,
7082 key_buffer_length);
7083 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007084#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007085 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 (void) key_buffer_length;
7087 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007088 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007089
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007091}
Darryl Green0c6575a2018-11-07 16:05:30 +00007092
Gilles Peskine449bd832023-01-11 14:50:10 +01007093psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7094 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007095{
7096 psa_status_t status;
7097 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007098 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007099 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007100
Ronald Cron81709fc2020-11-14 12:10:32 +01007101 *key = MBEDTLS_SVC_KEY_ID_INIT;
7102
Gilles Peskine0f84d622019-09-12 19:03:13 +02007103 /* Reject any attempt to create a zero-length key so that we don't
7104 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 if (psa_get_key_bits(attributes) == 0) {
7106 return PSA_ERROR_INVALID_ARGUMENT;
7107 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007108
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007109 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7111 return PSA_ERROR_INVALID_ARGUMENT;
7112 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007113
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7115 &slot, &driver);
7116 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 }
Gilles Peskine11792082019-08-06 18:36:36 +02007119
Ronald Cron977c2472020-10-13 08:32:21 +02007120 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307121 * storage ( thus not in the case of generating a key in a secure element
7122 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7123 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007124 if (slot->key.data == NULL) {
7125 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7126 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007127 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 attributes->core.type, attributes->core.bits);
7129 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007132
7133 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 attributes->core.type,
7135 attributes->core.bits);
7136 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007137 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 attributes, &key_buffer_size);
7139 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 }
Ronald Cron977c2472020-10-13 08:32:21 +02007142 }
Ronald Cron977c2472020-10-13 08:32:21 +02007143
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7145 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 }
Ronald Cron977c2472020-10-13 08:32:21 +02007148 }
7149
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 status = psa_driver_wrapper_generate_key(attributes,
7151 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007152
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 if (status != PSA_SUCCESS) {
7154 psa_remove_key_data_from_memory(slot);
7155 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007156
Gilles Peskine11792082019-08-06 18:36:36 +02007157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 if (status == PSA_SUCCESS) {
7159 status = psa_finish_key_creation(slot, driver, key);
7160 }
7161 if (status != PSA_SUCCESS) {
7162 psa_fail_key_creation(slot, driver);
7163 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007164
Gilles Peskine449bd832023-01-11 14:50:10 +01007165 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007166}
7167
Gilles Peskinee59236f2018-01-27 23:32:46 +01007168/****************************************************************/
7169/* Module setup */
7170/****************************************************************/
7171
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007172#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007173psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 void (* entropy_init)(mbedtls_entropy_context *ctx),
7175 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007176{
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7178 return PSA_ERROR_BAD_STATE;
7179 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007180 global_data.rng.entropy_init = entropy_init;
7181 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007182 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007183}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007184#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007185
Gilles Peskine449bd832023-01-11 14:50:10 +01007186void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007187{
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 psa_wipe_all_key_slots();
7189 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7190 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007191 }
7192 /* Wipe all remaining data, including configuration.
7193 * In particular, this sets all state indicator to the value
7194 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007196
7197 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007198 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007199}
7200
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007201#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7202/** Recover a transaction that was interrupted by a power failure.
7203 *
7204 * This function is called during initialization, before psa_crypto_init()
7205 * returns. If this function returns a failure status, the initialization
7206 * fails.
7207 */
7208static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007210{
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007212 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7213 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 /* TODO - fall through to the failure case until this
7215 * is implemented.
7216 * https://github.com/ARMmbed/mbed-crypto/issues/218
7217 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007218 default:
7219 /* We found an unsupported transaction in the storage.
7220 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007222 }
7223}
7224#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7225
Gilles Peskine449bd832023-01-11 14:50:10 +01007226psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007227{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007228 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007229
Gilles Peskinec6b69072018-11-20 21:42:52 +01007230 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 if (global_data.initialized != 0) {
7232 return PSA_SUCCESS;
7233 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007234
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007235 /* Init drivers */
7236 status = psa_driver_wrapper_init();
7237 if (status != PSA_SUCCESS) {
7238 goto exit;
7239 }
7240 global_data.drivers_initialized = 1;
7241
Gilles Peskine30524eb2020-11-13 17:02:26 +01007242 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007243 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007244 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007245 status = mbedtls_psa_random_seed(&global_data.rng);
7246 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007247 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007249 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007250
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 status = psa_initialize_key_slots();
7252 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007253 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007255
Gilles Peskine4b734222019-07-24 15:56:31 +02007256#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 status = psa_crypto_load_transaction();
7258 if (status == PSA_SUCCESS) {
7259 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7260 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007261 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 }
7263 status = psa_crypto_stop_transaction();
7264 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007265 /* There's no transaction to complete. It's all good. */
7266 status = PSA_SUCCESS;
7267 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007268#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007269
Gilles Peskinec6b69072018-11-20 21:42:52 +01007270 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007271 global_data.initialized = 1;
7272
7273exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 if (status != PSA_SUCCESS) {
7275 mbedtls_psa_crypto_free();
7276 }
7277 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007278}
7279
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007280psa_status_t psa_crypto_driver_pake_get_password_len(
7281 const psa_crypto_driver_pake_inputs_t *inputs,
7282 size_t *password_len)
7283{
7284 if (inputs->password_len == 0) {
7285 return PSA_ERROR_BAD_STATE;
7286 }
7287
7288 *password_len = inputs->password_len;
7289
7290 return PSA_SUCCESS;
7291}
7292
7293psa_status_t psa_crypto_driver_pake_get_password(
7294 const psa_crypto_driver_pake_inputs_t *inputs,
7295 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7296{
7297 if (inputs->password_len == 0) {
7298 return PSA_ERROR_BAD_STATE;
7299 }
7300
7301 if (buffer_size < inputs->password_len) {
7302 return PSA_ERROR_BUFFER_TOO_SMALL;
7303 }
7304
7305 memcpy(buffer, inputs->password, inputs->password_len);
7306 *buffer_length = inputs->password_len;
7307
7308 return PSA_SUCCESS;
7309}
7310
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007311psa_status_t psa_crypto_driver_pake_get_user_len(
7312 const psa_crypto_driver_pake_inputs_t *inputs,
7313 size_t *user_len)
7314{
7315 if (inputs->user_len == 0) {
7316 return PSA_ERROR_BAD_STATE;
7317 }
7318
7319 *user_len = inputs->user_len;
7320
7321 return PSA_SUCCESS;
7322}
7323
7324psa_status_t psa_crypto_driver_pake_get_user(
7325 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007326 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007327{
7328 if (inputs->user_len == 0) {
7329 return PSA_ERROR_BAD_STATE;
7330 }
7331
Przemek Stekielc0e62502023-03-14 11:49:36 +01007332 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007333 return PSA_ERROR_BUFFER_TOO_SMALL;
7334 }
7335
Przemek Stekielc0e62502023-03-14 11:49:36 +01007336 memcpy(user_id, inputs->user, inputs->user_len);
7337 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007338
7339 return PSA_SUCCESS;
7340}
7341
7342psa_status_t psa_crypto_driver_pake_get_peer_len(
7343 const psa_crypto_driver_pake_inputs_t *inputs,
7344 size_t *peer_len)
7345{
7346 if (inputs->peer_len == 0) {
7347 return PSA_ERROR_BAD_STATE;
7348 }
7349
7350 *peer_len = inputs->peer_len;
7351
7352 return PSA_SUCCESS;
7353}
7354
7355psa_status_t psa_crypto_driver_pake_get_peer(
7356 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007357 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007358{
7359 if (inputs->peer_len == 0) {
7360 return PSA_ERROR_BAD_STATE;
7361 }
7362
Przemek Stekielc0e62502023-03-14 11:49:36 +01007363 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007364 return PSA_ERROR_BUFFER_TOO_SMALL;
7365 }
7366
Przemek Stekielc0e62502023-03-14 11:49:36 +01007367 memcpy(peer_id, inputs->peer, inputs->peer_len);
7368 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007369
7370 return PSA_SUCCESS;
7371}
7372
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007373psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7374 const psa_crypto_driver_pake_inputs_t *inputs,
7375 psa_pake_cipher_suite_t *cipher_suite)
7376{
7377 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7378 return PSA_ERROR_BAD_STATE;
7379 }
7380
7381 *cipher_suite = inputs->cipher_suite;
7382
7383 return PSA_SUCCESS;
7384}
7385
Neil Armstronga7d08c32022-06-01 18:21:20 +02007386psa_status_t psa_pake_setup(
7387 psa_pake_operation_t *operation,
7388 const psa_pake_cipher_suite_t *cipher_suite)
7389{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007390 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007391
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007392 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007393 status = PSA_ERROR_BAD_STATE;
7394 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007395 }
7396
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007397 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007398 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007399 status = PSA_ERROR_INVALID_ARGUMENT;
7400 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007401 }
7402
Przemek Stekiel51eac532022-12-07 11:04:51 +01007403 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7404
Przemek Stekiele12ed362022-12-21 12:54:46 +01007405 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007406 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7407 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007408 operation->data.inputs.cipher_suite = *cipher_suite;
7409
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007410#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007411 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007412 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007413 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007414
Przemek Stekiele12ed362022-12-21 12:54:46 +01007415 computation_stage->state = PSA_PAKE_STATE_SETUP;
7416 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7417 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7418 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007419 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007420#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007421 {
7422 status = PSA_ERROR_NOT_SUPPORTED;
7423 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007424 }
7425
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007426 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7427
Przemek Stekiel51eac532022-12-07 11:04:51 +01007428 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007429exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007430 psa_pake_abort(operation);
7431 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007432}
7433
7434psa_status_t psa_pake_set_password_key(
7435 psa_pake_operation_t *operation,
7436 mbedtls_svc_key_id_t password)
7437{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007438 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007439 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7440 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007441
Przemek Stekiel51eac532022-12-07 11:04:51 +01007442 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007443 status = PSA_ERROR_BAD_STATE;
7444 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007445 }
7446
Przemek Stekiel6c764412022-11-22 14:05:12 +01007447 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7448 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007449 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007450 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007451 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007452 }
7453
Przemek Stekiel6c764412022-11-22 14:05:12 +01007454 psa_key_attributes_t attributes = {
7455 .core = slot->attr
7456 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007457
Przemek Stekiel51eac532022-12-07 11:04:51 +01007458 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007459
Przemek Stekiel51eac532022-12-07 11:04:51 +01007460 if (type != PSA_KEY_TYPE_PASSWORD &&
7461 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7462 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007463 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007464 }
7465
Przemek Stekiel51eac532022-12-07 11:04:51 +01007466 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7467 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007468 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007469 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007470 }
7471
7472 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7473 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007474 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007475exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007476 if (status != PSA_SUCCESS) {
7477 psa_pake_abort(operation);
7478 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007479 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007480 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007481}
7482
7483psa_status_t psa_pake_set_user(
7484 psa_pake_operation_t *operation,
7485 const uint8_t *user_id,
7486 size_t user_id_len)
7487{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007488 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007489
7490 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007491 status = PSA_ERROR_BAD_STATE;
7492 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007493 }
7494
Przemek Stekiel51eac532022-12-07 11:04:51 +01007495 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007496 status = PSA_ERROR_INVALID_ARGUMENT;
7497 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007498 }
7499
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007500 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007501 status = PSA_ERROR_BAD_STATE;
7502 goto exit;
7503 }
7504
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007505 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7506 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007507 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7508 goto exit;
7509 }
7510
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007511 memcpy(operation->data.inputs.user, user_id, user_id_len);
7512 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007513
7514 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007515exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007516 psa_pake_abort(operation);
7517 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007518}
7519
7520psa_status_t psa_pake_set_peer(
7521 psa_pake_operation_t *operation,
7522 const uint8_t *peer_id,
7523 size_t peer_id_len)
7524{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007525 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007526
7527 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007528 status = PSA_ERROR_BAD_STATE;
7529 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007530 }
7531
Przemek Stekiel51eac532022-12-07 11:04:51 +01007532 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007533 status = PSA_ERROR_INVALID_ARGUMENT;
7534 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007535 }
7536
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007537 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007538 status = PSA_ERROR_BAD_STATE;
7539 goto exit;
7540 }
7541
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007542 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7543 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007544 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7545 goto exit;
7546 }
7547
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007548 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7549 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007550
7551 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007552exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007553 psa_pake_abort(operation);
7554 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007555}
7556
7557psa_status_t psa_pake_set_role(
7558 psa_pake_operation_t *operation,
7559 psa_pake_role_t role)
7560{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007561 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007562
Przemek Stekiel51eac532022-12-07 11:04:51 +01007563 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007564 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007565 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007566 }
7567
Przemek Stekiel09104b82023-03-06 14:11:51 +01007568 switch (operation->alg) {
7569#if defined(PSA_WANT_ALG_JPAKE)
7570 case PSA_ALG_JPAKE:
7571 if (role == PSA_PAKE_ROLE_NONE) {
7572 return PSA_SUCCESS;
7573 }
7574 status = PSA_ERROR_INVALID_ARGUMENT;
7575 break;
7576#endif
7577 default:
7578 (void) role;
7579 status = PSA_ERROR_NOT_SUPPORTED;
7580 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007581 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007582exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007583 psa_pake_abort(operation);
7584 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007585}
7586
Przemek Stekielb09c4872023-01-17 12:05:38 +01007587/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007588#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007589static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007590 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007591{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007592 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007593 case PSA_PAKE_OUTPUT_X1_X2:
7594 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007595 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007596 case PSA_PAKE_X1_STEP_KEY_SHARE:
7597 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007598 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7599 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007600 case PSA_PAKE_X1_STEP_ZK_PROOF:
7601 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007602 case PSA_PAKE_X2_STEP_KEY_SHARE:
7603 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007604 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7605 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007606 case PSA_PAKE_X2_STEP_ZK_PROOF:
7607 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007608 default:
7609 return PSA_JPAKE_STEP_INVALID;
7610 }
7611 break;
7612 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007613 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007614 case PSA_PAKE_X1_STEP_KEY_SHARE:
7615 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007616 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7617 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007618 case PSA_PAKE_X1_STEP_ZK_PROOF:
7619 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007620 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007621 return PSA_JPAKE_STEP_INVALID;
7622 }
7623 break;
7624 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007625 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007626 case PSA_PAKE_X1_STEP_KEY_SHARE:
7627 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007628 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7629 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007630 case PSA_PAKE_X1_STEP_ZK_PROOF:
7631 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007632 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007633 return PSA_JPAKE_STEP_INVALID;
7634 }
7635 break;
7636 default:
7637 return PSA_JPAKE_STEP_INVALID;
7638 }
7639 return PSA_JPAKE_STEP_INVALID;
7640}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007641#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007642
Przemek Stekiel2797d372022-12-22 11:19:22 +01007643static psa_status_t psa_pake_complete_inputs(
7644 psa_pake_operation_t *operation)
7645{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007646 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007647 /* Create copy of the inputs on stack as inputs share memory
7648 with the driver context which will be setup by the driver. */
7649 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007650
Przemek Stekielfde11282023-03-13 16:06:09 +01007651 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007652 return PSA_ERROR_BAD_STATE;
7653 }
7654
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007655 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007656 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7657 return PSA_ERROR_BAD_STATE;
7658 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007659 }
7660
Przemek Stekiel18620a32023-01-17 16:34:52 +01007661 /* Clear driver context */
7662 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7663
7664 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007665
7666 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007667 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7668 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007669
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007670 /* User and peer are translated to role. */
7671 mbedtls_free(inputs.user);
7672 mbedtls_free(inputs.peer);
7673
Przemek Stekiel2797d372022-12-22 11:19:22 +01007674 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007675#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007676 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007677 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007678 psa_jpake_computation_stage_t *computation_stage =
7679 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007680 computation_stage->state = PSA_PAKE_STATE_READY;
7681 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7682 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7683 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007684 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007685#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007686 {
7687 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007688 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007689 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007690 return status;
7691}
7692
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007693#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007694static psa_status_t psa_jpake_output_prologue(
7695 psa_pake_operation_t *operation,
7696 psa_pake_step_t step)
7697{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007698 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7699 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7700 step != PSA_PAKE_STEP_ZK_PROOF) {
7701 return PSA_ERROR_INVALID_ARGUMENT;
7702 }
7703
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007704 psa_jpake_computation_stage_t *computation_stage =
7705 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007706
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007707 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7708 return PSA_ERROR_BAD_STATE;
7709 }
7710
7711 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7712 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7713 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7714 return PSA_ERROR_BAD_STATE;
7715 }
7716
7717 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7718 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007719 return PSA_ERROR_BAD_STATE;
7720 }
7721
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007722 switch (computation_stage->output_step) {
7723 case PSA_PAKE_STEP_X1_X2:
7724 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007725 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007726 case PSA_PAKE_STEP_X2S:
7727 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007728 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007729 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007730 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007731 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007732
7733 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7734 }
7735
7736 /* Check if step matches current sequence */
7737 switch (computation_stage->sequence) {
7738 case PSA_PAKE_X1_STEP_KEY_SHARE:
7739 case PSA_PAKE_X2_STEP_KEY_SHARE:
7740 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7741 return PSA_ERROR_BAD_STATE;
7742 }
7743 break;
7744
7745 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7746 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7747 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7748 return PSA_ERROR_BAD_STATE;
7749 }
7750 break;
7751
7752 case PSA_PAKE_X1_STEP_ZK_PROOF:
7753 case PSA_PAKE_X2_STEP_ZK_PROOF:
7754 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7755 return PSA_ERROR_BAD_STATE;
7756 }
7757 break;
7758
7759 default:
7760 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007761 }
7762
7763 return PSA_SUCCESS;
7764}
7765
7766static psa_status_t psa_jpake_output_epilogue(
7767 psa_pake_operation_t *operation)
7768{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007769 psa_jpake_computation_stage_t *computation_stage =
7770 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007771
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007772 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007773 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007774 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007775 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007776 computation_stage->state = PSA_PAKE_STATE_READY;
7777 computation_stage->output_step++;
7778 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7779 } else {
7780 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007781 }
7782
7783 return PSA_SUCCESS;
7784}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007785#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007786
Neil Armstronga7d08c32022-06-01 18:21:20 +02007787psa_status_t psa_pake_output(
7788 psa_pake_operation_t *operation,
7789 psa_pake_step_t step,
7790 uint8_t *output,
7791 size_t output_size,
7792 size_t *output_length)
7793{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007795 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007796 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007797
7798 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007799 status = psa_pake_complete_inputs(operation);
7800 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007801 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007802 }
7803 }
7804
7805 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007806 status = PSA_ERROR_BAD_STATE;
7807 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007808 }
7809
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007810 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007811 status = PSA_ERROR_INVALID_ARGUMENT;
7812 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007813 }
7814
Przemek Stekiele12ed362022-12-21 12:54:46 +01007815 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007816#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007817 case PSA_ALG_JPAKE:
7818 status = psa_jpake_output_prologue(operation, step);
7819 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007820 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007821 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007822 driver_step = convert_jpake_computation_stage_to_driver_step(
7823 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007824 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007825#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007826 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007827 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007828 status = PSA_ERROR_NOT_SUPPORTED;
7829 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007830 }
7831
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007832 status = psa_driver_wrapper_pake_output(operation, driver_step,
7833 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007834
7835 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007836 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007837 }
7838
7839 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007840#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007841 case PSA_ALG_JPAKE:
7842 status = psa_jpake_output_epilogue(operation);
7843 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007844 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007845 }
7846 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007847#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007848 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007849 status = PSA_ERROR_NOT_SUPPORTED;
7850 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007851 }
7852
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007853 return PSA_SUCCESS;
7854exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007855 psa_pake_abort(operation);
7856 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007857}
7858
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007859#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007860static psa_status_t psa_jpake_input_prologue(
7861 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007862 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007863{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007864 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7865 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7866 step != PSA_PAKE_STEP_ZK_PROOF) {
7867 return PSA_ERROR_INVALID_ARGUMENT;
7868 }
7869
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007870 psa_jpake_computation_stage_t *computation_stage =
7871 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007872
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007873 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7874 return PSA_ERROR_BAD_STATE;
7875 }
7876
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007877 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7878 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7879 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7880 return PSA_ERROR_BAD_STATE;
7881 }
7882
7883 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7884 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007885 return PSA_ERROR_BAD_STATE;
7886 }
7887
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007888 switch (computation_stage->input_step) {
7889 case PSA_PAKE_STEP_X1_X2:
7890 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007891 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007892 case PSA_PAKE_STEP_X2S:
7893 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007894 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007895 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007896 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007897 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007898
7899 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7900 }
7901
7902 /* Check if step matches current sequence */
7903 switch (computation_stage->sequence) {
7904 case PSA_PAKE_X1_STEP_KEY_SHARE:
7905 case PSA_PAKE_X2_STEP_KEY_SHARE:
7906 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7907 return PSA_ERROR_BAD_STATE;
7908 }
7909 break;
7910
7911 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7912 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7913 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7914 return PSA_ERROR_BAD_STATE;
7915 }
7916 break;
7917
7918 case PSA_PAKE_X1_STEP_ZK_PROOF:
7919 case PSA_PAKE_X2_STEP_ZK_PROOF:
7920 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7921 return PSA_ERROR_BAD_STATE;
7922 }
7923 break;
7924
7925 default:
7926 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007927 }
7928
7929 return PSA_SUCCESS;
7930}
7931
Przemek Stekiele12ed362022-12-21 12:54:46 +01007932static psa_status_t psa_jpake_input_epilogue(
7933 psa_pake_operation_t *operation)
7934{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007935 psa_jpake_computation_stage_t *computation_stage =
7936 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007937
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007938 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007939 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007940 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007941 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007942 computation_stage->state = PSA_PAKE_STATE_READY;
7943 computation_stage->input_step++;
7944 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7945 } else {
7946 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007947 }
7948
7949 return PSA_SUCCESS;
7950}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007951#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007952
Neil Armstronga7d08c32022-06-01 18:21:20 +02007953psa_status_t psa_pake_input(
7954 psa_pake_operation_t *operation,
7955 psa_pake_step_t step,
7956 const uint8_t *input,
7957 size_t input_length)
7958{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007960 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007961 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
7962 operation->primitive,
7963 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007964
7965 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007966 status = psa_pake_complete_inputs(operation);
7967 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007968 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007969 }
7970 }
7971
7972 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007973 status = PSA_ERROR_BAD_STATE;
7974 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007975 }
7976
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007977 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007978 status = PSA_ERROR_INVALID_ARGUMENT;
7979 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007980 }
7981
Przemek Stekiele12ed362022-12-21 12:54:46 +01007982 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007983#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007984 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007985 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007986 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007987 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007988 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007989 driver_step = convert_jpake_computation_stage_to_driver_step(
7990 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007991 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007992#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007993 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007994 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007995 status = PSA_ERROR_NOT_SUPPORTED;
7996 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007997 }
7998
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007999 status = psa_driver_wrapper_pake_input(operation, driver_step,
8000 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008001
8002 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008003 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008004 }
8005
8006 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008007#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008008 case PSA_ALG_JPAKE:
8009 status = psa_jpake_input_epilogue(operation);
8010 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008011 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008012 }
8013 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008014#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008015 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008016 status = PSA_ERROR_NOT_SUPPORTED;
8017 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008018 }
8019
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008020 return PSA_SUCCESS;
8021exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008022 psa_pake_abort(operation);
8023 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008024}
8025
8026psa_status_t psa_pake_get_implicit_key(
8027 psa_pake_operation_t *operation,
8028 psa_key_derivation_operation_t *output)
8029{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008030 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008031 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008032 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008033 size_t shared_key_len = 0;
8034
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008035 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008036 status = PSA_ERROR_BAD_STATE;
8037 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008038 }
8039
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008040#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008041 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008042 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008043 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008044 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8045 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008046 status = PSA_ERROR_BAD_STATE;
8047 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008048 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008049 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008050#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008051 {
8052 status = PSA_ERROR_NOT_SUPPORTED;
8053 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008054 }
8055
Przemek Stekiel0c781802022-11-29 14:53:13 +01008056 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8057 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008058 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008059 &shared_key_len);
8060
8061 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008062 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008063 }
8064
8065 status = psa_key_derivation_input_bytes(output,
8066 PSA_KEY_DERIVATION_INPUT_SECRET,
8067 shared_key,
8068 shared_key_len);
8069
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008070 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008071exit:
8072 abort_status = psa_pake_abort(operation);
8073 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008074}
8075
8076psa_status_t psa_pake_abort(
8077 psa_pake_operation_t *operation)
8078{
Przemek Stekield69dca92023-01-31 19:59:20 +01008079 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008080
Przemek Stekield69dca92023-01-31 19:59:20 +01008081 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008082 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008083 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008084
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008085 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8086 if (operation->data.inputs.password != NULL) {
8087 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008088 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008089 mbedtls_free(operation->data.inputs.password);
8090 }
8091 if (operation->data.inputs.user != NULL) {
8092 mbedtls_free(operation->data.inputs.user);
8093 }
8094 if (operation->data.inputs.peer != NULL) {
8095 mbedtls_free(operation->data.inputs.peer);
8096 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008097 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008098 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008099
Przemek Stekield69dca92023-01-31 19:59:20 +01008100 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008101}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008102
Gilles Peskinee59236f2018-01-27 23:32:46 +01008103#endif /* MBEDTLS_PSA_CRYPTO_C */