blob: 258a4057e0c8ad6e947a5e4f24e015e5143a2939 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010038#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010039#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010040#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010042#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020044#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020045#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010046#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010047/* Include internal declarations that are useful for implementing persistently
48 * stored keys. */
49#include "psa_crypto_storage.h"
50
Gilles Peskineb2b64d32020-12-14 16:43:58 +010051#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010052
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020062#include "mbedtls/chacha20.h"
63#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020068#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/error.h"
72#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010074#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000075#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010076#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000077#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010078#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000079#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/sha1.h"
83#include "mbedtls/sha256.h"
84#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000085#include "hash_info.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020088
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020089#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
91 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020092#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020093#endif
94
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010095/****************************************************************/
96/* Global data, support functions and library management */
97/****************************************************************/
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100{
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102}
103
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100104/* Values for psa_global_data_t::rng_state */
105#define RNG_NOT_INITIALIZED 0
106#define RNG_INITIALIZED 1
107#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100110 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100111 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100112 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100113 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114} psa_global_data_t;
115
116static psa_global_data_t global_data;
117
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100118#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
119mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
120 &global_data.rng.drbg;
121#endif
122
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (global_data.initialized == 0) \
125 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100127int psa_can_do_hash(psa_algorithm_t hash_alg)
128{
129 (void) hash_alg;
130 return global_data.drivers_initialized;
131}
Przemek Stekiel53410502023-04-28 13:18:43 +0200132#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
133 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
134 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200135static int psa_is_dh_key_size_valid(size_t bits)
136{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200137 if (bits != 2048 && bits != 3072 && bits != 4096 &&
138 bits != 6144 && bits != 8192) {
139 return 0;
140 }
141
142 return 1;
143}
Przemek Stekiel53410502023-04-28 13:18:43 +0200144#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR ||
145 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
146 PSA_WANT_KEY_TYPE_DH_KEY_PAIR */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100149{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100150 /* Mbed TLS error codes can combine a high-level error code and a
151 * low-level error code. The low-level error usually reflects the
152 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 int low_level_ret = -(-ret & 0x007f);
154 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100155 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100157
158 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
159 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200161 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
162 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
163 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
164 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
165 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200167 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200169 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182
Gilles Peskine26869f22019-05-06 15:25:00 +0200183 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200185
186 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200190
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
207 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100208 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
209 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
213 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100217#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100218
219 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100221
Gilles Peskinee59236f2018-01-27 23:32:46 +0100222 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
223 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
224 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100226
227 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200229 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100236 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
237 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100238 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100240 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
241 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100245#endif
246
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100260 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100266 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100268 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200296 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100312 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
313 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100334 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100336
Paul Elliott588f8ed2022-12-02 18:10:26 +0000337 case MBEDTLS_ERR_ECP_IN_PROGRESS:
338 return PSA_OPERATION_INCOMPLETE;
339
Janos Follatha13b9052019-11-22 12:48:59 +0000340 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300342
Gilles Peskinee59236f2018-01-27 23:32:46 +0100343 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100345 }
346}
347
Paul Elliottdc42ca82023-02-24 18:11:59 +0000348/**
349 * \brief For output buffers which contain "tags"
350 * (outputs that may be checked for validity like
351 * hashes, MACs and signatures), fill the unused
352 * part of the output buffer (the whole buffer on
353 * error, the trailing part on success) with
354 * something that isn't a valid tag (barring an
355 * attack on the tag and deliberately-crafted
356 * input), in case the caller doesn't check the
357 * return status properly.
358 *
359 * \param output_buffer Pointer to buffer to wipe. May not be NULL
360 * unless \p output_buffer_size is zero.
361 * \param status Status of function called to generate
362 * output_buffer originally
363 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
364 * could be NULL.
365 * \param output_buffer_length Length of data written to output_buffer, must be
366 * less than \p output_buffer_size
367 */
368static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000369 size_t output_buffer_size, size_t output_buffer_length)
370{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000371 size_t offset = 0;
372
373 if (output_buffer_size == 0) {
374 /* If output_buffer_size is 0 then we have nothing to do. We must not
375 call memset because output_buffer may be NULL in this case */
376 return;
377 }
378
379 if (status == PSA_SUCCESS) {
380 offset = output_buffer_length;
381 }
382
383 memset(output_buffer + offset, '!', output_buffer_size - offset);
384}
385
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200386
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200387
388
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100389/****************************************************************/
390/* Key management */
391/****************************************************************/
392
Valerio Settid4a5d462023-04-05 18:19:01 +0200393#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100394mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
395 size_t bits,
396 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200397{
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100399 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100401#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100402 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100404#endif
405#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100406 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100408#endif
409#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100410 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100412#endif
413#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100414 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100416#endif
417#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100418 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if (bits_is_sloppy) {
422 return MBEDTLS_ECP_DP_SECP521R1;
423 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100424 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100425#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 }
427 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100428
Paul Elliott8ff510a2020-06-02 17:19:28 +0100429 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100431#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100432 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100434#endif
435#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100436 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100438#endif
439#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100440 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100442#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100443 }
444 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100445
Paul Elliott8ff510a2020-06-02 17:19:28 +0100446 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100448#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (bits_is_sloppy) {
453 return MBEDTLS_ECP_DP_CURVE25519;
454 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100456#endif
457#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100458 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100460#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100461 }
462 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100463
Paul Elliott8ff510a2020-06-02 17:19:28 +0100464 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100466#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100467 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100469#endif
470#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100471 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100473#endif
474#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100475 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100477#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100478 }
479 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200480 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100481
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100482 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200484}
Valerio Settid4a5d462023-04-05 18:19:01 +0200485#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
488 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489{
490 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200493 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200494 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200495 case PSA_KEY_TYPE_PASSWORD:
496 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200497 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100498#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (bits != 128 && bits != 192 && bits != 256) {
501 return PSA_ERROR_INVALID_ARGUMENT;
502 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200503 break;
504#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200505#if defined(PSA_WANT_KEY_TYPE_ARIA)
506 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (bits != 128 && bits != 192 && bits != 256) {
508 return PSA_ERROR_INVALID_ARGUMENT;
509 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200510 break;
511#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100512#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200513 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (bits != 128 && bits != 192 && bits != 256) {
515 return PSA_ERROR_INVALID_ARGUMENT;
516 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517 break;
518#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100519#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200520 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (bits != 64 && bits != 128 && bits != 192) {
522 return PSA_ERROR_INVALID_ARGUMENT;
523 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200524 break;
525#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100526#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200527 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (bits != 256) {
529 return PSA_ERROR_INVALID_ARGUMENT;
530 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200531 break;
532#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200535 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (bits % 8 != 0) {
537 return PSA_ERROR_INVALID_ARGUMENT;
538 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200541}
542
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100543/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100544 *
Steven Cooremancd640932021-03-08 12:00:27 +0100545 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
546 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100547 *
548 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 * \param[in] key_type The key type of the key to be used with the
550 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100551 *
552 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100553 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100555 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100556 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100557MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100558 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100560{
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (PSA_ALG_IS_HMAC(algorithm)) {
562 if (key_type == PSA_KEY_TYPE_HMAC) {
563 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100565 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
568 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
569 * key. */
570 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
571 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
572 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
573 * the block length (larger than 1) for block ciphers. */
574 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
575 return PSA_SUCCESS;
576 }
577 }
578 }
579
580 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100581}
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
584 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200585{
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (slot->key.data != NULL) {
587 return PSA_ERROR_ALREADY_EXISTS;
588 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 slot->key.data = mbedtls_calloc(1, buffer_length);
591 if (slot->key.data == NULL) {
592 return PSA_ERROR_INSUFFICIENT_MEMORY;
593 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200594
Ronald Cronea0f8a62020-11-25 17:52:23 +0100595 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200597}
598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
600 const uint8_t *data,
601 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200602{
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 psa_status_t status = psa_allocate_buffer_to_slot(slot,
604 data_length);
605 if (status != PSA_SUCCESS) {
606 return status;
607 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 memcpy(slot->key.data, data, data_length);
610 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200611}
612
Ronald Crona0fe59f2020-11-29 11:14:53 +0100613psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100614 const psa_key_attributes_t *attributes,
615 const uint8_t *data, size_t data_length,
616 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100619 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
620 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100621
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200622 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if (data_length == 0) {
624 return PSA_ERROR_NOT_SUPPORTED;
625 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (key_type_is_raw_bytes(type)) {
628 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
631 *bits);
632 if (status != PSA_SUCCESS) {
633 return status;
634 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200635
Ronald Crondd04d422020-11-28 15:14:42 +0100636 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100638 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 return PSA_SUCCESS;
642 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200643#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
644 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100645 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200646 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100647 return PSA_ERROR_INVALID_ARGUMENT;
648 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200649 return mbedtls_psa_ffdh_import_key(attributes,
650 data, data_length,
651 key_buffer, key_buffer_size,
652 key_buffer_length,
653 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100654 }
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200655#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
656 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
John Durkop9814fa22020-11-04 12:28:15 -0800657#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
659 if (PSA_KEY_TYPE_IS_ECC(type)) {
660 return mbedtls_psa_ecp_import_key(attributes,
661 data, data_length,
662 key_buffer, key_buffer_size,
663 key_buffer_length,
664 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100665 }
John Durkop9814fa22020-11-04 12:28:15 -0800666#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
667 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700668#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
670 if (PSA_KEY_TYPE_IS_RSA(type)) {
671 return mbedtls_psa_rsa_import_key(attributes,
672 data, data_length,
673 key_buffer, key_buffer_size,
674 key_buffer_length,
675 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200676 }
John Durkop9814fa22020-11-04 12:28:15 -0800677#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
678 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100679 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100682}
683
Gilles Peskinef603c712019-01-19 13:40:11 +0100684/** Calculate the intersection of two algorithm usage policies.
685 *
686 * Return 0 (which allows no operation) on incompatibility.
687 */
688static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100689 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100690 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100692{
Gilles Peskine549ea862019-05-22 11:45:59 +0200693 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (alg1 == alg2) {
695 return alg1;
696 }
Steven Cooreman03488022021-02-18 12:07:20 +0100697 /* If the policies are from the same hash-and-sign family, check
698 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
700 PSA_ALG_IS_SIGN_HASH(alg2) &&
701 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
702 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
703 return alg2;
704 }
705 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
706 return alg1;
707 }
Steven Cooreman03488022021-02-18 12:07:20 +0100708 }
709 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100710 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100711 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
713 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
714 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
715 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
716 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100717 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100718
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100719 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
721 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
722 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
723 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100724 }
725 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
727 (alg1_len <= alg2_len)) {
728 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100729 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
731 (alg2_len <= alg1_len)) {
732 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100733 }
734 }
735 /* If the policies are from the same MAC family, check whether one
736 * of them is a minimum-MAC-length policy. Calculate the most
737 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
739 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
740 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100741 /* Validate the combination of key type and algorithm. Since the base
742 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
744 return 0;
745 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100746
Steven Cooreman31a876d2021-03-03 20:47:40 +0100747 /* Get the (exact or at-least) output lengths for both sides of the
748 * requested intersection. None of the currently supported algorithms
749 * have an output length dependent on the actual key size, so setting it
750 * to a bogus value of 0 is currently OK.
751 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100752 * Note that for at-least-this-length wildcard algorithms, the output
753 * length is set to the shortest allowed length, which allows us to
754 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
756 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100757 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100758
Steven Cooremana1d83222021-02-25 10:20:29 +0100759 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
761 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
762 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100763 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100764
765 /* If only one is an at-least-this-length policy, the intersection would
766 * be the other (fixed-length) policy as long as said fixed length is
767 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
769 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100770 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
772 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100773 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100774
Steven Cooremancd640932021-03-08 12:00:27 +0100775 /* If none of them are wildcards, check whether they define the same tag
776 * length. This is still possible here when one is default-length and
777 * the other specific-length. Ensure to always return the
778 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (alg1_len == alg2_len) {
780 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
781 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100782 }
783 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100785}
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787static int psa_key_algorithm_permits(psa_key_type_t key_type,
788 psa_algorithm_t policy_alg,
789 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200790{
Gilles Peskine549ea862019-05-22 11:45:59 +0200791 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (requested_alg == policy_alg) {
793 return 1;
794 }
Steven Cooreman03488022021-02-18 12:07:20 +0100795 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
796 * and requested_alg is the same hash-and-sign family with any hash,
797 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
799 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
800 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
801 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100802 }
803 /* If policy_alg is a wildcard AEAD algorithm of the same base as
804 * the requested algorithm, check the requested tag length to be
805 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (PSA_ALG_IS_AEAD(policy_alg) &&
807 PSA_ALG_IS_AEAD(requested_alg) &&
808 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
809 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
810 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
811 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
812 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100813 }
Steven Cooremancd640932021-03-08 12:00:27 +0100814 /* If policy_alg is a MAC algorithm of the same base as the requested
815 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (PSA_ALG_IS_MAC(policy_alg) &&
817 PSA_ALG_IS_MAC(requested_alg) &&
818 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
819 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100820 /* Validate the combination of key type and algorithm. Since the policy
821 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
823 return 0;
824 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100825
Steven Cooreman31a876d2021-03-03 20:47:40 +0100826 /* Get both the requested output length for the algorithm which is to be
827 * verified, and the default output length for the base algorithm.
828 * Note that none of the currently supported algorithms have an output
829 * length dependent on actual key size, so setting it to a bogus value
830 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100831 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100833 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 key_type, 0,
835 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100836
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100837 /* If the policy is default-length, only allow an algorithm with
838 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
840 return requested_output_length == default_output_length;
841 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100842
843 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100844 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
846 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
847 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100848 }
849
Steven Cooremancd640932021-03-08 12:00:27 +0100850 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
851 * check for the requested MAC length to be equal to or longer than the
852 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
854 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
855 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100856 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200857 }
Steven Cooremance48e852020-10-05 16:02:45 +0200858 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200859 * a key derivation with that key agreement should also be allowed. This
860 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
862 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
863 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
864 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200865 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100866 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200868}
869
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100870/** Test whether a policy permits an algorithm.
871 *
872 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100873 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100874 * \note This function requires providing the key type for which the policy is
875 * being validated, since some algorithm policy definitions (e.g. MAC)
876 * have different properties depending on what kind of cipher it is
877 * combined with.
878 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100879 * \retval PSA_SUCCESS When \p alg is a specific algorithm
880 * allowed by the \p policy.
881 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
882 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
883 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100884 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
886 psa_key_type_t key_type,
887 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100888{
Steven Cooremand788fab2021-02-25 11:29:17 +0100889 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (alg == 0) {
891 return PSA_ERROR_INVALID_ARGUMENT;
892 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100893
Steven Cooreman7e39f052021-02-23 14:18:32 +0100894 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (PSA_ALG_IS_WILDCARD(alg)) {
896 return PSA_ERROR_INVALID_ARGUMENT;
897 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
900 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
901 return PSA_SUCCESS;
902 } else {
903 return PSA_ERROR_NOT_PERMITTED;
904 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100905}
906
Gilles Peskinef603c712019-01-19 13:40:11 +0100907/** Restrict a key policy based on a constraint.
908 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100909 * \note This function requires providing the key type for which the policy is
910 * being restricted, since some algorithm policy definitions (e.g. MAC)
911 * have different properties depending on what kind of cipher it is
912 * combined with.
913 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100914 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100915 * \param[in,out] policy The policy to restrict.
916 * \param[in] constraint The policy constraint to apply.
917 *
918 * \retval #PSA_SUCCESS
919 * \c *policy contains the intersection of the original value of
920 * \c *policy and \c *constraint.
921 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100922 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100923 * \c *policy is unchanged.
924 */
925static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100926 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100927 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100929{
930 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 psa_key_policy_algorithm_intersection(key_type, policy->alg,
932 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200933 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
935 constraint->alg2);
936 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
937 return PSA_ERROR_INVALID_ARGUMENT;
938 }
939 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
940 return PSA_ERROR_INVALID_ARGUMENT;
941 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100942 policy->usage &= constraint->usage;
943 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200944 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100946}
947
Przemek Stekiel061f6942022-11-30 10:51:35 +0100948/** Get the description of a key given its identifier and policy constraints
949 * and lock it.
950 *
951 * The key must have allow all the usage flags set in \p usage. If \p alg is
952 * nonzero, the key must allow operations with this algorithm. If \p alg is
953 * zero, the algorithm is not checked.
954 *
955 * In case of a persistent key, the function loads the description of the key
956 * into a key slot if not already done.
957 *
958 * On success, the returned key slot is locked. It is the responsibility of
959 * the caller to unlock the key slot when it does not access it anymore.
960 */
961static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100962 mbedtls_svc_key_id_t key,
963 psa_key_slot_t **p_slot,
964 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100966{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200967 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100968 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 status = psa_get_and_lock_key_slot(key, p_slot);
971 if (status != PSA_SUCCESS) {
972 return status;
973 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200974 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100975
976 /* Enforce that usage policy for the key slot contains all the flags
977 * required by the usage parameter. There is one exception: public
978 * keys can always be exported, so we treat public key objects as
979 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100981 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100985 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200986 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100987 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100988
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800989 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (alg != 0) {
991 status = psa_key_policy_permits(&slot->attr.policy,
992 slot->attr.type,
993 alg);
994 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100995 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100997 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001000
1001error:
1002 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001006}
Darryl Green940d72c2018-07-13 13:18:51 +01001007
Ronald Cron5c522922020-11-14 16:35:34 +01001008/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001009 *
1010 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001011 * available, as opposed to a key in a secure element and/or to be used
1012 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001013 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001014 * This is a temporary function that may be used instead of
1015 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1016 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001017 *
Ronald Cron5c522922020-11-14 16:35:34 +01001018 * On success, the returned key slot is locked. It is the responsibility of the
1019 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001020 */
Ronald Cron5c522922020-11-14 16:35:34 +01001021static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1022 mbedtls_svc_key_id_t key,
1023 psa_key_slot_t **p_slot,
1024 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001026{
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1028 usage, alg);
1029 if (status != PSA_SUCCESS) {
1030 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001031 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1034 psa_unlock_key_slot(*p_slot);
1035 *p_slot = NULL;
1036 return PSA_ERROR_NOT_SUPPORTED;
1037 }
1038
1039 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001040}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001043{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001044 /* Data pointer will always be either a valid pointer or NULL in an
1045 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (slot->key.data != NULL) {
1047 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1048 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001051 slot->key.data = NULL;
1052 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001055}
1056
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001057/** Completely wipe a slot in memory, including its policy.
1058 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001060{
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 /*
1064 * As the return error code may not be handled in case of multiple errors,
1065 * do our best to report an unexpected lock counter. Assert with
1066 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1067 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1068 * function is called as part of the execution of a test suite, the
1069 * execution of the test suite is stopped in error if the assertion fails.
1070 */
1071 if (slot->lock_count != 1) {
1072 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001073 status = PSA_ERROR_CORRUPTION_DETECTED;
1074 }
1075
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001076 /* Multipart operations may still be using the key. This is safe
1077 * because all multipart operation objects are independent from
1078 * the key slot: if they need to access the key after the setup
1079 * phase, they have a copy of the key. Note that this means that
1080 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001081 /* At this point, key material and other type-specific content has
1082 * been wiped. Clear remaining metadata. We can call memset and not
1083 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 memset(slot, 0, sizeof(*slot));
1085 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001086}
1087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001089{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001090 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001091 psa_status_t status; /* status of the last operation */
1092 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001093#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1094 psa_se_drv_table_entry_t *driver;
1095#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (mbedtls_svc_key_id_is_null(key)) {
1098 return PSA_SUCCESS;
1099 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001100
Ronald Cronf2911112020-10-29 17:51:10 +01001101 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001102 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001103 * key, this will load the key description from persistent memory if not
1104 * done yet. We cannot avoid this loading as without it we don't know if
1105 * the key is operated by an SE or not and this information is needed by
1106 * the current implementation.
1107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 status = psa_get_and_lock_key_slot(key, &slot);
1109 if (status != PSA_SUCCESS) {
1110 return status;
1111 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001112
Ronald Cronf2911112020-10-29 17:51:10 +01001113 /*
1114 * If the key slot containing the key description is under access by the
1115 * library (apart from the present access), the key cannot be destroyed
1116 * yet. For the time being, just return in error. Eventually (to be
1117 * implemented), the key should be destroyed when all accesses have
1118 * stopped.
1119 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (slot->lock_count > 1) {
1121 psa_unlock_key_slot(slot);
1122 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001123 }
1124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001126 /* Refuse the destruction of a read-only key (which may or may not work
1127 * if we attempt it, depending on whether the key is merely read-only
1128 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001129 * Just do the best we can, which is to wipe the copy in memory
1130 * (done in this function's cleanup code). */
1131 overall_status = PSA_ERROR_NOT_PERMITTED;
1132 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001133 }
1134
Gilles Peskine354f7672019-07-12 23:46:38 +02001135#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1137 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001138 /* For a key in a secure element, we need to do three things:
1139 * remove the key file in internal storage, destroy the
1140 * key inside the secure element, and update the driver's
1141 * persistent data. Start a transaction that will encompass these
1142 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001144 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001146 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 status = psa_crypto_save_transaction();
1148 if (status != PSA_SUCCESS) {
1149 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001150 /* We should still try to destroy the key in the secure
1151 * element and the key metadata in storage. This is especially
1152 * important if the error is that the storage is full.
1153 * But how to do it exactly without risking an inconsistent
1154 * state after a reset?
1155 * https://github.com/ARMmbed/mbed-crypto/issues/215
1156 */
1157 overall_status = status;
1158 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001159 }
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 status = psa_destroy_se_key(driver,
1162 psa_key_slot_get_slot_number(slot));
1163 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001164 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001166 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001167#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1168
Darryl Greend49a4992018-06-18 17:27:26 +01001169#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1171 status = psa_destroy_persistent_key(slot->attr.id);
1172 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001173 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001175
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001176 /* TODO: other slots may have a copy of the same key. We should
1177 * invalidate them.
1178 * https://github.com/ARMmbed/mbed-crypto/issues/214
1179 */
Darryl Greend49a4992018-06-18 17:27:26 +01001180 }
1181#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001182
Gilles Peskinefc762652019-07-22 19:30:34 +02001183#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (driver != NULL) {
1185 status = psa_save_se_persistent_data(driver);
1186 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001187 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 }
1189 status = psa_crypto_stop_transaction();
1190 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001191 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001193 }
1194#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1195
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001196exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001198 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001200 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 }
1202 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001203}
1204
John Durkop07cc04a2020-11-16 22:08:34 -08001205#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001206 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001207static psa_status_t psa_get_rsa_public_exponent(
1208 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001210{
1211 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001212 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001213 uint8_t *buffer = NULL;
1214 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1218 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001219 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 }
1221 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001222 /* It's the default value, which is reported as an empty string,
1223 * so there's nothing to do. */
1224 goto exit;
1225 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 buflen = mbedtls_mpi_size(&mpi);
1228 buffer = mbedtls_calloc(1, buflen);
1229 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001230 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1231 goto exit;
1232 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1234 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001235 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001237 attributes->domain_parameters = buffer;
1238 attributes->domain_parameters_size = buflen;
1239
1240exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 mbedtls_mpi_free(&mpi);
1242 if (ret != 0) {
1243 mbedtls_free(buffer);
1244 }
1245 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001246}
John Durkop07cc04a2020-11-16 22:08:34 -08001247#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001248 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001249
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001250/** Retrieve all the publicly-accessible attributes of a key.
1251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001252psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1253 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001255 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001256 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001257 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1262 if (status != PSA_SUCCESS) {
1263 return status;
1264 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001265
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001266 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1268 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001269
1270#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1272 psa_set_key_slot_number(attributes,
1273 psa_key_slot_get_slot_number(slot));
1274 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001275#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001278#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001279 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001280 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001281 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001282 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001283 * is not yet implemented.
1284 * https://github.com/ARMmbed/mbed-crypto/issues/216
1285 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001287 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001288
Ronald Cron90857082020-11-25 14:58:33 +01001289 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 slot->attr.type,
1291 slot->key.data,
1292 slot->key.bytes,
1293 &rsa);
1294 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001295 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 status = psa_get_rsa_public_exponent(rsa,
1299 attributes);
1300 mbedtls_rsa_free(rsa);
1301 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001302 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001303 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001304#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001305 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001306 default:
1307 /* Nothing else to do. */
1308 break;
1309 }
1310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (status != PSA_SUCCESS) {
1312 psa_reset_key_attributes(attributes);
1313 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001318}
1319
Gilles Peskinec8000c02019-08-02 20:15:51 +02001320#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1321psa_status_t psa_get_key_slot_number(
1322 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001324{
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001326 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 return PSA_SUCCESS;
1328 } else {
1329 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001330 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001331}
1332#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1335 size_t key_buffer_size,
1336 uint8_t *data,
1337 size_t data_size,
1338 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001339{
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if (key_buffer_size > data_size) {
1341 return PSA_ERROR_BUFFER_TOO_SMALL;
1342 }
1343 memcpy(data, key_buffer, key_buffer_size);
1344 memset(data + key_buffer_size, 0,
1345 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001346 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001348}
1349
Ronald Cron7285cda2020-11-26 14:46:37 +01001350psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001351 const psa_key_attributes_t *attributes,
1352 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001354{
Ronald Crond18b5f82020-11-25 16:46:05 +01001355 psa_key_type_t type = attributes->core.type;
1356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if (key_type_is_raw_bytes(type) ||
1358 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001359 PSA_KEY_TYPE_IS_ECC(type) ||
1360 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 return psa_export_key_buffer_internal(
1362 key_buffer, key_buffer_size,
1363 data, data_size, data_length);
1364 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001365 /* This shouldn't happen in the reference implementation, but
1366 it is valid for a special-purpose implementation to omit
1367 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001369 }
1370}
1371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1373 uint8_t *data,
1374 size_t data_size,
1375 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001376{
1377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1378 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1379 psa_key_slot_t *slot;
1380
Ronald Crone907e552021-01-18 13:22:38 +01001381 /* Reject a zero-length output buffer now, since this can never be a
1382 * valid key representation. This way we know that data must be a valid
1383 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (data_size == 0) {
1385 return PSA_ERROR_BUFFER_TOO_SMALL;
1386 }
Ronald Crone907e552021-01-18 13:22:38 +01001387
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 /* Set the key to empty now, so that even when there are errors, we always
1389 * set data_length to a value between 0 and data_size. On error, setting
1390 * the key to empty is a good choice because an empty key representation is
1391 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001392 *data_length = 0;
1393
Ronald Cron9486f9d2020-11-26 13:36:23 +01001394 /* Export requires the EXPORT flag. There is an exception for public keys,
1395 * which don't require any flag, but
1396 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1399 PSA_KEY_USAGE_EXPORT, 0);
1400 if (status != PSA_SUCCESS) {
1401 return status;
1402 }
mohammad160306e79202018-03-28 13:17:44 +03001403
Ronald Crond18b5f82020-11-25 16:46:05 +01001404 psa_key_attributes_t attributes = {
1405 .core = slot->attr
1406 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 status = psa_driver_wrapper_export_key(&attributes,
1408 slot->key.data, slot->key.bytes,
1409 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001414}
1415
Ronald Cron7285cda2020-11-26 14:46:37 +01001416psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001417 const psa_key_attributes_t *attributes,
1418 const uint8_t *key_buffer,
1419 size_t key_buffer_size,
1420 uint8_t *data,
1421 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001423{
Ronald Crond18b5f82020-11-25 16:46:05 +01001424 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001425
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001426 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001427 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1428 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001429 /* Exporting public -> public */
1430 return psa_export_key_buffer_internal(
1431 key_buffer, key_buffer_size,
1432 data, data_size, data_length);
1433 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001434#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001435 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1436 return mbedtls_psa_rsa_export_public_key(attributes,
1437 key_buffer,
1438 key_buffer_size,
1439 data,
1440 data_size,
1441 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001442#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001443 /* We don't know how to convert a private RSA key to public. */
1444 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001445#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1446 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001447 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001448#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001449 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1450 return mbedtls_psa_ecp_export_public_key(attributes,
1451 key_buffer,
1452 key_buffer_size,
1453 data,
1454 data_size,
1455 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001456#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001457 /* We don't know how to convert a private ECC key to public */
1458 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001459#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1460 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001461 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001462#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) || \
1463 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001464 return mbedtls_psa_export_ffdh_public_key(attributes,
1465 key_buffer,
1466 key_buffer_size,
1467 data, data_size,
1468 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001469#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001470 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001471#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) ||
1472 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001474 (void) key_buffer;
1475 (void) key_buffer_size;
1476 (void) data;
1477 (void) data_size;
1478 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001480 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001481}
Ronald Crond18b5f82020-11-25 16:46:05 +01001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1484 uint8_t *data,
1485 size_t data_size,
1486 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001487{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001488 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001489 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001490 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001491
Ronald Crone907e552021-01-18 13:22:38 +01001492 /* Reject a zero-length output buffer now, since this can never be a
1493 * valid key representation. This way we know that data must be a valid
1494 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (data_size == 0) {
1496 return PSA_ERROR_BUFFER_TOO_SMALL;
1497 }
Ronald Crone907e552021-01-18 13:22:38 +01001498
Darryl Greendd8fb772018-11-07 16:00:44 +00001499 /* Set the key to empty now, so that even when there are errors, we always
1500 * set data_length to a value between 0 and data_size. On error, setting
1501 * the key to empty is a good choice because an empty key representation is
1502 * unlikely to be accepted anywhere. */
1503 *data_length = 0;
1504
1505 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1507 if (status != PSA_SUCCESS) {
1508 return status;
1509 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1512 status = PSA_ERROR_INVALID_ARGUMENT;
1513 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001514 }
1515
Ronald Crond18b5f82020-11-25 16:46:05 +01001516 psa_key_attributes_t attributes = {
1517 .core = slot->attr
1518 };
Ronald Cron67227982020-11-26 15:16:05 +01001519 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001520 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001522
1523exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001527}
1528
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001529MBEDTLS_STATIC_ASSERT(
1530 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1531 "One or more key attribute flag is listed as both external-only and dual-use")
1532MBEDTLS_STATIC_ASSERT(
1533 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1534 "One or more key attribute flag is listed as both internal-only and dual-use")
1535MBEDTLS_STATIC_ASSERT(
1536 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1537 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001538
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001539/** Validate that a key policy is internally well-formed.
1540 *
1541 * This function only rejects invalid policies. It does not validate the
1542 * consistency of the policy with respect to other attributes of the key
1543 * such as the key type.
1544 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001545static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001546{
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1548 PSA_KEY_USAGE_COPY |
1549 PSA_KEY_USAGE_ENCRYPT |
1550 PSA_KEY_USAGE_DECRYPT |
1551 PSA_KEY_USAGE_SIGN_MESSAGE |
1552 PSA_KEY_USAGE_VERIFY_MESSAGE |
1553 PSA_KEY_USAGE_SIGN_HASH |
1554 PSA_KEY_USAGE_VERIFY_HASH |
1555 PSA_KEY_USAGE_VERIFY_DERIVATION |
1556 PSA_KEY_USAGE_DERIVE)) != 0) {
1557 return PSA_ERROR_INVALID_ARGUMENT;
1558 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001561}
1562
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001563/** Validate the internal consistency of key attributes.
1564 *
1565 * This function only rejects invalid attribute values. If does not
1566 * validate the consistency of the attributes with any key data that may
1567 * be involved in the creation of the key.
1568 *
1569 * Call this function early in the key creation process.
1570 *
1571 * \param[in] attributes Key attributes for the new key.
1572 * \param[out] p_drv On any return, the driver for the key, if any.
1573 * NULL for a transparent key.
1574 *
1575 */
1576static psa_status_t psa_validate_key_attributes(
1577 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001579{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001580 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1582 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 status = psa_validate_key_location(lifetime, p_drv);
1585 if (status != PSA_SUCCESS) {
1586 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001587 }
1588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 status = psa_validate_key_persistence(lifetime);
1590 if (status != PSA_SUCCESS) {
1591 return status;
1592 }
1593
1594 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1595 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1596 return PSA_ERROR_INVALID_ARGUMENT;
1597 }
1598 } else {
1599 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1600 return PSA_ERROR_INVALID_ARGUMENT;
1601 }
1602 }
1603
1604 status = psa_validate_key_policy(&attributes->core.policy);
1605 if (status != PSA_SUCCESS) {
1606 return status;
1607 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001608
1609 /* Refuse to create overly large keys.
1610 * Note that this doesn't trigger on import if the attributes don't
1611 * explicitly specify a size (so psa_get_key_bits returns 0), so
1612 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1614 return PSA_ERROR_NOT_SUPPORTED;
1615 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001616
Gilles Peskine91e8c332019-08-02 19:19:39 +02001617 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1619 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1620 return PSA_ERROR_INVALID_ARGUMENT;
1621 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001624}
1625
Gilles Peskine4747d192019-04-17 15:05:45 +02001626/** Prepare a key slot to receive key material.
1627 *
1628 * This function allocates a key slot and sets its metadata.
1629 *
1630 * If this function fails, call psa_fail_key_creation().
1631 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001632 * This function is intended to be used as follows:
1633 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001634 * it with the specified attributes, and in case of a volatile key assign it
1635 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001636 * -# Populate the slot with the key material.
1637 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1638 * In case of failure at any step, stop the sequence and call
1639 * psa_fail_key_creation().
1640 *
Ronald Cron5c522922020-11-14 16:35:34 +01001641 * On success, the key slot is locked. It is the responsibility of the caller
1642 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001643 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001644 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001645 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001646 * \param[out] p_slot On success, a pointer to the prepared slot.
1647 * \param[out] p_drv On any return, the driver for the key, if any.
1648 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001649 *
1650 * \retval #PSA_SUCCESS
1651 * The key slot is ready to receive key material.
1652 * \return If this function fails, the key slot is an invalid state.
1653 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001654 */
1655static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001656 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001657 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001658 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001660{
1661 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001662 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001663 psa_key_slot_t *slot;
1664
Gilles Peskinedf179142019-07-15 22:02:14 +02001665 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001666 *p_drv = NULL;
1667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 status = psa_validate_key_attributes(attributes, p_drv);
1669 if (status != PSA_SUCCESS) {
1670 return status;
1671 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1674 if (status != PSA_SUCCESS) {
1675 return status;
1676 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001677 slot = *p_slot;
1678
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001679 /* We're storing the declared bit-size of the key. It's up to each
1680 * creation mechanism to verify that this information is correct.
1681 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001682 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001683 * is optional (import, copy). In case of a volatile key, assign it the
1684 * volatile key identifier associated to the slot returned to contain its
1685 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001686
1687 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001689#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1690 slot->attr.id = volatile_key_id;
1691#else
1692 slot->attr.id.key_id = volatile_key_id;
1693#endif
1694 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001695
Gilles Peskine91e8c332019-08-02 19:19:39 +02001696 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001697 * external-only flags, query `attributes`. Thanks to the check
1698 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001699 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001700 * may have set. */
1701 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001702
Gilles Peskinecbaff462019-07-12 23:46:04 +02001703#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001704 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001705 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001706 * create the key file in internal storage, create the
1707 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001708 * persistent data. This is done by starting a transaction that will
1709 * encompass these three actions.
1710 * For registering a volatile key, we just need to find an appropriate
1711 * slot number inside the SE. Since the key is designated volatile, creating
1712 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001713 /* The first thing to do is to find a slot number for the new key.
1714 * We save the slot number in persistent storage as part of the
1715 * transaction data. It will be needed to recover if the power
1716 * fails during the key creation process, to clean up on the secure
1717 * element side after restarting. Obtaining a slot number from the
1718 * secure element driver updates its persistent state, but we do not yet
1719 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001720 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001722 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1724 &slot_number);
1725 if (status != PSA_SUCCESS) {
1726 return status;
1727 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1730 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001731 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001732 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001733 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 status = psa_crypto_save_transaction();
1735 if (status != PSA_SUCCESS) {
1736 (void) psa_crypto_stop_transaction();
1737 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001738 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001739 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001740
1741 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001743 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001746 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001748 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001749#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001752}
Gilles Peskine4747d192019-04-17 15:05:45 +02001753
1754/** Finalize the creation of a key once its key material has been set.
1755 *
1756 * This entails writing the key to persistent storage.
1757 *
1758 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001759 * See the documentation of psa_start_key_creation() for the intended use
1760 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001761 *
Ronald Cron5c522922020-11-14 16:35:34 +01001762 * If the finalization succeeds, the function unlocks the key slot (it was
1763 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1764 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001765 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001766 * \param[in,out] slot Pointer to the slot with key material.
1767 * \param[in] driver The secure element driver for the key,
1768 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001769 * \param[out] key On success, identifier of the key. Note that the
1770 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001771 *
1772 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001773 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001774 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1775 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1776 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1777 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1778 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1779 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001780 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001781 * \return If this function fails, the key slot is an invalid state.
1782 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001783 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001784static psa_status_t psa_finish_key_creation(
1785 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001786 psa_se_drv_table_entry_t *driver,
1787 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001788{
1789 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001790 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001791 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001792
1793#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001795#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001797 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001798 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001800
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001801 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1802 sizeof(data.slot_number),
1803 "Slot number size does not match psa_se_key_data_storage_t");
1804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1806 status = psa_save_persistent_key(&slot->attr,
1807 (uint8_t *) &data,
1808 sizeof(data));
1809 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001810#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1811 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001812 /* Key material is saved in export representation in the slot, so
1813 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 status = psa_save_persistent_key(&slot->attr,
1815 slot->key.data,
1816 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001817 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001818 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001819#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1820
Gilles Peskinecbaff462019-07-12 23:46:04 +02001821#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001822 /* Finish the transaction for a key creation. This does not
1823 * happen when registering an existing key. Detect this case
1824 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001825 * creation of a persistent key in a secure element requires a transaction,
1826 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (driver != NULL &&
1828 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1829 status = psa_save_se_persistent_data(driver);
1830 if (status != PSA_SUCCESS) {
1831 psa_destroy_persistent_key(slot->attr.id);
1832 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001835 }
1836#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001839 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 status = psa_unlock_key_slot(slot);
1841 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001842 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001844 }
Ronald Cron50972942020-11-14 11:28:25 +01001845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001847}
1848
1849/** Abort the creation of a key.
1850 *
1851 * You may call this function after calling psa_start_key_creation(),
1852 * or after psa_finish_key_creation() fails. In other circumstances, this
1853 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001854 * See the documentation of psa_start_key_creation() for the intended use
1855 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001856 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001857 * \param[in,out] slot Pointer to the slot with key material.
1858 * \param[in] driver The secure element driver for the key,
1859 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001860 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001861static void psa_fail_key_creation(psa_key_slot_t *slot,
1862 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001863{
Gilles Peskine011e4282019-06-26 18:34:38 +02001864 (void) driver;
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001867 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001869
1870#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001871 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001872 * element, and the failure happened later (when saving metadata
1873 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001874 * element.
1875 * https://github.com/ARMmbed/mbed-crypto/issues/217
1876 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001877
Gilles Peskined7729582019-08-05 15:55:54 +02001878 /* Abort the ongoing transaction if any (there may not be one if
1879 * the creation process failed before starting one, or if the
1880 * key creation is a registration of a key in a secure element).
1881 * Earlier functions must already have done what it takes to undo any
1882 * partial creation. All that's left is to update the transaction data
1883 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001885#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001888}
1889
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001890/** Validate optional attributes during key creation.
1891 *
1892 * Some key attributes are optional during key creation. If they are
1893 * specified in the attributes structure, check that they are consistent
1894 * with the data in the slot.
1895 *
1896 * This function should be called near the end of key creation, after
1897 * the slot in memory is fully populated but before saving persistent data.
1898 */
1899static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001902{
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 if (attributes->core.type != 0) {
1904 if (attributes->core.type != slot->attr.type) {
1905 return PSA_ERROR_INVALID_ARGUMENT;
1906 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001907 }
1908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001910#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1912 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001913 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001914 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001915 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001916
Ronald Cron90857082020-11-25 14:58:33 +01001917 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 slot->attr.type,
1919 slot->key.data,
1920 slot->key.bytes,
1921 &rsa);
1922 if (status != PSA_SUCCESS) {
1923 return status;
1924 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 mbedtls_mpi_init(&actual);
1927 mbedtls_mpi_init(&required);
1928 ret = mbedtls_rsa_export(rsa,
1929 NULL, NULL, NULL, NULL, &actual);
1930 mbedtls_rsa_free(rsa);
1931 mbedtls_free(rsa);
1932 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001933 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 }
1935 ret = mbedtls_mpi_read_binary(&required,
1936 attributes->domain_parameters,
1937 attributes->domain_parameters_size);
1938 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001939 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 }
1941 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001942 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 }
1944rsa_exit:
1945 mbedtls_mpi_free(&actual);
1946 mbedtls_mpi_free(&required);
1947 if (ret != 0) {
1948 return mbedtls_to_psa_error(ret);
1949 }
1950 } else
John Durkop9814fa22020-11-04 12:28:15 -08001951#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1952 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001955 }
1956 }
1957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if (attributes->core.bits != 0) {
1959 if (attributes->core.bits != slot->attr.bits) {
1960 return PSA_ERROR_INVALID_ARGUMENT;
1961 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001962 }
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001965}
1966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1968 const uint8_t *data,
1969 size_t data_length,
1970 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001971{
1972 psa_status_t status;
1973 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001974 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001975 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301976 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001977
Ronald Cron81709fc2020-11-14 12:10:32 +01001978 *key = MBEDTLS_SVC_KEY_ID_INIT;
1979
Gilles Peskine0f84d622019-09-12 19:03:13 +02001980 /* Reject zero-length symmetric keys (including raw data key objects).
1981 * This also rejects any key which might be encoded as an empty string,
1982 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if (data_length == 0) {
1984 return PSA_ERROR_INVALID_ARGUMENT;
1985 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001986
Archana449608b2021-09-08 15:36:05 +05301987 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 if (data_length > SIZE_MAX / 8) {
1989 return PSA_ERROR_NOT_SUPPORTED;
1990 }
Archana6ed4bda2021-08-04 10:47:15 +05301991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1993 &slot, &driver);
1994 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001995 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001997
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001998 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301999 * storage ( thus not in the case of importing a key in a secure element
2000 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2001 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 if (slot->key.data == NULL) {
2003 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302004 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 attributes, data, data_length, &storage_size);
2006 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302007 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 }
Archanad8a83dc2021-06-14 10:04:16 +05302009 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 status = psa_allocate_buffer_to_slot(slot, storage_size);
2011 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002012 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002014 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002015
2016 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 status = psa_driver_wrapper_import_key(attributes,
2018 data, data_length,
2019 slot->key.data,
2020 slot->key.bytes,
2021 &slot->key.bytes, &bits);
2022 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002023 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002027 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002029 status = PSA_ERROR_INVALID_ARGUMENT;
2030 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002031 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002032
Archana6ed4bda2021-08-04 10:47:15 +05302033 /* Enforce a size limit, and in particular ensure that the bit
2034 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302036 status = PSA_ERROR_NOT_SUPPORTED;
2037 goto exit;
2038 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 status = psa_validate_optional_attributes(slot, attributes);
2040 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002041 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002045exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (status != PSA_SUCCESS) {
2047 psa_fail_key_creation(slot, driver);
2048 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002051}
2052
Gilles Peskined7729582019-08-05 15:55:54 +02002053#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2054psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002056{
2057 psa_status_t status;
2058 psa_key_slot_t *slot = NULL;
2059 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002060 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002061
2062 /* Leaving attributes unspecified is not currently supported.
2063 * It could make sense to query the key type and size from the
2064 * secure element, but not all secure elements support this
2065 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2067 return PSA_ERROR_NOT_SUPPORTED;
2068 }
2069 if (psa_get_key_bits(attributes) == 0) {
2070 return PSA_ERROR_NOT_SUPPORTED;
2071 }
Gilles Peskined7729582019-08-05 15:55:54 +02002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2074 &slot, &driver);
2075 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 }
Gilles Peskined7729582019-08-05 15:55:54 +02002078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002080
2081exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 if (status != PSA_SUCCESS) {
2083 psa_fail_key_creation(slot, driver);
2084 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002085
Gilles Peskined7729582019-08-05 15:55:54 +02002086 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 psa_close_key(key);
2088 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002089}
2090#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2091
Gilles Peskine449bd832023-01-11 14:50:10 +01002092psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2093 const psa_key_attributes_t *specified_attributes,
2094 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002095{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002096 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002097 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002098 psa_key_slot_t *source_slot = NULL;
2099 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002100 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002101 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302102 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002103
Ronald Cron81709fc2020-11-14 12:10:32 +01002104 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2105
Archana8a180362021-07-05 02:18:48 +05302106 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2108 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 status = psa_validate_optional_attributes(source_slot,
2113 specified_attributes);
2114 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002117
Archana9d17bf42021-09-10 06:22:44 +05302118 /* The target key type and number of bits have been validated by
2119 * psa_validate_optional_attributes() to be either equal to zero or
2120 * equal to the ones of the source key. So it is safe to inherit
2121 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302122 * */
Archana9d17bf42021-09-10 06:22:44 +05302123 actual_attributes.core.bits = source_slot->attr.bits;
2124 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302125
2126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 status = psa_restrict_key_policy(source_slot->attr.type,
2128 &actual_attributes.core.policy,
2129 &source_slot->attr.policy);
2130 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2135 &target_slot, &driver);
2136 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 }
2139 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2140 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302141 /*
Archana9d17bf42021-09-10 06:22:44 +05302142 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302143 * the source key would need to be exported as plaintext and re-imported
2144 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302145 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302146 * appropriate API invocations from the application, if needed.
2147 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002148 status = PSA_ERROR_NOT_SUPPORTED;
2149 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002150 }
Archana8a180362021-07-05 02:18:48 +05302151 /*
2152 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302153 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302154 * - For opaque keys this translates to an invocation of the drivers'
2155 * copy_key entry point through the dispatch layer.
2156 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2158 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2159 &storage_size);
2160 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 }
Archana374fe5b2021-09-08 15:50:28 +05302163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2165 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302166 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 }
Archana374fe5b2021-09-08 15:50:28 +05302168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 status = psa_driver_wrapper_copy_key(&actual_attributes,
2170 source_slot->key.data,
2171 source_slot->key.bytes,
2172 target_slot->key.data,
2173 target_slot->key.bytes,
2174 &target_slot->key.bytes);
2175 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302176 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 }
2178 } else {
2179 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302180 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 source_slot->key.bytes);
2182 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302183 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 }
Archana8a180362021-07-05 02:18:48 +05302185 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (status != PSA_SUCCESS) {
2189 psa_fail_key_creation(target_slot, driver);
2190 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002195}
2196
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002197
2198
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002199/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002200/* Message digests */
2201/****************************************************************/
2202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002204{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002205 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (operation->id == 0) {
2207 return PSA_SUCCESS;
2208 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002211 operation->id = 0;
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002214}
2215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2217 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002218{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002219 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002220
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002221 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002223 status = PSA_ERROR_BAD_STATE;
2224 goto exit;
2225 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002228 status = PSA_ERROR_INVALID_ARGUMENT;
2229 goto exit;
2230 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002231
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002232 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2233 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002237
2238exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if (status != PSA_SUCCESS) {
2240 psa_hash_abort(operation);
2241 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002242
2243 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244}
2245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2247 const uint8_t *input,
2248 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002253 status = PSA_ERROR_BAD_STATE;
2254 goto exit;
2255 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002256
Steven Cooremanc8288352021-03-04 14:02:19 +01002257 /* Don't require hash implementations to behave correctly on a
2258 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if (input_length == 0) {
2260 return PSA_SUCCESS;
2261 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002264
2265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if (status != PSA_SUCCESS) {
2267 psa_hash_abort(operation);
2268 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271}
2272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2274 uint8_t *hash,
2275 size_t hash_size,
2276 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002277{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002278 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 if (operation->id == 0) {
2280 return PSA_ERROR_BAD_STATE;
2281 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002282
Steven Cooreman1e582352021-02-18 17:24:37 +01002283 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 operation, hash, hash_size, hash_length);
2285 psa_hash_abort(operation);
2286 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002287}
2288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2290 const uint8_t *hash,
2291 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292{
Ronald Cron69a63422021-10-18 09:47:58 +02002293 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002294 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002295 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 operation,
2297 actual_hash, sizeof(actual_hash),
2298 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002301 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002305 status = PSA_ERROR_INVALID_SIGNATURE;
2306 goto exit;
2307 }
2308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002310 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002312
2313exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2315 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002316 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320}
2321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322psa_status_t psa_hash_compute(psa_algorithm_t alg,
2323 const uint8_t *input, size_t input_length,
2324 uint8_t *hash, size_t hash_size,
2325 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002326{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002327 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (!PSA_ALG_IS_HASH(alg)) {
2329 return PSA_ERROR_INVALID_ARGUMENT;
2330 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2333 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002334}
2335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336psa_status_t psa_hash_compare(psa_algorithm_t alg,
2337 const uint8_t *input, size_t input_length,
2338 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002339{
Ronald Cron69a63422021-10-18 09:47:58 +02002340 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002341 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (!PSA_ALG_IS_HASH(alg)) {
2344 return PSA_ERROR_INVALID_ARGUMENT;
2345 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002346
Steven Cooreman1e582352021-02-18 17:24:37 +01002347 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 alg, input, input_length,
2349 actual_hash, sizeof(actual_hash),
2350 &actual_hash_length);
2351 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002352 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 }
2354 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002355 status = PSA_ERROR_INVALID_SIGNATURE;
2356 goto exit;
2357 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002359 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002361
2362exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2364 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002365}
2366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2368 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002369{
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (source_operation->id == 0 ||
2371 target_operation->id != 0) {
2372 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002373 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2376 target_operation);
2377 if (status != PSA_SUCCESS) {
2378 psa_hash_abort(target_operation);
2379 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002382}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002383
2384
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002385/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002386/* MAC */
2387/****************************************************************/
2388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002390{
Steven Cooremane6804192021-03-19 18:28:56 +01002391 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 if (operation->id == 0) {
2393 return PSA_SUCCESS;
2394 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002397 operation->mac_size = 0;
2398 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002399 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002402}
2403
Ronald Cron2dff3b22021-06-17 16:33:22 +02002404static psa_status_t psa_mac_finalize_alg_and_key_validation(
2405 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002406 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002408{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002409 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 psa_key_type_t key_type = psa_get_key_type(attributes);
2411 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 if (!PSA_ALG_IS_MAC(alg)) {
2414 return PSA_ERROR_INVALID_ARGUMENT;
2415 }
Steven Cooremane6804192021-03-19 18:28:56 +01002416
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002417 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 status = psa_mac_key_can_do(alg, key_type);
2419 if (status != PSA_SUCCESS) {
2420 return status;
2421 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002422
Steven Cooremand1a68f12021-05-10 11:13:41 +02002423 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002427 /* A very short MAC is too short for security since it can be
2428 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2429 * so we make this our minimum, even though 32 bits is still
2430 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002432 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2435 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002436 /* It's impossible to "truncate" to a larger length than the full length
2437 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002439 }
2440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002442 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2443 * that is disabled in the compile-time configuration. The result can
2444 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2445 * configuration into account. In this case, force a return of
2446 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2447 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2448 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2449 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2450 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002452 }
2453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002455}
2456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2458 mbedtls_svc_key_id_t key,
2459 psa_algorithm_t alg,
2460 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002461{
2462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2463 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002464 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002465
2466 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002468 status = PSA_ERROR_BAD_STATE;
2469 goto exit;
2470 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002471
2472 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 key,
2474 &slot,
2475 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2476 alg);
2477 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002478 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480
2481 psa_key_attributes_t attributes = {
2482 .core = slot->attr
2483 };
2484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2486 &operation->mac_size);
2487 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490
2491 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002492 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (is_sign) {
2494 status = psa_driver_wrapper_mac_sign_setup(operation,
2495 &attributes,
2496 slot->key.data,
2497 slot->key.bytes,
2498 alg);
2499 } else {
2500 status = psa_driver_wrapper_mac_verify_setup(operation,
2501 &attributes,
2502 slot->key.data,
2503 slot->key.bytes,
2504 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002506
Gilles Peskinefbfac682018-07-08 20:51:54 +02002507exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (status != PSA_SUCCESS) {
2509 psa_mac_abort(operation);
2510 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002515}
2516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2518 mbedtls_svc_key_id_t key,
2519 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002520{
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002522}
2523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2525 mbedtls_svc_key_id_t key,
2526 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002527{
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002529}
2530
Gilles Peskine449bd832023-01-11 14:50:10 +01002531psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2532 const uint8_t *input,
2533 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534{
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (operation->id == 0) {
2536 return PSA_ERROR_BAD_STATE;
2537 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002538
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002539 /* Don't require hash implementations to behave correctly on a
2540 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (input_length == 0) {
2542 return PSA_SUCCESS;
2543 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2546 input, input_length);
2547 if (status != PSA_SUCCESS) {
2548 psa_mac_abort(operation);
2549 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552}
2553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2555 uint8_t *mac,
2556 size_t mac_size,
2557 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002558{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2560 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002563 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002564 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002565 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002568 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002569 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002570 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002571
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002572 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2573 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002575 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002576 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002577 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002580 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002581 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002582 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 status = psa_driver_wrapper_mac_sign_finish(operation,
2585 mac, operation->mac_size,
2586 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002587
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002588exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002589 /* In case of success, set the potential excess room in the output buffer
2590 * to an invalid value, to avoid potentially leaking a longer MAC.
2591 * In case of error, set the output length and content to a safe default,
2592 * such that in case the caller misses an error check, the output would be
2593 * an unachievable MAC.
2594 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002596 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002597 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002598 }
mohammad16036df908f2018-04-02 08:34:15 -07002599
Paul Elliottdc42ca82023-02-24 18:11:59 +00002600 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002605}
2606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2608 const uint8_t *mac,
2609 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002611 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2612 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002615 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002616 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002617 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002618
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002620 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002621 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002622 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002625 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002626 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002627 }
2628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 status = psa_driver_wrapper_mac_verify_finish(operation,
2630 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002631
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002632exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002636}
2637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2639 psa_algorithm_t alg,
2640 const uint8_t *input,
2641 size_t input_length,
2642 uint8_t *mac,
2643 size_t mac_size,
2644 size_t *mac_length,
2645 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002646{
2647 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002648 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2649 psa_key_slot_t *slot;
2650 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002651
Ronald Cron51131b52021-06-17 17:17:20 +02002652 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 key,
2654 &slot,
2655 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2656 alg);
2657 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002658 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002660
Ronald Cron51131b52021-06-17 17:17:20 +02002661 psa_key_attributes_t attributes = {
2662 .core = slot->attr
2663 };
2664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2666 &operation_mac_size);
2667 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002668 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002672 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002673 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002674 }
2675
2676 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 &attributes,
2678 slot->key.data, slot->key.bytes,
2679 alg,
2680 input, input_length,
2681 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002682
2683exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002684 /* In case of success, set the potential excess room in the output buffer
2685 * to an invalid value, to avoid potentially leaking a longer MAC.
2686 * In case of error, set the output length and content to a safe default,
2687 * such that in case the caller misses an error check, the output would be
2688 * an unachievable MAC.
2689 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002691 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002692 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002693 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002694
2695 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002700}
2701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002703 psa_algorithm_t alg,
2704 const uint8_t *input,
2705 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 uint8_t *mac,
2707 size_t mac_size,
2708 size_t *mac_length)
2709{
2710 return psa_mac_compute_internal(key, alg,
2711 input, input_length,
2712 mac, mac_size, mac_length, 1);
2713}
2714
2715psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2716 psa_algorithm_t alg,
2717 const uint8_t *input,
2718 size_t input_length,
2719 const uint8_t *mac,
2720 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002721{
2722 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002723 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2724 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 status = psa_mac_compute_internal(key, alg,
2727 input, input_length,
2728 actual_mac, sizeof(actual_mac),
2729 &actual_mac_length, 0);
2730 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002731 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002735 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002736 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002737 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002739 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002740 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002741 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002742
2743exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002747}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002748
Gilles Peskinee59236f2018-01-27 23:32:46 +01002749/****************************************************************/
2750/* Asymmetric cryptography */
2751/****************************************************************/
2752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2754 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002755{
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 if (input_is_message) {
2757 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2758 return PSA_ERROR_INVALID_ARGUMENT;
2759 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2762 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2763 return PSA_ERROR_INVALID_ARGUMENT;
2764 }
2765 }
2766 } else {
2767 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2768 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002769 }
2770 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002771
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002773}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2776 int input_is_message,
2777 psa_algorithm_t alg,
2778 const uint8_t *input,
2779 size_t input_length,
2780 uint8_t *signature,
2781 size_t signature_size,
2782 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002783{
2784 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2785 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2786 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002787
2788 *signature_length = 0;
2789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 status = psa_sign_verify_check_alg(input_is_message, alg);
2791 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002792 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002794
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002795 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002796 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002797 * buffer can in principle be empty since it doesn't actually have
2798 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 if (signature_size == 0) {
2800 return PSA_ERROR_BUFFER_TOO_SMALL;
2801 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002802
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002803 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 key, &slot,
2805 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2806 PSA_KEY_USAGE_SIGN_HASH,
2807 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002810 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002814 status = PSA_ERROR_INVALID_ARGUMENT;
2815 goto exit;
2816 }
2817
2818 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002820 };
2821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002823 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002824 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002825 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 signature, signature_size, signature_length);
2827 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002828
2829 status = psa_driver_wrapper_sign_hash(
2830 &attributes, slot->key.data, slot->key.bytes,
2831 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002833 }
2834
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002835
2836exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002837 psa_wipe_tag_output_buffer(signature, status, signature_size,
2838 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002843}
2844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2846 int input_is_message,
2847 psa_algorithm_t alg,
2848 const uint8_t *input,
2849 size_t input_length,
2850 const uint8_t *signature,
2851 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002852{
2853 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2854 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2855 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 status = psa_sign_verify_check_alg(input_is_message, alg);
2858 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002859 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002861
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002862 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 key, &slot,
2864 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2865 PSA_KEY_USAGE_VERIFY_HASH,
2866 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (status != PSA_SUCCESS) {
2869 return status;
2870 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002871
2872 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002874 };
2875
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002877 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002878 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002879 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 signature, signature_length);
2881 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002882 status = psa_driver_wrapper_verify_hash(
2883 &attributes, slot->key.data, slot->key.bytes,
2884 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002886 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002891
2892}
2893
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002894psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002895 const psa_key_attributes_t *attributes,
2896 const uint8_t *key_buffer,
2897 size_t key_buffer_size,
2898 psa_algorithm_t alg,
2899 const uint8_t *input,
2900 size_t input_length,
2901 uint8_t *signature,
2902 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002904{
2905 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002908 size_t hash_length;
2909 uint8_t hash[PSA_HASH_MAX_SIZE];
2910
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002911 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 PSA_ALG_SIGN_GET_HASH(alg),
2913 input, input_length,
2914 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002917 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002919
2920 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 attributes, key_buffer, key_buffer_size,
2922 alg, hash, hash_length,
2923 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002924 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002927}
2928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2930 psa_algorithm_t alg,
2931 const uint8_t *input,
2932 size_t input_length,
2933 uint8_t *signature,
2934 size_t signature_size,
2935 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002936{
2937 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002938 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002940}
2941
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002942psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002943 const psa_key_attributes_t *attributes,
2944 const uint8_t *key_buffer,
2945 size_t key_buffer_size,
2946 psa_algorithm_t alg,
2947 const uint8_t *input,
2948 size_t input_length,
2949 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002951{
2952 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002955 size_t hash_length;
2956 uint8_t hash[PSA_HASH_MAX_SIZE];
2957
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002958 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 PSA_ALG_SIGN_GET_HASH(alg),
2960 input, input_length,
2961 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002964 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002966
2967 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 attributes, key_buffer, key_buffer_size,
2969 alg, hash, hash_length,
2970 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002971 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002974}
2975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2977 psa_algorithm_t alg,
2978 const uint8_t *input,
2979 size_t input_length,
2980 const uint8_t *signature,
2981 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002982{
2983 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002984 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002986}
2987
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002988psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002989 const psa_key_attributes_t *attributes,
2990 const uint8_t *key_buffer, size_t key_buffer_size,
2991 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002993{
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2995 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2996 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002997#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2999 return mbedtls_psa_rsa_sign_hash(
3000 attributes,
3001 key_buffer, key_buffer_size,
3002 alg, hash, hash_length,
3003 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003004#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3005 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 } else {
3007 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003008 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3010 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003011#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3013 return mbedtls_psa_ecdsa_sign_hash(
3014 attributes,
3015 key_buffer, key_buffer_size,
3016 alg, hash, hash_length,
3017 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003018#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3019 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 } else {
3021 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003022 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003023 }
Ronald Cron4501c982021-03-19 20:09:32 +01003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 (void) key_buffer;
3026 (void) key_buffer_size;
3027 (void) hash;
3028 (void) hash_length;
3029 (void) signature;
3030 (void) signature_size;
3031 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003034}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3037 psa_algorithm_t alg,
3038 const uint8_t *hash,
3039 size_t hash_length,
3040 uint8_t *signature,
3041 size_t signature_size,
3042 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003043{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003044 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003045 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003047}
3048
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003049psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003050 const psa_key_attributes_t *attributes,
3051 const uint8_t *key_buffer, size_t key_buffer_size,
3052 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003054{
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3056 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3057 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003058#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3060 return mbedtls_psa_rsa_verify_hash(
3061 attributes,
3062 key_buffer, key_buffer_size,
3063 alg, hash, hash_length,
3064 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003065#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3066 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 } else {
3068 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003069 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3071 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003072#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3074 return mbedtls_psa_ecdsa_verify_hash(
3075 attributes,
3076 key_buffer, key_buffer_size,
3077 alg, hash, hash_length,
3078 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003079#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3080 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 } else {
3082 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003083 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003084 }
Ronald Cron4501c982021-03-19 20:09:32 +01003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 (void) key_buffer;
3087 (void) key_buffer_size;
3088 (void) hash;
3089 (void) hash_length;
3090 (void) signature;
3091 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003094}
3095
Gilles Peskine449bd832023-01-11 14:50:10 +01003096psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3097 psa_algorithm_t alg,
3098 const uint8_t *hash,
3099 size_t hash_length,
3100 const uint8_t *signature,
3101 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003102{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003103 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003104 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003106}
3107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3109 psa_algorithm_t alg,
3110 const uint8_t *input,
3111 size_t input_length,
3112 const uint8_t *salt,
3113 size_t salt_length,
3114 uint8_t *output,
3115 size_t output_size,
3116 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003118 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003119 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003120 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003121
Darryl Green5cc689a2018-07-24 15:34:10 +01003122 (void) input;
3123 (void) input_length;
3124 (void) salt;
3125 (void) output;
3126 (void) output_size;
3127
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003128 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3131 return PSA_ERROR_INVALID_ARGUMENT;
3132 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003133
Ronald Cron5c522922020-11-14 16:35:34 +01003134 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3136 if (status != PSA_SUCCESS) {
3137 return status;
3138 }
3139 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3140 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003141 status = PSA_ERROR_INVALID_ARGUMENT;
3142 goto exit;
3143 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003144
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003145 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003147 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003148
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003149 status = psa_driver_wrapper_asymmetric_encrypt(
3150 &attributes, slot->key.data, slot->key.bytes,
3151 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003153exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003155
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003157}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003158
Gilles Peskine449bd832023-01-11 14:50:10 +01003159psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3160 psa_algorithm_t alg,
3161 const uint8_t *input,
3162 size_t input_length,
3163 const uint8_t *salt,
3164 size_t salt_length,
3165 uint8_t *output,
3166 size_t output_size,
3167 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003168{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003170 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003171 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003172
Darryl Green5cc689a2018-07-24 15:34:10 +01003173 (void) input;
3174 (void) input_length;
3175 (void) salt;
3176 (void) output;
3177 (void) output_size;
3178
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003179 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3182 return PSA_ERROR_INVALID_ARGUMENT;
3183 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003184
Ronald Cron5c522922020-11-14 16:35:34 +01003185 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3187 if (status != PSA_SUCCESS) {
3188 return status;
3189 }
3190 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003191 status = PSA_ERROR_INVALID_ARGUMENT;
3192 goto exit;
3193 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003195 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003197 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003198
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003199 status = psa_driver_wrapper_asymmetric_decrypt(
3200 &attributes, slot->key.data, slot->key.bytes,
3201 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003203
3204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003208}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003209
Paul Elliott9fe12f62022-11-30 19:16:02 +00003210/****************************************************************/
3211/* Asymmetric interruptible cryptography */
3212/****************************************************************/
3213
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003214static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3215
Paul Elliott9fe12f62022-11-30 19:16:02 +00003216void psa_interruptible_set_max_ops(uint32_t max_ops)
3217{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003218 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003219}
3220
3221uint32_t psa_interruptible_get_max_ops(void)
3222{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003223 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003224}
3225
Paul Elliott9fe12f62022-11-30 19:16:02 +00003226uint32_t psa_sign_hash_get_num_ops(
3227 const psa_sign_hash_interruptible_operation_t *operation)
3228{
Paul Elliott296ede92022-12-15 17:00:30 +00003229 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003230}
3231
3232uint32_t psa_verify_hash_get_num_ops(
3233 const psa_verify_hash_interruptible_operation_t *operation)
3234{
Paul Elliott296ede92022-12-15 17:00:30 +00003235 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003236}
3237
Paul Elliott59ad9452022-12-18 15:09:02 +00003238static psa_status_t psa_sign_hash_abort_internal(
3239 psa_sign_hash_interruptible_operation_t *operation)
3240{
3241 if (operation->id == 0) {
3242 /* The object has (apparently) been initialized but it is not (yet)
3243 * in use. It's ok to call abort on such an object, and there's
3244 * nothing to do. */
3245 return PSA_SUCCESS;
3246 }
3247
3248 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3249
3250 status = psa_driver_wrapper_sign_hash_abort(operation);
3251
3252 operation->id = 0;
3253
Paul Elliotte6145dc2023-02-07 12:51:21 +00003254 /* Do not clear either the error_occurred or num_ops elements here as they
3255 * only want to be cleared by the application calling abort, not by abort
3256 * being called at completion of an operation. */
3257
Paul Elliott59ad9452022-12-18 15:09:02 +00003258 return status;
3259}
3260
Paul Elliott9fe12f62022-11-30 19:16:02 +00003261psa_status_t psa_sign_hash_start(
3262 psa_sign_hash_interruptible_operation_t *operation,
3263 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3264 const uint8_t *hash, size_t hash_length)
3265{
3266 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3267 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3268 psa_key_slot_t *slot;
3269
Paul Elliottc9774412023-02-06 15:14:07 +00003270 /* Check that start has not been previously called, or operation has not
3271 * previously errored. */
3272 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003273 return PSA_ERROR_BAD_STATE;
3274 }
3275
Paul Elliott9fe12f62022-11-30 19:16:02 +00003276 status = psa_sign_verify_check_alg(0, alg);
3277 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003278 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003279 return status;
3280 }
3281
3282 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3283 PSA_KEY_USAGE_SIGN_HASH,
3284 alg);
3285
3286 if (status != PSA_SUCCESS) {
3287 goto exit;
3288 }
3289
3290 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3291 status = PSA_ERROR_INVALID_ARGUMENT;
3292 goto exit;
3293 }
3294
3295 psa_key_attributes_t attributes = {
3296 .core = slot->attr
3297 };
3298
Paul Elliott296ede92022-12-15 17:00:30 +00003299 /* Ensure ops count gets reset, in case of operation re-use. */
3300 operation->num_ops = 0;
3301
Paul Elliott9fe12f62022-11-30 19:16:02 +00003302 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3303 slot->key.data,
3304 slot->key.bytes, alg,
3305 hash, hash_length);
3306exit:
3307
3308 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003309 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003310 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003311 }
3312
3313 unlock_status = psa_unlock_key_slot(slot);
3314
Paul Elliottc9774412023-02-06 15:14:07 +00003315 if (unlock_status != PSA_SUCCESS) {
3316 operation->error_occurred = 1;
3317 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003318
Paul Elliottc9774412023-02-06 15:14:07 +00003319 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003320}
3321
3322
3323psa_status_t psa_sign_hash_complete(
3324 psa_sign_hash_interruptible_operation_t *operation,
3325 uint8_t *signature, size_t signature_size,
3326 size_t *signature_length)
3327{
3328 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3329
3330 *signature_length = 0;
3331
Paul Elliottc9774412023-02-06 15:14:07 +00003332 /* Check that start has been called first, and that operation has not
3333 * previously errored. */
3334 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335 status = PSA_ERROR_BAD_STATE;
3336 goto exit;
3337 }
3338
Paul Elliott096abc42023-02-03 18:33:23 +00003339 /* Immediately reject a zero-length signature buffer. This guarantees that
3340 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003341 if (signature_size == 0) {
3342 status = PSA_ERROR_BUFFER_TOO_SMALL;
3343 goto exit;
3344 }
3345
3346 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3347 signature_size,
3348 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003349
Paul Elliott296ede92022-12-15 17:00:30 +00003350 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003351 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003352
Paul Elliottebe225c2023-02-10 14:32:53 +00003353exit:
3354
Paul Elliott59200a22023-02-21 15:07:40 +00003355 psa_wipe_tag_output_buffer(signature, status, signature_size,
3356 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003357
Paul Elliott53bb3122023-02-10 14:22:22 +00003358 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003359 if (status != PSA_SUCCESS) {
3360 operation->error_occurred = 1;
3361 }
3362
Paul Elliott59ad9452022-12-18 15:09:02 +00003363 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003364 }
3365
3366 return status;
3367}
3368
3369psa_status_t psa_sign_hash_abort(
3370 psa_sign_hash_interruptible_operation_t *operation)
3371{
Paul Elliott59ad9452022-12-18 15:09:02 +00003372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3373
3374 status = psa_sign_hash_abort_internal(operation);
3375
3376 /* We clear the number of ops done here, so that it is not cleared when
3377 * the operation fails or succeeds, only on manual abort. */
3378 operation->num_ops = 0;
3379
Paul Elliottc9774412023-02-06 15:14:07 +00003380 /* Likewise, failure state. */
3381 operation->error_occurred = 0;
3382
Paul Elliott59ad9452022-12-18 15:09:02 +00003383 return status;
3384}
3385
3386static psa_status_t psa_verify_hash_abort_internal(
3387 psa_verify_hash_interruptible_operation_t *operation)
3388{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003389 if (operation->id == 0) {
3390 /* The object has (apparently) been initialized but it is not (yet)
3391 * in use. It's ok to call abort on such an object, and there's
3392 * nothing to do. */
3393 return PSA_SUCCESS;
3394 }
3395
Paul Elliott59ad9452022-12-18 15:09:02 +00003396 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3397
3398 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003399
3400 operation->id = 0;
3401
Paul Elliotte6145dc2023-02-07 12:51:21 +00003402 /* Do not clear either the error_occurred or num_ops elements here as they
3403 * only want to be cleared by the application calling abort, not by abort
3404 * being called at completion of an operation. */
3405
Paul Elliott59ad9452022-12-18 15:09:02 +00003406 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003407}
3408
3409psa_status_t psa_verify_hash_start(
3410 psa_verify_hash_interruptible_operation_t *operation,
3411 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3412 const uint8_t *hash, size_t hash_length,
3413 const uint8_t *signature, size_t signature_length)
3414{
3415 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3416 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3417 psa_key_slot_t *slot;
3418
Paul Elliottc9774412023-02-06 15:14:07 +00003419 /* Check that start has not been previously called, or operation has not
3420 * previously errored. */
3421 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003422 return PSA_ERROR_BAD_STATE;
3423 }
3424
3425 status = psa_sign_verify_check_alg(0, alg);
3426 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003427 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003428 return status;
3429 }
3430
3431 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3432 PSA_KEY_USAGE_VERIFY_HASH,
3433 alg);
3434
3435 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003436 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003437 return status;
3438 }
3439
3440 psa_key_attributes_t attributes = {
3441 .core = slot->attr
3442 };
3443
Paul Elliott296ede92022-12-15 17:00:30 +00003444 /* Ensure ops count gets reset, in case of operation re-use. */
3445 operation->num_ops = 0;
3446
Paul Elliott9fe12f62022-11-30 19:16:02 +00003447 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3448 slot->key.data,
3449 slot->key.bytes,
3450 alg, hash, hash_length,
3451 signature, signature_length);
3452
3453 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003454 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003455 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003456 }
3457
3458 unlock_status = psa_unlock_key_slot(slot);
3459
Paul Elliottc9774412023-02-06 15:14:07 +00003460 if (unlock_status != PSA_SUCCESS) {
3461 operation->error_occurred = 1;
3462 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003463
Paul Elliottc9774412023-02-06 15:14:07 +00003464 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003465}
3466
3467psa_status_t psa_verify_hash_complete(
3468 psa_verify_hash_interruptible_operation_t *operation)
3469{
3470 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3471
Paul Elliottc9774412023-02-06 15:14:07 +00003472 /* Check that start has been called first, and that operation has not
3473 * previously errored. */
3474 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475 status = PSA_ERROR_BAD_STATE;
3476 goto exit;
3477 }
3478
3479 status = psa_driver_wrapper_verify_hash_complete(operation);
3480
Paul Elliott296ede92022-12-15 17:00:30 +00003481 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003482 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003483 operation);
3484
Paul Elliottebe225c2023-02-10 14:32:53 +00003485exit:
3486
Paul Elliott9fe12f62022-11-30 19:16:02 +00003487 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003488 if (status != PSA_SUCCESS) {
3489 operation->error_occurred = 1;
3490 }
3491
Paul Elliott59ad9452022-12-18 15:09:02 +00003492 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003493 }
3494
3495 return status;
3496}
3497
3498psa_status_t psa_verify_hash_abort(
3499 psa_verify_hash_interruptible_operation_t *operation)
3500{
Paul Elliott59ad9452022-12-18 15:09:02 +00003501 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003502
Paul Elliott59ad9452022-12-18 15:09:02 +00003503 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003504
Paul Elliott59ad9452022-12-18 15:09:02 +00003505 /* We clear the number of ops done here, so that it is not cleared when
3506 * the operation fails or succeeds, only on manual abort. */
3507 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003508
Paul Elliottc9774412023-02-06 15:14:07 +00003509 /* Likewise, failure state. */
3510 operation->error_occurred = 0;
3511
Paul Elliott59ad9452022-12-18 15:09:02 +00003512 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003513}
3514
Paul Elliott588f8ed2022-12-02 18:10:26 +00003515/****************************************************************/
3516/* Asymmetric interruptible cryptography internal */
3517/* implementations */
3518/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003519
Paul Elliott588f8ed2022-12-02 18:10:26 +00003520void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3521{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003522
Paul Elliott588f8ed2022-12-02 18:10:26 +00003523#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3524 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3525 defined(MBEDTLS_ECP_RESTARTABLE)
3526
3527 /* Internal implementation uses zero to indicate infinite number max ops,
3528 * therefore avoid this value, and set to minimum possible. */
3529 if (max_ops == 0) {
3530 max_ops = 1;
3531 }
3532
Paul Elliott588f8ed2022-12-02 18:10:26 +00003533 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003534#else
3535 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003536#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3537 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3538 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3539}
3540
Paul Elliott588f8ed2022-12-02 18:10:26 +00003541uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003542 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003543{
3544#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3545 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3546 defined(MBEDTLS_ECP_RESTARTABLE)
3547
Paul Elliott93d9ca82023-02-15 18:14:21 +00003548 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003549#else
3550 (void) operation;
3551 return 0;
3552#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3553 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3554 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3555}
3556
3557uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003558 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003559{
3560 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3561 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3562 defined(MBEDTLS_ECP_RESTARTABLE)
3563
Paul Elliott93d9ca82023-02-15 18:14:21 +00003564 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003565#else
3566 (void) operation;
3567 return 0;
3568#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3569 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3570 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3571}
3572
3573psa_status_t mbedtls_psa_sign_hash_start(
3574 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3575 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3576 size_t key_buffer_size, psa_algorithm_t alg,
3577 const uint8_t *hash, size_t hash_length)
3578{
3579 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003580 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003581
Paul Elliott068fe072023-01-16 13:59:15 +00003582 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3583 return PSA_ERROR_NOT_SUPPORTED;
3584 }
3585
3586 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003587 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003588 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003589
3590#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003591 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3592 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003593
Paul Elliotta1c94092023-02-15 16:38:04 +00003594 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3595
Paul Elliott93d9ca82023-02-15 18:14:21 +00003596 /* Ensure num_ops is zero'ed in case of context re-use. */
3597 operation->num_ops = 0;
3598
Paul Elliott068fe072023-01-16 13:59:15 +00003599 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3600 attributes->core.bits,
3601 key_buffer,
3602 key_buffer_size,
3603 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003604
Paul Elliott068fe072023-01-16 13:59:15 +00003605 if (status != PSA_SUCCESS) {
3606 return status;
3607 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003608
Paul Elliott1bc59df2023-02-05 13:41:57 +00003609 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003610 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003611
Paul Elliott068fe072023-01-16 13:59:15 +00003612 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3613 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3614 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003615
Paul Elliott0290a762023-02-09 14:30:24 +00003616 /* We only need to store the same length of hash as the private key size
3617 * here, it would be truncated by the internal implementation anyway. */
3618 required_hash_length = (hash_length < operation->coordinate_bytes ?
3619 hash_length : operation->coordinate_bytes);
3620
Paul Elliottba70ad42023-02-15 18:23:53 +00003621 if (required_hash_length > sizeof(operation->hash)) {
3622 /* Shouldn't happen, but better safe than sorry. */
3623 return PSA_ERROR_CORRUPTION_DETECTED;
3624 }
3625
Paul Elliott0290a762023-02-09 14:30:24 +00003626 memcpy(operation->hash, hash, required_hash_length);
3627 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003628
3629 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003630
3631#else
Paul Elliott068fe072023-01-16 13:59:15 +00003632 (void) operation;
3633 (void) key_buffer;
3634 (void) key_buffer_size;
3635 (void) alg;
3636 (void) hash;
3637 (void) hash_length;
3638 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003639 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640
Paul Elliott068fe072023-01-16 13:59:15 +00003641 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003642#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3643 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3644 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003645}
3646
3647psa_status_t mbedtls_psa_sign_hash_complete(
3648 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3649 uint8_t *signature, size_t signature_size,
3650 size_t *signature_length)
3651{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003652#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3653 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3654 defined(MBEDTLS_ECP_RESTARTABLE)
3655
Paul Elliotta1c94092023-02-15 16:38:04 +00003656 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003657 mbedtls_mpi r;
3658 mbedtls_mpi s;
3659
Paul Elliott46845252023-02-03 14:59:11 +00003660 mbedtls_mpi_init(&r);
3661 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003662
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003663 /* Ensure max_ops is set to the current value (or default). */
3664 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3665
Paul Elliotta1c94092023-02-15 16:38:04 +00003666 if (signature_size < 2 * operation->coordinate_bytes) {
3667 status = PSA_ERROR_BUFFER_TOO_SMALL;
3668 goto exit;
3669 }
3670
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003672
Paul Elliott588f8ed2022-12-02 18:10:26 +00003673#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3674 status = mbedtls_to_psa_error(
3675 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003676 &r,
3677 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678 &operation->ctx->d,
3679 operation->hash,
3680 operation->hash_length,
3681 operation->md_alg,
3682 mbedtls_psa_get_random,
3683 MBEDTLS_PSA_RANDOM_STATE,
3684 &operation->restart_ctx));
3685#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003686 status = PSA_ERROR_NOT_SUPPORTED;
3687 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3689 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690 status = mbedtls_to_psa_error(
3691 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003692 &r,
3693 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003694 &operation->ctx->d,
3695 operation->hash,
3696 operation->hash_length,
3697 mbedtls_psa_get_random,
3698 MBEDTLS_PSA_RANDOM_STATE,
3699 mbedtls_psa_get_random,
3700 MBEDTLS_PSA_RANDOM_STATE,
3701 &operation->restart_ctx));
3702 }
3703
Paul Elliottf8e5b562023-02-19 18:43:45 +00003704 /* Hide the fact that the restart context only holds a delta of number of
3705 * ops done during the last operation, not an absolute value. */
3706 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3707
Paul Elliott724bd252023-02-08 12:35:08 +00003708 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003710 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003712 operation->coordinate_bytes)
3713 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003714
3715 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003716 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717 }
3718
3719 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003720 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003721 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003722 operation->coordinate_bytes,
3723 operation->coordinate_bytes)
3724 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725
3726 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003727 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003728 }
3729
Paul Elliott1bc59df2023-02-05 13:41:57 +00003730 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731
Paul Elliott724bd252023-02-08 12:35:08 +00003732 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003733 }
Paul Elliott724bd252023-02-08 12:35:08 +00003734
3735exit:
3736
3737 mbedtls_mpi_free(&r);
3738 mbedtls_mpi_free(&s);
3739 return status;
3740
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741 #else
3742
3743 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003744 (void) signature;
3745 (void) signature_size;
3746 (void) signature_length;
3747
3748 return PSA_ERROR_NOT_SUPPORTED;
3749
3750#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3751 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3752 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3753}
3754
3755psa_status_t mbedtls_psa_sign_hash_abort(
3756 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3757{
3758
3759#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3760 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3761 defined(MBEDTLS_ECP_RESTARTABLE)
3762
3763 if (operation->ctx) {
3764 mbedtls_ecdsa_free(operation->ctx);
3765 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003766 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767 }
3768
3769 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3770
Paul Elliott93d9ca82023-02-15 18:14:21 +00003771 operation->num_ops = 0;
3772
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773 return PSA_SUCCESS;
3774
3775#else
3776
3777 (void) operation;
3778
3779 return PSA_ERROR_NOT_SUPPORTED;
3780
3781#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3782 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3783 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3784}
3785
3786psa_status_t mbedtls_psa_verify_hash_start(
3787 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3788 const psa_key_attributes_t *attributes,
3789 const uint8_t *key_buffer, size_t key_buffer_size,
3790 psa_algorithm_t alg,
3791 const uint8_t *hash, size_t hash_length,
3792 const uint8_t *signature, size_t signature_length)
3793{
3794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003795 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003796 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003797
Paul Elliott068fe072023-01-16 13:59:15 +00003798 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3799 return PSA_ERROR_NOT_SUPPORTED;
3800 }
3801
3802 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003803 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003804 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805
3806#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003807 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3808 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003809
Paul Elliotta1c94092023-02-15 16:38:04 +00003810 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3811 mbedtls_mpi_init(&operation->r);
3812 mbedtls_mpi_init(&operation->s);
3813
Paul Elliott93d9ca82023-02-15 18:14:21 +00003814 /* Ensure num_ops is zero'ed in case of context re-use. */
3815 operation->num_ops = 0;
3816
Paul Elliott068fe072023-01-16 13:59:15 +00003817 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3818 attributes->core.bits,
3819 key_buffer,
3820 key_buffer_size,
3821 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003822
Paul Elliott068fe072023-01-16 13:59:15 +00003823 if (status != PSA_SUCCESS) {
3824 return status;
3825 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826
Paul Elliottc569fc22023-02-10 13:02:54 +00003827 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003828
Paul Elliott1bc59df2023-02-05 13:41:57 +00003829 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003830 return PSA_ERROR_INVALID_SIGNATURE;
3831 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003832
Paul Elliott068fe072023-01-16 13:59:15 +00003833 status = mbedtls_to_psa_error(
3834 mbedtls_mpi_read_binary(&operation->r,
3835 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003836 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837
Paul Elliott068fe072023-01-16 13:59:15 +00003838 if (status != PSA_SUCCESS) {
3839 return status;
3840 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841
Paul Elliott068fe072023-01-16 13:59:15 +00003842 status = mbedtls_to_psa_error(
3843 mbedtls_mpi_read_binary(&operation->s,
3844 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003845 coordinate_bytes,
3846 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003847
Paul Elliott068fe072023-01-16 13:59:15 +00003848 if (status != PSA_SUCCESS) {
3849 return status;
3850 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851
Paul Elliott2c9843f2023-02-15 17:32:42 +00003852 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853
Paul Elliott2c9843f2023-02-15 17:32:42 +00003854 if (status != PSA_SUCCESS) {
3855 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003856 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003857
Paul Elliott0290a762023-02-09 14:30:24 +00003858 /* We only need to store the same length of hash as the private key size
3859 * here, it would be truncated by the internal implementation anyway. */
3860 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3861 coordinate_bytes);
3862
Paul Elliottba70ad42023-02-15 18:23:53 +00003863 if (required_hash_length > sizeof(operation->hash)) {
3864 /* Shouldn't happen, but better safe than sorry. */
3865 return PSA_ERROR_CORRUPTION_DETECTED;
3866 }
3867
Paul Elliott0290a762023-02-09 14:30:24 +00003868 memcpy(operation->hash, hash, required_hash_length);
3869 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003870
3871 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003872#else
Paul Elliott068fe072023-01-16 13:59:15 +00003873 (void) operation;
3874 (void) key_buffer;
3875 (void) key_buffer_size;
3876 (void) alg;
3877 (void) hash;
3878 (void) hash_length;
3879 (void) signature;
3880 (void) signature_length;
3881 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003882 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003883 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884
Paul Elliott068fe072023-01-16 13:59:15 +00003885 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003886#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3887 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3888 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003889}
3890
3891psa_status_t mbedtls_psa_verify_hash_complete(
3892 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3893{
3894
3895#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3896 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3897 defined(MBEDTLS_ECP_RESTARTABLE)
3898
Paul Elliottf8e5b562023-02-19 18:43:45 +00003899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3900
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003901 /* Ensure max_ops is set to the current value (or default). */
3902 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3903
Paul Elliottf8e5b562023-02-19 18:43:45 +00003904 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003905 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3906 operation->hash,
3907 operation->hash_length,
3908 &operation->ctx->Q,
3909 &operation->r,
3910 &operation->s,
3911 &operation->restart_ctx));
3912
Paul Elliottf8e5b562023-02-19 18:43:45 +00003913 /* Hide the fact that the restart context only holds a delta of number of
3914 * ops done during the last operation, not an absolute value. */
3915 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3916
3917 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003918#else
3919 (void) operation;
3920
3921 return PSA_ERROR_NOT_SUPPORTED;
3922
3923#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3924 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3925 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3926}
3927
3928psa_status_t mbedtls_psa_verify_hash_abort(
3929 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3930{
3931
3932#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3933 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3934 defined(MBEDTLS_ECP_RESTARTABLE)
3935
3936 if (operation->ctx) {
3937 mbedtls_ecdsa_free(operation->ctx);
3938 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003939 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003940 }
3941
3942 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3943
Paul Elliott93d9ca82023-02-15 18:14:21 +00003944 operation->num_ops = 0;
3945
Paul Elliott588f8ed2022-12-02 18:10:26 +00003946 mbedtls_mpi_free(&operation->r);
3947 mbedtls_mpi_free(&operation->s);
3948
3949 return PSA_SUCCESS;
3950
3951#else
3952 (void) operation;
3953
3954 return PSA_ERROR_NOT_SUPPORTED;
3955
3956#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3957 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3958 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3959}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003960
mohammad1603503973b2018-03-12 15:59:30 +02003961/****************************************************************/
3962/* Symmetric cryptography */
3963/****************************************************************/
3964
Gilles Peskine449bd832023-01-11 14:50:10 +01003965static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3966 mbedtls_svc_key_id_t key,
3967 psa_algorithm_t alg,
3968 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003969{
3970 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3971 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003972 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3974 PSA_KEY_USAGE_ENCRYPT :
3975 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003976
3977 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003979 status = PSA_ERROR_BAD_STATE;
3980 goto exit;
3981 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003982
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003984 status = PSA_ERROR_INVALID_ARGUMENT;
3985 goto exit;
3986 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003987
Gilles Peskine449bd832023-01-11 14:50:10 +01003988 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3989 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003990 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003992
Ronald Cron49fafa92021-03-10 08:34:23 +01003993 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003994 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003995 * so we only set it (in the driver wrapper) after resources have been
3996 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003997 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003999 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004001 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 }
4003 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004004
Ronald Crona4af55f2020-12-14 14:36:06 +01004005 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004007 };
4008
Ronald Cronab99ac22020-10-01 16:38:28 +02004009 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 if (cipher_operation == MBEDTLS_ENCRYPT) {
4011 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4012 &attributes,
4013 slot->key.data,
4014 slot->key.bytes,
4015 alg);
4016 } else {
4017 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4018 &attributes,
4019 slot->key.data,
4020 slot->key.bytes,
4021 alg);
4022 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004023
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004024exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 if (status != PSA_SUCCESS) {
4026 psa_cipher_abort(operation);
4027 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004028
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004030
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004032}
4033
Gilles Peskine449bd832023-01-11 14:50:10 +01004034psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4035 mbedtls_svc_key_id_t key,
4036 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004037{
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004039}
4040
Gilles Peskine449bd832023-01-11 14:50:10 +01004041psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4042 mbedtls_svc_key_id_t key,
4043 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004044{
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004046}
4047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4049 uint8_t *iv,
4050 size_t iv_size,
4051 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004052{
Ronald Cron6d051732020-10-01 14:10:20 +02004053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004054 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4055 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004058 status = PSA_ERROR_BAD_STATE;
4059 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004060 }
4061
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004063 status = PSA_ERROR_BAD_STATE;
4064 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004065 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004066
Ronald Cron2fb90522021-07-05 12:31:44 +02004067 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004069 status = PSA_ERROR_BUFFER_TOO_SMALL;
4070 goto exit;
4071 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004072
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004074 status = PSA_ERROR_GENERIC_ERROR;
4075 goto exit;
4076 }
4077
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 status = psa_generate_random(local_iv, default_iv_length);
4079 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004080 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 }
Ronald Cron5618a392021-03-26 09:52:26 +01004082
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 status = psa_driver_wrapper_cipher_set_iv(operation,
4084 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004085
4086exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 if (status == PSA_SUCCESS) {
4088 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004089 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004090 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004092 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004094 }
Ronald Cron6d051732020-10-01 14:10:20 +02004095
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004097}
4098
Gilles Peskine449bd832023-01-11 14:50:10 +01004099psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4100 const uint8_t *iv,
4101 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004102{
Ronald Cron6d051732020-10-01 14:10:20 +02004103 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004106 status = PSA_ERROR_BAD_STATE;
4107 goto exit;
4108 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004109
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004111 status = PSA_ERROR_BAD_STATE;
4112 goto exit;
4113 }
Ronald Crona0d68172021-03-26 10:15:08 +01004114
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004116 status = PSA_ERROR_INVALID_ARGUMENT;
4117 goto exit;
4118 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004119
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 status = psa_driver_wrapper_cipher_set_iv(operation,
4121 iv,
4122 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004123
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004124exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004126 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 } else {
4128 psa_cipher_abort(operation);
4129 }
4130 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004131}
4132
Gilles Peskine449bd832023-01-11 14:50:10 +01004133psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4134 const uint8_t *input,
4135 size_t input_length,
4136 uint8_t *output,
4137 size_t output_size,
4138 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004139{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004140 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004141
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004143 status = PSA_ERROR_BAD_STATE;
4144 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004145 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004146
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004148 status = PSA_ERROR_BAD_STATE;
4149 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004150 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 status = psa_driver_wrapper_cipher_update(operation,
4153 input,
4154 input_length,
4155 output,
4156 output_size,
4157 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004158
4159exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 if (status != PSA_SUCCESS) {
4161 psa_cipher_abort(operation);
4162 }
Ronald Cron6d051732020-10-01 14:10:20 +02004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004165}
4166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4168 uint8_t *output,
4169 size_t output_size,
4170 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004171{
David Saadab4ecc272019-02-14 13:48:10 +02004172 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004173
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004175 status = PSA_ERROR_BAD_STATE;
4176 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004177 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004178
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004180 status = PSA_ERROR_BAD_STATE;
4181 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004182 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004183
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 status = psa_driver_wrapper_cipher_finish(operation,
4185 output,
4186 output_size,
4187 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004188
4189exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 if (status == PSA_SUCCESS) {
4191 return psa_cipher_abort(operation);
4192 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004193 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004197 }
mohammad1603503973b2018-03-12 15:59:30 +02004198}
4199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004201{
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004203 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004204 * in use. It's ok to call abort on such an object, and there's
4205 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004207 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004210
Ronald Cron49fafa92021-03-10 08:34:23 +01004211 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004212 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004213 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004214
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004216}
4217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4219 psa_algorithm_t alg,
4220 const uint8_t *input,
4221 size_t input_length,
4222 uint8_t *output,
4223 size_t output_size,
4224 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004225{
4226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004227 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004228 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004229 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4230 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004231
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004233 status = PSA_ERROR_INVALID_ARGUMENT;
4234 goto exit;
4235 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4238 PSA_KEY_USAGE_ENCRYPT,
4239 alg);
4240 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004241 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004243
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004244 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004246 };
4247
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4249 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004250 status = PSA_ERROR_GENERIC_ERROR;
4251 goto exit;
4252 }
4253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 if (default_iv_length > 0) {
4255 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004256 status = PSA_ERROR_BUFFER_TOO_SMALL;
4257 goto exit;
4258 }
4259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 status = psa_generate_random(local_iv, default_iv_length);
4261 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004262 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004264 }
4265
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004266 status = psa_driver_wrapper_cipher_encrypt(
4267 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004268 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004269 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004271
4272exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 unlock_status = psa_unlock_key_slot(slot);
4274 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004275 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004276 }
Ronald Cron23919522021-07-08 19:00:07 +02004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 if (status == PSA_SUCCESS) {
4279 if (default_iv_length > 0) {
4280 memcpy(output, local_iv, default_iv_length);
4281 }
4282 *output_length += default_iv_length;
4283 } else {
4284 *output_length = 0;
4285 }
4286
4287 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004288}
4289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4291 psa_algorithm_t alg,
4292 const uint8_t *input,
4293 size_t input_length,
4294 uint8_t *output,
4295 size_t output_size,
4296 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004297{
4298 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004299 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004300 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004301
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004303 status = PSA_ERROR_INVALID_ARGUMENT;
4304 goto exit;
4305 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004306
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4308 PSA_KEY_USAGE_DECRYPT,
4309 alg);
4310 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004311 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004313
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004314 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004316 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4319 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004320 status = PSA_ERROR_INVALID_ARGUMENT;
4321 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004323 status = PSA_ERROR_INVALID_ARGUMENT;
4324 goto exit;
4325 }
4326
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004327 status = psa_driver_wrapper_cipher_decrypt(
4328 &attributes, slot->key.data, slot->key.bytes,
4329 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004331
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004332exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 unlock_status = psa_unlock_key_slot(slot);
4334 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004335 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004337
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004339 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 }
Ronald Cron23919522021-07-08 19:00:07 +02004341
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004343}
4344
4345
mohammad16035955c982018-04-26 00:53:03 +03004346/****************************************************************/
4347/* AEAD */
4348/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004349
Paul Elliottbaff51c2021-09-28 17:44:45 +01004350/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004351static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004352{
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004354}
4355
4356/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004357static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4358 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004359{
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004361
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004363#if defined(PSA_WANT_ALG_GCM)
4364 case PSA_ALG_GCM:
4365 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 * arbitrarily large nonces. Please note that we do not generally
4367 * recommend the usage of nonces of greater length than
4368 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4369 * size, which can then lead to collisions if you encrypt a very
4370 * large number of messages.*/
4371 if (nonce_length != 0) {
4372 return PSA_SUCCESS;
4373 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004374 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004375#endif /* PSA_WANT_ALG_GCM */
4376#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004377 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 if (nonce_length >= 7 && nonce_length <= 13) {
4379 return PSA_SUCCESS;
4380 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004381 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004382#endif /* PSA_WANT_ALG_CCM */
4383#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004384 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 if (nonce_length == 12) {
4386 return PSA_SUCCESS;
4387 } else if (nonce_length == 8) {
4388 return PSA_ERROR_NOT_SUPPORTED;
4389 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004390 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004391#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004392 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004393 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004395 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004398}
4399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004401{
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4403 return PSA_ERROR_INVALID_ARGUMENT;
4404 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004407}
4408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4410 psa_algorithm_t alg,
4411 const uint8_t *nonce,
4412 size_t nonce_length,
4413 const uint8_t *additional_data,
4414 size_t additional_data_length,
4415 const uint8_t *plaintext,
4416 size_t plaintext_length,
4417 uint8_t *ciphertext,
4418 size_t ciphertext_size,
4419 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004420{
Ronald Cron215633c2021-03-16 17:15:37 +01004421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4422 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004423
mohammad1603f08a5502018-06-03 15:05:47 +03004424 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004425
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 status = psa_aead_check_algorithm(alg);
4427 if (status != PSA_SUCCESS) {
4428 return status;
4429 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004430
Ronald Cron9a986162021-03-26 12:40:07 +01004431 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4433 if (status != PSA_SUCCESS) {
4434 return status;
4435 }
mohammad16035955c982018-04-26 00:53:03 +03004436
Ronald Cron215633c2021-03-16 17:15:37 +01004437 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004439 };
mohammad16035955c982018-04-26 00:53:03 +03004440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 status = psa_aead_check_nonce_length(alg, nonce_length);
4442 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004443 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004445
Ronald Cronde822812021-03-17 16:08:20 +01004446 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004447 &attributes, slot->key.data, slot->key.bytes,
4448 alg,
4449 nonce, nonce_length,
4450 additional_data, additional_data_length,
4451 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4455 memset(ciphertext, 0, ciphertext_size);
4456 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004457
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004458exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004462}
4463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4465 psa_algorithm_t alg,
4466 const uint8_t *nonce,
4467 size_t nonce_length,
4468 const uint8_t *additional_data,
4469 size_t additional_data_length,
4470 const uint8_t *ciphertext,
4471 size_t ciphertext_length,
4472 uint8_t *plaintext,
4473 size_t plaintext_size,
4474 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004475{
Ronald Cron215633c2021-03-16 17:15:37 +01004476 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4477 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004478
Gilles Peskineee652a32018-06-01 19:23:52 +02004479 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004480
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 status = psa_aead_check_algorithm(alg);
4482 if (status != PSA_SUCCESS) {
4483 return status;
4484 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004485
Ronald Cron9a986162021-03-26 12:40:07 +01004486 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4488 if (status != PSA_SUCCESS) {
4489 return status;
4490 }
mohammad16035955c982018-04-26 00:53:03 +03004491
Ronald Cron215633c2021-03-16 17:15:37 +01004492 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004494 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004495
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 status = psa_aead_check_nonce_length(alg, nonce_length);
4497 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004498 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004500
Ronald Cronde822812021-03-17 16:08:20 +01004501 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004502 &attributes, slot->key.data, slot->key.bytes,
4503 alg,
4504 nonce, nonce_length,
4505 additional_data, additional_data_length,
4506 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004508
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 if (status != PSA_SUCCESS && plaintext_size != 0) {
4510 memset(plaintext, 0, plaintext_size);
4511 }
mohammad160360a64d02018-06-03 17:20:42 +03004512
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004513exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 return status;
mohammad16035955c982018-04-26 00:53:03 +03004517}
4518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4520{
4521 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004524#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004526 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4528 return PSA_ERROR_INVALID_ARGUMENT;
4529 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004530 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004531#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004532
Przemek Stekiel86679c72022-10-06 17:06:56 +02004533#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004535 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4537 return PSA_ERROR_INVALID_ARGUMENT;
4538 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004539 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004540#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004541
Przemek Stekiel86679c72022-10-06 17:06:56 +02004542#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004544 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 if (tag_len != 16) {
4546 return PSA_ERROR_INVALID_ARGUMENT;
4547 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004548 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004549#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004550
4551 default:
4552 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004554 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004556}
4557
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004558/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004559static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4560 int is_encrypt,
4561 mbedtls_svc_key_id_t key,
4562 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004563{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004564 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4565 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4566 psa_key_slot_t *slot = NULL;
4567 psa_key_usage_t key_usage = 0;
4568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 status = psa_aead_check_algorithm(alg);
4570 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004571 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004575 status = PSA_ERROR_BAD_STATE;
4576 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004577 }
4578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 if (operation->nonce_set || operation->lengths_set ||
4580 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004581 status = PSA_ERROR_BAD_STATE;
4582 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004583 }
4584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004586 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004588 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4592 alg);
4593 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004594 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004596
4597 psa_key_attributes_t attributes = {
4598 .core = slot->attr
4599 };
4600
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004602 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 if (is_encrypt) {
4606 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4607 &attributes,
4608 slot->key.data,
4609 slot->key.bytes,
4610 alg);
4611 } else {
4612 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4613 &attributes,
4614 slot->key.data,
4615 slot->key.bytes,
4616 alg);
4617 }
4618 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004623
4624exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004628 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004630 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 } else {
4632 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004633 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004636}
4637
Paul Elliott302ff6b2021-04-20 18:10:30 +01004638/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004639psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4640 mbedtls_svc_key_id_t key,
4641 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004642{
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004644}
4645
4646/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004647psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4648 mbedtls_svc_key_id_t key,
4649 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004650{
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004652}
4653
4654/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004655psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4656 uint8_t *nonce,
4657 size_t nonce_size,
4658 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004659{
4660 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004661 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004662 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004663
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004664 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004667 status = PSA_ERROR_BAD_STATE;
4668 goto exit;
4669 }
4670
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004672 status = PSA_ERROR_BAD_STATE;
4673 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004674 }
4675
Paul Elliotte193ea82021-10-01 13:00:16 +01004676 /* For CCM, this size may not be correct according to the PSA
4677 * specification. The PSA Crypto 1.0.1 specification states:
4678 *
4679 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4680 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4681 *
4682 * However this restriction that L has to be the smallest integer is not
4683 * applied in practice, and it is not implementable here since the
4684 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4686 operation->alg);
4687 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004688 status = PSA_ERROR_BUFFER_TOO_SMALL;
4689 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004690 }
4691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 status = psa_generate_random(local_nonce, required_nonce_size);
4693 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004694 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004696
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004698
Paul Elliott39dc6b82021-05-11 19:16:09 +01004699exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 if (status == PSA_SUCCESS) {
4701 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004702 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 } else {
4704 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004705 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004706
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004708}
4709
4710/* Set the nonce for a multipart authenticated encryption or decryption
4711 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004712psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4713 const uint8_t *nonce,
4714 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004715{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004716 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004719 status = PSA_ERROR_BAD_STATE;
4720 goto exit;
4721 }
4722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004724 status = PSA_ERROR_BAD_STATE;
4725 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004726 }
4727
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4729 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004730 status = PSA_ERROR_INVALID_ARGUMENT;
4731 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004732 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4735 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004736
Paul Elliott39dc6b82021-05-11 19:16:09 +01004737exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004739 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 } else {
4741 psa_aead_abort(operation);
4742 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004745}
4746
4747/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004748psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4749 size_t ad_length,
4750 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004751{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004752 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004755 status = PSA_ERROR_BAD_STATE;
4756 goto exit;
4757 }
4758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 if (operation->lengths_set || operation->ad_started ||
4760 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004761 status = PSA_ERROR_BAD_STATE;
4762 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004763 }
4764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004766#if defined(PSA_WANT_ALG_GCM)
4767 case PSA_ALG_GCM:
4768 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 * bits. Without the guard this code will generate warnings on 32bit
4770 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004771#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 if (((uint64_t) ad_length) >> 61 != 0 ||
4773 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004774 status = PSA_ERROR_INVALID_ARGUMENT;
4775 goto exit;
4776 }
Paul Elliott325d3742021-09-27 17:56:28 +01004777#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004778 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004779#endif /* PSA_WANT_ALG_GCM */
4780#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004781 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004783 status = PSA_ERROR_INVALID_ARGUMENT;
4784 goto exit;
4785 }
4786 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004787#endif /* PSA_WANT_ALG_CCM */
4788#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004789 case PSA_ALG_CHACHA20_POLY1305:
4790 /* No length restrictions for ChaChaPoly. */
4791 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004792#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004793 default:
4794 break;
4795 }
Paul Elliott325d3742021-09-27 17:56:28 +01004796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4798 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004799
Paul Elliott39dc6b82021-05-11 19:16:09 +01004800exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004802 operation->ad_remaining = ad_length;
4803 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004804 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 } else {
4806 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004807 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004810}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004811
4812/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004813psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4814 const uint8_t *input,
4815 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004816{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004817 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4818
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004820 status = PSA_ERROR_BAD_STATE;
4821 goto exit;
4822 }
4823
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004825 status = PSA_ERROR_BAD_STATE;
4826 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004827 }
4828
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 if (operation->lengths_set) {
4830 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004831 status = PSA_ERROR_INVALID_ARGUMENT;
4832 goto exit;
4833 }
4834
4835 operation->ad_remaining -= input_length;
4836 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004837#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004839 status = PSA_ERROR_BAD_STATE;
4840 goto exit;
4841 }
4842#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 status = psa_driver_wrapper_aead_update_ad(operation, input,
4845 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004846
Paul Elliott39dc6b82021-05-11 19:16:09 +01004847exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004849 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 } else {
4851 psa_aead_abort(operation);
4852 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004853
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004855}
4856
4857/* Encrypt or decrypt a message fragment in an active multipart AEAD
4858 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004859psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4860 const uint8_t *input,
4861 size_t input_length,
4862 uint8_t *output,
4863 size_t output_size,
4864 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004865{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004866 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004867
4868 *output_length = 0;
4869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004871 status = PSA_ERROR_BAD_STATE;
4872 goto exit;
4873 }
4874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004876 status = PSA_ERROR_BAD_STATE;
4877 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004878 }
4879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004881 /* Additional data length was supplied, but not all the additional
4882 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004884 status = PSA_ERROR_INVALID_ARGUMENT;
4885 goto exit;
4886 }
4887
4888 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004890 status = PSA_ERROR_INVALID_ARGUMENT;
4891 goto exit;
4892 }
4893
4894 operation->body_remaining -= input_length;
4895 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004896#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004898 status = PSA_ERROR_BAD_STATE;
4899 goto exit;
4900 }
4901#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4904 output, output_size,
4905 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004906
Paul Elliott39dc6b82021-05-11 19:16:09 +01004907exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004909 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 } else {
4911 psa_aead_abort(operation);
4912 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004913
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004915}
4916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004918{
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 if (operation->id == 0 || !operation->nonce_set) {
4920 return PSA_ERROR_BAD_STATE;
4921 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004922
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4924 operation->body_remaining != 0)) {
4925 return PSA_ERROR_INVALID_ARGUMENT;
4926 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004929}
4930
Paul Elliott302ff6b2021-04-20 18:10:30 +01004931/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004932psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4933 uint8_t *ciphertext,
4934 size_t ciphertext_size,
4935 size_t *ciphertext_length,
4936 uint8_t *tag,
4937 size_t tag_size,
4938 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004939{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004940 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4941
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004943 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004944
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 status = psa_aead_final_checks(operation);
4946 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004947 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004951 status = PSA_ERROR_BAD_STATE;
4952 goto exit;
4953 }
4954
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4956 ciphertext_size,
4957 ciphertext_length,
4958 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004959
Paul Elliott39dc6b82021-05-11 19:16:09 +01004960exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004961
4962
Paul Elliottf47b0952021-05-21 18:02:33 +01004963 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004964 * the zero tag size, make sure the tag is set to something implausible.
4965 * Even if the operation succeeds, make sure we clear the rest of the
4966 * buffer to prevent potential leakage of anything previously placed in
4967 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004968 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004971
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004973}
4974
4975/* Finish authenticating and decrypting a message in a multipart AEAD
4976 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004977psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4978 uint8_t *plaintext,
4979 size_t plaintext_size,
4980 size_t *plaintext_length,
4981 const uint8_t *tag,
4982 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004984 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4985
Paul Elliott302ff6b2021-04-20 18:10:30 +01004986 *plaintext_length = 0;
4987
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 status = psa_aead_final_checks(operation);
4989 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004990 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004992
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004994 status = PSA_ERROR_BAD_STATE;
4995 goto exit;
4996 }
4997
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4999 plaintext_size,
5000 plaintext_length,
5001 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005002
5003exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005005
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005007}
5008
5009/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005010psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005011{
5012 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5013
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005015 /* The object has (apparently) been initialized but it is not (yet)
5016 * in use. It's ok to call abort on such an object, and there's
5017 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005019 }
5020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005022
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005024
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005026}
5027
Gilles Peskinee59236f2018-01-27 23:32:46 +01005028/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005029/* Generators */
5030/****************************************************************/
5031
Przemek Stekiel69c46792022-06-10 12:59:51 +02005032#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005033 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005034 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305035 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
5036 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
John Durkop07cc04a2020-11-16 22:08:34 -08005037#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005038#endif /* At least one builtin KDF */
5039
Przemek Stekiel69c46792022-06-10 12:59:51 +02005040#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005041 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5042 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5043static psa_status_t psa_key_derivation_start_hmac(
5044 psa_mac_operation_t *operation,
5045 psa_algorithm_t hash_alg,
5046 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005048{
5049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5050 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5052 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5053 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005054
Steven Cooreman72f736a2021-05-07 14:14:37 +02005055 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 status = psa_driver_wrapper_mac_sign_setup(operation,
5059 &attributes,
5060 hmac_key, hmac_key_length,
5061 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005062
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 psa_reset_key_attributes(&attributes);
5064 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005065}
5066#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005067
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005068#define HKDF_STATE_INIT 0 /* no input yet */
5069#define HKDF_STATE_STARTED 1 /* got salt */
5070#define HKDF_STATE_KEYED 2 /* got key */
5071#define HKDF_STATE_OUTPUT 3 /* output started */
5072
Gilles Peskinecbe66502019-05-16 16:59:18 +02005073static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005075{
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5077 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5078 } else {
5079 return operation->alg;
5080 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005081}
5082
Gilles Peskine449bd832023-01-11 14:50:10 +01005083psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005084{
5085 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5087 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005088 /* The object has (apparently) been initialized but it is not
5089 * in use. It's ok to call abort on such an object, and there's
5090 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005092#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5094 mbedtls_free(operation->ctx.hkdf.info);
5095 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5096 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005097#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005098#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5099 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5101 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5102 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5103 if (operation->ctx.tls12_prf.secret != NULL) {
5104 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5105 operation->ctx.tls12_prf.secret_length);
5106 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005107 }
5108
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 if (operation->ctx.tls12_prf.seed != NULL) {
5110 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5111 operation->ctx.tls12_prf.seed_length);
5112 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005113 }
5114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 if (operation->ctx.tls12_prf.label != NULL) {
5116 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5117 operation->ctx.tls12_prf.label_length);
5118 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005119 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005120#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 if (operation->ctx.tls12_prf.other_secret != NULL) {
5122 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5123 operation->ctx.tls12_prf.other_secret_length);
5124 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005125 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005126#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005127 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005128
5129 /* We leave the fields Ai and output_block to be erased safely by the
5130 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005132#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5133 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005134#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5136 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5137 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5138 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005139#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305140#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5141 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305142 if (operation->ctx.pbkdf2.salt != NULL) {
5143 mbedtls_platform_zeroize(operation->ctx.pbkdf2.salt,
5144 operation->ctx.pbkdf2.salt_length);
5145 mbedtls_free(operation->ctx.pbkdf2.salt);
5146 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305147
5148 status = PSA_SUCCESS;
5149 } else
5150#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005151 {
5152 status = PSA_ERROR_BAD_STATE;
5153 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 mbedtls_platform_zeroize(operation, sizeof(*operation));
5155 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005156}
5157
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005158psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005160{
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005164 }
5165
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005166 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005168}
5169
Gilles Peskine449bd832023-01-11 14:50:10 +01005170psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5171 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005172{
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if (operation->alg == 0) {
5174 return PSA_ERROR_BAD_STATE;
5175 }
5176 if (capacity > operation->capacity) {
5177 return PSA_ERROR_INVALID_ARGUMENT;
5178 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005179 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005181}
5182
Przemek Stekiel69c46792022-06-10 12:59:51 +02005183#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005184/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005185static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5186 psa_algorithm_t kdf_alg,
5187 uint8_t *output,
5188 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005189{
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5191 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005192 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005193 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005194#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005196#else
5197 const uint8_t last_block = 0xff;
5198#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005199
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 if (hkdf->state < HKDF_STATE_KEYED ||
5201 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005202#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005204#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 )) {
5206 return PSA_ERROR_BAD_STATE;
5207 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005208 hkdf->state = HKDF_STATE_OUTPUT;
5209
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005211 /* Copy what remains of the current block */
5212 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005214 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 }
5216 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005217 output += n;
5218 output_length -= n;
5219 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005221 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005223 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005224 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005225 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005226 * object was corrupted or if this function is called directly
5227 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 if (hkdf->block_number == last_block) {
5229 return PSA_ERROR_BAD_STATE;
5230 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005231
5232 /* We need a new block */
5233 ++hkdf->block_number;
5234 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005235
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5237 hash_alg,
5238 hkdf->prk,
5239 hash_length);
5240 if (status != PSA_SUCCESS) {
5241 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005242 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005243
5244 if (hkdf->block_number != 1) {
5245 status = psa_mac_update(&hkdf->hmac,
5246 hkdf->output_block,
5247 hash_length);
5248 if (status != PSA_SUCCESS) {
5249 return status;
5250 }
5251 }
5252 status = psa_mac_update(&hkdf->hmac,
5253 hkdf->info,
5254 hkdf->info_length);
5255 if (status != PSA_SUCCESS) {
5256 return status;
5257 }
5258 status = psa_mac_update(&hkdf->hmac,
5259 &hkdf->block_number, 1);
5260 if (status != PSA_SUCCESS) {
5261 return status;
5262 }
5263 status = psa_mac_sign_finish(&hkdf->hmac,
5264 hkdf->output_block,
5265 sizeof(hkdf->output_block),
5266 &hmac_output_length);
5267 if (status != PSA_SUCCESS) {
5268 return status;
5269 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005270 }
5271
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005273}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005274#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005275
John Durkop07cc04a2020-11-16 22:08:34 -08005276#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5277 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005278static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5279 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005281{
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5283 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005284 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5285 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005286 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005287
Janos Follath7742fee2019-06-17 12:58:10 +01005288 /* We can't be wanting more output after block 0xff, otherwise
5289 * the capacity check in psa_key_derivation_output_bytes() would have
5290 * prevented this call. It could happen only if the operation
5291 * object was corrupted or if this function is called directly
5292 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 if (tls12_prf->block_number == 0xff) {
5294 return PSA_ERROR_CORRUPTION_DETECTED;
5295 }
Janos Follath7742fee2019-06-17 12:58:10 +01005296
5297 /* We need a new block */
5298 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005299 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005300
5301 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5302 *
5303 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5304 *
5305 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5306 * HMAC_hash(secret, A(2) + seed) +
5307 * HMAC_hash(secret, A(3) + seed) + ...
5308 *
5309 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005310 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005311 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005312 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005313 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005314 * is currently extracted as `output_block` and where i is
5315 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005316 */
5317
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 status = psa_key_derivation_start_hmac(&hmac,
5319 hash_alg,
5320 tls12_prf->secret,
5321 tls12_prf->secret_length);
5322 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005323 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005325
5326 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005328 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5329 * the variable seed and in this instance means it in the context of the
5330 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 status = psa_mac_update(&hmac,
5332 tls12_prf->label,
5333 tls12_prf->label_length);
5334 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005335 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 }
5337 status = psa_mac_update(&hmac,
5338 tls12_prf->seed,
5339 tls12_prf->seed_length);
5340 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005341 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 }
5343 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005344 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5346 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005347 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005349 }
5350
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 status = psa_mac_sign_finish(&hmac,
5352 tls12_prf->Ai, hash_length,
5353 &hmac_output_length);
5354 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005355 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 }
5357 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005358 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005360
5361 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 status = psa_key_derivation_start_hmac(&hmac,
5363 hash_alg,
5364 tls12_prf->secret,
5365 tls12_prf->secret_length);
5366 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005367 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 }
5369 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5370 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005371 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 }
5373 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5374 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005375 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 }
5377 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5378 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005379 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 }
5381 status = psa_mac_sign_finish(&hmac,
5382 tls12_prf->output_block, hash_length,
5383 &hmac_output_length);
5384 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005385 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005387
Janos Follath7742fee2019-06-17 12:58:10 +01005388
5389cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 cleanup_status = psa_mac_abort(&hmac);
5391 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005392 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005396}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005397
Janos Follath844eb0e2019-06-19 12:10:49 +01005398static psa_status_t psa_key_derivation_tls12_prf_read(
5399 psa_tls12_prf_key_derivation_t *tls12_prf,
5400 psa_algorithm_t alg,
5401 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005403{
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5405 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005406 psa_status_t status;
5407 uint8_t offset, length;
5408
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005410 case PSA_TLS12_PRF_STATE_LABEL_SET:
5411 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5412 break;
5413 case PSA_TLS12_PRF_STATE_OUTPUT:
5414 break;
5415 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005417 }
5418
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005420 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 if (tls12_prf->left_in_block == 0) {
5422 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5423 alg);
5424 if (status != PSA_SUCCESS) {
5425 return status;
5426 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005427
5428 continue;
5429 }
5430
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005432 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005434 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005436
5437 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005439 output += length;
5440 output_length -= length;
5441 tls12_prf->left_in_block -= length;
5442 }
5443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005445}
John Durkop07cc04a2020-11-16 22:08:34 -08005446#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5447 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005448
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005449#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5450static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5451 psa_tls12_ecjpake_to_pms_t *ecjpake,
5452 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005454{
5455 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005456 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 if (output_length != 32) {
5459 return PSA_ERROR_INVALID_ARGUMENT;
5460 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005461
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5463 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5464 &output_size);
5465 if (status != PSA_SUCCESS) {
5466 return status;
5467 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005468
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 if (output_size != output_length) {
5470 return PSA_ERROR_GENERIC_ERROR;
5471 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005474}
5475#endif
5476
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305477#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5478static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5479 psa_pbkdf2_key_derivation_t *pbkdf2,
5480 psa_algorithm_t prf_alg,
5481 uint8_t prf_output_length,
5482 psa_key_attributes_t *attributes)
5483{
5484 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305485 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305486 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305487 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305488 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305489 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305490 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305491
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305492 mac_operation.is_sign = 1;
5493 mac_operation.mac_size = prf_output_length;
5494 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305495
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305496 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5497 attributes,
5498 pbkdf2->password,
5499 pbkdf2->password_length,
5500 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305501 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305502 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305503 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305504 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5505 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305506 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305507 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305508 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305509 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305510 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305511 }
5512 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5513 &mac_output_length);
5514 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305515 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305516 }
5517
5518 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305519 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305520 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305521 }
5522
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305523 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305524
5525 for (i = 1; i < pbkdf2->input_cost; i++) {
5526 status = psa_driver_wrapper_mac_compute(attributes,
5527 pbkdf2->password,
5528 pbkdf2->password_length,
5529 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305530 U_i, sizeof(U_i),
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305531 &mac_output_length);
5532 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305533 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305534 }
5535
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305536 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305537 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305538
5539cleanup:
5540 /* Zeroise buffers to clear sensitive data from memory. */
5541 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5542 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305543}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305544
5545static psa_status_t psa_key_derivation_pbkdf2_read(
5546 psa_pbkdf2_key_derivation_t *pbkdf2,
5547 psa_algorithm_t kdf_alg,
5548 uint8_t *output,
5549 size_t output_length)
5550{
5551 psa_status_t status;
5552 psa_algorithm_t prf_alg;
5553 uint8_t prf_output_length;
5554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5555 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5556 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5557
5558 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5559 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5560 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5561 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305562 } else {
5563 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305564 }
5565
5566 switch (pbkdf2->state) {
5567 case PSA_PBKDF2_STATE_PASSWORD_SET:
5568 /* Initially we need a new block so bytes_used is equal to block size*/
5569 pbkdf2->bytes_used = prf_output_length;
5570 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5571 break;
5572 case PSA_PBKDF2_STATE_OUTPUT:
5573 break;
5574 default:
5575 return PSA_ERROR_BAD_STATE;
5576 }
5577
5578 while (output_length != 0) {
5579 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5580 if (n > output_length) {
5581 n = (uint8_t) output_length;
5582 }
5583 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5584 output += n;
5585 output_length -= n;
5586 pbkdf2->bytes_used += n;
5587
5588 if (output_length == 0) {
5589 break;
5590 }
5591
5592 /* We need a new block */
5593 pbkdf2->bytes_used = 0;
5594 pbkdf2->block_number++;
5595
5596 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5597 prf_output_length,
5598 &attributes);
5599 if (status != PSA_SUCCESS) {
5600 return status;
5601 }
5602 }
5603
5604 return PSA_SUCCESS;
5605}
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305606#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
5607
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005608psa_status_t psa_key_derivation_output_bytes(
5609 psa_key_derivation_operation_t *operation,
5610 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005612{
5613 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005615
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005617 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005619 }
5620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005622 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005623 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005624 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005625 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005626 goto exit;
5627 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005629 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005630 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005631 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5632 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005633 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005634 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005636 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005637 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005638
Przemek Stekiel69c46792022-06-10 12:59:51 +02005639#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5641 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5642 output, output_length);
5643 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005644#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005645#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5646 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5648 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5649 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5650 kdf_alg, output,
5651 output_length);
5652 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005653#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5654 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005655#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005657 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5659 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005660#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305661#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5662 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305663 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5664 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305665 } else
5666#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005667
Gilles Peskineeab56e42018-07-12 17:12:33 +02005668 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005669 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005671 }
5672
5673exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005675 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005676 * This allows us to differentiate between exhausted operations and
5677 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5678 * operations. */
5679 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005681 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005683 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005685}
5686
David Brown7807bf72021-02-09 16:28:23 -07005687#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005688static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005689{
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 if (data_size >= 8) {
5691 mbedtls_des_key_set_parity(data);
5692 }
5693 if (data_size >= 16) {
5694 mbedtls_des_key_set_parity(data + 8);
5695 }
5696 if (data_size >= 24) {
5697 mbedtls_des_key_set_parity(data + 16);
5698 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005699}
David Brown7807bf72021-02-09 16:28:23 -07005700#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005701
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005702/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 * ECC keys on a Weierstrass elliptic curve require the generation
5704 * of a private key which is an integer
5705 * in the range [1, N - 1], where N is the boundary of the private key domain:
5706 * N is the prime p for Diffie-Hellman, or the order of the
5707 * curve’s base point for ECC.
5708 *
5709 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5710 * This function generates the private key using the following process:
5711 *
5712 * 1. Draw a byte string of length ceiling(m/8) bytes.
5713 * 2. If m is not a multiple of 8, set the most significant
5714 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5715 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5716 * 4. If k > N - 2, discard the result and return to step 1.
5717 * 5. Output k + 1 as the private key.
5718 *
5719 * This method allows compliance to NIST standards, specifically the methods titled
5720 * Key-Pair Generation by Testing Candidates in the following publications:
5721 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5722 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5723 * Diffie-Hellman keys.
5724 *
5725 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5726 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5727 *
5728 * Note: Function allocates memory for *data buffer, so given *data should be
5729 * always NULL.
5730 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005731#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5732 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005733 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5734 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5735 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005736static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005737 psa_key_slot_t *slot,
5738 size_t bits,
5739 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005740 uint8_t **data
5741 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005742{
Valerio Setti52789862023-04-07 12:13:11 +02005743#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005744 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005745 mbedtls_mpi k;
5746 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005748 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005749
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 mbedtls_mpi_init(&k);
5751 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005752
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005753 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005755 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005759 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005760 goto cleanup;
5761 }
5762
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005763 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005765
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005767
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005768 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005769 /* Let m be the bit size of N. */
5770 size_t m = ecp_group.nbits;
5771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005773
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005774 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005776
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005777 /* Note: This function is always called with *data == NULL and it
5778 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 *data = mbedtls_calloc(1, m_bytes);
5780 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005781 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005782 goto cleanup;
5783 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005786 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005788 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005790
5791 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005793 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005794 * (8 * ceiling(m/8) - m) bits of the first byte in
5795 * the string to zero.
5796 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005798 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005799 }
5800
5801 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 * big-endian byte string.
5803 */
5804 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005805
5806 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 * Result of comparison is returned. When it indicates error
5808 * then this function is called again.
5809 */
5810 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005811 }
5812
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005813 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5815 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005816cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 if (ret != 0) {
5818 status = mbedtls_to_psa_error(ret);
5819 }
5820 if (status != PSA_SUCCESS) {
5821 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005822 *data = NULL;
5823 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 mbedtls_mpi_free(&k);
5825 mbedtls_mpi_free(&diff_N_2);
5826 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005827#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005828 (void) slot;
5829 (void) bits;
5830 (void) operation;
5831 (void) data;
5832 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005833#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005834}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005835
5836/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5837 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5838 *
5839 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5840 * draw a 32-byte string and process it as specified in
5841 * Elliptic Curves for Security [RFC7748] §5.
5842 *
5843 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5844 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5845 *
5846 * Note: Function allocates memory for *data buffer, so given *data should be
5847 * always NULL.
5848 */
5849
5850static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5851 size_t bits,
5852 psa_key_derivation_operation_t *operation,
5853 uint8_t **data
5854 )
5855{
5856 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005857 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005860 case 255:
5861 output_length = 32;
5862 break;
5863 case 448:
5864 output_length = 56;
5865 break;
5866 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005868 break;
5869 }
5870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 if (*data == NULL) {
5874 return PSA_ERROR_INSUFFICIENT_MEMORY;
5875 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005880 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005884 case 255:
5885 (*data)[0] &= 248;
5886 (*data)[31] &= 127;
5887 (*data)[31] |= 64;
5888 break;
5889 case 448:
5890 (*data)[0] &= 252;
5891 (*data)[55] |= 128;
5892 break;
5893 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005895 break;
5896 }
5897
5898 return status;
5899}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005900#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5901 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005902 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5903 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5904 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005905
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005906static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005907 psa_key_slot_t *slot,
5908 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005910{
5911 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305913 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005914 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5917 return PSA_ERROR_INVALID_ARGUMENT;
5918 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005919
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005920#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5921 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005922 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5923 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5924 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5926 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5927 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005928 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5930 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005931 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 }
5933 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005934 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5936 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005937 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005939 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005940 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005941#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5942 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005943 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5944 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5945 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 if (key_type_is_raw_bytes(slot->attr.type)) {
5947 if (bits % 8 != 0) {
5948 return PSA_ERROR_INVALID_ARGUMENT;
5949 }
5950 data = mbedtls_calloc(1, bytes);
5951 if (data == NULL) {
5952 return PSA_ERROR_INSUFFICIENT_MEMORY;
5953 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005954
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 status = psa_key_derivation_output_bytes(operation, data, bytes);
5956 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005957 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 }
David Brown12ca5032021-02-11 11:02:00 -07005959#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5961 psa_des_set_key_parity(data, bytes);
5962 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005963#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 } else {
5965 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005966 }
Ronald Crondd04d422020-11-28 15:14:42 +01005967
Ronald Cronbf33c932020-11-28 18:06:53 +01005968 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005969 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005971 };
5972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5974 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5975 &storage_size);
5976 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305977 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 }
Archanad8a83dc2021-06-14 10:04:16 +05305979 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 status = psa_allocate_buffer_to_slot(slot, storage_size);
5981 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305982 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 }
Archanad8a83dc2021-06-14 10:04:16 +05305984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 status = psa_driver_wrapper_import_key(&attributes,
5986 data, bytes,
5987 slot->key.data,
5988 slot->key.bytes,
5989 &slot->key.bytes, &bits);
5990 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005991 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005993
5994exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 mbedtls_free(data);
5996 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005997}
5998
Gilles Peskine449bd832023-01-11 14:50:10 +01005999psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6000 psa_key_derivation_operation_t *operation,
6001 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006002{
6003 psa_status_t status;
6004 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006005 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006006
Ronald Cron81709fc2020-11-14 12:10:32 +01006007 *key = MBEDTLS_SVC_KEY_ID_INIT;
6008
Gilles Peskine0f84d622019-09-12 19:03:13 +02006009 /* Reject any attempt to create a zero-length key so that we don't
6010 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 if (psa_get_key_bits(attributes) == 0) {
6012 return PSA_ERROR_INVALID_ARGUMENT;
6013 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006014
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 if (operation->alg == PSA_ALG_NONE) {
6016 return PSA_ERROR_BAD_STATE;
6017 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006018
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 if (!operation->can_output_key) {
6020 return PSA_ERROR_NOT_PERMITTED;
6021 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006022
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6024 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006025#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006027 /* Deriving a key in a secure element is not implemented yet. */
6028 status = PSA_ERROR_NOT_SUPPORTED;
6029 }
6030#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 if (status == PSA_SUCCESS) {
6032 status = psa_generate_derived_key_internal(slot,
6033 attributes->core.bits,
6034 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006035 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 if (status == PSA_SUCCESS) {
6037 status = psa_finish_key_creation(slot, driver, key);
6038 }
6039 if (status != PSA_SUCCESS) {
6040 psa_fail_key_creation(slot, driver);
6041 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006042
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006044}
6045
Gilles Peskineeab56e42018-07-12 17:12:33 +02006046
6047
6048/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006049/* Key derivation */
6050/****************************************************************/
6051
Steven Cooreman932ffb72021-02-15 12:14:32 +01006052#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006053static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006054{
6055#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6057 return 1;
6058 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006059#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006060#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6062 return 1;
6063 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006064#endif
6065#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6067 return 1;
6068 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006069#endif
6070#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6072 return 1;
6073 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006074#endif
6075#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6077 return 1;
6078 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006079#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006080#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6082 return 1;
6083 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006084#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306085#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6086 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6087 return 1;
6088 }
6089#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006091}
6092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006094{
6095 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 psa_status_t status = psa_hash_setup(&operation, alg);
6097 psa_hash_abort(&operation);
6098 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006099}
6100
Gilles Peskine969c5d62019-01-16 15:53:06 +01006101static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006102 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006104{
Janos Follath5fe19732019-06-20 15:09:30 +01006105 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6106 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006108
Gilles Peskine969c5d62019-01-16 15:53:06 +01006109 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 if (!is_kdf_alg_supported(kdf_alg)) {
6111 return PSA_ERROR_NOT_SUPPORTED;
6112 }
John Durkop07cc04a2020-11-16 22:08:34 -08006113
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006114 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006115 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6117 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6118 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6119 if (hash_size == 0) {
6120 return PSA_ERROR_NOT_SUPPORTED;
6121 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006122
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006123 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6124 * we might fail later, which is somewhat unfriendly and potentially
6125 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 psa_status_t status = psa_hash_try_support(hash_alg);
6127 if (status != PSA_SUCCESS) {
6128 return status;
6129 }
6130 } else {
6131 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006132 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6135 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6136 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6137 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006138 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006139#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006140 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6142 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006143 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006145#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6146 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 operation->capacity = 255 * hash_size;
6148 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006149}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006150
Gilles Peskine449bd832023-01-11 14:50:10 +01006151static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006152{
6153#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 if (alg == PSA_ALG_ECDH) {
6155 return PSA_SUCCESS;
6156 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006157#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006158#if defined(PSA_WANT_ALG_FFDH)
6159 if (alg == PSA_ALG_FFDH) {
6160 return PSA_SUCCESS;
6161 }
6162#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006163 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006165}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006166
6167static int psa_key_derivation_allows_free_form_secret_input(
6168 psa_algorithm_t kdf_alg)
6169{
6170#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6171 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6172 return 0;
6173 }
6174#endif
6175 (void) kdf_alg;
6176 return 1;
6177}
John Durkop07cc04a2020-11-16 22:08:34 -08006178#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6181 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006182{
6183 psa_status_t status;
6184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 if (operation->alg != 0) {
6186 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006187 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6190 return PSA_ERROR_INVALID_ARGUMENT;
6191 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6192#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6193 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6194 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6195 status = psa_key_agreement_try_support(ka_alg);
6196 if (status != PSA_SUCCESS) {
6197 return status;
6198 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006199 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6200 return PSA_ERROR_INVALID_ARGUMENT;
6201 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6203#else
6204 return PSA_ERROR_NOT_SUPPORTED;
6205#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6206 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6207#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6208 status = psa_key_derivation_setup_kdf(operation, alg);
6209#else
6210 return PSA_ERROR_NOT_SUPPORTED;
6211#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6212 } else {
6213 return PSA_ERROR_INVALID_ARGUMENT;
6214 }
6215
6216 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006217 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 }
6219 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006220}
6221
Przemek Stekiel69c46792022-06-10 12:59:51 +02006222#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006223static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6224 psa_algorithm_t kdf_alg,
6225 psa_key_derivation_step_t step,
6226 const uint8_t *data,
6227 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006228{
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006230 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006232 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006233#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6235 return PSA_ERROR_INVALID_ARGUMENT;
6236 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006237#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 if (hkdf->state != HKDF_STATE_INIT) {
6239 return PSA_ERROR_BAD_STATE;
6240 } else {
6241 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6242 hash_alg,
6243 data, data_length);
6244 if (status != PSA_SUCCESS) {
6245 return status;
6246 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006247 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006249 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006250 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006251#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006253 /* We shouldn't be in different state as HKDF_EXPAND only allows
6254 * two inputs: SECRET (this case) and INFO which does not modify
6255 * the state. It could happen only if the hkdf
6256 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 if (hkdf->state != HKDF_STATE_INIT) {
6258 return PSA_ERROR_BAD_STATE;
6259 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006260
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006261 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6263 return PSA_ERROR_INVALID_ARGUMENT;
6264 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 memcpy(hkdf->prk, data, data_length);
6267 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006268#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006269 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006270 /* HKDF: If no salt was provided, use an empty salt.
6271 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006273#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6275 return PSA_ERROR_BAD_STATE;
6276 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006277#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6279 hash_alg,
6280 NULL, 0);
6281 if (status != PSA_SUCCESS) {
6282 return status;
6283 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006284 hkdf->state = HKDF_STATE_STARTED;
6285 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 if (hkdf->state != HKDF_STATE_STARTED) {
6287 return PSA_ERROR_BAD_STATE;
6288 }
6289 status = psa_mac_update(&hkdf->hmac,
6290 data, data_length);
6291 if (status != PSA_SUCCESS) {
6292 return status;
6293 }
6294 status = psa_mac_sign_finish(&hkdf->hmac,
6295 hkdf->prk,
6296 sizeof(hkdf->prk),
6297 &data_length);
6298 if (status != PSA_SUCCESS) {
6299 return status;
6300 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006301 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006302
Gilles Peskine2b522db2019-04-12 15:11:49 +02006303 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006304 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006305#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006307 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006309 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006311#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006312 {
6313 /* Block 0 is empty, and the next block will be
6314 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006316 }
6317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006319 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006320#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6322 return PSA_ERROR_INVALID_ARGUMENT;
6323 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006324#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006325#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6327 hkdf->state == HKDF_STATE_INIT) {
6328 return PSA_ERROR_BAD_STATE;
6329 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006330#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 if (hkdf->state == HKDF_STATE_OUTPUT) {
6332 return PSA_ERROR_BAD_STATE;
6333 }
6334 if (hkdf->info_set) {
6335 return PSA_ERROR_BAD_STATE;
6336 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006337 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 if (data_length != 0) {
6339 hkdf->info = mbedtls_calloc(1, data_length);
6340 if (hkdf->info == NULL) {
6341 return PSA_ERROR_INSUFFICIENT_MEMORY;
6342 }
6343 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006344 }
6345 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006347 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006349 }
6350}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006351#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006352
John Durkop07cc04a2020-11-16 22:08:34 -08006353#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6354 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006355static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6356 const uint8_t *data,
6357 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006358{
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6360 return PSA_ERROR_BAD_STATE;
6361 }
Janos Follathf08e2652019-06-13 09:05:41 +01006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 if (data_length != 0) {
6364 prf->seed = mbedtls_calloc(1, data_length);
6365 if (prf->seed == NULL) {
6366 return PSA_ERROR_INSUFFICIENT_MEMORY;
6367 }
Janos Follathf08e2652019-06-13 09:05:41 +01006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006370 prf->seed_length = data_length;
6371 }
Janos Follathf08e2652019-06-13 09:05:41 +01006372
Gilles Peskine43f958b2020-12-13 14:55:14 +01006373 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006376}
6377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6379 const uint8_t *data,
6380 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006381{
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6383 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6384 return PSA_ERROR_BAD_STATE;
6385 }
Janos Follath81550542019-06-13 14:26:34 +01006386
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 if (data_length != 0) {
6388 prf->secret = mbedtls_calloc(1, data_length);
6389 if (prf->secret == NULL) {
6390 return PSA_ERROR_INSUFFICIENT_MEMORY;
6391 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006394 prf->secret_length = data_length;
6395 }
Janos Follath81550542019-06-13 14:26:34 +01006396
Gilles Peskine43f958b2020-12-13 14:55:14 +01006397 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006400}
6401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6403 const uint8_t *data,
6404 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006405{
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6407 return PSA_ERROR_BAD_STATE;
6408 }
Janos Follath63028dd2019-06-13 09:15:47 +01006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 if (data_length != 0) {
6411 prf->label = mbedtls_calloc(1, data_length);
6412 if (prf->label == NULL) {
6413 return PSA_ERROR_INSUFFICIENT_MEMORY;
6414 }
Janos Follath63028dd2019-06-13 09:15:47 +01006415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006417 prf->label_length = data_length;
6418 }
Janos Follath63028dd2019-06-13 09:15:47 +01006419
Gilles Peskine43f958b2020-12-13 14:55:14 +01006420 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006423}
6424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6426 psa_key_derivation_step_t step,
6427 const uint8_t *data,
6428 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006429{
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006431 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006433 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006435 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006437 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006439 }
6440}
John Durkop07cc04a2020-11-16 22:08:34 -08006441#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6442 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6443
6444#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6445static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6446 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006447 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006449{
6450 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6452 4 + data_length + prf->other_secret_length :
6453 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006454
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6456 return PSA_ERROR_INVALID_ARGUMENT;
6457 }
John Durkop07cc04a2020-11-16 22:08:34 -08006458
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 uint8_t *pms = mbedtls_calloc(1, pms_len);
6460 if (pms == NULL) {
6461 return PSA_ERROR_INSUFFICIENT_MEMORY;
6462 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006463 uint8_t *cur = pms;
6464
6465 /* pure-PSK:
6466 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006467 *
6468 * The premaster secret is formed as follows: if the PSK is N octets
6469 * long, concatenate a uint16 with the value N, N zero octets, a second
6470 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006471 *
6472 * mixed-PSK:
6473 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6474 * follows: concatenate a uint16 with the length of the other secret,
6475 * the other secret itself, uint16 with the length of PSK, and the
6476 * PSK itself.
6477 * For details please check:
6478 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6479 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6480 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006481 */
6482
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6484 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6485 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6486 if (prf->other_secret_length != 0) {
6487 memcpy(cur, prf->other_secret, prf->other_secret_length);
6488 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006489 cur += prf->other_secret_length;
6490 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 } else {
6492 *cur++ = MBEDTLS_BYTE_1(data_length);
6493 *cur++ = MBEDTLS_BYTE_0(data_length);
6494 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006495 cur += data_length;
6496 }
6497
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 *cur++ = MBEDTLS_BYTE_1(data_length);
6499 *cur++ = MBEDTLS_BYTE_0(data_length);
6500 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006501 cur += data_length;
6502
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006504
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 mbedtls_platform_zeroize(pms, pms_len);
6506 mbedtls_free(pms);
6507 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006508}
Janos Follath6660f0e2019-06-17 08:44:03 +01006509
Przemek Stekiele47201b2022-04-19 13:53:28 +02006510static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6511 psa_tls12_prf_key_derivation_t *prf,
6512 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006514{
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6516 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006517 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006518
6519 if (data_length != 0) {
6520 prf->other_secret = mbedtls_calloc(1, data_length);
6521 if (prf->other_secret == NULL) {
6522 return PSA_ERROR_INSUFFICIENT_MEMORY;
6523 }
6524
6525 memcpy(prf->other_secret, data, data_length);
6526 prf->other_secret_length = data_length;
6527 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006528 prf->other_secret_length = 0;
6529 }
6530
6531 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6532
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006534}
6535
Janos Follath6660f0e2019-06-17 08:44:03 +01006536static psa_status_t psa_tls12_prf_psk_to_ms_input(
6537 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006538 psa_key_derivation_step_t step,
6539 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006541{
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006543 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 return psa_tls12_prf_psk_to_ms_set_key(prf,
6545 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006546 break;
6547 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6549 data,
6550 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006551 break;
6552 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006554 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006555
Przemek Stekiele47201b2022-04-19 13:53:28 +02006556 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006557}
John Durkop07cc04a2020-11-16 22:08:34 -08006558#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006559
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006560#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6561static psa_status_t psa_tls12_ecjpake_to_pms_input(
6562 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006563 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006564 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006566{
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6568 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6569 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006570 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006571
6572 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 if (data[0] != 0x04) {
6574 return PSA_ERROR_INVALID_ARGUMENT;
6575 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006576
6577 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006579
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006581}
6582#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306583
6584#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6585static psa_status_t psa_pbkdf2_set_input_cost(
6586 psa_pbkdf2_key_derivation_t *pbkdf2,
6587 psa_key_derivation_step_t step,
6588 uint64_t data)
6589{
6590 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6591 return PSA_ERROR_INVALID_ARGUMENT;
6592 }
6593
6594 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6595 return PSA_ERROR_BAD_STATE;
6596 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306597
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306598 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306599 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306600 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306601
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306602 if (data == 0) {
6603 return PSA_ERROR_INVALID_ARGUMENT;
6604 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306605
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306606 pbkdf2->input_cost = data;
6607 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6608
6609 return PSA_SUCCESS;
6610}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306611
6612static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6613 const uint8_t *data,
6614 size_t data_length)
6615{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306616 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6617 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6618 return PSA_ERROR_BAD_STATE;
6619 }
6620
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306621 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6622 pbkdf2->salt = mbedtls_calloc(1, data_length);
6623 if (pbkdf2->salt == NULL) {
6624 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306625 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306626
6627 memcpy(pbkdf2->salt, data, data_length);
6628 pbkdf2->salt_length = data_length;
6629 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306630 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306631
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306632 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6633 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306634 return PSA_ERROR_INSUFFICIENT_MEMORY;
6635 }
6636
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306637 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6638 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306639 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306640 mbedtls_free(pbkdf2->salt);
6641 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306642 }
6643
6644 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6645
6646 return PSA_SUCCESS;
6647}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306648
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306649static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306650 const uint8_t *input,
6651 size_t input_len,
6652 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306653 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306654{
6655 psa_status_t status = PSA_SUCCESS;
6656 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6657 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306658 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306659 } else {
6660 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306661 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306662 }
6663 return status;
6664}
6665
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306666static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306667 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306668 const uint8_t *data,
6669 size_t data_length)
6670{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306671 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306672 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6673 return PSA_ERROR_BAD_STATE;
6674 }
6675
6676 if (data_length != 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306677 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6678 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6679 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6680 pbkdf2->password,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306681 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306682 }
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306683 }
6684
6685 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6686
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306687 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306688}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306689
6690static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306691 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306692 psa_key_derivation_step_t step,
6693 const uint8_t *data,
6694 size_t data_length)
6695{
6696 switch (step) {
6697 case PSA_KEY_DERIVATION_INPUT_SALT:
6698 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6699 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306700 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306701 default:
6702 return PSA_ERROR_INVALID_ARGUMENT;
6703 }
6704}
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306705#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6706
Gilles Peskineb8965192019-09-24 16:21:10 +02006707/** Check whether the given key type is acceptable for the given
6708 * input step of a key derivation.
6709 *
6710 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6711 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6712 * Both secret and non-secret inputs can alternatively have the type
6713 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6714 * that the input was passed as a buffer rather than via a key object.
6715 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006716static int psa_key_derivation_check_input_type(
6717 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006719{
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006721 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 if (key_type == PSA_KEY_TYPE_DERIVE) {
6723 return PSA_SUCCESS;
6724 }
6725 if (key_type == PSA_KEY_TYPE_NONE) {
6726 return PSA_SUCCESS;
6727 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006728 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006729 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 if (key_type == PSA_KEY_TYPE_DERIVE) {
6731 return PSA_SUCCESS;
6732 }
6733 if (key_type == PSA_KEY_TYPE_NONE) {
6734 return PSA_SUCCESS;
6735 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006736 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006737 case PSA_KEY_DERIVATION_INPUT_LABEL:
6738 case PSA_KEY_DERIVATION_INPUT_SALT:
6739 case PSA_KEY_DERIVATION_INPUT_INFO:
6740 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6742 return PSA_SUCCESS;
6743 }
6744 if (key_type == PSA_KEY_TYPE_NONE) {
6745 return PSA_SUCCESS;
6746 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006747 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306748 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6749 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6750 return PSA_SUCCESS;
6751 }
6752 if (key_type == PSA_KEY_TYPE_DERIVE) {
6753 return PSA_SUCCESS;
6754 }
6755 if (key_type == PSA_KEY_TYPE_NONE) {
6756 return PSA_SUCCESS;
6757 }
6758 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006759 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006761}
6762
Janos Follathb80a94e2019-06-12 15:54:46 +01006763static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006764 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006765 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006766 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006767 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006769{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006770 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006772
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 status = psa_key_derivation_check_input_type(step, key_type);
6774 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006775 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006777
Przemek Stekiel69c46792022-06-10 12:59:51 +02006778#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6780 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6781 step, data, data_length);
6782 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006783#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006784#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6786 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6787 step, data, data_length);
6788 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006789#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6790#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6792 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6793 step, data, data_length);
6794 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006795#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006796#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006798 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6800 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006801#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306802#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6803 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306804 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6805 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306806 } else
6807#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006808 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006809 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006810 (void) data;
6811 (void) data_length;
6812 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006814 }
6815
Gilles Peskine224b0d62019-09-23 18:13:17 +02006816exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 if (status != PSA_SUCCESS) {
6818 psa_key_derivation_abort(operation);
6819 }
6820 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006821}
6822
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306823static psa_status_t psa_key_derivation_input_integer_internal(
6824 psa_key_derivation_operation_t *operation,
6825 psa_key_derivation_step_t step,
6826 uint64_t value)
6827{
6828 psa_status_t status;
6829 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6830
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306831#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6832 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6833 status = psa_pbkdf2_set_input_cost(
6834 &operation->ctx.pbkdf2, step, value);
6835 } else
6836#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306837 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306838 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306839 (void) value;
6840 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306841 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306842 }
6843
6844 if (status != PSA_SUCCESS) {
6845 psa_key_derivation_abort(operation);
6846 }
6847 return status;
6848}
6849
Janos Follath51f4a0f2019-06-14 11:35:55 +01006850psa_status_t psa_key_derivation_input_bytes(
6851 psa_key_derivation_operation_t *operation,
6852 psa_key_derivation_step_t step,
6853 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006855{
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 return psa_key_derivation_input_internal(operation, step,
6857 PSA_KEY_TYPE_NONE,
6858 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006859}
6860
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306861psa_status_t psa_key_derivation_input_integer(
6862 psa_key_derivation_operation_t *operation,
6863 psa_key_derivation_step_t step,
6864 uint64_t value)
6865{
6866 return psa_key_derivation_input_integer_internal(operation, step, value);
6867}
6868
Janos Follath51f4a0f2019-06-14 11:35:55 +01006869psa_status_t psa_key_derivation_input_key(
6870 psa_key_derivation_operation_t *operation,
6871 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006873{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006874 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006875 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006876 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006877
Ronald Cron5c522922020-11-14 16:35:34 +01006878 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6880 if (status != PSA_SUCCESS) {
6881 psa_key_derivation_abort(operation);
6882 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006883 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006884
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306885 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6886 * permission to output to a key object. */
6887 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6888 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006889 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006891
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 status = psa_key_derivation_input_internal(operation,
6893 step, slot->attr.type,
6894 slot->key.data,
6895 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006896
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006898
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006900}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006901
6902
6903
6904/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006905/* Key agreement */
6906/****************************************************************/
6907
Gilles Peskine449bd832023-01-11 14:50:10 +01006908psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6909 const uint8_t *key_buffer,
6910 size_t key_buffer_size,
6911 psa_algorithm_t alg,
6912 const uint8_t *peer_key,
6913 size_t peer_key_length,
6914 uint8_t *shared_secret,
6915 size_t shared_secret_size,
6916 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006917{
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006919#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006920 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6922 key_buffer_size, alg,
6923 peer_key, peer_key_length,
6924 shared_secret,
6925 shared_secret_size,
6926 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006927#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006928
6929#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6930 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006931 return mbedtls_psa_key_agreement_ffdh(attributes,
6932 peer_key,
6933 peer_key_length,
6934 key_buffer,
6935 key_buffer_size,
6936 shared_secret,
6937 shared_secret_size,
6938 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006939#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6940
Gilles Peskine01d718c2018-09-18 12:01:02 +02006941 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006942 (void) attributes;
6943 (void) key_buffer;
6944 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006945 (void) peer_key;
6946 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006947 (void) shared_secret;
6948 (void) shared_secret_size;
6949 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006951 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006952}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006953
Aditya Deshpande17845b82022-10-13 17:21:01 +01006954/** Internal function for raw key agreement
6955 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006956 * to the driver's implementation if a driver is present.
6957 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006958 * (psa_key_agreement_raw_builtin).
6959 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006960static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6961 psa_key_slot_t *private_key,
6962 const uint8_t *peer_key,
6963 size_t peer_key_length,
6964 uint8_t *shared_secret,
6965 size_t shared_secret_size,
6966 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006967{
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6969 return PSA_ERROR_NOT_SUPPORTED;
6970 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006971
6972 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006974 };
6975
Gilles Peskine449bd832023-01-11 14:50:10 +01006976 return psa_driver_wrapper_key_agreement(&attributes,
6977 private_key->key.data,
6978 private_key->key.bytes, alg,
6979 peer_key, peer_key_length,
6980 shared_secret,
6981 shared_secret_size,
6982 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006983}
6984
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006985/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006986 * to potentially free embedded data structures and wipe confidential data.
6987 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006988static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6989 psa_key_derivation_step_t step,
6990 psa_key_slot_t *private_key,
6991 const uint8_t *peer_key,
6992 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006993{
6994 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006995 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006996 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006998
6999 /* Step 1: run the secret agreement algorithm to generate the shared
7000 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 status = psa_key_agreement_raw_internal(ka_alg,
7002 private_key,
7003 peer_key, peer_key_length,
7004 shared_secret,
7005 sizeof(shared_secret),
7006 &shared_secret_length);
7007 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007008 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007010
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007011 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007012 * the shared secret. A shared secret is permitted wherever a key
7013 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 status = psa_key_derivation_input_internal(operation, step,
7015 PSA_KEY_TYPE_DERIVE,
7016 shared_secret,
7017 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007018exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7020 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007021}
7022
Gilles Peskine449bd832023-01-11 14:50:10 +01007023psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7024 psa_key_derivation_step_t step,
7025 mbedtls_svc_key_id_t private_key,
7026 const uint8_t *peer_key,
7027 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007028{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007029 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007030 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007031 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007032
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7034 return PSA_ERROR_INVALID_ARGUMENT;
7035 }
Ronald Cron5c522922020-11-14 16:35:34 +01007036 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7038 if (status != PSA_SUCCESS) {
7039 return status;
7040 }
7041 status = psa_key_agreement_internal(operation, step,
7042 slot,
7043 peer_key, peer_key_length);
7044 if (status != PSA_SUCCESS) {
7045 psa_key_derivation_abort(operation);
7046 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007047 /* If a private key has been added as SECRET, we allow the derived
7048 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007049 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007050 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007052 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007053
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007055
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007057}
7058
Gilles Peskine449bd832023-01-11 14:50:10 +01007059psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7060 mbedtls_svc_key_id_t private_key,
7061 const uint8_t *peer_key,
7062 size_t peer_key_length,
7063 uint8_t *output,
7064 size_t output_size,
7065 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007066{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007067 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007068 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007069 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007070
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007072 status = PSA_ERROR_INVALID_ARGUMENT;
7073 goto exit;
7074 }
Ronald Cron5c522922020-11-14 16:35:34 +01007075 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007076 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7077 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007078 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007080
Gilles Peskine3e561302022-04-14 00:17:15 +02007081 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7082 * for the output size. The PSA specification only guarantees that this
7083 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7084 * but it might be nice to allow smaller buffers if the output fits.
7085 * At the time of writing this comment, with only ECDH implemented,
7086 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7087 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7088 * be exact for it as well. */
7089 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7091 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007092 status = PSA_ERROR_BUFFER_TOO_SMALL;
7093 goto exit;
7094 }
7095
Gilles Peskine449bd832023-01-11 14:50:10 +01007096 status = psa_key_agreement_raw_internal(alg, slot,
7097 peer_key, peer_key_length,
7098 output, output_size,
7099 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007100
7101exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007103 /* If an error happens and is not handled properly, the output
7104 * may be used as a key to protect sensitive data. Arrange for such
7105 * a key to be random, which is likely to result in decryption or
7106 * verification errors. This is better than filling the buffer with
7107 * some constant data such as zeros, which would result in the data
7108 * being protected with a reproducible, easily knowable key.
7109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007111 *output_length = output_size;
7112 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007113
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007115
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007117}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007118
7119
Gilles Peskine30524eb2020-11-13 17:02:26 +01007120
Gilles Peskine86a440b2018-11-12 18:39:40 +01007121/****************************************************************/
7122/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007123/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007124
Gilles Peskine30524eb2020-11-13 17:02:26 +01007125/** Initialize the PSA random generator.
7126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007127static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007128{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007129#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007131#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7132
Gilles Peskine30524eb2020-11-13 17:02:26 +01007133 /* Set default configuration if
7134 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007136 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 }
7138 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007139 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007141
Gilles Peskine449bd832023-01-11 14:50:10 +01007142 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007143#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7144 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7145 /* The PSA entropy injection feature depends on using NV seed as an entropy
7146 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 mbedtls_entropy_add_source(&rng->entropy,
7148 mbedtls_nv_seed_poll, NULL,
7149 MBEDTLS_ENTROPY_BLOCK_SIZE,
7150 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007151#endif
7152
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007154#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007155}
7156
7157/** Deinitialize the PSA random generator.
7158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007159static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007160{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007161#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007163#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007164 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7165 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007166#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007167}
7168
7169/** Seed the PSA random generator.
7170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007171static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007172{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007173#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7174 /* Do nothing: the external RNG seeds itself. */
7175 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007176 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007177#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007178 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7180 drbg_seed, sizeof(drbg_seed) - 1);
7181 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007182#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007183}
7184
Gilles Peskine449bd832023-01-11 14:50:10 +01007185psa_status_t psa_generate_random(uint8_t *output,
7186 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007187{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007188 GUARD_MODULE_INITIALIZED;
7189
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007190#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7191
7192 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7194 output, output_size,
7195 &output_length);
7196 if (status != PSA_SUCCESS) {
7197 return status;
7198 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007199 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007200 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 if (output_length != output_size) {
7202 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7203 }
7204 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007205
7206#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7207
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007209 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007210 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7211 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7212 output_size);
7213 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7214 output, request_size);
7215 if (ret != 0) {
7216 return mbedtls_to_psa_error(ret);
7217 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007218 output_size -= request_size;
7219 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007220 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007222#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007223}
7224
Gilles Peskine8814fc42020-12-14 15:33:44 +01007225/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007226 *
7227 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7228 * `psa_generate_random(...)`. The state parameter is ignored since the
7229 * PSA API doesn't support passing an explicit state.
7230 *
7231 * In the non-external case, psa_generate_random() calls an
7232 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7233 * and semantics as mbedtls_psa_get_random(). As an optimization,
7234 * instead of doing this back-and-forth between the PSA API and the
7235 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7236 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007237 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007238#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7239int mbedtls_psa_get_random(void *p_rng,
7240 unsigned char *output,
7241 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007242{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007243 /* This function takes a pointer to the RNG state because that's what
7244 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7245 * its own state internally and doesn't let the caller access that state.
7246 * So we just ignore the state parameter, and in practice we'll pass
7247 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007248 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 psa_status_t status = psa_generate_random(output, output_size);
7250 if (status == PSA_SUCCESS) {
7251 return 0;
7252 } else {
7253 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7254 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007255}
7256#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7257
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007258#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00007259#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007260
Gilles Peskine449bd832023-01-11 14:50:10 +01007261psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7262 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007263{
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 if (global_data.initialized) {
7265 return PSA_ERROR_NOT_PERMITTED;
7266 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007267
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7269 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7270 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7271 return PSA_ERROR_INVALID_ARGUMENT;
7272 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007273
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007275}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007276#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007277
Ronald Cron3772afe2021-02-08 16:10:05 +01007278/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007279 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007280 * \param type The key type
7281 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007282 *
7283 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007284 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007285 * \retval #PSA_ERROR_INVALID_ARGUMENT
7286 * The size in bits of the key is not valid.
7287 * \retval #PSA_ERROR_NOT_SUPPORTED
7288 * The type and/or the size in bits of the key or the combination of
7289 * the two is not supported.
7290 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007291static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007292 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007293{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007294 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007295
Gilles Peskine449bd832023-01-11 14:50:10 +01007296 if (key_type_is_raw_bytes(type)) {
7297 status = psa_validate_unstructured_key_bit_size(type, bits);
7298 if (status != PSA_SUCCESS) {
7299 return status;
7300 }
7301 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007302#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007303 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7304 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7305 return PSA_ERROR_NOT_SUPPORTED;
7306 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007307
Ronald Cron01b2aba2020-10-05 09:42:02 +02007308 /* Accept only byte-aligned keys, for the same reasons as
7309 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 if (bits % 8 != 0) {
7311 return PSA_ERROR_NOT_SUPPORTED;
7312 }
7313 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007314#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007315
Ronald Cron5c4d3862020-12-07 11:07:24 +01007316#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007318 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 return PSA_SUCCESS;
7320 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007321#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007322
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007323#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007324 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007325 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007326 return PSA_ERROR_NOT_SUPPORTED;
7327 }
7328 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007329#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007330 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007332 }
7333
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007335}
7336
Ronald Cron55ed0592020-10-05 10:30:40 +02007337psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007338 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007340{
7341 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007342 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007343
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 if ((attributes->domain_parameters == NULL) &&
7345 (attributes->domain_parameters_size != 0)) {
7346 return PSA_ERROR_INVALID_ARGUMENT;
7347 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007348
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 if (key_type_is_raw_bytes(type)) {
7350 status = psa_generate_random(key_buffer, key_buffer_size);
7351 if (status != PSA_SUCCESS) {
7352 return status;
7353 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007354
David Brown12ca5032021-02-11 11:02:00 -07007355#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 if (type == PSA_KEY_TYPE_DES) {
7357 psa_des_set_key_parity(key_buffer, key_buffer_size);
7358 }
David Brown8107e312021-02-12 08:08:46 -07007359#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007361
Jaeden Amero424fa932021-05-14 08:34:32 +01007362#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7363 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7365 return mbedtls_psa_rsa_generate_key(attributes,
7366 key_buffer,
7367 key_buffer_size,
7368 key_buffer_length);
7369 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007370#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7371 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007372
John Durkop9814fa22020-11-04 12:28:15 -08007373#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7375 return mbedtls_psa_ecp_generate_key(attributes,
7376 key_buffer,
7377 key_buffer_size,
7378 key_buffer_length);
7379 } else
John Durkop9814fa22020-11-04 12:28:15 -08007380#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007381
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007382#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007383 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7384 return mbedtls_psa_ffdh_generate_key(attributes,
7385 key_buffer,
7386 key_buffer_size,
7387 key_buffer_length);
7388 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007389#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007390 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 (void) key_buffer_length;
7392 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007393 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007394
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007396}
Darryl Green0c6575a2018-11-07 16:05:30 +00007397
Gilles Peskine449bd832023-01-11 14:50:10 +01007398psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7399 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007400{
7401 psa_status_t status;
7402 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007403 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007404 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007405
Ronald Cron81709fc2020-11-14 12:10:32 +01007406 *key = MBEDTLS_SVC_KEY_ID_INIT;
7407
Gilles Peskine0f84d622019-09-12 19:03:13 +02007408 /* Reject any attempt to create a zero-length key so that we don't
7409 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 if (psa_get_key_bits(attributes) == 0) {
7411 return PSA_ERROR_INVALID_ARGUMENT;
7412 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007413
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007414 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007415 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7416 return PSA_ERROR_INVALID_ARGUMENT;
7417 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007418
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7420 &slot, &driver);
7421 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007422 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 }
Gilles Peskine11792082019-08-06 18:36:36 +02007424
Ronald Cron977c2472020-10-13 08:32:21 +02007425 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307426 * storage ( thus not in the case of generating a key in a secure element
7427 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7428 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 if (slot->key.data == NULL) {
7430 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7431 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007432 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 attributes->core.type, attributes->core.bits);
7434 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007435 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007437
7438 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 attributes->core.type,
7440 attributes->core.bits);
7441 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007442 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 attributes, &key_buffer_size);
7444 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007445 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 }
Ronald Cron977c2472020-10-13 08:32:21 +02007447 }
Ronald Cron977c2472020-10-13 08:32:21 +02007448
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7450 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007451 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 }
Ronald Cron977c2472020-10-13 08:32:21 +02007453 }
7454
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 status = psa_driver_wrapper_generate_key(attributes,
7456 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007457
Gilles Peskine449bd832023-01-11 14:50:10 +01007458 if (status != PSA_SUCCESS) {
7459 psa_remove_key_data_from_memory(slot);
7460 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007461
Gilles Peskine11792082019-08-06 18:36:36 +02007462exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 if (status == PSA_SUCCESS) {
7464 status = psa_finish_key_creation(slot, driver, key);
7465 }
7466 if (status != PSA_SUCCESS) {
7467 psa_fail_key_creation(slot, driver);
7468 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007469
Gilles Peskine449bd832023-01-11 14:50:10 +01007470 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007471}
7472
Gilles Peskinee59236f2018-01-27 23:32:46 +01007473/****************************************************************/
7474/* Module setup */
7475/****************************************************************/
7476
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007477#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007478psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 void (* entropy_init)(mbedtls_entropy_context *ctx),
7480 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007481{
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7483 return PSA_ERROR_BAD_STATE;
7484 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007485 global_data.rng.entropy_init = entropy_init;
7486 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007487 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007488}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007489#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007490
Gilles Peskine449bd832023-01-11 14:50:10 +01007491void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007492{
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 psa_wipe_all_key_slots();
7494 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7495 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007496 }
7497 /* Wipe all remaining data, including configuration.
7498 * In particular, this sets all state indicator to the value
7499 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007501
7502 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007504}
7505
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007506#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7507/** Recover a transaction that was interrupted by a power failure.
7508 *
7509 * This function is called during initialization, before psa_crypto_init()
7510 * returns. If this function returns a failure status, the initialization
7511 * fails.
7512 */
7513static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007515{
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007517 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7518 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007519 /* TODO - fall through to the failure case until this
7520 * is implemented.
7521 * https://github.com/ARMmbed/mbed-crypto/issues/218
7522 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007523 default:
7524 /* We found an unsupported transaction in the storage.
7525 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007527 }
7528}
7529#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7530
Gilles Peskine449bd832023-01-11 14:50:10 +01007531psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007532{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007533 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007534
Gilles Peskinec6b69072018-11-20 21:42:52 +01007535 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 if (global_data.initialized != 0) {
7537 return PSA_SUCCESS;
7538 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007539
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007540 /* Init drivers */
7541 status = psa_driver_wrapper_init();
7542 if (status != PSA_SUCCESS) {
7543 goto exit;
7544 }
7545 global_data.drivers_initialized = 1;
7546
Gilles Peskine30524eb2020-11-13 17:02:26 +01007547 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007548 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007549 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 status = mbedtls_psa_random_seed(&global_data.rng);
7551 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007552 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007554 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007555
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 status = psa_initialize_key_slots();
7557 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007558 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007560
Gilles Peskine4b734222019-07-24 15:56:31 +02007561#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 status = psa_crypto_load_transaction();
7563 if (status == PSA_SUCCESS) {
7564 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7565 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007566 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 }
7568 status = psa_crypto_stop_transaction();
7569 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007570 /* There's no transaction to complete. It's all good. */
7571 status = PSA_SUCCESS;
7572 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007573#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007574
Gilles Peskinec6b69072018-11-20 21:42:52 +01007575 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007576 global_data.initialized = 1;
7577
7578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 if (status != PSA_SUCCESS) {
7580 mbedtls_psa_crypto_free();
7581 }
7582 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007583}
7584
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007585psa_status_t psa_crypto_driver_pake_get_password_len(
7586 const psa_crypto_driver_pake_inputs_t *inputs,
7587 size_t *password_len)
7588{
7589 if (inputs->password_len == 0) {
7590 return PSA_ERROR_BAD_STATE;
7591 }
7592
7593 *password_len = inputs->password_len;
7594
7595 return PSA_SUCCESS;
7596}
7597
7598psa_status_t psa_crypto_driver_pake_get_password(
7599 const psa_crypto_driver_pake_inputs_t *inputs,
7600 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7601{
7602 if (inputs->password_len == 0) {
7603 return PSA_ERROR_BAD_STATE;
7604 }
7605
7606 if (buffer_size < inputs->password_len) {
7607 return PSA_ERROR_BUFFER_TOO_SMALL;
7608 }
7609
7610 memcpy(buffer, inputs->password, inputs->password_len);
7611 *buffer_length = inputs->password_len;
7612
7613 return PSA_SUCCESS;
7614}
7615
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007616psa_status_t psa_crypto_driver_pake_get_user_len(
7617 const psa_crypto_driver_pake_inputs_t *inputs,
7618 size_t *user_len)
7619{
7620 if (inputs->user_len == 0) {
7621 return PSA_ERROR_BAD_STATE;
7622 }
7623
7624 *user_len = inputs->user_len;
7625
7626 return PSA_SUCCESS;
7627}
7628
7629psa_status_t psa_crypto_driver_pake_get_user(
7630 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007631 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007632{
7633 if (inputs->user_len == 0) {
7634 return PSA_ERROR_BAD_STATE;
7635 }
7636
Przemek Stekielc0e62502023-03-14 11:49:36 +01007637 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007638 return PSA_ERROR_BUFFER_TOO_SMALL;
7639 }
7640
Przemek Stekielc0e62502023-03-14 11:49:36 +01007641 memcpy(user_id, inputs->user, inputs->user_len);
7642 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007643
7644 return PSA_SUCCESS;
7645}
7646
7647psa_status_t psa_crypto_driver_pake_get_peer_len(
7648 const psa_crypto_driver_pake_inputs_t *inputs,
7649 size_t *peer_len)
7650{
7651 if (inputs->peer_len == 0) {
7652 return PSA_ERROR_BAD_STATE;
7653 }
7654
7655 *peer_len = inputs->peer_len;
7656
7657 return PSA_SUCCESS;
7658}
7659
7660psa_status_t psa_crypto_driver_pake_get_peer(
7661 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007662 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007663{
7664 if (inputs->peer_len == 0) {
7665 return PSA_ERROR_BAD_STATE;
7666 }
7667
Przemek Stekielc0e62502023-03-14 11:49:36 +01007668 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007669 return PSA_ERROR_BUFFER_TOO_SMALL;
7670 }
7671
Przemek Stekielc0e62502023-03-14 11:49:36 +01007672 memcpy(peer_id, inputs->peer, inputs->peer_len);
7673 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007674
7675 return PSA_SUCCESS;
7676}
7677
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007678psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7679 const psa_crypto_driver_pake_inputs_t *inputs,
7680 psa_pake_cipher_suite_t *cipher_suite)
7681{
7682 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7683 return PSA_ERROR_BAD_STATE;
7684 }
7685
7686 *cipher_suite = inputs->cipher_suite;
7687
7688 return PSA_SUCCESS;
7689}
7690
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01007691#if defined(PSA_WANT_ALG_SOME_PAKE)
Neil Armstronga7d08c32022-06-01 18:21:20 +02007692psa_status_t psa_pake_setup(
7693 psa_pake_operation_t *operation,
7694 const psa_pake_cipher_suite_t *cipher_suite)
7695{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007697
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007698 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007699 status = PSA_ERROR_BAD_STATE;
7700 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007701 }
7702
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007703 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007704 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007705 status = PSA_ERROR_INVALID_ARGUMENT;
7706 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007707 }
7708
Przemek Stekiel51eac532022-12-07 11:04:51 +01007709 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7710
Przemek Stekiele12ed362022-12-21 12:54:46 +01007711 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007712 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7713 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007714 operation->data.inputs.cipher_suite = *cipher_suite;
7715
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007716#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007717 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007718 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007719 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007720
Przemek Stekiele12ed362022-12-21 12:54:46 +01007721 computation_stage->state = PSA_PAKE_STATE_SETUP;
7722 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7723 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7724 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007725 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007726#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007727 {
7728 status = PSA_ERROR_NOT_SUPPORTED;
7729 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007730 }
7731
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007732 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7733
Przemek Stekiel51eac532022-12-07 11:04:51 +01007734 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007735exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007736 psa_pake_abort(operation);
7737 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007738}
7739
7740psa_status_t psa_pake_set_password_key(
7741 psa_pake_operation_t *operation,
7742 mbedtls_svc_key_id_t password)
7743{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007744 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007745 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7746 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007747
Przemek Stekiel51eac532022-12-07 11:04:51 +01007748 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007749 status = PSA_ERROR_BAD_STATE;
7750 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007751 }
7752
Przemek Stekiel6c764412022-11-22 14:05:12 +01007753 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7754 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007755 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007756 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007757 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007758 }
7759
Przemek Stekiel6c764412022-11-22 14:05:12 +01007760 psa_key_attributes_t attributes = {
7761 .core = slot->attr
7762 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007763
Przemek Stekiel51eac532022-12-07 11:04:51 +01007764 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007765
Przemek Stekiel51eac532022-12-07 11:04:51 +01007766 if (type != PSA_KEY_TYPE_PASSWORD &&
7767 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7768 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007769 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007770 }
7771
Przemek Stekiel51eac532022-12-07 11:04:51 +01007772 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7773 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007774 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007775 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007776 }
7777
7778 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7779 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007780 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007781exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007782 if (status != PSA_SUCCESS) {
7783 psa_pake_abort(operation);
7784 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007785 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007786 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007787}
7788
7789psa_status_t psa_pake_set_user(
7790 psa_pake_operation_t *operation,
7791 const uint8_t *user_id,
7792 size_t user_id_len)
7793{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007795
7796 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007797 status = PSA_ERROR_BAD_STATE;
7798 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007799 }
7800
Przemek Stekiel51eac532022-12-07 11:04:51 +01007801 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007802 status = PSA_ERROR_INVALID_ARGUMENT;
7803 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007804 }
7805
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007806 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007807 status = PSA_ERROR_BAD_STATE;
7808 goto exit;
7809 }
7810
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007811 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7812 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007813 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7814 goto exit;
7815 }
7816
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007817 memcpy(operation->data.inputs.user, user_id, user_id_len);
7818 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007819
7820 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007821exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007822 psa_pake_abort(operation);
7823 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007824}
7825
7826psa_status_t psa_pake_set_peer(
7827 psa_pake_operation_t *operation,
7828 const uint8_t *peer_id,
7829 size_t peer_id_len)
7830{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007831 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007832
7833 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007834 status = PSA_ERROR_BAD_STATE;
7835 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007836 }
7837
Przemek Stekiel51eac532022-12-07 11:04:51 +01007838 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007839 status = PSA_ERROR_INVALID_ARGUMENT;
7840 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007841 }
7842
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007843 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007844 status = PSA_ERROR_BAD_STATE;
7845 goto exit;
7846 }
7847
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007848 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7849 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007850 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7851 goto exit;
7852 }
7853
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007854 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7855 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007856
7857 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007858exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007859 psa_pake_abort(operation);
7860 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007861}
7862
7863psa_status_t psa_pake_set_role(
7864 psa_pake_operation_t *operation,
7865 psa_pake_role_t role)
7866{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007868
Przemek Stekiel51eac532022-12-07 11:04:51 +01007869 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007870 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007871 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007872 }
7873
Przemek Stekiel09104b82023-03-06 14:11:51 +01007874 switch (operation->alg) {
7875#if defined(PSA_WANT_ALG_JPAKE)
7876 case PSA_ALG_JPAKE:
7877 if (role == PSA_PAKE_ROLE_NONE) {
7878 return PSA_SUCCESS;
7879 }
7880 status = PSA_ERROR_INVALID_ARGUMENT;
7881 break;
7882#endif
7883 default:
7884 (void) role;
7885 status = PSA_ERROR_NOT_SUPPORTED;
7886 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007887 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007888exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007889 psa_pake_abort(operation);
7890 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007891}
7892
Przemek Stekielb09c4872023-01-17 12:05:38 +01007893/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007894#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007895static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007896 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007897{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007898 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007899 case PSA_PAKE_OUTPUT_X1_X2:
7900 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007901 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007902 case PSA_PAKE_X1_STEP_KEY_SHARE:
7903 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007904 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7905 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007906 case PSA_PAKE_X1_STEP_ZK_PROOF:
7907 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007908 case PSA_PAKE_X2_STEP_KEY_SHARE:
7909 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007910 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7911 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007912 case PSA_PAKE_X2_STEP_ZK_PROOF:
7913 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007914 default:
7915 return PSA_JPAKE_STEP_INVALID;
7916 }
7917 break;
7918 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007919 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007920 case PSA_PAKE_X1_STEP_KEY_SHARE:
7921 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007922 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7923 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007924 case PSA_PAKE_X1_STEP_ZK_PROOF:
7925 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007926 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007927 return PSA_JPAKE_STEP_INVALID;
7928 }
7929 break;
7930 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007931 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007932 case PSA_PAKE_X1_STEP_KEY_SHARE:
7933 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007934 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7935 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007936 case PSA_PAKE_X1_STEP_ZK_PROOF:
7937 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007938 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007939 return PSA_JPAKE_STEP_INVALID;
7940 }
7941 break;
7942 default:
7943 return PSA_JPAKE_STEP_INVALID;
7944 }
7945 return PSA_JPAKE_STEP_INVALID;
7946}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007947#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007948
Przemek Stekiel2797d372022-12-22 11:19:22 +01007949static psa_status_t psa_pake_complete_inputs(
7950 psa_pake_operation_t *operation)
7951{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007952 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007953 /* Create copy of the inputs on stack as inputs share memory
7954 with the driver context which will be setup by the driver. */
7955 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007956
Przemek Stekielfde11282023-03-13 16:06:09 +01007957 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007958 return PSA_ERROR_BAD_STATE;
7959 }
7960
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007961 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007962 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7963 return PSA_ERROR_BAD_STATE;
7964 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007965 }
7966
Przemek Stekiel18620a32023-01-17 16:34:52 +01007967 /* Clear driver context */
7968 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7969
7970 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007971
7972 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007973 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7974 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007975
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007976 /* User and peer are translated to role. */
7977 mbedtls_free(inputs.user);
7978 mbedtls_free(inputs.peer);
7979
Przemek Stekiel2797d372022-12-22 11:19:22 +01007980 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007981#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007982 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007983 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007984 psa_jpake_computation_stage_t *computation_stage =
7985 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007986 computation_stage->state = PSA_PAKE_STATE_READY;
7987 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7988 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7989 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007990 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007991#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007992 {
7993 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007994 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007995 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007996 return status;
7997}
7998
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007999#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008000static psa_status_t psa_jpake_output_prologue(
8001 psa_pake_operation_t *operation,
8002 psa_pake_step_t step)
8003{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008004 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8005 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8006 step != PSA_PAKE_STEP_ZK_PROOF) {
8007 return PSA_ERROR_INVALID_ARGUMENT;
8008 }
8009
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008010 psa_jpake_computation_stage_t *computation_stage =
8011 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008012
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008013 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
8014 return PSA_ERROR_BAD_STATE;
8015 }
8016
8017 if (computation_stage->state != PSA_PAKE_STATE_READY &&
8018 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
8019 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
8020 return PSA_ERROR_BAD_STATE;
8021 }
8022
8023 if (computation_stage->state == PSA_PAKE_STATE_READY) {
8024 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008025 return PSA_ERROR_BAD_STATE;
8026 }
8027
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008028 switch (computation_stage->output_step) {
8029 case PSA_PAKE_STEP_X1_X2:
8030 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008031 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008032 case PSA_PAKE_STEP_X2S:
8033 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008034 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008035 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01008036 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008037 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008038
8039 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
8040 }
8041
8042 /* Check if step matches current sequence */
8043 switch (computation_stage->sequence) {
8044 case PSA_PAKE_X1_STEP_KEY_SHARE:
8045 case PSA_PAKE_X2_STEP_KEY_SHARE:
8046 if (step != PSA_PAKE_STEP_KEY_SHARE) {
8047 return PSA_ERROR_BAD_STATE;
8048 }
8049 break;
8050
8051 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
8052 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
8053 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
8054 return PSA_ERROR_BAD_STATE;
8055 }
8056 break;
8057
8058 case PSA_PAKE_X1_STEP_ZK_PROOF:
8059 case PSA_PAKE_X2_STEP_ZK_PROOF:
8060 if (step != PSA_PAKE_STEP_ZK_PROOF) {
8061 return PSA_ERROR_BAD_STATE;
8062 }
8063 break;
8064
8065 default:
8066 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008067 }
8068
8069 return PSA_SUCCESS;
8070}
8071
8072static psa_status_t psa_jpake_output_epilogue(
8073 psa_pake_operation_t *operation)
8074{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008075 psa_jpake_computation_stage_t *computation_stage =
8076 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008077
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008078 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008079 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008080 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008081 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008082 computation_stage->state = PSA_PAKE_STATE_READY;
8083 computation_stage->output_step++;
8084 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8085 } else {
8086 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008087 }
8088
8089 return PSA_SUCCESS;
8090}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008091#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008092
Neil Armstronga7d08c32022-06-01 18:21:20 +02008093psa_status_t psa_pake_output(
8094 psa_pake_operation_t *operation,
8095 psa_pake_step_t step,
8096 uint8_t *output,
8097 size_t output_size,
8098 size_t *output_length)
8099{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008100 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008101 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008102 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008103
8104 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008105 status = psa_pake_complete_inputs(operation);
8106 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008107 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008108 }
8109 }
8110
8111 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008112 status = PSA_ERROR_BAD_STATE;
8113 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008114 }
8115
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008116 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008117 status = PSA_ERROR_INVALID_ARGUMENT;
8118 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008119 }
8120
Przemek Stekiele12ed362022-12-21 12:54:46 +01008121 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008122#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008123 case PSA_ALG_JPAKE:
8124 status = psa_jpake_output_prologue(operation, step);
8125 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008126 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008127 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008128 driver_step = convert_jpake_computation_stage_to_driver_step(
8129 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008130 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008131#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008132 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008133 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008134 status = PSA_ERROR_NOT_SUPPORTED;
8135 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008136 }
8137
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008138 status = psa_driver_wrapper_pake_output(operation, driver_step,
8139 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008140
8141 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008142 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008143 }
8144
8145 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008146#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008147 case PSA_ALG_JPAKE:
8148 status = psa_jpake_output_epilogue(operation);
8149 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008150 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151 }
8152 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008153#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008154 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008155 status = PSA_ERROR_NOT_SUPPORTED;
8156 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008157 }
8158
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008159 return PSA_SUCCESS;
8160exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008161 psa_pake_abort(operation);
8162 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008163}
8164
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008165#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008166static psa_status_t psa_jpake_input_prologue(
8167 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008168 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008169{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008170 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8171 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8172 step != PSA_PAKE_STEP_ZK_PROOF) {
8173 return PSA_ERROR_INVALID_ARGUMENT;
8174 }
8175
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008176 psa_jpake_computation_stage_t *computation_stage =
8177 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008178
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008179 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
8180 return PSA_ERROR_BAD_STATE;
8181 }
8182
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008183 if (computation_stage->state != PSA_PAKE_STATE_READY &&
8184 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
8185 computation_stage->state != PSA_PAKE_INPUT_X4S) {
8186 return PSA_ERROR_BAD_STATE;
8187 }
8188
8189 if (computation_stage->state == PSA_PAKE_STATE_READY) {
8190 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008191 return PSA_ERROR_BAD_STATE;
8192 }
8193
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008194 switch (computation_stage->input_step) {
8195 case PSA_PAKE_STEP_X1_X2:
8196 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008197 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008198 case PSA_PAKE_STEP_X2S:
8199 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008200 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008201 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01008202 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008203 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008204
8205 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
8206 }
8207
8208 /* Check if step matches current sequence */
8209 switch (computation_stage->sequence) {
8210 case PSA_PAKE_X1_STEP_KEY_SHARE:
8211 case PSA_PAKE_X2_STEP_KEY_SHARE:
8212 if (step != PSA_PAKE_STEP_KEY_SHARE) {
8213 return PSA_ERROR_BAD_STATE;
8214 }
8215 break;
8216
8217 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
8218 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
8219 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
8220 return PSA_ERROR_BAD_STATE;
8221 }
8222 break;
8223
8224 case PSA_PAKE_X1_STEP_ZK_PROOF:
8225 case PSA_PAKE_X2_STEP_ZK_PROOF:
8226 if (step != PSA_PAKE_STEP_ZK_PROOF) {
8227 return PSA_ERROR_BAD_STATE;
8228 }
8229 break;
8230
8231 default:
8232 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008233 }
8234
8235 return PSA_SUCCESS;
8236}
8237
Przemek Stekiele12ed362022-12-21 12:54:46 +01008238static psa_status_t psa_jpake_input_epilogue(
8239 psa_pake_operation_t *operation)
8240{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008241 psa_jpake_computation_stage_t *computation_stage =
8242 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008243
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008244 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008245 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008246 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008247 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008248 computation_stage->state = PSA_PAKE_STATE_READY;
8249 computation_stage->input_step++;
8250 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8251 } else {
8252 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008253 }
8254
8255 return PSA_SUCCESS;
8256}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008257#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258
Neil Armstronga7d08c32022-06-01 18:21:20 +02008259psa_status_t psa_pake_input(
8260 psa_pake_operation_t *operation,
8261 psa_pake_step_t step,
8262 const uint8_t *input,
8263 size_t input_length)
8264{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008265 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008266 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008267 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8268 operation->primitive,
8269 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008270
8271 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008272 status = psa_pake_complete_inputs(operation);
8273 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008274 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008275 }
8276 }
8277
8278 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008279 status = PSA_ERROR_BAD_STATE;
8280 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008281 }
8282
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008283 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008284 status = PSA_ERROR_INVALID_ARGUMENT;
8285 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008286 }
8287
Przemek Stekiele12ed362022-12-21 12:54:46 +01008288 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008289#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008290 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008291 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008292 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008293 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008294 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008295 driver_step = convert_jpake_computation_stage_to_driver_step(
8296 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008297 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008298#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008299 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008300 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008301 status = PSA_ERROR_NOT_SUPPORTED;
8302 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008303 }
8304
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008305 status = psa_driver_wrapper_pake_input(operation, driver_step,
8306 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008307
8308 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008309 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008310 }
8311
8312 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008313#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008314 case PSA_ALG_JPAKE:
8315 status = psa_jpake_input_epilogue(operation);
8316 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008317 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008318 }
8319 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008320#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008321 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008322 status = PSA_ERROR_NOT_SUPPORTED;
8323 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008324 }
8325
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008326 return PSA_SUCCESS;
8327exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008328 psa_pake_abort(operation);
8329 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008330}
8331
8332psa_status_t psa_pake_get_implicit_key(
8333 psa_pake_operation_t *operation,
8334 psa_key_derivation_operation_t *output)
8335{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008336 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008337 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008338 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008339 size_t shared_key_len = 0;
8340
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008341 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008342 status = PSA_ERROR_BAD_STATE;
8343 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008344 }
8345
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008346#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008347 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008348 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008349 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008350 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8351 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008352 status = PSA_ERROR_BAD_STATE;
8353 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008354 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008355 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008356#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008357 {
8358 status = PSA_ERROR_NOT_SUPPORTED;
8359 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008360 }
8361
Przemek Stekiel0c781802022-11-29 14:53:13 +01008362 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8363 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008364 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008365 &shared_key_len);
8366
8367 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008368 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008369 }
8370
8371 status = psa_key_derivation_input_bytes(output,
8372 PSA_KEY_DERIVATION_INPUT_SECRET,
8373 shared_key,
8374 shared_key_len);
8375
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008376 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008377exit:
8378 abort_status = psa_pake_abort(operation);
8379 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008380}
8381
8382psa_status_t psa_pake_abort(
8383 psa_pake_operation_t *operation)
8384{
Przemek Stekield69dca92023-01-31 19:59:20 +01008385 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008386
Przemek Stekield69dca92023-01-31 19:59:20 +01008387 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008388 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008389 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008390
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008391 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8392 if (operation->data.inputs.password != NULL) {
8393 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008394 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008395 mbedtls_free(operation->data.inputs.password);
8396 }
8397 if (operation->data.inputs.user != NULL) {
8398 mbedtls_free(operation->data.inputs.user);
8399 }
8400 if (operation->data.inputs.peer != NULL) {
8401 mbedtls_free(operation->data.inputs.peer);
8402 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008403 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008404 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008405
Przemek Stekield69dca92023-01-31 19:59:20 +01008406 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008407}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008408#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008409
Gilles Peskinee59236f2018-01-27 23:32:46 +01008410#endif /* MBEDTLS_PSA_CRYPTO_C */