blob: f438c87ce120d5d4dc5f58d9cf02bba07b57ee4f [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;
5485 size_t mac_output_length;
5486 uint8_t U_i[PSA_HASH_MAX_SIZE];
5487 uint8_t U_accumulator[PSA_HASH_MAX_SIZE];
5488 uint8_t j;
5489 uint64_t i;
5490
5491 uint8_t *input = mbedtls_calloc(pbkdf2->salt_length + 4, 1);
5492 memcpy(input, pbkdf2->salt, pbkdf2->salt_length);
5493 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, input, pbkdf2->salt_length);
5494
5495 status = psa_driver_wrapper_mac_compute(attributes, pbkdf2->password,
5496 pbkdf2->password_length, prf_alg,
5497 input, (pbkdf2->salt_length + 4),
5498 U_i, prf_output_length,
5499 &mac_output_length);
5500 if (status != PSA_SUCCESS) {
5501 goto cleanup;
5502 }
5503 memcpy(U_accumulator, U_i, mac_output_length);
5504
5505 for (i = 1; i < pbkdf2->input_cost; i++) {
5506 status = psa_driver_wrapper_mac_compute(attributes,
5507 pbkdf2->password,
5508 pbkdf2->password_length,
5509 prf_alg, U_i, prf_output_length,
5510 U_i, prf_output_length,
5511 &mac_output_length);
5512 if (status != PSA_SUCCESS) {
5513 goto cleanup;
5514 }
5515
5516 // U1 xor U2
5517 for (j = 0; j < prf_output_length; j++) {
5518 U_accumulator[j] ^= U_i[j];
5519 }
5520 }
5521
5522 memcpy(pbkdf2->output_block, U_accumulator, prf_output_length);
5523
5524cleanup:
5525 /* Zeroise buffers to clear sensitive data from memory. */
5526 mbedtls_platform_zeroize(U_accumulator, PSA_HASH_MAX_SIZE);
5527 mbedtls_platform_zeroize(U_i, PSA_HASH_MAX_SIZE);
5528 mbedtls_platform_zeroize(input, pbkdf2->salt_length + 4);
5529 mbedtls_free(input);
5530 return status;
5531}
5532#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
5533
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005534psa_status_t psa_key_derivation_output_bytes(
5535 psa_key_derivation_operation_t *operation,
5536 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005538{
5539 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005543 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005545 }
5546
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005548 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005549 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005550 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005551 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005552 goto exit;
5553 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005556 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005557 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5558 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005559 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005560 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005562 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005563 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005564
Przemek Stekiel69c46792022-06-10 12:59:51 +02005565#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5567 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5568 output, output_length);
5569 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005570#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005571#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5572 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5574 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5575 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5576 kdf_alg, output,
5577 output_length);
5578 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005579#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5580 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005581#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005583 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5585 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005586#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305587#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5588 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5589 /* As output functionality is not added yet return
5590 * PSA_ERROR_NOT_SUPPORTED for now if inputs are passed correctly.
5591 * If input validation fails operation is aborted and output_bytes
5592 * will return PSA_ERROR_BAD_STATE */
5593 status = PSA_ERROR_NOT_SUPPORTED;
5594 } else
5595#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005596
Gilles Peskineeab56e42018-07-12 17:12:33 +02005597 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005598 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005600 }
5601
5602exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005604 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005605 * This allows us to differentiate between exhausted operations and
5606 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5607 * operations. */
5608 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005610 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005612 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005614}
5615
David Brown7807bf72021-02-09 16:28:23 -07005616#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005617static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005618{
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 if (data_size >= 8) {
5620 mbedtls_des_key_set_parity(data);
5621 }
5622 if (data_size >= 16) {
5623 mbedtls_des_key_set_parity(data + 8);
5624 }
5625 if (data_size >= 24) {
5626 mbedtls_des_key_set_parity(data + 16);
5627 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005628}
David Brown7807bf72021-02-09 16:28:23 -07005629#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005630
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005631/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 * ECC keys on a Weierstrass elliptic curve require the generation
5633 * of a private key which is an integer
5634 * in the range [1, N - 1], where N is the boundary of the private key domain:
5635 * N is the prime p for Diffie-Hellman, or the order of the
5636 * curve’s base point for ECC.
5637 *
5638 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5639 * This function generates the private key using the following process:
5640 *
5641 * 1. Draw a byte string of length ceiling(m/8) bytes.
5642 * 2. If m is not a multiple of 8, set the most significant
5643 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5644 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5645 * 4. If k > N - 2, discard the result and return to step 1.
5646 * 5. Output k + 1 as the private key.
5647 *
5648 * This method allows compliance to NIST standards, specifically the methods titled
5649 * Key-Pair Generation by Testing Candidates in the following publications:
5650 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5651 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5652 * Diffie-Hellman keys.
5653 *
5654 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5655 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5656 *
5657 * Note: Function allocates memory for *data buffer, so given *data should be
5658 * always NULL.
5659 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005660#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5661 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005662 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5663 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5664 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005665static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005666 psa_key_slot_t *slot,
5667 size_t bits,
5668 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005669 uint8_t **data
5670 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005671{
Valerio Setti52789862023-04-07 12:13:11 +02005672#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005673 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005674 mbedtls_mpi k;
5675 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005677 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 mbedtls_mpi_init(&k);
5680 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005681
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005682 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005684 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005688 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005689 goto cleanup;
5690 }
5691
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005692 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005696
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005697 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005698 /* Let m be the bit size of N. */
5699 size_t m = ecp_group.nbits;
5700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005702
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005703 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005705
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005706 /* Note: This function is always called with *data == NULL and it
5707 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 *data = mbedtls_calloc(1, m_bytes);
5709 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005710 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005711 goto cleanup;
5712 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005713
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005715 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005717 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005719
5720 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005722 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005723 * (8 * ceiling(m/8) - m) bits of the first byte in
5724 * the string to zero.
5725 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005727 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005728 }
5729
5730 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 * big-endian byte string.
5732 */
5733 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005734
5735 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 * Result of comparison is returned. When it indicates error
5737 * then this function is called again.
5738 */
5739 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005740 }
5741
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005742 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5744 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005745cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 if (ret != 0) {
5747 status = mbedtls_to_psa_error(ret);
5748 }
5749 if (status != PSA_SUCCESS) {
5750 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005751 *data = NULL;
5752 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 mbedtls_mpi_free(&k);
5754 mbedtls_mpi_free(&diff_N_2);
5755 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005756#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005757 (void) slot;
5758 (void) bits;
5759 (void) operation;
5760 (void) data;
5761 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005762#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005763}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005764
5765/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5766 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5767 *
5768 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5769 * draw a 32-byte string and process it as specified in
5770 * Elliptic Curves for Security [RFC7748] §5.
5771 *
5772 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5773 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5774 *
5775 * Note: Function allocates memory for *data buffer, so given *data should be
5776 * always NULL.
5777 */
5778
5779static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5780 size_t bits,
5781 psa_key_derivation_operation_t *operation,
5782 uint8_t **data
5783 )
5784{
5785 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005786 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005789 case 255:
5790 output_length = 32;
5791 break;
5792 case 448:
5793 output_length = 56;
5794 break;
5795 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005797 break;
5798 }
5799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 if (*data == NULL) {
5803 return PSA_ERROR_INSUFFICIENT_MEMORY;
5804 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005809 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005811
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005813 case 255:
5814 (*data)[0] &= 248;
5815 (*data)[31] &= 127;
5816 (*data)[31] |= 64;
5817 break;
5818 case 448:
5819 (*data)[0] &= 252;
5820 (*data)[55] |= 128;
5821 break;
5822 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005824 break;
5825 }
5826
5827 return status;
5828}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005829#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5830 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005831 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5832 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5833 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005834
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005835static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005836 psa_key_slot_t *slot,
5837 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005839{
5840 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305842 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005843 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5846 return PSA_ERROR_INVALID_ARGUMENT;
5847 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005848
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005849#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5850 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005851 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5852 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5853 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5855 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5856 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005857 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5859 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005860 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 }
5862 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005863 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5865 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005866 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005868 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005869 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005870#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5871 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005872 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5873 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5874 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (key_type_is_raw_bytes(slot->attr.type)) {
5876 if (bits % 8 != 0) {
5877 return PSA_ERROR_INVALID_ARGUMENT;
5878 }
5879 data = mbedtls_calloc(1, bytes);
5880 if (data == NULL) {
5881 return PSA_ERROR_INSUFFICIENT_MEMORY;
5882 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 status = psa_key_derivation_output_bytes(operation, data, bytes);
5885 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005886 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 }
David Brown12ca5032021-02-11 11:02:00 -07005888#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5890 psa_des_set_key_parity(data, bytes);
5891 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005892#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 } else {
5894 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005895 }
Ronald Crondd04d422020-11-28 15:14:42 +01005896
Ronald Cronbf33c932020-11-28 18:06:53 +01005897 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005898 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005900 };
5901
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5903 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5904 &storage_size);
5905 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305906 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 }
Archanad8a83dc2021-06-14 10:04:16 +05305908 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 status = psa_allocate_buffer_to_slot(slot, storage_size);
5910 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305911 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 }
Archanad8a83dc2021-06-14 10:04:16 +05305913
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 status = psa_driver_wrapper_import_key(&attributes,
5915 data, bytes,
5916 slot->key.data,
5917 slot->key.bytes,
5918 &slot->key.bytes, &bits);
5919 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005920 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005922
5923exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 mbedtls_free(data);
5925 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005926}
5927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5929 psa_key_derivation_operation_t *operation,
5930 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005931{
5932 psa_status_t status;
5933 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005934 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005935
Ronald Cron81709fc2020-11-14 12:10:32 +01005936 *key = MBEDTLS_SVC_KEY_ID_INIT;
5937
Gilles Peskine0f84d622019-09-12 19:03:13 +02005938 /* Reject any attempt to create a zero-length key so that we don't
5939 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 if (psa_get_key_bits(attributes) == 0) {
5941 return PSA_ERROR_INVALID_ARGUMENT;
5942 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005943
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 if (operation->alg == PSA_ALG_NONE) {
5945 return PSA_ERROR_BAD_STATE;
5946 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 if (!operation->can_output_key) {
5949 return PSA_ERROR_NOT_PERMITTED;
5950 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5953 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005954#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005956 /* Deriving a key in a secure element is not implemented yet. */
5957 status = PSA_ERROR_NOT_SUPPORTED;
5958 }
5959#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 if (status == PSA_SUCCESS) {
5961 status = psa_generate_derived_key_internal(slot,
5962 attributes->core.bits,
5963 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005964 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 if (status == PSA_SUCCESS) {
5966 status = psa_finish_key_creation(slot, driver, key);
5967 }
5968 if (status != PSA_SUCCESS) {
5969 psa_fail_key_creation(slot, driver);
5970 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005973}
5974
Gilles Peskineeab56e42018-07-12 17:12:33 +02005975
5976
5977/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005978/* Key derivation */
5979/****************************************************************/
5980
Steven Cooreman932ffb72021-02-15 12:14:32 +01005981#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005982static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005983{
5984#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5986 return 1;
5987 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005988#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005989#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5991 return 1;
5992 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005993#endif
5994#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5996 return 1;
5997 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005998#endif
5999#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6001 return 1;
6002 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006003#endif
6004#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6006 return 1;
6007 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006008#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006009#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6011 return 1;
6012 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006013#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306014#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6015 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6016 return 1;
6017 }
6018#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006020}
6021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006023{
6024 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 psa_status_t status = psa_hash_setup(&operation, alg);
6026 psa_hash_abort(&operation);
6027 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006028}
6029
Gilles Peskine969c5d62019-01-16 15:53:06 +01006030static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006031 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006033{
Janos Follath5fe19732019-06-20 15:09:30 +01006034 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6035 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006037
Gilles Peskine969c5d62019-01-16 15:53:06 +01006038 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 if (!is_kdf_alg_supported(kdf_alg)) {
6040 return PSA_ERROR_NOT_SUPPORTED;
6041 }
John Durkop07cc04a2020-11-16 22:08:34 -08006042
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006043 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006044 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6046 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6047 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6048 if (hash_size == 0) {
6049 return PSA_ERROR_NOT_SUPPORTED;
6050 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006051
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006052 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6053 * we might fail later, which is somewhat unfriendly and potentially
6054 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 psa_status_t status = psa_hash_try_support(hash_alg);
6056 if (status != PSA_SUCCESS) {
6057 return status;
6058 }
6059 } else {
6060 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006061 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6064 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6065 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6066 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006067 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006068#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006069 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6071 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006072 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006074#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6075 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 operation->capacity = 255 * hash_size;
6077 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006078}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006081{
6082#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 if (alg == PSA_ALG_ECDH) {
6084 return PSA_SUCCESS;
6085 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006086#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006087#if defined(PSA_WANT_ALG_FFDH)
6088 if (alg == PSA_ALG_FFDH) {
6089 return PSA_SUCCESS;
6090 }
6091#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006092 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006094}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006095
6096static int psa_key_derivation_allows_free_form_secret_input(
6097 psa_algorithm_t kdf_alg)
6098{
6099#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6100 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6101 return 0;
6102 }
6103#endif
6104 (void) kdf_alg;
6105 return 1;
6106}
John Durkop07cc04a2020-11-16 22:08:34 -08006107#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6110 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006111{
6112 psa_status_t status;
6113
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 if (operation->alg != 0) {
6115 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006116 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006117
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6119 return PSA_ERROR_INVALID_ARGUMENT;
6120 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6121#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6122 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6123 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6124 status = psa_key_agreement_try_support(ka_alg);
6125 if (status != PSA_SUCCESS) {
6126 return status;
6127 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006128 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6129 return PSA_ERROR_INVALID_ARGUMENT;
6130 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6132#else
6133 return PSA_ERROR_NOT_SUPPORTED;
6134#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6135 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6136#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6137 status = psa_key_derivation_setup_kdf(operation, alg);
6138#else
6139 return PSA_ERROR_NOT_SUPPORTED;
6140#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6141 } else {
6142 return PSA_ERROR_INVALID_ARGUMENT;
6143 }
6144
6145 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006146 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 }
6148 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006149}
6150
Przemek Stekiel69c46792022-06-10 12:59:51 +02006151#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006152static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6153 psa_algorithm_t kdf_alg,
6154 psa_key_derivation_step_t step,
6155 const uint8_t *data,
6156 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006157{
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006159 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006161 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006162#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6164 return PSA_ERROR_INVALID_ARGUMENT;
6165 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006166#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 if (hkdf->state != HKDF_STATE_INIT) {
6168 return PSA_ERROR_BAD_STATE;
6169 } else {
6170 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6171 hash_alg,
6172 data, data_length);
6173 if (status != PSA_SUCCESS) {
6174 return status;
6175 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006176 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006178 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006179 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006180#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006182 /* We shouldn't be in different state as HKDF_EXPAND only allows
6183 * two inputs: SECRET (this case) and INFO which does not modify
6184 * the state. It could happen only if the hkdf
6185 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 if (hkdf->state != HKDF_STATE_INIT) {
6187 return PSA_ERROR_BAD_STATE;
6188 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006189
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006190 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6192 return PSA_ERROR_INVALID_ARGUMENT;
6193 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 memcpy(hkdf->prk, data, data_length);
6196 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006197#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006198 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006199 /* HKDF: If no salt was provided, use an empty salt.
6200 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006202#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6204 return PSA_ERROR_BAD_STATE;
6205 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006206#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6208 hash_alg,
6209 NULL, 0);
6210 if (status != PSA_SUCCESS) {
6211 return status;
6212 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006213 hkdf->state = HKDF_STATE_STARTED;
6214 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 if (hkdf->state != HKDF_STATE_STARTED) {
6216 return PSA_ERROR_BAD_STATE;
6217 }
6218 status = psa_mac_update(&hkdf->hmac,
6219 data, data_length);
6220 if (status != PSA_SUCCESS) {
6221 return status;
6222 }
6223 status = psa_mac_sign_finish(&hkdf->hmac,
6224 hkdf->prk,
6225 sizeof(hkdf->prk),
6226 &data_length);
6227 if (status != PSA_SUCCESS) {
6228 return status;
6229 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006230 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006231
Gilles Peskine2b522db2019-04-12 15:11:49 +02006232 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006233 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006234#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006236 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006238 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006240#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006241 {
6242 /* Block 0 is empty, and the next block will be
6243 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006245 }
6246
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006248 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006249#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6251 return PSA_ERROR_INVALID_ARGUMENT;
6252 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006253#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006254#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6256 hkdf->state == HKDF_STATE_INIT) {
6257 return PSA_ERROR_BAD_STATE;
6258 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006259#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 if (hkdf->state == HKDF_STATE_OUTPUT) {
6261 return PSA_ERROR_BAD_STATE;
6262 }
6263 if (hkdf->info_set) {
6264 return PSA_ERROR_BAD_STATE;
6265 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006266 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 if (data_length != 0) {
6268 hkdf->info = mbedtls_calloc(1, data_length);
6269 if (hkdf->info == NULL) {
6270 return PSA_ERROR_INSUFFICIENT_MEMORY;
6271 }
6272 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006273 }
6274 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006276 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006278 }
6279}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006280#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006281
John Durkop07cc04a2020-11-16 22:08:34 -08006282#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6283 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006284static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6285 const uint8_t *data,
6286 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006287{
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6289 return PSA_ERROR_BAD_STATE;
6290 }
Janos Follathf08e2652019-06-13 09:05:41 +01006291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 if (data_length != 0) {
6293 prf->seed = mbedtls_calloc(1, data_length);
6294 if (prf->seed == NULL) {
6295 return PSA_ERROR_INSUFFICIENT_MEMORY;
6296 }
Janos Follathf08e2652019-06-13 09:05:41 +01006297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006299 prf->seed_length = data_length;
6300 }
Janos Follathf08e2652019-06-13 09:05:41 +01006301
Gilles Peskine43f958b2020-12-13 14:55:14 +01006302 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006305}
6306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6308 const uint8_t *data,
6309 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006310{
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6312 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6313 return PSA_ERROR_BAD_STATE;
6314 }
Janos Follath81550542019-06-13 14:26:34 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 if (data_length != 0) {
6317 prf->secret = mbedtls_calloc(1, data_length);
6318 if (prf->secret == NULL) {
6319 return PSA_ERROR_INSUFFICIENT_MEMORY;
6320 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006323 prf->secret_length = data_length;
6324 }
Janos Follath81550542019-06-13 14:26:34 +01006325
Gilles Peskine43f958b2020-12-13 14:55:14 +01006326 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006329}
6330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6332 const uint8_t *data,
6333 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006334{
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6336 return PSA_ERROR_BAD_STATE;
6337 }
Janos Follath63028dd2019-06-13 09:15:47 +01006338
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 if (data_length != 0) {
6340 prf->label = mbedtls_calloc(1, data_length);
6341 if (prf->label == NULL) {
6342 return PSA_ERROR_INSUFFICIENT_MEMORY;
6343 }
Janos Follath63028dd2019-06-13 09:15:47 +01006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006346 prf->label_length = data_length;
6347 }
Janos Follath63028dd2019-06-13 09:15:47 +01006348
Gilles Peskine43f958b2020-12-13 14:55:14 +01006349 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006352}
6353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6355 psa_key_derivation_step_t step,
6356 const uint8_t *data,
6357 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006358{
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006360 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006362 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006364 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006366 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006368 }
6369}
John Durkop07cc04a2020-11-16 22:08:34 -08006370#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6371 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6372
6373#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6374static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6375 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006376 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006378{
6379 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6381 4 + data_length + prf->other_secret_length :
6382 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6385 return PSA_ERROR_INVALID_ARGUMENT;
6386 }
John Durkop07cc04a2020-11-16 22:08:34 -08006387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 uint8_t *pms = mbedtls_calloc(1, pms_len);
6389 if (pms == NULL) {
6390 return PSA_ERROR_INSUFFICIENT_MEMORY;
6391 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006392 uint8_t *cur = pms;
6393
6394 /* pure-PSK:
6395 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006396 *
6397 * The premaster secret is formed as follows: if the PSK is N octets
6398 * long, concatenate a uint16 with the value N, N zero octets, a second
6399 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006400 *
6401 * mixed-PSK:
6402 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6403 * follows: concatenate a uint16 with the length of the other secret,
6404 * the other secret itself, uint16 with the length of PSK, and the
6405 * PSK itself.
6406 * For details please check:
6407 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6408 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6409 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006410 */
6411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6413 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6414 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6415 if (prf->other_secret_length != 0) {
6416 memcpy(cur, prf->other_secret, prf->other_secret_length);
6417 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006418 cur += prf->other_secret_length;
6419 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 } else {
6421 *cur++ = MBEDTLS_BYTE_1(data_length);
6422 *cur++ = MBEDTLS_BYTE_0(data_length);
6423 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006424 cur += data_length;
6425 }
6426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 *cur++ = MBEDTLS_BYTE_1(data_length);
6428 *cur++ = MBEDTLS_BYTE_0(data_length);
6429 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006430 cur += data_length;
6431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 mbedtls_platform_zeroize(pms, pms_len);
6435 mbedtls_free(pms);
6436 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006437}
Janos Follath6660f0e2019-06-17 08:44:03 +01006438
Przemek Stekiele47201b2022-04-19 13:53:28 +02006439static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6440 psa_tls12_prf_key_derivation_t *prf,
6441 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006443{
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6445 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006446 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006447
6448 if (data_length != 0) {
6449 prf->other_secret = mbedtls_calloc(1, data_length);
6450 if (prf->other_secret == NULL) {
6451 return PSA_ERROR_INSUFFICIENT_MEMORY;
6452 }
6453
6454 memcpy(prf->other_secret, data, data_length);
6455 prf->other_secret_length = data_length;
6456 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006457 prf->other_secret_length = 0;
6458 }
6459
6460 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6461
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006463}
6464
Janos Follath6660f0e2019-06-17 08:44:03 +01006465static psa_status_t psa_tls12_prf_psk_to_ms_input(
6466 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006467 psa_key_derivation_step_t step,
6468 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006470{
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006472 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 return psa_tls12_prf_psk_to_ms_set_key(prf,
6474 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006475 break;
6476 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6478 data,
6479 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006480 break;
6481 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006483 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006484
Przemek Stekiele47201b2022-04-19 13:53:28 +02006485 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006486}
John Durkop07cc04a2020-11-16 22:08:34 -08006487#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006488
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006489#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6490static psa_status_t psa_tls12_ecjpake_to_pms_input(
6491 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006492 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006493 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006495{
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6497 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6498 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006499 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006500
6501 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 if (data[0] != 0x04) {
6503 return PSA_ERROR_INVALID_ARGUMENT;
6504 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006505
6506 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006510}
6511#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306512
6513#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6514static psa_status_t psa_pbkdf2_set_input_cost(
6515 psa_pbkdf2_key_derivation_t *pbkdf2,
6516 psa_key_derivation_step_t step,
6517 uint64_t data)
6518{
6519 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6520 return PSA_ERROR_INVALID_ARGUMENT;
6521 }
6522
6523 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6524 return PSA_ERROR_BAD_STATE;
6525 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306526
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306527 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306528 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306529 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306530
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306531 if (data == 0) {
6532 return PSA_ERROR_INVALID_ARGUMENT;
6533 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306534
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306535 pbkdf2->input_cost = data;
6536 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6537
6538 return PSA_SUCCESS;
6539}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306540
6541static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6542 const uint8_t *data,
6543 size_t data_length)
6544{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306545 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6546 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6547 return PSA_ERROR_BAD_STATE;
6548 }
6549
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306550 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6551 pbkdf2->salt = mbedtls_calloc(1, data_length);
6552 if (pbkdf2->salt == NULL) {
6553 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306554 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306555
6556 memcpy(pbkdf2->salt, data, data_length);
6557 pbkdf2->salt_length = data_length;
6558 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306559 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306560
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306561 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6562 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306563 return PSA_ERROR_INSUFFICIENT_MEMORY;
6564 }
6565
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306566 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6567 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306568 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306569 mbedtls_free(pbkdf2->salt);
6570 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306571 }
6572
6573 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6574
6575 return PSA_SUCCESS;
6576}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306577
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306578static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306579 const uint8_t *input,
6580 size_t input_len,
6581 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306582 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306583{
6584 psa_status_t status = PSA_SUCCESS;
6585 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6586 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306587 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306588 } else {
6589 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306590 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306591 }
6592 return status;
6593}
6594
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306595static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306596 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306597 const uint8_t *data,
6598 size_t data_length)
6599{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306600 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306601 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6602 return PSA_ERROR_BAD_STATE;
6603 }
6604
6605 if (data_length != 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306606 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6607 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6608 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6609 pbkdf2->password,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306610 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306611 }
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306612 }
6613
6614 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6615
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306616 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306617}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306618
6619static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306620 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306621 psa_key_derivation_step_t step,
6622 const uint8_t *data,
6623 size_t data_length)
6624{
6625 switch (step) {
6626 case PSA_KEY_DERIVATION_INPUT_SALT:
6627 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6628 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306629 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306630 default:
6631 return PSA_ERROR_INVALID_ARGUMENT;
6632 }
6633}
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306634#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6635
Gilles Peskineb8965192019-09-24 16:21:10 +02006636/** Check whether the given key type is acceptable for the given
6637 * input step of a key derivation.
6638 *
6639 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6640 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6641 * Both secret and non-secret inputs can alternatively have the type
6642 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6643 * that the input was passed as a buffer rather than via a key object.
6644 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006645static int psa_key_derivation_check_input_type(
6646 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006648{
Gilles Peskine449bd832023-01-11 14:50:10 +01006649 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006650 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 if (key_type == PSA_KEY_TYPE_DERIVE) {
6652 return PSA_SUCCESS;
6653 }
6654 if (key_type == PSA_KEY_TYPE_NONE) {
6655 return PSA_SUCCESS;
6656 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006657 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006658 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 if (key_type == PSA_KEY_TYPE_DERIVE) {
6660 return PSA_SUCCESS;
6661 }
6662 if (key_type == PSA_KEY_TYPE_NONE) {
6663 return PSA_SUCCESS;
6664 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006665 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006666 case PSA_KEY_DERIVATION_INPUT_LABEL:
6667 case PSA_KEY_DERIVATION_INPUT_SALT:
6668 case PSA_KEY_DERIVATION_INPUT_INFO:
6669 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6671 return PSA_SUCCESS;
6672 }
6673 if (key_type == PSA_KEY_TYPE_NONE) {
6674 return PSA_SUCCESS;
6675 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006676 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306677 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6678 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6679 return PSA_SUCCESS;
6680 }
6681 if (key_type == PSA_KEY_TYPE_DERIVE) {
6682 return PSA_SUCCESS;
6683 }
6684 if (key_type == PSA_KEY_TYPE_NONE) {
6685 return PSA_SUCCESS;
6686 }
6687 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006688 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006690}
6691
Janos Follathb80a94e2019-06-12 15:54:46 +01006692static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006693 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006694 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006695 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006696 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006698{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006699 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006701
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 status = psa_key_derivation_check_input_type(step, key_type);
6703 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006704 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006706
Przemek Stekiel69c46792022-06-10 12:59:51 +02006707#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006708 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6709 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6710 step, data, data_length);
6711 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006712#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006713#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6715 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6716 step, data, data_length);
6717 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006718#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6719#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6721 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6722 step, data, data_length);
6723 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006724#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006725#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006727 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6729 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006730#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306731#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6732 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306733 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6734 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306735 } else
6736#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006737 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006738 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006739 (void) data;
6740 (void) data_length;
6741 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006743 }
6744
Gilles Peskine224b0d62019-09-23 18:13:17 +02006745exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 if (status != PSA_SUCCESS) {
6747 psa_key_derivation_abort(operation);
6748 }
6749 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006750}
6751
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306752static psa_status_t psa_key_derivation_input_integer_internal(
6753 psa_key_derivation_operation_t *operation,
6754 psa_key_derivation_step_t step,
6755 uint64_t value)
6756{
6757 psa_status_t status;
6758 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6759
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306760#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6761 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6762 status = psa_pbkdf2_set_input_cost(
6763 &operation->ctx.pbkdf2, step, value);
6764 } else
6765#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306766 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306767 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306768 (void) value;
6769 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306770 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306771 }
6772
6773 if (status != PSA_SUCCESS) {
6774 psa_key_derivation_abort(operation);
6775 }
6776 return status;
6777}
6778
Janos Follath51f4a0f2019-06-14 11:35:55 +01006779psa_status_t psa_key_derivation_input_bytes(
6780 psa_key_derivation_operation_t *operation,
6781 psa_key_derivation_step_t step,
6782 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006784{
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 return psa_key_derivation_input_internal(operation, step,
6786 PSA_KEY_TYPE_NONE,
6787 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006788}
6789
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306790psa_status_t psa_key_derivation_input_integer(
6791 psa_key_derivation_operation_t *operation,
6792 psa_key_derivation_step_t step,
6793 uint64_t value)
6794{
6795 return psa_key_derivation_input_integer_internal(operation, step, value);
6796}
6797
Janos Follath51f4a0f2019-06-14 11:35:55 +01006798psa_status_t psa_key_derivation_input_key(
6799 psa_key_derivation_operation_t *operation,
6800 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006801 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006802{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006803 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006804 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006805 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006806
Ronald Cron5c522922020-11-14 16:35:34 +01006807 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006808 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6809 if (status != PSA_SUCCESS) {
6810 psa_key_derivation_abort(operation);
6811 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006812 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006813
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306814 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6815 * permission to output to a key object. */
6816 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6817 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006818 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006820
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 status = psa_key_derivation_input_internal(operation,
6822 step, slot->attr.type,
6823 slot->key.data,
6824 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006825
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006827
Gilles Peskine449bd832023-01-11 14:50:10 +01006828 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006829}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006830
6831
6832
6833/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006834/* Key agreement */
6835/****************************************************************/
6836
Gilles Peskine449bd832023-01-11 14:50:10 +01006837psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6838 const uint8_t *key_buffer,
6839 size_t key_buffer_size,
6840 psa_algorithm_t alg,
6841 const uint8_t *peer_key,
6842 size_t peer_key_length,
6843 uint8_t *shared_secret,
6844 size_t shared_secret_size,
6845 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006846{
Gilles Peskine449bd832023-01-11 14:50:10 +01006847 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006848#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006849 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006850 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6851 key_buffer_size, alg,
6852 peer_key, peer_key_length,
6853 shared_secret,
6854 shared_secret_size,
6855 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006856#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006857
6858#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6859 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006860 return mbedtls_psa_key_agreement_ffdh(attributes,
6861 peer_key,
6862 peer_key_length,
6863 key_buffer,
6864 key_buffer_size,
6865 shared_secret,
6866 shared_secret_size,
6867 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006868#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6869
Gilles Peskine01d718c2018-09-18 12:01:02 +02006870 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006871 (void) attributes;
6872 (void) key_buffer;
6873 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006874 (void) peer_key;
6875 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006876 (void) shared_secret;
6877 (void) shared_secret_size;
6878 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006880 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006881}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006882
Aditya Deshpande17845b82022-10-13 17:21:01 +01006883/** Internal function for raw key agreement
6884 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006885 * to the driver's implementation if a driver is present.
6886 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006887 * (psa_key_agreement_raw_builtin).
6888 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006889static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6890 psa_key_slot_t *private_key,
6891 const uint8_t *peer_key,
6892 size_t peer_key_length,
6893 uint8_t *shared_secret,
6894 size_t shared_secret_size,
6895 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006896{
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6898 return PSA_ERROR_NOT_SUPPORTED;
6899 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006900
6901 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006903 };
6904
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 return psa_driver_wrapper_key_agreement(&attributes,
6906 private_key->key.data,
6907 private_key->key.bytes, alg,
6908 peer_key, peer_key_length,
6909 shared_secret,
6910 shared_secret_size,
6911 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006912}
6913
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006914/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006915 * to potentially free embedded data structures and wipe confidential data.
6916 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006917static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6918 psa_key_derivation_step_t step,
6919 psa_key_slot_t *private_key,
6920 const uint8_t *peer_key,
6921 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006922{
6923 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006924 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006925 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006927
6928 /* Step 1: run the secret agreement algorithm to generate the shared
6929 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 status = psa_key_agreement_raw_internal(ka_alg,
6931 private_key,
6932 peer_key, peer_key_length,
6933 shared_secret,
6934 sizeof(shared_secret),
6935 &shared_secret_length);
6936 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006937 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006939
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006940 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006941 * the shared secret. A shared secret is permitted wherever a key
6942 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 status = psa_key_derivation_input_internal(operation, step,
6944 PSA_KEY_TYPE_DERIVE,
6945 shared_secret,
6946 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006947exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6949 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006950}
6951
Gilles Peskine449bd832023-01-11 14:50:10 +01006952psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6953 psa_key_derivation_step_t step,
6954 mbedtls_svc_key_id_t private_key,
6955 const uint8_t *peer_key,
6956 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006957{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006958 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006959 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006960 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006961
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6963 return PSA_ERROR_INVALID_ARGUMENT;
6964 }
Ronald Cron5c522922020-11-14 16:35:34 +01006965 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6967 if (status != PSA_SUCCESS) {
6968 return status;
6969 }
6970 status = psa_key_agreement_internal(operation, step,
6971 slot,
6972 peer_key, peer_key_length);
6973 if (status != PSA_SUCCESS) {
6974 psa_key_derivation_abort(operation);
6975 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006976 /* If a private key has been added as SECRET, we allow the derived
6977 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006979 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006981 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006982
Gilles Peskine449bd832023-01-11 14:50:10 +01006983 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006984
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006986}
6987
Gilles Peskine449bd832023-01-11 14:50:10 +01006988psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6989 mbedtls_svc_key_id_t private_key,
6990 const uint8_t *peer_key,
6991 size_t peer_key_length,
6992 uint8_t *output,
6993 size_t output_size,
6994 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006995{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006996 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006997 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006998 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006999
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007001 status = PSA_ERROR_INVALID_ARGUMENT;
7002 goto exit;
7003 }
Ronald Cron5c522922020-11-14 16:35:34 +01007004 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7006 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007007 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007008 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007009
Gilles Peskine3e561302022-04-14 00:17:15 +02007010 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7011 * for the output size. The PSA specification only guarantees that this
7012 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7013 * but it might be nice to allow smaller buffers if the output fits.
7014 * At the time of writing this comment, with only ECDH implemented,
7015 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7016 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7017 * be exact for it as well. */
7018 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7020 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007021 status = PSA_ERROR_BUFFER_TOO_SMALL;
7022 goto exit;
7023 }
7024
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 status = psa_key_agreement_raw_internal(alg, slot,
7026 peer_key, peer_key_length,
7027 output, output_size,
7028 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007029
7030exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007032 /* If an error happens and is not handled properly, the output
7033 * may be used as a key to protect sensitive data. Arrange for such
7034 * a key to be random, which is likely to result in decryption or
7035 * verification errors. This is better than filling the buffer with
7036 * some constant data such as zeros, which would result in the data
7037 * being protected with a reproducible, easily knowable key.
7038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007040 *output_length = output_size;
7041 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007042
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007044
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007046}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007047
7048
Gilles Peskine30524eb2020-11-13 17:02:26 +01007049
Gilles Peskine86a440b2018-11-12 18:39:40 +01007050/****************************************************************/
7051/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007052/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007053
Gilles Peskine30524eb2020-11-13 17:02:26 +01007054/** Initialize the PSA random generator.
7055 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007056static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007057{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007058#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007060#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7061
Gilles Peskine30524eb2020-11-13 17:02:26 +01007062 /* Set default configuration if
7063 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007065 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 }
7067 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007068 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007070
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007072#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7073 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7074 /* The PSA entropy injection feature depends on using NV seed as an entropy
7075 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007076 mbedtls_entropy_add_source(&rng->entropy,
7077 mbedtls_nv_seed_poll, NULL,
7078 MBEDTLS_ENTROPY_BLOCK_SIZE,
7079 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007080#endif
7081
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007083#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007084}
7085
7086/** Deinitialize the PSA random generator.
7087 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007088static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007089{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007090#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007092#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7094 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007095#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007096}
7097
7098/** Seed the PSA random generator.
7099 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007100static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007101{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007102#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7103 /* Do nothing: the external RNG seeds itself. */
7104 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007106#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007107 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7109 drbg_seed, sizeof(drbg_seed) - 1);
7110 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007111#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007112}
7113
Gilles Peskine449bd832023-01-11 14:50:10 +01007114psa_status_t psa_generate_random(uint8_t *output,
7115 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007116{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007117 GUARD_MODULE_INITIALIZED;
7118
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007119#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7120
7121 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007122 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7123 output, output_size,
7124 &output_length);
7125 if (status != PSA_SUCCESS) {
7126 return status;
7127 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007128 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007129 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 if (output_length != output_size) {
7131 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7132 }
7133 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007134
7135#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7136
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007138 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7140 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7141 output_size);
7142 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7143 output, request_size);
7144 if (ret != 0) {
7145 return mbedtls_to_psa_error(ret);
7146 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007147 output_size -= request_size;
7148 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007149 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007151#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007152}
7153
Gilles Peskine8814fc42020-12-14 15:33:44 +01007154/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007155 *
7156 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7157 * `psa_generate_random(...)`. The state parameter is ignored since the
7158 * PSA API doesn't support passing an explicit state.
7159 *
7160 * In the non-external case, psa_generate_random() calls an
7161 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7162 * and semantics as mbedtls_psa_get_random(). As an optimization,
7163 * instead of doing this back-and-forth between the PSA API and the
7164 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7165 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007166 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007167#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7168int mbedtls_psa_get_random(void *p_rng,
7169 unsigned char *output,
7170 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007171{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007172 /* This function takes a pointer to the RNG state because that's what
7173 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7174 * its own state internally and doesn't let the caller access that state.
7175 * So we just ignore the state parameter, and in practice we'll pass
7176 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007177 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 psa_status_t status = psa_generate_random(output, output_size);
7179 if (status == PSA_SUCCESS) {
7180 return 0;
7181 } else {
7182 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7183 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007184}
7185#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7186
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007187#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00007188#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007189
Gilles Peskine449bd832023-01-11 14:50:10 +01007190psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7191 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007192{
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 if (global_data.initialized) {
7194 return PSA_ERROR_NOT_PERMITTED;
7195 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007196
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7198 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7199 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7200 return PSA_ERROR_INVALID_ARGUMENT;
7201 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007202
Gilles Peskine449bd832023-01-11 14:50:10 +01007203 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007204}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007205#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007206
Ronald Cron3772afe2021-02-08 16:10:05 +01007207/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007208 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007209 * \param type The key type
7210 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007211 *
7212 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007213 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007214 * \retval #PSA_ERROR_INVALID_ARGUMENT
7215 * The size in bits of the key is not valid.
7216 * \retval #PSA_ERROR_NOT_SUPPORTED
7217 * The type and/or the size in bits of the key or the combination of
7218 * the two is not supported.
7219 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007220static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007222{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007224
Gilles Peskine449bd832023-01-11 14:50:10 +01007225 if (key_type_is_raw_bytes(type)) {
7226 status = psa_validate_unstructured_key_bit_size(type, bits);
7227 if (status != PSA_SUCCESS) {
7228 return status;
7229 }
7230 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007231#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7233 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7234 return PSA_ERROR_NOT_SUPPORTED;
7235 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007236
Ronald Cron01b2aba2020-10-05 09:42:02 +02007237 /* Accept only byte-aligned keys, for the same reasons as
7238 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 if (bits % 8 != 0) {
7240 return PSA_ERROR_NOT_SUPPORTED;
7241 }
7242 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007243#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007244
Ronald Cron5c4d3862020-12-07 11:07:24 +01007245#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007247 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 return PSA_SUCCESS;
7249 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01007250#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007251
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007252#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007253 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007254 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007255 return PSA_ERROR_NOT_SUPPORTED;
7256 }
7257 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007258#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007259 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007261 }
7262
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007264}
7265
Ronald Cron55ed0592020-10-05 10:30:40 +02007266psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007267 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007269{
7270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007271 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007272
Gilles Peskine449bd832023-01-11 14:50:10 +01007273 if ((attributes->domain_parameters == NULL) &&
7274 (attributes->domain_parameters_size != 0)) {
7275 return PSA_ERROR_INVALID_ARGUMENT;
7276 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007277
Gilles Peskine449bd832023-01-11 14:50:10 +01007278 if (key_type_is_raw_bytes(type)) {
7279 status = psa_generate_random(key_buffer, key_buffer_size);
7280 if (status != PSA_SUCCESS) {
7281 return status;
7282 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007283
David Brown12ca5032021-02-11 11:02:00 -07007284#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007285 if (type == PSA_KEY_TYPE_DES) {
7286 psa_des_set_key_parity(key_buffer, key_buffer_size);
7287 }
David Brown8107e312021-02-12 08:08:46 -07007288#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007290
Jaeden Amero424fa932021-05-14 08:34:32 +01007291#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7292 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7294 return mbedtls_psa_rsa_generate_key(attributes,
7295 key_buffer,
7296 key_buffer_size,
7297 key_buffer_length);
7298 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007299#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7300 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007301
John Durkop9814fa22020-11-04 12:28:15 -08007302#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007303 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7304 return mbedtls_psa_ecp_generate_key(attributes,
7305 key_buffer,
7306 key_buffer_size,
7307 key_buffer_length);
7308 } else
John Durkop9814fa22020-11-04 12:28:15 -08007309#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007310
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007311#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007312 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7313 return mbedtls_psa_ffdh_generate_key(attributes,
7314 key_buffer,
7315 key_buffer_size,
7316 key_buffer_length);
7317 } else
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02007318#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007319 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 (void) key_buffer_length;
7321 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007322 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007323
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007325}
Darryl Green0c6575a2018-11-07 16:05:30 +00007326
Gilles Peskine449bd832023-01-11 14:50:10 +01007327psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7328 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007329{
7330 psa_status_t status;
7331 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007332 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007333 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007334
Ronald Cron81709fc2020-11-14 12:10:32 +01007335 *key = MBEDTLS_SVC_KEY_ID_INIT;
7336
Gilles Peskine0f84d622019-09-12 19:03:13 +02007337 /* Reject any attempt to create a zero-length key so that we don't
7338 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 if (psa_get_key_bits(attributes) == 0) {
7340 return PSA_ERROR_INVALID_ARGUMENT;
7341 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007342
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007343 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7345 return PSA_ERROR_INVALID_ARGUMENT;
7346 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007347
Gilles Peskine449bd832023-01-11 14:50:10 +01007348 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7349 &slot, &driver);
7350 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007351 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 }
Gilles Peskine11792082019-08-06 18:36:36 +02007353
Ronald Cron977c2472020-10-13 08:32:21 +02007354 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307355 * storage ( thus not in the case of generating a key in a secure element
7356 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7357 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007358 if (slot->key.data == NULL) {
7359 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7360 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007361 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 attributes->core.type, attributes->core.bits);
7363 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007364 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007366
7367 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007368 attributes->core.type,
7369 attributes->core.bits);
7370 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007371 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007372 attributes, &key_buffer_size);
7373 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007374 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 }
Ronald Cron977c2472020-10-13 08:32:21 +02007376 }
Ronald Cron977c2472020-10-13 08:32:21 +02007377
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7379 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007380 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007381 }
Ronald Cron977c2472020-10-13 08:32:21 +02007382 }
7383
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 status = psa_driver_wrapper_generate_key(attributes,
7385 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007386
Gilles Peskine449bd832023-01-11 14:50:10 +01007387 if (status != PSA_SUCCESS) {
7388 psa_remove_key_data_from_memory(slot);
7389 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007390
Gilles Peskine11792082019-08-06 18:36:36 +02007391exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 if (status == PSA_SUCCESS) {
7393 status = psa_finish_key_creation(slot, driver, key);
7394 }
7395 if (status != PSA_SUCCESS) {
7396 psa_fail_key_creation(slot, driver);
7397 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007398
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007400}
7401
Gilles Peskinee59236f2018-01-27 23:32:46 +01007402/****************************************************************/
7403/* Module setup */
7404/****************************************************************/
7405
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007406#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007407psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 void (* entropy_init)(mbedtls_entropy_context *ctx),
7409 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007410{
Gilles Peskine449bd832023-01-11 14:50:10 +01007411 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7412 return PSA_ERROR_BAD_STATE;
7413 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007414 global_data.rng.entropy_init = entropy_init;
7415 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007417}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007418#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007419
Gilles Peskine449bd832023-01-11 14:50:10 +01007420void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007421{
Gilles Peskine449bd832023-01-11 14:50:10 +01007422 psa_wipe_all_key_slots();
7423 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7424 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007425 }
7426 /* Wipe all remaining data, including configuration.
7427 * In particular, this sets all state indicator to the value
7428 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007430
7431 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007433}
7434
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007435#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7436/** Recover a transaction that was interrupted by a power failure.
7437 *
7438 * This function is called during initialization, before psa_crypto_init()
7439 * returns. If this function returns a failure status, the initialization
7440 * fails.
7441 */
7442static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007444{
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007446 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7447 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 /* TODO - fall through to the failure case until this
7449 * is implemented.
7450 * https://github.com/ARMmbed/mbed-crypto/issues/218
7451 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007452 default:
7453 /* We found an unsupported transaction in the storage.
7454 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007456 }
7457}
7458#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7459
Gilles Peskine449bd832023-01-11 14:50:10 +01007460psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007461{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007462 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007463
Gilles Peskinec6b69072018-11-20 21:42:52 +01007464 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 if (global_data.initialized != 0) {
7466 return PSA_SUCCESS;
7467 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007468
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007469 /* Init drivers */
7470 status = psa_driver_wrapper_init();
7471 if (status != PSA_SUCCESS) {
7472 goto exit;
7473 }
7474 global_data.drivers_initialized = 1;
7475
Gilles Peskine30524eb2020-11-13 17:02:26 +01007476 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007478 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 status = mbedtls_psa_random_seed(&global_data.rng);
7480 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007481 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007483 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007484
Gilles Peskine449bd832023-01-11 14:50:10 +01007485 status = psa_initialize_key_slots();
7486 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007487 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007489
Gilles Peskine4b734222019-07-24 15:56:31 +02007490#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 status = psa_crypto_load_transaction();
7492 if (status == PSA_SUCCESS) {
7493 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7494 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007495 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 }
7497 status = psa_crypto_stop_transaction();
7498 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007499 /* There's no transaction to complete. It's all good. */
7500 status = PSA_SUCCESS;
7501 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007502#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007503
Gilles Peskinec6b69072018-11-20 21:42:52 +01007504 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007505 global_data.initialized = 1;
7506
7507exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 if (status != PSA_SUCCESS) {
7509 mbedtls_psa_crypto_free();
7510 }
7511 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007512}
7513
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007514psa_status_t psa_crypto_driver_pake_get_password_len(
7515 const psa_crypto_driver_pake_inputs_t *inputs,
7516 size_t *password_len)
7517{
7518 if (inputs->password_len == 0) {
7519 return PSA_ERROR_BAD_STATE;
7520 }
7521
7522 *password_len = inputs->password_len;
7523
7524 return PSA_SUCCESS;
7525}
7526
7527psa_status_t psa_crypto_driver_pake_get_password(
7528 const psa_crypto_driver_pake_inputs_t *inputs,
7529 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7530{
7531 if (inputs->password_len == 0) {
7532 return PSA_ERROR_BAD_STATE;
7533 }
7534
7535 if (buffer_size < inputs->password_len) {
7536 return PSA_ERROR_BUFFER_TOO_SMALL;
7537 }
7538
7539 memcpy(buffer, inputs->password, inputs->password_len);
7540 *buffer_length = inputs->password_len;
7541
7542 return PSA_SUCCESS;
7543}
7544
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007545psa_status_t psa_crypto_driver_pake_get_user_len(
7546 const psa_crypto_driver_pake_inputs_t *inputs,
7547 size_t *user_len)
7548{
7549 if (inputs->user_len == 0) {
7550 return PSA_ERROR_BAD_STATE;
7551 }
7552
7553 *user_len = inputs->user_len;
7554
7555 return PSA_SUCCESS;
7556}
7557
7558psa_status_t psa_crypto_driver_pake_get_user(
7559 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007560 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007561{
7562 if (inputs->user_len == 0) {
7563 return PSA_ERROR_BAD_STATE;
7564 }
7565
Przemek Stekielc0e62502023-03-14 11:49:36 +01007566 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007567 return PSA_ERROR_BUFFER_TOO_SMALL;
7568 }
7569
Przemek Stekielc0e62502023-03-14 11:49:36 +01007570 memcpy(user_id, inputs->user, inputs->user_len);
7571 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007572
7573 return PSA_SUCCESS;
7574}
7575
7576psa_status_t psa_crypto_driver_pake_get_peer_len(
7577 const psa_crypto_driver_pake_inputs_t *inputs,
7578 size_t *peer_len)
7579{
7580 if (inputs->peer_len == 0) {
7581 return PSA_ERROR_BAD_STATE;
7582 }
7583
7584 *peer_len = inputs->peer_len;
7585
7586 return PSA_SUCCESS;
7587}
7588
7589psa_status_t psa_crypto_driver_pake_get_peer(
7590 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007591 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007592{
7593 if (inputs->peer_len == 0) {
7594 return PSA_ERROR_BAD_STATE;
7595 }
7596
Przemek Stekielc0e62502023-03-14 11:49:36 +01007597 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007598 return PSA_ERROR_BUFFER_TOO_SMALL;
7599 }
7600
Przemek Stekielc0e62502023-03-14 11:49:36 +01007601 memcpy(peer_id, inputs->peer, inputs->peer_len);
7602 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007603
7604 return PSA_SUCCESS;
7605}
7606
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007607psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7608 const psa_crypto_driver_pake_inputs_t *inputs,
7609 psa_pake_cipher_suite_t *cipher_suite)
7610{
7611 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7612 return PSA_ERROR_BAD_STATE;
7613 }
7614
7615 *cipher_suite = inputs->cipher_suite;
7616
7617 return PSA_SUCCESS;
7618}
7619
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01007620#if defined(PSA_WANT_ALG_SOME_PAKE)
Neil Armstronga7d08c32022-06-01 18:21:20 +02007621psa_status_t psa_pake_setup(
7622 psa_pake_operation_t *operation,
7623 const psa_pake_cipher_suite_t *cipher_suite)
7624{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007625 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007626
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007627 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007628 status = PSA_ERROR_BAD_STATE;
7629 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007630 }
7631
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007632 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007633 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007634 status = PSA_ERROR_INVALID_ARGUMENT;
7635 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007636 }
7637
Przemek Stekiel51eac532022-12-07 11:04:51 +01007638 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7639
Przemek Stekiele12ed362022-12-21 12:54:46 +01007640 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007641 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7642 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007643 operation->data.inputs.cipher_suite = *cipher_suite;
7644
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007645#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007646 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007647 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007648 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007649
Przemek Stekiele12ed362022-12-21 12:54:46 +01007650 computation_stage->state = PSA_PAKE_STATE_SETUP;
7651 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7652 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7653 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007654 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007655#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007656 {
7657 status = PSA_ERROR_NOT_SUPPORTED;
7658 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007659 }
7660
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007661 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7662
Przemek Stekiel51eac532022-12-07 11:04:51 +01007663 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007664exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007665 psa_pake_abort(operation);
7666 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007667}
7668
7669psa_status_t psa_pake_set_password_key(
7670 psa_pake_operation_t *operation,
7671 mbedtls_svc_key_id_t password)
7672{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007673 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007674 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7675 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007676
Przemek Stekiel51eac532022-12-07 11:04:51 +01007677 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007678 status = PSA_ERROR_BAD_STATE;
7679 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007680 }
7681
Przemek Stekiel6c764412022-11-22 14:05:12 +01007682 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7683 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007684 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007685 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007686 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007687 }
7688
Przemek Stekiel6c764412022-11-22 14:05:12 +01007689 psa_key_attributes_t attributes = {
7690 .core = slot->attr
7691 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007692
Przemek Stekiel51eac532022-12-07 11:04:51 +01007693 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007694
Przemek Stekiel51eac532022-12-07 11:04:51 +01007695 if (type != PSA_KEY_TYPE_PASSWORD &&
7696 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7697 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007698 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007699 }
7700
Przemek Stekiel51eac532022-12-07 11:04:51 +01007701 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7702 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007703 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007704 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007705 }
7706
7707 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7708 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007709 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007710exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007711 if (status != PSA_SUCCESS) {
7712 psa_pake_abort(operation);
7713 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007714 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007715 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007716}
7717
7718psa_status_t psa_pake_set_user(
7719 psa_pake_operation_t *operation,
7720 const uint8_t *user_id,
7721 size_t user_id_len)
7722{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007723 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007724
7725 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007726 status = PSA_ERROR_BAD_STATE;
7727 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007728 }
7729
Przemek Stekiel51eac532022-12-07 11:04:51 +01007730 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007731 status = PSA_ERROR_INVALID_ARGUMENT;
7732 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007733 }
7734
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007735 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007736 status = PSA_ERROR_BAD_STATE;
7737 goto exit;
7738 }
7739
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007740 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7741 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007742 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7743 goto exit;
7744 }
7745
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007746 memcpy(operation->data.inputs.user, user_id, user_id_len);
7747 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007748
7749 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007750exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007751 psa_pake_abort(operation);
7752 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007753}
7754
7755psa_status_t psa_pake_set_peer(
7756 psa_pake_operation_t *operation,
7757 const uint8_t *peer_id,
7758 size_t peer_id_len)
7759{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007760 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007761
7762 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007763 status = PSA_ERROR_BAD_STATE;
7764 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007765 }
7766
Przemek Stekiel51eac532022-12-07 11:04:51 +01007767 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007768 status = PSA_ERROR_INVALID_ARGUMENT;
7769 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007770 }
7771
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007772 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007773 status = PSA_ERROR_BAD_STATE;
7774 goto exit;
7775 }
7776
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007777 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7778 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007779 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7780 goto exit;
7781 }
7782
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007783 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7784 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007785
7786 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007787exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007788 psa_pake_abort(operation);
7789 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007790}
7791
7792psa_status_t psa_pake_set_role(
7793 psa_pake_operation_t *operation,
7794 psa_pake_role_t role)
7795{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007796 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007797
Przemek Stekiel51eac532022-12-07 11:04:51 +01007798 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007799 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007800 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007801 }
7802
Przemek Stekiel09104b82023-03-06 14:11:51 +01007803 switch (operation->alg) {
7804#if defined(PSA_WANT_ALG_JPAKE)
7805 case PSA_ALG_JPAKE:
7806 if (role == PSA_PAKE_ROLE_NONE) {
7807 return PSA_SUCCESS;
7808 }
7809 status = PSA_ERROR_INVALID_ARGUMENT;
7810 break;
7811#endif
7812 default:
7813 (void) role;
7814 status = PSA_ERROR_NOT_SUPPORTED;
7815 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007816 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007817exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007818 psa_pake_abort(operation);
7819 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007820}
7821
Przemek Stekielb09c4872023-01-17 12:05:38 +01007822/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007823#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007824static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007825 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007826{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007827 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007828 case PSA_PAKE_OUTPUT_X1_X2:
7829 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007830 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007831 case PSA_PAKE_X1_STEP_KEY_SHARE:
7832 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007833 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7834 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007835 case PSA_PAKE_X1_STEP_ZK_PROOF:
7836 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007837 case PSA_PAKE_X2_STEP_KEY_SHARE:
7838 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007839 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7840 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007841 case PSA_PAKE_X2_STEP_ZK_PROOF:
7842 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007843 default:
7844 return PSA_JPAKE_STEP_INVALID;
7845 }
7846 break;
7847 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007848 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007849 case PSA_PAKE_X1_STEP_KEY_SHARE:
7850 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007851 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7852 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007853 case PSA_PAKE_X1_STEP_ZK_PROOF:
7854 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007855 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007856 return PSA_JPAKE_STEP_INVALID;
7857 }
7858 break;
7859 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007860 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007861 case PSA_PAKE_X1_STEP_KEY_SHARE:
7862 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007863 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7864 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007865 case PSA_PAKE_X1_STEP_ZK_PROOF:
7866 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007867 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007868 return PSA_JPAKE_STEP_INVALID;
7869 }
7870 break;
7871 default:
7872 return PSA_JPAKE_STEP_INVALID;
7873 }
7874 return PSA_JPAKE_STEP_INVALID;
7875}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007876#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007877
Przemek Stekiel2797d372022-12-22 11:19:22 +01007878static psa_status_t psa_pake_complete_inputs(
7879 psa_pake_operation_t *operation)
7880{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007882 /* Create copy of the inputs on stack as inputs share memory
7883 with the driver context which will be setup by the driver. */
7884 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007885
Przemek Stekielfde11282023-03-13 16:06:09 +01007886 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007887 return PSA_ERROR_BAD_STATE;
7888 }
7889
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007890 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007891 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7892 return PSA_ERROR_BAD_STATE;
7893 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007894 }
7895
Przemek Stekiel18620a32023-01-17 16:34:52 +01007896 /* Clear driver context */
7897 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7898
7899 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007900
7901 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007902 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7903 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007904
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007905 /* User and peer are translated to role. */
7906 mbedtls_free(inputs.user);
7907 mbedtls_free(inputs.peer);
7908
Przemek Stekiel2797d372022-12-22 11:19:22 +01007909 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007910#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007911 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007912 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007913 psa_jpake_computation_stage_t *computation_stage =
7914 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007915 computation_stage->state = PSA_PAKE_STATE_READY;
7916 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7917 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7918 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007919 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007920#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007921 {
7922 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007923 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007924 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007925 return status;
7926}
7927
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007928#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007929static psa_status_t psa_jpake_output_prologue(
7930 psa_pake_operation_t *operation,
7931 psa_pake_step_t step)
7932{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007933 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7934 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7935 step != PSA_PAKE_STEP_ZK_PROOF) {
7936 return PSA_ERROR_INVALID_ARGUMENT;
7937 }
7938
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007939 psa_jpake_computation_stage_t *computation_stage =
7940 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007941
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007942 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7943 return PSA_ERROR_BAD_STATE;
7944 }
7945
7946 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7947 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7948 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7949 return PSA_ERROR_BAD_STATE;
7950 }
7951
7952 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7953 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007954 return PSA_ERROR_BAD_STATE;
7955 }
7956
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007957 switch (computation_stage->output_step) {
7958 case PSA_PAKE_STEP_X1_X2:
7959 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007960 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007961 case PSA_PAKE_STEP_X2S:
7962 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007963 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007964 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007965 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007966 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007967
7968 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7969 }
7970
7971 /* Check if step matches current sequence */
7972 switch (computation_stage->sequence) {
7973 case PSA_PAKE_X1_STEP_KEY_SHARE:
7974 case PSA_PAKE_X2_STEP_KEY_SHARE:
7975 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7976 return PSA_ERROR_BAD_STATE;
7977 }
7978 break;
7979
7980 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7981 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7982 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7983 return PSA_ERROR_BAD_STATE;
7984 }
7985 break;
7986
7987 case PSA_PAKE_X1_STEP_ZK_PROOF:
7988 case PSA_PAKE_X2_STEP_ZK_PROOF:
7989 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7990 return PSA_ERROR_BAD_STATE;
7991 }
7992 break;
7993
7994 default:
7995 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007996 }
7997
7998 return PSA_SUCCESS;
7999}
8000
8001static psa_status_t psa_jpake_output_epilogue(
8002 psa_pake_operation_t *operation)
8003{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008004 psa_jpake_computation_stage_t *computation_stage =
8005 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008006
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008007 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008008 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008009 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008010 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008011 computation_stage->state = PSA_PAKE_STATE_READY;
8012 computation_stage->output_step++;
8013 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8014 } else {
8015 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008016 }
8017
8018 return PSA_SUCCESS;
8019}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008020#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008021
Neil Armstronga7d08c32022-06-01 18:21:20 +02008022psa_status_t psa_pake_output(
8023 psa_pake_operation_t *operation,
8024 psa_pake_step_t step,
8025 uint8_t *output,
8026 size_t output_size,
8027 size_t *output_length)
8028{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008029 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008030 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008031 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008032
8033 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008034 status = psa_pake_complete_inputs(operation);
8035 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008036 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008037 }
8038 }
8039
8040 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008041 status = PSA_ERROR_BAD_STATE;
8042 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008043 }
8044
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008045 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008046 status = PSA_ERROR_INVALID_ARGUMENT;
8047 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008048 }
8049
Przemek Stekiele12ed362022-12-21 12:54:46 +01008050 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008051#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008052 case PSA_ALG_JPAKE:
8053 status = psa_jpake_output_prologue(operation, step);
8054 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008055 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008056 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008057 driver_step = convert_jpake_computation_stage_to_driver_step(
8058 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008059 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008060#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008061 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008062 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008063 status = PSA_ERROR_NOT_SUPPORTED;
8064 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008065 }
8066
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008067 status = psa_driver_wrapper_pake_output(operation, driver_step,
8068 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008069
8070 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008071 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008072 }
8073
8074 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008075#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008076 case PSA_ALG_JPAKE:
8077 status = psa_jpake_output_epilogue(operation);
8078 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008079 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008080 }
8081 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008082#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008083 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008084 status = PSA_ERROR_NOT_SUPPORTED;
8085 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008086 }
8087
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008088 return PSA_SUCCESS;
8089exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008090 psa_pake_abort(operation);
8091 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008092}
8093
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008094#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008095static psa_status_t psa_jpake_input_prologue(
8096 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008097 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008098{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008099 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8100 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8101 step != PSA_PAKE_STEP_ZK_PROOF) {
8102 return PSA_ERROR_INVALID_ARGUMENT;
8103 }
8104
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008105 psa_jpake_computation_stage_t *computation_stage =
8106 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008107
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008108 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
8109 return PSA_ERROR_BAD_STATE;
8110 }
8111
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008112 if (computation_stage->state != PSA_PAKE_STATE_READY &&
8113 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
8114 computation_stage->state != PSA_PAKE_INPUT_X4S) {
8115 return PSA_ERROR_BAD_STATE;
8116 }
8117
8118 if (computation_stage->state == PSA_PAKE_STATE_READY) {
8119 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008120 return PSA_ERROR_BAD_STATE;
8121 }
8122
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008123 switch (computation_stage->input_step) {
8124 case PSA_PAKE_STEP_X1_X2:
8125 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008126 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008127 case PSA_PAKE_STEP_X2S:
8128 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008129 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008130 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01008131 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008132 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008133
8134 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
8135 }
8136
8137 /* Check if step matches current sequence */
8138 switch (computation_stage->sequence) {
8139 case PSA_PAKE_X1_STEP_KEY_SHARE:
8140 case PSA_PAKE_X2_STEP_KEY_SHARE:
8141 if (step != PSA_PAKE_STEP_KEY_SHARE) {
8142 return PSA_ERROR_BAD_STATE;
8143 }
8144 break;
8145
8146 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
8147 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
8148 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
8149 return PSA_ERROR_BAD_STATE;
8150 }
8151 break;
8152
8153 case PSA_PAKE_X1_STEP_ZK_PROOF:
8154 case PSA_PAKE_X2_STEP_ZK_PROOF:
8155 if (step != PSA_PAKE_STEP_ZK_PROOF) {
8156 return PSA_ERROR_BAD_STATE;
8157 }
8158 break;
8159
8160 default:
8161 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008162 }
8163
8164 return PSA_SUCCESS;
8165}
8166
Przemek Stekiele12ed362022-12-21 12:54:46 +01008167static psa_status_t psa_jpake_input_epilogue(
8168 psa_pake_operation_t *operation)
8169{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008170 psa_jpake_computation_stage_t *computation_stage =
8171 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008172
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008173 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008174 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008175 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01008176 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008177 computation_stage->state = PSA_PAKE_STATE_READY;
8178 computation_stage->input_step++;
8179 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
8180 } else {
8181 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008182 }
8183
8184 return PSA_SUCCESS;
8185}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008186#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008187
Neil Armstronga7d08c32022-06-01 18:21:20 +02008188psa_status_t psa_pake_input(
8189 psa_pake_operation_t *operation,
8190 psa_pake_step_t step,
8191 const uint8_t *input,
8192 size_t input_length)
8193{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008194 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008195 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008196 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8197 operation->primitive,
8198 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008199
8200 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008201 status = psa_pake_complete_inputs(operation);
8202 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008203 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008204 }
8205 }
8206
8207 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008208 status = PSA_ERROR_BAD_STATE;
8209 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008210 }
8211
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008212 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008213 status = PSA_ERROR_INVALID_ARGUMENT;
8214 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008215 }
8216
Przemek Stekiele12ed362022-12-21 12:54:46 +01008217 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008218#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008219 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008220 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008221 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008222 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008223 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008224 driver_step = convert_jpake_computation_stage_to_driver_step(
8225 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008226 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008227#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008228 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008229 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008230 status = PSA_ERROR_NOT_SUPPORTED;
8231 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008232 }
8233
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008234 status = psa_driver_wrapper_pake_input(operation, driver_step,
8235 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008236
8237 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008238 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008239 }
8240
8241 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008242#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008243 case PSA_ALG_JPAKE:
8244 status = psa_jpake_input_epilogue(operation);
8245 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008246 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008247 }
8248 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008249#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008250 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008251 status = PSA_ERROR_NOT_SUPPORTED;
8252 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008253 }
8254
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008255 return PSA_SUCCESS;
8256exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008257 psa_pake_abort(operation);
8258 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008259}
8260
8261psa_status_t psa_pake_get_implicit_key(
8262 psa_pake_operation_t *operation,
8263 psa_key_derivation_operation_t *output)
8264{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008265 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008266 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008267 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008268 size_t shared_key_len = 0;
8269
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008270 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008271 status = PSA_ERROR_BAD_STATE;
8272 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008273 }
8274
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008275#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008277 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008278 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008279 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8280 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008281 status = PSA_ERROR_BAD_STATE;
8282 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008283 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008284 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008285#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008286 {
8287 status = PSA_ERROR_NOT_SUPPORTED;
8288 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008289 }
8290
Przemek Stekiel0c781802022-11-29 14:53:13 +01008291 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8292 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008293 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008294 &shared_key_len);
8295
8296 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008297 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008298 }
8299
8300 status = psa_key_derivation_input_bytes(output,
8301 PSA_KEY_DERIVATION_INPUT_SECRET,
8302 shared_key,
8303 shared_key_len);
8304
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008305 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008306exit:
8307 abort_status = psa_pake_abort(operation);
8308 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008309}
8310
8311psa_status_t psa_pake_abort(
8312 psa_pake_operation_t *operation)
8313{
Przemek Stekield69dca92023-01-31 19:59:20 +01008314 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008315
Przemek Stekield69dca92023-01-31 19:59:20 +01008316 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008317 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008318 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008319
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008320 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8321 if (operation->data.inputs.password != NULL) {
8322 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008323 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008324 mbedtls_free(operation->data.inputs.password);
8325 }
8326 if (operation->data.inputs.user != NULL) {
8327 mbedtls_free(operation->data.inputs.user);
8328 }
8329 if (operation->data.inputs.peer != NULL) {
8330 mbedtls_free(operation->data.inputs.peer);
8331 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008332 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008333 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008334
Przemek Stekield69dca92023-01-31 19:59:20 +01008335 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008336}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008337#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008338
Gilles Peskinee59236f2018-01-27 23:32:46 +01008339#endif /* MBEDTLS_PSA_CRYPTO_C */