blob: ba204f7ef2d36645ec881ec836f5afc28f8d52ad [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"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
Gilles Peskinee59236f2018-01-27 23:32:46 +010029#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010030#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031
Ronald Crond6d28882020-12-14 14:56:02 +010032#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020035#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010037#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010038#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010039#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010040#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020042#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010044#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010045/* Include internal declarations that are useful for implementing persistently
46 * stored keys. */
47#include "psa_crypto_storage.h"
48
Gilles Peskineb2b64d32020-12-14 16:43:58 +010049#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010050
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include <stdlib.h>
52#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010055#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000057#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020058#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020060#include "mbedtls/chacha20.h"
61#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/cipher.h"
63#include "mbedtls/ccm.h"
64#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020066#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010067#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010068#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/error.h"
70#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000073#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000075#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000077#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000083#include "hash_info.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084
Gilles Peskine449bd832023-01-11 14:50:10 +010085#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020086
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020090#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020091#endif
92
Przemek Stekielfde11282023-03-13 16:06:09 +010093/* The only two JPAKE user/peer identifiers supported for the time being. */
94static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
95static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
Przemek Stekiel18cd6c92023-03-06 15:40:35 +010096
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010097/****************************************************************/
98/* Global data, support functions and library management */
99/****************************************************************/
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102{
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104}
105
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100106/* Values for psa_global_data_t::rng_state */
107#define RNG_NOT_INITIALIZED 0
108#define RNG_INITIALIZED 1
109#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100113 unsigned rng_state : 2;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100114 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100115} psa_global_data_t;
116
117static psa_global_data_t global_data;
118
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100119#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
120mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
121 &global_data.rng.drbg;
122#endif
123
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (global_data.initialized == 0) \
126 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100130 /* Mbed TLS error codes can combine a high-level error code and a
131 * low-level error code. The low-level error usually reflects the
132 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 int low_level_ret = -(-ret & 0x007f);
134 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100137
138 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
139 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200141 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
142 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
143 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
144 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
145 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200147 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200151
Jaeden Amero93e21112019-02-20 13:57:28 +0000152#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000153 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000154#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100155 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100157
158 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100160 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100162
Gilles Peskine26869f22019-05-06 15:25:00 +0200163 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200165
166 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200168 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200170
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100179 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
187 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100188 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
189 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
193 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100197#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100198
199 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201
Gilles Peskinee59236f2018-01-27 23:32:46 +0100202 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
203 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
204 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100206
207 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200209 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213
Gilles Peskine82e57d12020-11-13 21:31:17 +0100214#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100216 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
217 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100218 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100220 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
221 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100223 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100225#endif
226
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100233 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100235
Gilles Peskinef76aa772018-10-29 19:24:33 +0100236 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100238 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100240 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100242 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100244 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100246 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100248 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100250 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100252
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100253 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100255 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
256 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100258 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100260 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
261 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100263 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
266 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_INVALID_ALG:
271 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
272 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200276 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100278
Gilles Peskineff2d2002019-05-06 15:26:23 +0200279 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200281 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200283
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100290 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
293 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200301
itayzafrir5c753392018-05-08 11:18:38 +0300302 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300303 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300305 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300307 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300309 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
310 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300312 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100314 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100316
Paul Elliott588f8ed2022-12-02 18:10:26 +0000317 case MBEDTLS_ERR_ECP_IN_PROGRESS:
318 return PSA_OPERATION_INCOMPLETE;
319
Janos Follatha13b9052019-11-22 12:48:59 +0000320 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300322
Gilles Peskinee59236f2018-01-27 23:32:46 +0100323 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100325 }
326}
327
Paul Elliottdc42ca82023-02-24 18:11:59 +0000328/**
329 * \brief For output buffers which contain "tags"
330 * (outputs that may be checked for validity like
331 * hashes, MACs and signatures), fill the unused
332 * part of the output buffer (the whole buffer on
333 * error, the trailing part on success) with
334 * something that isn't a valid tag (barring an
335 * attack on the tag and deliberately-crafted
336 * input), in case the caller doesn't check the
337 * return status properly.
338 *
339 * \param output_buffer Pointer to buffer to wipe. May not be NULL
340 * unless \p output_buffer_size is zero.
341 * \param status Status of function called to generate
342 * output_buffer originally
343 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
344 * could be NULL.
345 * \param output_buffer_length Length of data written to output_buffer, must be
346 * less than \p output_buffer_size
347 */
348static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000349 size_t output_buffer_size, size_t output_buffer_length)
350{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000351 size_t offset = 0;
352
353 if (output_buffer_size == 0) {
354 /* If output_buffer_size is 0 then we have nothing to do. We must not
355 call memset because output_buffer may be NULL in this case */
356 return;
357 }
358
359 if (status == PSA_SUCCESS) {
360 offset = output_buffer_length;
361 }
362
363 memset(output_buffer + offset, '!', output_buffer_size - offset);
364}
365
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200366
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200367
368
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100369/****************************************************************/
370/* Key management */
371/****************************************************************/
372
John Durkop9814fa22020-11-04 12:28:15 -0800373#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800374 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100375 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
376 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
377 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100378mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
379 size_t bits,
380 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200381{
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100383 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100385#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100386 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100388#endif
389#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100392#endif
393#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100396#endif
397#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100398 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100400#endif
401#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100402 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100404 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if (bits_is_sloppy) {
406 return MBEDTLS_ECP_DP_SECP521R1;
407 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100408 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100409#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100410 }
411 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100412
Paul Elliott8ff510a2020-06-02 17:19:28 +0100413 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100415#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100416 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100418#endif
419#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100422#endif
423#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100424 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100426#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100427 }
428 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100429
Paul Elliott8ff510a2020-06-02 17:19:28 +0100430 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100432#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (bits_is_sloppy) {
437 return MBEDTLS_ECP_DP_CURVE25519;
438 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100439 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100440#endif
441#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100442 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100444#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100445 }
446 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100447
Paul Elliott8ff510a2020-06-02 17:19:28 +0100448 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#endif
454#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#endif
458#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100461#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100462 }
463 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200464 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100466 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200468}
John Durkop9814fa22020-11-04 12:28:15 -0800469#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100470 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
471 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
472 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
473 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
476 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477{
478 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200480 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200482 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200483 case PSA_KEY_TYPE_PASSWORD:
484 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200485 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100486#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (bits != 128 && bits != 192 && bits != 256) {
489 return PSA_ERROR_INVALID_ARGUMENT;
490 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 break;
492#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200493#if defined(PSA_WANT_KEY_TYPE_ARIA)
494 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (bits != 128 && bits != 192 && bits != 256) {
496 return PSA_ERROR_INVALID_ARGUMENT;
497 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200498 break;
499#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100500#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (bits != 128 && bits != 192 && bits != 256) {
503 return PSA_ERROR_INVALID_ARGUMENT;
504 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505 break;
506#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100507#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (bits != 64 && bits != 128 && bits != 192) {
510 return PSA_ERROR_INVALID_ARGUMENT;
511 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200512 break;
513#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100514#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200515 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if (bits != 256) {
517 return PSA_ERROR_INVALID_ARGUMENT;
518 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200519 break;
520#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200521 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200523 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (bits % 8 != 0) {
525 return PSA_ERROR_INVALID_ARGUMENT;
526 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200529}
530
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100531/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100532 *
Steven Cooremancd640932021-03-08 12:00:27 +0100533 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
534 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100535 *
536 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100537 * \param[in] key_type The key type of the key to be used with the
538 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100539 *
540 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100541 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100542 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100543 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100544 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100545MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100546 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100548{
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (PSA_ALG_IS_HMAC(algorithm)) {
550 if (key_type == PSA_KEY_TYPE_HMAC) {
551 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100552 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100553 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
556 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
557 * key. */
558 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
559 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
560 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
561 * the block length (larger than 1) for block ciphers. */
562 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
563 return PSA_SUCCESS;
564 }
565 }
566 }
567
568 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100569}
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
572 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200573{
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (slot->key.data != NULL) {
575 return PSA_ERROR_ALREADY_EXISTS;
576 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 slot->key.data = mbedtls_calloc(1, buffer_length);
579 if (slot->key.data == NULL) {
580 return PSA_ERROR_INSUFFICIENT_MEMORY;
581 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200582
Ronald Cronea0f8a62020-11-25 17:52:23 +0100583 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200585}
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
588 const uint8_t *data,
589 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200590{
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 psa_status_t status = psa_allocate_buffer_to_slot(slot,
592 data_length);
593 if (status != PSA_SUCCESS) {
594 return status;
595 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 memcpy(slot->key.data, data, data_length);
598 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200599}
600
Ronald Crona0fe59f2020-11-29 11:14:53 +0100601psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100602 const psa_key_attributes_t *attributes,
603 const uint8_t *data, size_t data_length,
604 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100607 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
608 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200610 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (data_length == 0) {
612 return PSA_ERROR_NOT_SUPPORTED;
613 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (key_type_is_raw_bytes(type)) {
616 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
619 *bits);
620 if (status != PSA_SUCCESS) {
621 return status;
622 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200623
Ronald Crondd04d422020-11-28 15:14:42 +0100624 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100626 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 return PSA_SUCCESS;
630 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800631#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
633 if (PSA_KEY_TYPE_IS_ECC(type)) {
634 return mbedtls_psa_ecp_import_key(attributes,
635 data, data_length,
636 key_buffer, key_buffer_size,
637 key_buffer_length,
638 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100639 }
John Durkop9814fa22020-11-04 12:28:15 -0800640#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
641 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700642#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
644 if (PSA_KEY_TYPE_IS_RSA(type)) {
645 return mbedtls_psa_rsa_import_key(attributes,
646 data, data_length,
647 key_buffer, key_buffer_size,
648 key_buffer_length,
649 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200650 }
John Durkop9814fa22020-11-04 12:28:15 -0800651#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
652 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100653 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100656}
657
Gilles Peskinef603c712019-01-19 13:40:11 +0100658/** Calculate the intersection of two algorithm usage policies.
659 *
660 * Return 0 (which allows no operation) on incompatibility.
661 */
662static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100663 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100664 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100666{
Gilles Peskine549ea862019-05-22 11:45:59 +0200667 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (alg1 == alg2) {
669 return alg1;
670 }
Steven Cooreman03488022021-02-18 12:07:20 +0100671 /* If the policies are from the same hash-and-sign family, check
672 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
674 PSA_ALG_IS_SIGN_HASH(alg2) &&
675 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
676 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
677 return alg2;
678 }
679 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
680 return alg1;
681 }
Steven Cooreman03488022021-02-18 12:07:20 +0100682 }
683 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100684 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100685 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
687 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
688 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
689 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
690 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100691 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100692
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100693 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
695 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
696 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
697 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100698 }
699 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
701 (alg1_len <= alg2_len)) {
702 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100703 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
705 (alg2_len <= alg1_len)) {
706 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100707 }
708 }
709 /* If the policies are from the same MAC family, check whether one
710 * of them is a minimum-MAC-length policy. Calculate the most
711 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
713 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
714 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100715 /* Validate the combination of key type and algorithm. Since the base
716 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
718 return 0;
719 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100720
Steven Cooreman31a876d2021-03-03 20:47:40 +0100721 /* Get the (exact or at-least) output lengths for both sides of the
722 * requested intersection. None of the currently supported algorithms
723 * have an output length dependent on the actual key size, so setting it
724 * to a bogus value of 0 is currently OK.
725 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100726 * Note that for at-least-this-length wildcard algorithms, the output
727 * length is set to the shortest allowed length, which allows us to
728 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
730 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100731 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100732
Steven Cooremana1d83222021-02-25 10:20:29 +0100733 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
735 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
736 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100737 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100738
739 /* If only one is an at-least-this-length policy, the intersection would
740 * be the other (fixed-length) policy as long as said fixed length is
741 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
743 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100744 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
746 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100747 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100748
Steven Cooremancd640932021-03-08 12:00:27 +0100749 /* If none of them are wildcards, check whether they define the same tag
750 * length. This is still possible here when one is default-length and
751 * the other specific-length. Ensure to always return the
752 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (alg1_len == alg2_len) {
754 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
755 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100756 }
757 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100759}
760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761static int psa_key_algorithm_permits(psa_key_type_t key_type,
762 psa_algorithm_t policy_alg,
763 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200764{
Gilles Peskine549ea862019-05-22 11:45:59 +0200765 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if (requested_alg == policy_alg) {
767 return 1;
768 }
Steven Cooreman03488022021-02-18 12:07:20 +0100769 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
770 * and requested_alg is the same hash-and-sign family with any hash,
771 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
773 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
774 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
775 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100776 }
777 /* If policy_alg is a wildcard AEAD algorithm of the same base as
778 * the requested algorithm, check the requested tag length to be
779 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (PSA_ALG_IS_AEAD(policy_alg) &&
781 PSA_ALG_IS_AEAD(requested_alg) &&
782 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
783 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
784 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
785 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
786 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100787 }
Steven Cooremancd640932021-03-08 12:00:27 +0100788 /* If policy_alg is a MAC algorithm of the same base as the requested
789 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (PSA_ALG_IS_MAC(policy_alg) &&
791 PSA_ALG_IS_MAC(requested_alg) &&
792 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
793 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100794 /* Validate the combination of key type and algorithm. Since the policy
795 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
797 return 0;
798 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100799
Steven Cooreman31a876d2021-03-03 20:47:40 +0100800 /* Get both the requested output length for the algorithm which is to be
801 * verified, and the default output length for the base algorithm.
802 * Note that none of the currently supported algorithms have an output
803 * length dependent on actual key size, so setting it to a bogus value
804 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100805 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100807 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 key_type, 0,
809 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100810
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100811 /* If the policy is default-length, only allow an algorithm with
812 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
814 return requested_output_length == default_output_length;
815 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100816
817 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100818 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
820 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
821 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100822 }
823
Steven Cooremancd640932021-03-08 12:00:27 +0100824 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
825 * check for the requested MAC length to be equal to or longer than the
826 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
828 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
829 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100830 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200831 }
Steven Cooremance48e852020-10-05 16:02:45 +0200832 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200833 * a key derivation with that key agreement should also be allowed. This
834 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
836 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
837 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
838 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200839 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100840 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200842}
843
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100844/** Test whether a policy permits an algorithm.
845 *
846 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100847 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100848 * \note This function requires providing the key type for which the policy is
849 * being validated, since some algorithm policy definitions (e.g. MAC)
850 * have different properties depending on what kind of cipher it is
851 * combined with.
852 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100853 * \retval PSA_SUCCESS When \p alg is a specific algorithm
854 * allowed by the \p policy.
855 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
856 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
857 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100858 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
860 psa_key_type_t key_type,
861 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100862{
Steven Cooremand788fab2021-02-25 11:29:17 +0100863 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (alg == 0) {
865 return PSA_ERROR_INVALID_ARGUMENT;
866 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100867
Steven Cooreman7e39f052021-02-23 14:18:32 +0100868 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (PSA_ALG_IS_WILDCARD(alg)) {
870 return PSA_ERROR_INVALID_ARGUMENT;
871 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
874 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
875 return PSA_SUCCESS;
876 } else {
877 return PSA_ERROR_NOT_PERMITTED;
878 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100879}
880
Gilles Peskinef603c712019-01-19 13:40:11 +0100881/** Restrict a key policy based on a constraint.
882 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100883 * \note This function requires providing the key type for which the policy is
884 * being restricted, since some algorithm policy definitions (e.g. MAC)
885 * have different properties depending on what kind of cipher it is
886 * combined with.
887 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100888 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100889 * \param[in,out] policy The policy to restrict.
890 * \param[in] constraint The policy constraint to apply.
891 *
892 * \retval #PSA_SUCCESS
893 * \c *policy contains the intersection of the original value of
894 * \c *policy and \c *constraint.
895 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100896 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100897 * \c *policy is unchanged.
898 */
899static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100900 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100901 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100903{
904 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 psa_key_policy_algorithm_intersection(key_type, policy->alg,
906 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200907 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
909 constraint->alg2);
910 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
911 return PSA_ERROR_INVALID_ARGUMENT;
912 }
913 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
914 return PSA_ERROR_INVALID_ARGUMENT;
915 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100916 policy->usage &= constraint->usage;
917 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200918 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100920}
921
Przemek Stekiel061f6942022-11-30 10:51:35 +0100922/** Get the description of a key given its identifier and policy constraints
923 * and lock it.
924 *
925 * The key must have allow all the usage flags set in \p usage. If \p alg is
926 * nonzero, the key must allow operations with this algorithm. If \p alg is
927 * zero, the algorithm is not checked.
928 *
929 * In case of a persistent key, the function loads the description of the key
930 * into a key slot if not already done.
931 *
932 * On success, the returned key slot is locked. It is the responsibility of
933 * the caller to unlock the key slot when it does not access it anymore.
934 */
935static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100936 mbedtls_svc_key_id_t key,
937 psa_key_slot_t **p_slot,
938 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100940{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100942 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 status = psa_get_and_lock_key_slot(key, p_slot);
945 if (status != PSA_SUCCESS) {
946 return status;
947 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200948 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100949
950 /* Enforce that usage policy for the key slot contains all the flags
951 * required by the usage parameter. There is one exception: public
952 * keys can always be exported, so we treat public key objects as
953 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100955 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100959 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200960 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100961 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100962
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800963 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (alg != 0) {
965 status = psa_key_policy_permits(&slot->attr.policy,
966 slot->attr.type,
967 alg);
968 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100969 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100971 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200974
975error:
976 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100980}
Darryl Green940d72c2018-07-13 13:18:51 +0100981
Ronald Cron5c522922020-11-14 16:35:34 +0100982/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200983 *
984 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200985 * available, as opposed to a key in a secure element and/or to be used
986 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200987 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200988 * This is a temporary function that may be used instead of
989 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
990 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200991 *
Ronald Cron5c522922020-11-14 16:35:34 +0100992 * On success, the returned key slot is locked. It is the responsibility of the
993 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200994 */
Ronald Cron5c522922020-11-14 16:35:34 +0100995static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
996 mbedtls_svc_key_id_t key,
997 psa_key_slot_t **p_slot,
998 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001000{
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1002 usage, alg);
1003 if (status != PSA_SUCCESS) {
1004 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001005 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1008 psa_unlock_key_slot(*p_slot);
1009 *p_slot = NULL;
1010 return PSA_ERROR_NOT_SUPPORTED;
1011 }
1012
1013 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001017{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001018 /* Data pointer will always be either a valid pointer or NULL in an
1019 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if (slot->key.data != NULL) {
1021 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1022 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001025 slot->key.data = NULL;
1026 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001029}
1030
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001031/** Completely wipe a slot in memory, including its policy.
1032 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001033psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001034{
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 /*
1038 * As the return error code may not be handled in case of multiple errors,
1039 * do our best to report an unexpected lock counter. Assert with
1040 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1041 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1042 * function is called as part of the execution of a test suite, the
1043 * execution of the test suite is stopped in error if the assertion fails.
1044 */
1045 if (slot->lock_count != 1) {
1046 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001047 status = PSA_ERROR_CORRUPTION_DETECTED;
1048 }
1049
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001050 /* Multipart operations may still be using the key. This is safe
1051 * because all multipart operation objects are independent from
1052 * the key slot: if they need to access the key after the setup
1053 * phase, they have a copy of the key. Note that this means that
1054 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001055 /* At this point, key material and other type-specific content has
1056 * been wiped. Clear remaining metadata. We can call memset and not
1057 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 memset(slot, 0, sizeof(*slot));
1059 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001060}
1061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001063{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001064 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001065 psa_status_t status; /* status of the last operation */
1066 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001067#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1068 psa_se_drv_table_entry_t *driver;
1069#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (mbedtls_svc_key_id_is_null(key)) {
1072 return PSA_SUCCESS;
1073 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001074
Ronald Cronf2911112020-10-29 17:51:10 +01001075 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001076 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001077 * key, this will load the key description from persistent memory if not
1078 * done yet. We cannot avoid this loading as without it we don't know if
1079 * the key is operated by an SE or not and this information is needed by
1080 * the current implementation.
1081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 status = psa_get_and_lock_key_slot(key, &slot);
1083 if (status != PSA_SUCCESS) {
1084 return status;
1085 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001086
Ronald Cronf2911112020-10-29 17:51:10 +01001087 /*
1088 * If the key slot containing the key description is under access by the
1089 * library (apart from the present access), the key cannot be destroyed
1090 * yet. For the time being, just return in error. Eventually (to be
1091 * implemented), the key should be destroyed when all accesses have
1092 * stopped.
1093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if (slot->lock_count > 1) {
1095 psa_unlock_key_slot(slot);
1096 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001097 }
1098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001100 /* Refuse the destruction of a read-only key (which may or may not work
1101 * if we attempt it, depending on whether the key is merely read-only
1102 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001103 * Just do the best we can, which is to wipe the copy in memory
1104 * (done in this function's cleanup code). */
1105 overall_status = PSA_ERROR_NOT_PERMITTED;
1106 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001107 }
1108
Gilles Peskine354f7672019-07-12 23:46:38 +02001109#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1111 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001112 /* For a key in a secure element, we need to do three things:
1113 * remove the key file in internal storage, destroy the
1114 * key inside the secure element, and update the driver's
1115 * persistent data. Start a transaction that will encompass these
1116 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001118 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001120 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 status = psa_crypto_save_transaction();
1122 if (status != PSA_SUCCESS) {
1123 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001124 /* We should still try to destroy the key in the secure
1125 * element and the key metadata in storage. This is especially
1126 * important if the error is that the storage is full.
1127 * But how to do it exactly without risking an inconsistent
1128 * state after a reset?
1129 * https://github.com/ARMmbed/mbed-crypto/issues/215
1130 */
1131 overall_status = status;
1132 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001133 }
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 status = psa_destroy_se_key(driver,
1136 psa_key_slot_get_slot_number(slot));
1137 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001138 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001140 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001141#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1142
Darryl Greend49a4992018-06-18 17:27:26 +01001143#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1145 status = psa_destroy_persistent_key(slot->attr.id);
1146 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001147 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001149
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001150 /* TODO: other slots may have a copy of the same key. We should
1151 * invalidate them.
1152 * https://github.com/ARMmbed/mbed-crypto/issues/214
1153 */
Darryl Greend49a4992018-06-18 17:27:26 +01001154 }
1155#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001156
Gilles Peskinefc762652019-07-22 19:30:34 +02001157#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (driver != NULL) {
1159 status = psa_save_se_persistent_data(driver);
1160 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001161 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 }
1163 status = psa_crypto_stop_transaction();
1164 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001165 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001167 }
1168#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1169
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001170exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001172 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001174 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 }
1176 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001177}
1178
John Durkop07cc04a2020-11-16 22:08:34 -08001179#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001180 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001181static psa_status_t psa_get_rsa_public_exponent(
1182 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001184{
1185 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001187 uint8_t *buffer = NULL;
1188 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1192 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001193 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 }
1195 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001196 /* It's the default value, which is reported as an empty string,
1197 * so there's nothing to do. */
1198 goto exit;
1199 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 buflen = mbedtls_mpi_size(&mpi);
1202 buffer = mbedtls_calloc(1, buflen);
1203 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001204 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1205 goto exit;
1206 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1208 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001209 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001211 attributes->domain_parameters = buffer;
1212 attributes->domain_parameters_size = buflen;
1213
1214exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 mbedtls_mpi_free(&mpi);
1216 if (ret != 0) {
1217 mbedtls_free(buffer);
1218 }
1219 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001220}
John Durkop07cc04a2020-11-16 22:08:34 -08001221#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001222 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001223
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001224/** Retrieve all the publicly-accessible attributes of a key.
1225 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001226psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1227 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001228{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001230 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001231 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1236 if (status != PSA_SUCCESS) {
1237 return status;
1238 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001239
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001240 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1242 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001243
1244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1246 psa_set_key_slot_number(attributes,
1247 psa_key_slot_get_slot_number(slot));
1248 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001249#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001252#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001253 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001254 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001255 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001256 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001257 * is not yet implemented.
1258 * https://github.com/ARMmbed/mbed-crypto/issues/216
1259 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001261 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001262
Ronald Cron90857082020-11-25 14:58:33 +01001263 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 slot->attr.type,
1265 slot->key.data,
1266 slot->key.bytes,
1267 &rsa);
1268 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001269 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 status = psa_get_rsa_public_exponent(rsa,
1273 attributes);
1274 mbedtls_rsa_free(rsa);
1275 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001276 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001277 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001278#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001279 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001280 default:
1281 /* Nothing else to do. */
1282 break;
1283 }
1284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (status != PSA_SUCCESS) {
1286 psa_reset_key_attributes(attributes);
1287 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001292}
1293
Gilles Peskinec8000c02019-08-02 20:15:51 +02001294#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1295psa_status_t psa_get_key_slot_number(
1296 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001298{
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001300 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 return PSA_SUCCESS;
1302 } else {
1303 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001304 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001305}
1306#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1309 size_t key_buffer_size,
1310 uint8_t *data,
1311 size_t data_size,
1312 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001313{
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (key_buffer_size > data_size) {
1315 return PSA_ERROR_BUFFER_TOO_SMALL;
1316 }
1317 memcpy(data, key_buffer, key_buffer_size);
1318 memset(data + key_buffer_size, 0,
1319 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001320 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001322}
1323
Ronald Cron7285cda2020-11-26 14:46:37 +01001324psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001325 const psa_key_attributes_t *attributes,
1326 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001328{
Ronald Crond18b5f82020-11-25 16:46:05 +01001329 psa_key_type_t type = attributes->core.type;
1330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (key_type_is_raw_bytes(type) ||
1332 PSA_KEY_TYPE_IS_RSA(type) ||
1333 PSA_KEY_TYPE_IS_ECC(type)) {
1334 return psa_export_key_buffer_internal(
1335 key_buffer, key_buffer_size,
1336 data, data_size, data_length);
1337 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001338 /* This shouldn't happen in the reference implementation, but
1339 it is valid for a special-purpose implementation to omit
1340 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001342 }
1343}
1344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1346 uint8_t *data,
1347 size_t data_size,
1348 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001349{
1350 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1351 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1352 psa_key_slot_t *slot;
1353
Ronald Crone907e552021-01-18 13:22:38 +01001354 /* Reject a zero-length output buffer now, since this can never be a
1355 * valid key representation. This way we know that data must be a valid
1356 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if (data_size == 0) {
1358 return PSA_ERROR_BUFFER_TOO_SMALL;
1359 }
Ronald Crone907e552021-01-18 13:22:38 +01001360
Ronald Cron9486f9d2020-11-26 13:36:23 +01001361 /* Set the key to empty now, so that even when there are errors, we always
1362 * set data_length to a value between 0 and data_size. On error, setting
1363 * the key to empty is a good choice because an empty key representation is
1364 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001365 *data_length = 0;
1366
Ronald Cron9486f9d2020-11-26 13:36:23 +01001367 /* Export requires the EXPORT flag. There is an exception for public keys,
1368 * which don't require any flag, but
1369 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1372 PSA_KEY_USAGE_EXPORT, 0);
1373 if (status != PSA_SUCCESS) {
1374 return status;
1375 }
mohammad160306e79202018-03-28 13:17:44 +03001376
Ronald Crond18b5f82020-11-25 16:46:05 +01001377 psa_key_attributes_t attributes = {
1378 .core = slot->attr
1379 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 status = psa_driver_wrapper_export_key(&attributes,
1381 slot->key.data, slot->key.bytes,
1382 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001387}
1388
Ronald Cron7285cda2020-11-26 14:46:37 +01001389psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001390 const psa_key_attributes_t *attributes,
1391 const uint8_t *key_buffer,
1392 size_t key_buffer_size,
1393 uint8_t *data,
1394 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001396{
Ronald Crond18b5f82020-11-25 16:46:05 +01001397 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1400 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001401 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 return psa_export_key_buffer_internal(
1403 key_buffer, key_buffer_size,
1404 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001405 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001408#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1410 return mbedtls_psa_rsa_export_public_key(attributes,
1411 key_buffer,
1412 key_buffer_size,
1413 data,
1414 data_size,
1415 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001416#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001417 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001419#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1420 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001422#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1424 return mbedtls_psa_ecp_export_public_key(attributes,
1425 key_buffer,
1426 key_buffer_size,
1427 data,
1428 data_size,
1429 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001430#else
1431 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001433#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1434 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001435 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001437 /* This shouldn't happen in the reference implementation, but
1438 it is valid for a special-purpose implementation to omit
1439 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001441 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001442}
Ronald Crond18b5f82020-11-25 16:46:05 +01001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1445 uint8_t *data,
1446 size_t data_size,
1447 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001448{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001450 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001451 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001452
Ronald Crone907e552021-01-18 13:22:38 +01001453 /* Reject a zero-length output buffer now, since this can never be a
1454 * valid key representation. This way we know that data must be a valid
1455 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (data_size == 0) {
1457 return PSA_ERROR_BUFFER_TOO_SMALL;
1458 }
Ronald Crone907e552021-01-18 13:22:38 +01001459
Darryl Greendd8fb772018-11-07 16:00:44 +00001460 /* Set the key to empty now, so that even when there are errors, we always
1461 * set data_length to a value between 0 and data_size. On error, setting
1462 * the key to empty is a good choice because an empty key representation is
1463 * unlikely to be accepted anywhere. */
1464 *data_length = 0;
1465
1466 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1468 if (status != PSA_SUCCESS) {
1469 return status;
1470 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1473 status = PSA_ERROR_INVALID_ARGUMENT;
1474 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001475 }
1476
Ronald Crond18b5f82020-11-25 16:46:05 +01001477 psa_key_attributes_t attributes = {
1478 .core = slot->attr
1479 };
Ronald Cron67227982020-11-26 15:16:05 +01001480 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001481 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001483
1484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001488}
1489
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001490MBEDTLS_STATIC_ASSERT(
1491 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1492 "One or more key attribute flag is listed as both external-only and dual-use")
1493MBEDTLS_STATIC_ASSERT(
1494 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1495 "One or more key attribute flag is listed as both internal-only and dual-use")
1496MBEDTLS_STATIC_ASSERT(
1497 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1498 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001499
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500/** Validate that a key policy is internally well-formed.
1501 *
1502 * This function only rejects invalid policies. It does not validate the
1503 * consistency of the policy with respect to other attributes of the key
1504 * such as the key type.
1505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001507{
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1509 PSA_KEY_USAGE_COPY |
1510 PSA_KEY_USAGE_ENCRYPT |
1511 PSA_KEY_USAGE_DECRYPT |
1512 PSA_KEY_USAGE_SIGN_MESSAGE |
1513 PSA_KEY_USAGE_VERIFY_MESSAGE |
1514 PSA_KEY_USAGE_SIGN_HASH |
1515 PSA_KEY_USAGE_VERIFY_HASH |
1516 PSA_KEY_USAGE_VERIFY_DERIVATION |
1517 PSA_KEY_USAGE_DERIVE)) != 0) {
1518 return PSA_ERROR_INVALID_ARGUMENT;
1519 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001522}
1523
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001524/** Validate the internal consistency of key attributes.
1525 *
1526 * This function only rejects invalid attribute values. If does not
1527 * validate the consistency of the attributes with any key data that may
1528 * be involved in the creation of the key.
1529 *
1530 * Call this function early in the key creation process.
1531 *
1532 * \param[in] attributes Key attributes for the new key.
1533 * \param[out] p_drv On any return, the driver for the key, if any.
1534 * NULL for a transparent key.
1535 *
1536 */
1537static psa_status_t psa_validate_key_attributes(
1538 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001540{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001541 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1543 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 status = psa_validate_key_location(lifetime, p_drv);
1546 if (status != PSA_SUCCESS) {
1547 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001548 }
1549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 status = psa_validate_key_persistence(lifetime);
1551 if (status != PSA_SUCCESS) {
1552 return status;
1553 }
1554
1555 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1556 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1557 return PSA_ERROR_INVALID_ARGUMENT;
1558 }
1559 } else {
1560 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1561 return PSA_ERROR_INVALID_ARGUMENT;
1562 }
1563 }
1564
1565 status = psa_validate_key_policy(&attributes->core.policy);
1566 if (status != PSA_SUCCESS) {
1567 return status;
1568 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001569
1570 /* Refuse to create overly large keys.
1571 * Note that this doesn't trigger on import if the attributes don't
1572 * explicitly specify a size (so psa_get_key_bits returns 0), so
1573 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1575 return PSA_ERROR_NOT_SUPPORTED;
1576 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001577
Gilles Peskine91e8c332019-08-02 19:19:39 +02001578 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1580 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1581 return PSA_ERROR_INVALID_ARGUMENT;
1582 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001585}
1586
Gilles Peskine4747d192019-04-17 15:05:45 +02001587/** Prepare a key slot to receive key material.
1588 *
1589 * This function allocates a key slot and sets its metadata.
1590 *
1591 * If this function fails, call psa_fail_key_creation().
1592 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001593 * This function is intended to be used as follows:
1594 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001595 * it with the specified attributes, and in case of a volatile key assign it
1596 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001597 * -# Populate the slot with the key material.
1598 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1599 * In case of failure at any step, stop the sequence and call
1600 * psa_fail_key_creation().
1601 *
Ronald Cron5c522922020-11-14 16:35:34 +01001602 * On success, the key slot is locked. It is the responsibility of the caller
1603 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001604 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001605 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001606 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001607 * \param[out] p_slot On success, a pointer to the prepared slot.
1608 * \param[out] p_drv On any return, the driver for the key, if any.
1609 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001610 *
1611 * \retval #PSA_SUCCESS
1612 * The key slot is ready to receive key material.
1613 * \return If this function fails, the key slot is an invalid state.
1614 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001615 */
1616static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001617 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001618 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001619 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001621{
1622 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001623 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 psa_key_slot_t *slot;
1625
Gilles Peskinedf179142019-07-15 22:02:14 +02001626 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001627 *p_drv = NULL;
1628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 status = psa_validate_key_attributes(attributes, p_drv);
1630 if (status != PSA_SUCCESS) {
1631 return status;
1632 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1635 if (status != PSA_SUCCESS) {
1636 return status;
1637 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001638 slot = *p_slot;
1639
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001640 /* We're storing the declared bit-size of the key. It's up to each
1641 * creation mechanism to verify that this information is correct.
1642 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001643 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001644 * is optional (import, copy). In case of a volatile key, assign it the
1645 * volatile key identifier associated to the slot returned to contain its
1646 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001647
1648 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001650#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1651 slot->attr.id = volatile_key_id;
1652#else
1653 slot->attr.id.key_id = volatile_key_id;
1654#endif
1655 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001656
Gilles Peskine91e8c332019-08-02 19:19:39 +02001657 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001658 * external-only flags, query `attributes`. Thanks to the check
1659 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001660 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001661 * may have set. */
1662 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001663
Gilles Peskinecbaff462019-07-12 23:46:04 +02001664#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001665 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001666 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001667 * create the key file in internal storage, create the
1668 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001669 * persistent data. This is done by starting a transaction that will
1670 * encompass these three actions.
1671 * For registering a volatile key, we just need to find an appropriate
1672 * slot number inside the SE. Since the key is designated volatile, creating
1673 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001674 /* The first thing to do is to find a slot number for the new key.
1675 * We save the slot number in persistent storage as part of the
1676 * transaction data. It will be needed to recover if the power
1677 * fails during the key creation process, to clean up on the secure
1678 * element side after restarting. Obtaining a slot number from the
1679 * secure element driver updates its persistent state, but we do not yet
1680 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001681 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001683 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1685 &slot_number);
1686 if (status != PSA_SUCCESS) {
1687 return status;
1688 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1691 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001692 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001693 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001694 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 status = psa_crypto_save_transaction();
1696 if (status != PSA_SUCCESS) {
1697 (void) psa_crypto_stop_transaction();
1698 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001699 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001700 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001701
1702 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001704 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001707 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001709 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001710#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001713}
Gilles Peskine4747d192019-04-17 15:05:45 +02001714
1715/** Finalize the creation of a key once its key material has been set.
1716 *
1717 * This entails writing the key to persistent storage.
1718 *
1719 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001720 * See the documentation of psa_start_key_creation() for the intended use
1721 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001722 *
Ronald Cron5c522922020-11-14 16:35:34 +01001723 * If the finalization succeeds, the function unlocks the key slot (it was
1724 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1725 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001726 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001727 * \param[in,out] slot Pointer to the slot with key material.
1728 * \param[in] driver The secure element driver for the key,
1729 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001730 * \param[out] key On success, identifier of the key. Note that the
1731 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001732 *
1733 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001734 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001735 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1736 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1737 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1738 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1739 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1740 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001741 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001742 * \return If this function fails, the key slot is an invalid state.
1743 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001745static psa_status_t psa_finish_key_creation(
1746 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001747 psa_se_drv_table_entry_t *driver,
1748 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001749{
1750 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001751 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001752 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001753
1754#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001756#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001758 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001759 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001761
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001762 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1763 sizeof(data.slot_number),
1764 "Slot number size does not match psa_se_key_data_storage_t");
1765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1767 status = psa_save_persistent_key(&slot->attr,
1768 (uint8_t *) &data,
1769 sizeof(data));
1770 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001771#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1772 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001773 /* Key material is saved in export representation in the slot, so
1774 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 status = psa_save_persistent_key(&slot->attr,
1776 slot->key.data,
1777 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001778 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001779 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001780#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1781
Gilles Peskinecbaff462019-07-12 23:46:04 +02001782#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001783 /* Finish the transaction for a key creation. This does not
1784 * happen when registering an existing key. Detect this case
1785 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001786 * creation of a persistent key in a secure element requires a transaction,
1787 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 if (driver != NULL &&
1789 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1790 status = psa_save_se_persistent_data(driver);
1791 if (status != PSA_SUCCESS) {
1792 psa_destroy_persistent_key(slot->attr.id);
1793 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001794 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001796 }
1797#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001800 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 status = psa_unlock_key_slot(slot);
1802 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001803 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001805 }
Ronald Cron50972942020-11-14 11:28:25 +01001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001808}
1809
1810/** Abort the creation of a key.
1811 *
1812 * You may call this function after calling psa_start_key_creation(),
1813 * or after psa_finish_key_creation() fails. In other circumstances, this
1814 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001815 * See the documentation of psa_start_key_creation() for the intended use
1816 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001817 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001818 * \param[in,out] slot Pointer to the slot with key material.
1819 * \param[in] driver The secure element driver for the key,
1820 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001821 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001822static void psa_fail_key_creation(psa_key_slot_t *slot,
1823 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001824{
Gilles Peskine011e4282019-06-26 18:34:38 +02001825 (void) driver;
1826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001828 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001830
1831#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001832 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001833 * element, and the failure happened later (when saving metadata
1834 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001835 * element.
1836 * https://github.com/ARMmbed/mbed-crypto/issues/217
1837 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001838
Gilles Peskined7729582019-08-05 15:55:54 +02001839 /* Abort the ongoing transaction if any (there may not be one if
1840 * the creation process failed before starting one, or if the
1841 * key creation is a registration of a key in a secure element).
1842 * Earlier functions must already have done what it takes to undo any
1843 * partial creation. All that's left is to update the transaction data
1844 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001846#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001849}
1850
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001851/** Validate optional attributes during key creation.
1852 *
1853 * Some key attributes are optional during key creation. If they are
1854 * specified in the attributes structure, check that they are consistent
1855 * with the data in the slot.
1856 *
1857 * This function should be called near the end of key creation, after
1858 * the slot in memory is fully populated but before saving persistent data.
1859 */
1860static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001861 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001863{
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (attributes->core.type != 0) {
1865 if (attributes->core.type != slot->attr.type) {
1866 return PSA_ERROR_INVALID_ARGUMENT;
1867 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868 }
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001871#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1873 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001874 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001875 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001877
Ronald Cron90857082020-11-25 14:58:33 +01001878 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 slot->attr.type,
1880 slot->key.data,
1881 slot->key.bytes,
1882 &rsa);
1883 if (status != PSA_SUCCESS) {
1884 return status;
1885 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 mbedtls_mpi_init(&actual);
1888 mbedtls_mpi_init(&required);
1889 ret = mbedtls_rsa_export(rsa,
1890 NULL, NULL, NULL, NULL, &actual);
1891 mbedtls_rsa_free(rsa);
1892 mbedtls_free(rsa);
1893 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001894 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 }
1896 ret = mbedtls_mpi_read_binary(&required,
1897 attributes->domain_parameters,
1898 attributes->domain_parameters_size);
1899 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 }
1902 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001903 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 }
1905rsa_exit:
1906 mbedtls_mpi_free(&actual);
1907 mbedtls_mpi_free(&required);
1908 if (ret != 0) {
1909 return mbedtls_to_psa_error(ret);
1910 }
1911 } else
John Durkop9814fa22020-11-04 12:28:15 -08001912#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1913 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001914 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001916 }
1917 }
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if (attributes->core.bits != 0) {
1920 if (attributes->core.bits != slot->attr.bits) {
1921 return PSA_ERROR_INVALID_ARGUMENT;
1922 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001923 }
1924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001926}
1927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1929 const uint8_t *data,
1930 size_t data_length,
1931 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001932{
1933 psa_status_t status;
1934 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001935 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001936 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301937 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001938
Ronald Cron81709fc2020-11-14 12:10:32 +01001939 *key = MBEDTLS_SVC_KEY_ID_INIT;
1940
Gilles Peskine0f84d622019-09-12 19:03:13 +02001941 /* Reject zero-length symmetric keys (including raw data key objects).
1942 * This also rejects any key which might be encoded as an empty string,
1943 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 if (data_length == 0) {
1945 return PSA_ERROR_INVALID_ARGUMENT;
1946 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001947
Archana449608b2021-09-08 15:36:05 +05301948 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (data_length > SIZE_MAX / 8) {
1950 return PSA_ERROR_NOT_SUPPORTED;
1951 }
Archana6ed4bda2021-08-04 10:47:15 +05301952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1954 &slot, &driver);
1955 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001956 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001958
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001959 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301960 * storage ( thus not in the case of importing a key in a secure element
1961 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1962 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 if (slot->key.data == NULL) {
1964 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301965 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 attributes, data, data_length, &storage_size);
1967 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301968 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 }
Archanad8a83dc2021-06-14 10:04:16 +05301970 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 status = psa_allocate_buffer_to_slot(slot, storage_size);
1972 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001973 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001975 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001976
1977 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 status = psa_driver_wrapper_import_key(attributes,
1979 data, data_length,
1980 slot->key.data,
1981 slot->key.bytes,
1982 &slot->key.bytes, &bits);
1983 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001984 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001988 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001990 status = PSA_ERROR_INVALID_ARGUMENT;
1991 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001992 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001993
Archana6ed4bda2021-08-04 10:47:15 +05301994 /* Enforce a size limit, and in particular ensure that the bit
1995 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301997 status = PSA_ERROR_NOT_SUPPORTED;
1998 goto exit;
1999 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 status = psa_validate_optional_attributes(slot, attributes);
2001 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002002 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002006exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 if (status != PSA_SUCCESS) {
2008 psa_fail_key_creation(slot, driver);
2009 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002012}
2013
Gilles Peskined7729582019-08-05 15:55:54 +02002014#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2015psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002017{
2018 psa_status_t status;
2019 psa_key_slot_t *slot = NULL;
2020 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002021 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002022
2023 /* Leaving attributes unspecified is not currently supported.
2024 * It could make sense to query the key type and size from the
2025 * secure element, but not all secure elements support this
2026 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2028 return PSA_ERROR_NOT_SUPPORTED;
2029 }
2030 if (psa_get_key_bits(attributes) == 0) {
2031 return PSA_ERROR_NOT_SUPPORTED;
2032 }
Gilles Peskined7729582019-08-05 15:55:54 +02002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2035 &slot, &driver);
2036 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002037 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 }
Gilles Peskined7729582019-08-05 15:55:54 +02002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002041
2042exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if (status != PSA_SUCCESS) {
2044 psa_fail_key_creation(slot, driver);
2045 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002046
Gilles Peskined7729582019-08-05 15:55:54 +02002047 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 psa_close_key(key);
2049 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002050}
2051#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2054 const psa_key_attributes_t *specified_attributes,
2055 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002056{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002057 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002058 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002059 psa_key_slot_t *source_slot = NULL;
2060 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002061 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002062 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302063 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002064
Ronald Cron81709fc2020-11-14 12:10:32 +01002065 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2066
Archana8a180362021-07-05 02:18:48 +05302067 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2069 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002070 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 status = psa_validate_optional_attributes(source_slot,
2074 specified_attributes);
2075 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002078
Archana9d17bf42021-09-10 06:22:44 +05302079 /* The target key type and number of bits have been validated by
2080 * psa_validate_optional_attributes() to be either equal to zero or
2081 * equal to the ones of the source key. So it is safe to inherit
2082 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302083 * */
Archana9d17bf42021-09-10 06:22:44 +05302084 actual_attributes.core.bits = source_slot->attr.bits;
2085 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302086
2087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 status = psa_restrict_key_policy(source_slot->attr.type,
2089 &actual_attributes.core.policy,
2090 &source_slot->attr.policy);
2091 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2096 &target_slot, &driver);
2097 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002098 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 }
2100 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2101 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302102 /*
Archana9d17bf42021-09-10 06:22:44 +05302103 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302104 * the source key would need to be exported as plaintext and re-imported
2105 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302106 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302107 * appropriate API invocations from the application, if needed.
2108 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002109 status = PSA_ERROR_NOT_SUPPORTED;
2110 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002111 }
Archana8a180362021-07-05 02:18:48 +05302112 /*
2113 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302114 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302115 * - For opaque keys this translates to an invocation of the drivers'
2116 * copy_key entry point through the dispatch layer.
2117 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2119 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2120 &storage_size);
2121 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302122 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 }
Archana374fe5b2021-09-08 15:50:28 +05302124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2126 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 }
Archana374fe5b2021-09-08 15:50:28 +05302129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 status = psa_driver_wrapper_copy_key(&actual_attributes,
2131 source_slot->key.data,
2132 source_slot->key.bytes,
2133 target_slot->key.data,
2134 target_slot->key.bytes,
2135 &target_slot->key.bytes);
2136 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 }
2139 } else {
2140 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302141 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 source_slot->key.bytes);
2143 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 }
Archana8a180362021-07-05 02:18:48 +05302146 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (status != PSA_SUCCESS) {
2150 psa_fail_key_creation(target_slot, driver);
2151 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002156}
2157
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002158
2159
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002160/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002161/* Message digests */
2162/****************************************************************/
2163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002165{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002166 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (operation->id == 0) {
2168 return PSA_SUCCESS;
2169 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002172 operation->id = 0;
2173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002175}
2176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2178 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002179{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002181
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002182 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002184 status = PSA_ERROR_BAD_STATE;
2185 goto exit;
2186 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002189 status = PSA_ERROR_INVALID_ARGUMENT;
2190 goto exit;
2191 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002192
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002193 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2194 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002198
2199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (status != PSA_SUCCESS) {
2201 psa_hash_abort(operation);
2202 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002203
2204 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002205}
2206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2208 const uint8_t *input,
2209 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002210{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002211 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002214 status = PSA_ERROR_BAD_STATE;
2215 goto exit;
2216 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002217
Steven Cooremanc8288352021-03-04 14:02:19 +01002218 /* Don't require hash implementations to behave correctly on a
2219 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (input_length == 0) {
2221 return PSA_SUCCESS;
2222 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002225
2226exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (status != PSA_SUCCESS) {
2228 psa_hash_abort(operation);
2229 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002232}
2233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2235 uint8_t *hash,
2236 size_t hash_size,
2237 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002239 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 if (operation->id == 0) {
2241 return PSA_ERROR_BAD_STATE;
2242 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002243
Steven Cooreman1e582352021-02-18 17:24:37 +01002244 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 operation, hash, hash_size, hash_length);
2246 psa_hash_abort(operation);
2247 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002248}
2249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2251 const uint8_t *hash,
2252 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002253{
Ronald Cron69a63422021-10-18 09:47:58 +02002254 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002255 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002256 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 operation,
2258 actual_hash, sizeof(actual_hash),
2259 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002262 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002266 status = PSA_ERROR_INVALID_SIGNATURE;
2267 goto exit;
2268 }
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002271 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002273
2274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2276 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002277 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281}
2282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283psa_status_t psa_hash_compute(psa_algorithm_t alg,
2284 const uint8_t *input, size_t input_length,
2285 uint8_t *hash, size_t hash_size,
2286 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002287{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002288 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 if (!PSA_ALG_IS_HASH(alg)) {
2290 return PSA_ERROR_INVALID_ARGUMENT;
2291 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2294 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002295}
2296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297psa_status_t psa_hash_compare(psa_algorithm_t alg,
2298 const uint8_t *input, size_t input_length,
2299 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002300{
Ronald Cron69a63422021-10-18 09:47:58 +02002301 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002302 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (!PSA_ALG_IS_HASH(alg)) {
2305 return PSA_ERROR_INVALID_ARGUMENT;
2306 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002307
Steven Cooreman1e582352021-02-18 17:24:37 +01002308 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 alg, input, input_length,
2310 actual_hash, sizeof(actual_hash),
2311 &actual_hash_length);
2312 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002313 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 }
2315 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002316 status = PSA_ERROR_INVALID_SIGNATURE;
2317 goto exit;
2318 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002320 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002322
2323exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2325 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002326}
2327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2329 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002330{
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (source_operation->id == 0 ||
2332 target_operation->id != 0) {
2333 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002334 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2337 target_operation);
2338 if (status != PSA_SUCCESS) {
2339 psa_hash_abort(target_operation);
2340 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002343}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002344
2345
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002347/* MAC */
2348/****************************************************************/
2349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002351{
Steven Cooremane6804192021-03-19 18:28:56 +01002352 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (operation->id == 0) {
2354 return PSA_SUCCESS;
2355 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002358 operation->mac_size = 0;
2359 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002360 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002363}
2364
Ronald Cron2dff3b22021-06-17 16:33:22 +02002365static psa_status_t psa_mac_finalize_alg_and_key_validation(
2366 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002367 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002369{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 psa_key_type_t key_type = psa_get_key_type(attributes);
2372 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (!PSA_ALG_IS_MAC(alg)) {
2375 return PSA_ERROR_INVALID_ARGUMENT;
2376 }
Steven Cooremane6804192021-03-19 18:28:56 +01002377
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002378 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 status = psa_mac_key_can_do(alg, key_type);
2380 if (status != PSA_SUCCESS) {
2381 return status;
2382 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002383
Steven Cooremand1a68f12021-05-10 11:13:41 +02002384 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002388 /* A very short MAC is too short for security since it can be
2389 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2390 * so we make this our minimum, even though 32 bits is still
2391 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002393 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2396 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002397 /* It's impossible to "truncate" to a larger length than the full length
2398 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002400 }
2401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002403 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2404 * that is disabled in the compile-time configuration. The result can
2405 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2406 * configuration into account. In this case, force a return of
2407 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2408 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2409 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2410 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2411 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002413 }
2414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002416}
2417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2419 mbedtls_svc_key_id_t key,
2420 psa_algorithm_t alg,
2421 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002422{
2423 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2424 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002425 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426
2427 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002429 status = PSA_ERROR_BAD_STATE;
2430 goto exit;
2431 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432
2433 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 key,
2435 &slot,
2436 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2437 alg);
2438 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002439 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441
2442 psa_key_attributes_t attributes = {
2443 .core = slot->attr
2444 };
2445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2447 &operation->mac_size);
2448 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451
2452 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002453 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 if (is_sign) {
2455 status = psa_driver_wrapper_mac_sign_setup(operation,
2456 &attributes,
2457 slot->key.data,
2458 slot->key.bytes,
2459 alg);
2460 } else {
2461 status = psa_driver_wrapper_mac_verify_setup(operation,
2462 &attributes,
2463 slot->key.data,
2464 slot->key.bytes,
2465 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002466 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002467
Gilles Peskinefbfac682018-07-08 20:51:54 +02002468exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 if (status != PSA_SUCCESS) {
2470 psa_mac_abort(operation);
2471 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002476}
2477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2479 mbedtls_svc_key_id_t key,
2480 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002481{
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002483}
2484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2486 mbedtls_svc_key_id_t key,
2487 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002488{
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002490}
2491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2493 const uint8_t *input,
2494 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495{
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (operation->id == 0) {
2497 return PSA_ERROR_BAD_STATE;
2498 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002500 /* Don't require hash implementations to behave correctly on a
2501 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if (input_length == 0) {
2503 return PSA_SUCCESS;
2504 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2507 input, input_length);
2508 if (status != PSA_SUCCESS) {
2509 psa_mac_abort(operation);
2510 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513}
2514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2516 uint8_t *mac,
2517 size_t mac_size,
2518 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002519{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002520 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2521 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002524 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002525 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002526 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002529 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002530 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002531 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002532
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002533 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2534 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002536 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002537 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002538 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002541 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002542 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002543 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 status = psa_driver_wrapper_mac_sign_finish(operation,
2546 mac, operation->mac_size,
2547 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002548
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002549exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002550 /* In case of success, set the potential excess room in the output buffer
2551 * to an invalid value, to avoid potentially leaking a longer MAC.
2552 * In case of error, set the output length and content to a safe default,
2553 * such that in case the caller misses an error check, the output would be
2554 * an unachievable MAC.
2555 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002557 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002558 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002559 }
mohammad16036df908f2018-04-02 08:34:15 -07002560
Paul Elliottdc42ca82023-02-24 18:11:59 +00002561 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002566}
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2569 const uint8_t *mac,
2570 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002572 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2573 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002576 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002577 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002578 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002581 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002582 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002583 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002586 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002587 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002588 }
2589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 status = psa_driver_wrapper_mac_verify_finish(operation,
2591 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002592
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002593exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597}
2598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2600 psa_algorithm_t alg,
2601 const uint8_t *input,
2602 size_t input_length,
2603 uint8_t *mac,
2604 size_t mac_size,
2605 size_t *mac_length,
2606 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002607{
2608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002609 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2610 psa_key_slot_t *slot;
2611 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612
Ronald Cron51131b52021-06-17 17:17:20 +02002613 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 key,
2615 &slot,
2616 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2617 alg);
2618 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002621
Ronald Cron51131b52021-06-17 17:17:20 +02002622 psa_key_attributes_t attributes = {
2623 .core = slot->attr
2624 };
2625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2627 &operation_mac_size);
2628 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002633 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002634 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002635 }
2636
2637 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 &attributes,
2639 slot->key.data, slot->key.bytes,
2640 alg,
2641 input, input_length,
2642 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002643
2644exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002645 /* In case of success, set the potential excess room in the output buffer
2646 * to an invalid value, to avoid potentially leaking a longer MAC.
2647 * In case of error, set the output length and content to a safe default,
2648 * such that in case the caller misses an error check, the output would be
2649 * an unachievable MAC.
2650 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002652 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002653 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002654 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002655
2656 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002661}
2662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002664 psa_algorithm_t alg,
2665 const uint8_t *input,
2666 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 uint8_t *mac,
2668 size_t mac_size,
2669 size_t *mac_length)
2670{
2671 return psa_mac_compute_internal(key, alg,
2672 input, input_length,
2673 mac, mac_size, mac_length, 1);
2674}
2675
2676psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2677 psa_algorithm_t alg,
2678 const uint8_t *input,
2679 size_t input_length,
2680 const uint8_t *mac,
2681 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002682{
2683 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002684 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2685 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 status = psa_mac_compute_internal(key, alg,
2688 input, input_length,
2689 actual_mac, sizeof(actual_mac),
2690 &actual_mac_length, 0);
2691 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002692 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002696 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002697 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002698 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002700 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002701 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002702 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002703
2704exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002708}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002709
Gilles Peskinee59236f2018-01-27 23:32:46 +01002710/****************************************************************/
2711/* Asymmetric cryptography */
2712/****************************************************************/
2713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2715 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002716{
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 if (input_is_message) {
2718 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2719 return PSA_ERROR_INVALID_ARGUMENT;
2720 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2723 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2724 return PSA_ERROR_INVALID_ARGUMENT;
2725 }
2726 }
2727 } else {
2728 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2729 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002730 }
2731 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002734}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2737 int input_is_message,
2738 psa_algorithm_t alg,
2739 const uint8_t *input,
2740 size_t input_length,
2741 uint8_t *signature,
2742 size_t signature_size,
2743 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002744{
2745 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2746 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2747 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002748
2749 *signature_length = 0;
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 status = psa_sign_verify_check_alg(input_is_message, alg);
2752 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002753 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002755
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002756 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002757 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002758 * buffer can in principle be empty since it doesn't actually have
2759 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 if (signature_size == 0) {
2761 return PSA_ERROR_BUFFER_TOO_SMALL;
2762 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002763
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002764 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 key, &slot,
2766 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2767 PSA_KEY_USAGE_SIGN_HASH,
2768 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002771 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002775 status = PSA_ERROR_INVALID_ARGUMENT;
2776 goto exit;
2777 }
2778
2779 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002781 };
2782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002784 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002785 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002786 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 signature, signature_size, signature_length);
2788 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002789
2790 status = psa_driver_wrapper_sign_hash(
2791 &attributes, slot->key.data, slot->key.bytes,
2792 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002794 }
2795
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002796
2797exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002798 psa_wipe_tag_output_buffer(signature, status, signature_size,
2799 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002804}
2805
Gilles Peskine449bd832023-01-11 14:50:10 +01002806static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2807 int input_is_message,
2808 psa_algorithm_t alg,
2809 const uint8_t *input,
2810 size_t input_length,
2811 const uint8_t *signature,
2812 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002813{
2814 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2815 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2816 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 status = psa_sign_verify_check_alg(input_is_message, alg);
2819 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002820 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002822
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002823 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 key, &slot,
2825 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2826 PSA_KEY_USAGE_VERIFY_HASH,
2827 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 if (status != PSA_SUCCESS) {
2830 return status;
2831 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002832
2833 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002835 };
2836
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002838 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002839 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002840 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 signature, signature_length);
2842 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002843 status = psa_driver_wrapper_verify_hash(
2844 &attributes, slot->key.data, slot->key.bytes,
2845 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002847 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002848
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002852
2853}
2854
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002855psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002856 const psa_key_attributes_t *attributes,
2857 const uint8_t *key_buffer,
2858 size_t key_buffer_size,
2859 psa_algorithm_t alg,
2860 const uint8_t *input,
2861 size_t input_length,
2862 uint8_t *signature,
2863 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002865{
2866 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002869 size_t hash_length;
2870 uint8_t hash[PSA_HASH_MAX_SIZE];
2871
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002872 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 PSA_ALG_SIGN_GET_HASH(alg),
2874 input, input_length,
2875 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002878 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002880
2881 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 attributes, key_buffer, key_buffer_size,
2883 alg, hash, hash_length,
2884 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002885 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002888}
2889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2891 psa_algorithm_t alg,
2892 const uint8_t *input,
2893 size_t input_length,
2894 uint8_t *signature,
2895 size_t signature_size,
2896 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002897{
2898 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002899 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002901}
2902
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002903psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002904 const psa_key_attributes_t *attributes,
2905 const uint8_t *key_buffer,
2906 size_t key_buffer_size,
2907 psa_algorithm_t alg,
2908 const uint8_t *input,
2909 size_t input_length,
2910 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002912{
2913 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002916 size_t hash_length;
2917 uint8_t hash[PSA_HASH_MAX_SIZE];
2918
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002919 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 PSA_ALG_SIGN_GET_HASH(alg),
2921 input, input_length,
2922 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002925 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002927
2928 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 attributes, key_buffer, key_buffer_size,
2930 alg, hash, hash_length,
2931 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002932 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002935}
2936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2938 psa_algorithm_t alg,
2939 const uint8_t *input,
2940 size_t input_length,
2941 const uint8_t *signature,
2942 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002943{
2944 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002945 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002947}
2948
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002949psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002950 const psa_key_attributes_t *attributes,
2951 const uint8_t *key_buffer, size_t key_buffer_size,
2952 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002954{
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2956 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2957 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002958#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2960 return mbedtls_psa_rsa_sign_hash(
2961 attributes,
2962 key_buffer, key_buffer_size,
2963 alg, hash, hash_length,
2964 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002965#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2966 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 } else {
2968 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002969 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2971 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002972#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2974 return mbedtls_psa_ecdsa_sign_hash(
2975 attributes,
2976 key_buffer, key_buffer_size,
2977 alg, hash, hash_length,
2978 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002979#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2980 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 } else {
2982 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002983 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002984 }
Ronald Cron4501c982021-03-19 20:09:32 +01002985
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 (void) key_buffer;
2987 (void) key_buffer_size;
2988 (void) hash;
2989 (void) hash_length;
2990 (void) signature;
2991 (void) signature_size;
2992 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002995}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2998 psa_algorithm_t alg,
2999 const uint8_t *hash,
3000 size_t hash_length,
3001 uint8_t *signature,
3002 size_t signature_size,
3003 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003004{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003005 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003006 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003008}
3009
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003010psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003011 const psa_key_attributes_t *attributes,
3012 const uint8_t *key_buffer, size_t key_buffer_size,
3013 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003015{
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3017 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3018 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003019#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3021 return mbedtls_psa_rsa_verify_hash(
3022 attributes,
3023 key_buffer, key_buffer_size,
3024 alg, hash, hash_length,
3025 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003026#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3027 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 } else {
3029 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003030 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3032 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003033#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3035 return mbedtls_psa_ecdsa_verify_hash(
3036 attributes,
3037 key_buffer, key_buffer_size,
3038 alg, hash, hash_length,
3039 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003040#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3041 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 } else {
3043 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003044 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003045 }
Ronald Cron4501c982021-03-19 20:09:32 +01003046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 (void) key_buffer;
3048 (void) key_buffer_size;
3049 (void) hash;
3050 (void) hash_length;
3051 (void) signature;
3052 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003053
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003055}
3056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3058 psa_algorithm_t alg,
3059 const uint8_t *hash,
3060 size_t hash_length,
3061 const uint8_t *signature,
3062 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003063{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003064 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003065 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003067}
3068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3070 psa_algorithm_t alg,
3071 const uint8_t *input,
3072 size_t input_length,
3073 const uint8_t *salt,
3074 size_t salt_length,
3075 uint8_t *output,
3076 size_t output_size,
3077 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003079 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003080 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003081 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003082
Darryl Green5cc689a2018-07-24 15:34:10 +01003083 (void) input;
3084 (void) input_length;
3085 (void) salt;
3086 (void) output;
3087 (void) output_size;
3088
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003089 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3092 return PSA_ERROR_INVALID_ARGUMENT;
3093 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003094
Ronald Cron5c522922020-11-14 16:35:34 +01003095 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3097 if (status != PSA_SUCCESS) {
3098 return status;
3099 }
3100 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3101 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003102 status = PSA_ERROR_INVALID_ARGUMENT;
3103 goto exit;
3104 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003105
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003106 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003108 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003109
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003110 status = psa_driver_wrapper_asymmetric_encrypt(
3111 &attributes, slot->key.data, slot->key.bytes,
3112 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003114exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003118}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3121 psa_algorithm_t alg,
3122 const uint8_t *input,
3123 size_t input_length,
3124 const uint8_t *salt,
3125 size_t salt_length,
3126 uint8_t *output,
3127 size_t output_size,
3128 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003129{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003130 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003131 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003132 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003133
Darryl Green5cc689a2018-07-24 15:34:10 +01003134 (void) input;
3135 (void) input_length;
3136 (void) salt;
3137 (void) output;
3138 (void) output_size;
3139
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003140 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3143 return PSA_ERROR_INVALID_ARGUMENT;
3144 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003145
Ronald Cron5c522922020-11-14 16:35:34 +01003146 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3148 if (status != PSA_SUCCESS) {
3149 return status;
3150 }
3151 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003152 status = PSA_ERROR_INVALID_ARGUMENT;
3153 goto exit;
3154 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003155
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003156 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003158 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003159
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003160 status = psa_driver_wrapper_asymmetric_decrypt(
3161 &attributes, slot->key.data, slot->key.bytes,
3162 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003164
3165exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003169}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003170
Paul Elliott9fe12f62022-11-30 19:16:02 +00003171/****************************************************************/
3172/* Asymmetric interruptible cryptography */
3173/****************************************************************/
3174
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003175static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3176
Paul Elliott9fe12f62022-11-30 19:16:02 +00003177void psa_interruptible_set_max_ops(uint32_t max_ops)
3178{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003179 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003180}
3181
3182uint32_t psa_interruptible_get_max_ops(void)
3183{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003184 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003185}
3186
Paul Elliott9fe12f62022-11-30 19:16:02 +00003187uint32_t psa_sign_hash_get_num_ops(
3188 const psa_sign_hash_interruptible_operation_t *operation)
3189{
Paul Elliott296ede92022-12-15 17:00:30 +00003190 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003191}
3192
3193uint32_t psa_verify_hash_get_num_ops(
3194 const psa_verify_hash_interruptible_operation_t *operation)
3195{
Paul Elliott296ede92022-12-15 17:00:30 +00003196 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003197}
3198
Paul Elliott59ad9452022-12-18 15:09:02 +00003199static psa_status_t psa_sign_hash_abort_internal(
3200 psa_sign_hash_interruptible_operation_t *operation)
3201{
3202 if (operation->id == 0) {
3203 /* The object has (apparently) been initialized but it is not (yet)
3204 * in use. It's ok to call abort on such an object, and there's
3205 * nothing to do. */
3206 return PSA_SUCCESS;
3207 }
3208
3209 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3210
3211 status = psa_driver_wrapper_sign_hash_abort(operation);
3212
3213 operation->id = 0;
3214
Paul Elliotte6145dc2023-02-07 12:51:21 +00003215 /* Do not clear either the error_occurred or num_ops elements here as they
3216 * only want to be cleared by the application calling abort, not by abort
3217 * being called at completion of an operation. */
3218
Paul Elliott59ad9452022-12-18 15:09:02 +00003219 return status;
3220}
3221
Paul Elliott9fe12f62022-11-30 19:16:02 +00003222psa_status_t psa_sign_hash_start(
3223 psa_sign_hash_interruptible_operation_t *operation,
3224 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3225 const uint8_t *hash, size_t hash_length)
3226{
3227 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3228 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3229 psa_key_slot_t *slot;
3230
Paul Elliottc9774412023-02-06 15:14:07 +00003231 /* Check that start has not been previously called, or operation has not
3232 * previously errored. */
3233 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003234 return PSA_ERROR_BAD_STATE;
3235 }
3236
Paul Elliott9fe12f62022-11-30 19:16:02 +00003237 status = psa_sign_verify_check_alg(0, alg);
3238 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003239 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003240 return status;
3241 }
3242
3243 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3244 PSA_KEY_USAGE_SIGN_HASH,
3245 alg);
3246
3247 if (status != PSA_SUCCESS) {
3248 goto exit;
3249 }
3250
3251 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3252 status = PSA_ERROR_INVALID_ARGUMENT;
3253 goto exit;
3254 }
3255
3256 psa_key_attributes_t attributes = {
3257 .core = slot->attr
3258 };
3259
Paul Elliott296ede92022-12-15 17:00:30 +00003260 /* Ensure ops count gets reset, in case of operation re-use. */
3261 operation->num_ops = 0;
3262
Paul Elliott9fe12f62022-11-30 19:16:02 +00003263 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3264 slot->key.data,
3265 slot->key.bytes, alg,
3266 hash, hash_length);
3267exit:
3268
3269 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003270 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003271 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003272 }
3273
3274 unlock_status = psa_unlock_key_slot(slot);
3275
Paul Elliottc9774412023-02-06 15:14:07 +00003276 if (unlock_status != PSA_SUCCESS) {
3277 operation->error_occurred = 1;
3278 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003279
Paul Elliottc9774412023-02-06 15:14:07 +00003280 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003281}
3282
3283
3284psa_status_t psa_sign_hash_complete(
3285 psa_sign_hash_interruptible_operation_t *operation,
3286 uint8_t *signature, size_t signature_size,
3287 size_t *signature_length)
3288{
3289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3290
3291 *signature_length = 0;
3292
Paul Elliottc9774412023-02-06 15:14:07 +00003293 /* Check that start has been called first, and that operation has not
3294 * previously errored. */
3295 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003296 status = PSA_ERROR_BAD_STATE;
3297 goto exit;
3298 }
3299
Paul Elliott096abc42023-02-03 18:33:23 +00003300 /* Immediately reject a zero-length signature buffer. This guarantees that
3301 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003302 if (signature_size == 0) {
3303 status = PSA_ERROR_BUFFER_TOO_SMALL;
3304 goto exit;
3305 }
3306
3307 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3308 signature_size,
3309 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003310
Paul Elliott296ede92022-12-15 17:00:30 +00003311 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003312 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003313
Paul Elliottebe225c2023-02-10 14:32:53 +00003314exit:
3315
Paul Elliott59200a22023-02-21 15:07:40 +00003316 psa_wipe_tag_output_buffer(signature, status, signature_size,
3317 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003318
Paul Elliott53bb3122023-02-10 14:22:22 +00003319 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003320 if (status != PSA_SUCCESS) {
3321 operation->error_occurred = 1;
3322 }
3323
Paul Elliott59ad9452022-12-18 15:09:02 +00003324 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003325 }
3326
3327 return status;
3328}
3329
3330psa_status_t psa_sign_hash_abort(
3331 psa_sign_hash_interruptible_operation_t *operation)
3332{
Paul Elliott59ad9452022-12-18 15:09:02 +00003333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3334
3335 status = psa_sign_hash_abort_internal(operation);
3336
3337 /* We clear the number of ops done here, so that it is not cleared when
3338 * the operation fails or succeeds, only on manual abort. */
3339 operation->num_ops = 0;
3340
Paul Elliottc9774412023-02-06 15:14:07 +00003341 /* Likewise, failure state. */
3342 operation->error_occurred = 0;
3343
Paul Elliott59ad9452022-12-18 15:09:02 +00003344 return status;
3345}
3346
3347static psa_status_t psa_verify_hash_abort_internal(
3348 psa_verify_hash_interruptible_operation_t *operation)
3349{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003350 if (operation->id == 0) {
3351 /* The object has (apparently) been initialized but it is not (yet)
3352 * in use. It's ok to call abort on such an object, and there's
3353 * nothing to do. */
3354 return PSA_SUCCESS;
3355 }
3356
Paul Elliott59ad9452022-12-18 15:09:02 +00003357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3358
3359 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003360
3361 operation->id = 0;
3362
Paul Elliotte6145dc2023-02-07 12:51:21 +00003363 /* Do not clear either the error_occurred or num_ops elements here as they
3364 * only want to be cleared by the application calling abort, not by abort
3365 * being called at completion of an operation. */
3366
Paul Elliott59ad9452022-12-18 15:09:02 +00003367 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003368}
3369
3370psa_status_t psa_verify_hash_start(
3371 psa_verify_hash_interruptible_operation_t *operation,
3372 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3373 const uint8_t *hash, size_t hash_length,
3374 const uint8_t *signature, size_t signature_length)
3375{
3376 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3377 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3378 psa_key_slot_t *slot;
3379
Paul Elliottc9774412023-02-06 15:14:07 +00003380 /* Check that start has not been previously called, or operation has not
3381 * previously errored. */
3382 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003383 return PSA_ERROR_BAD_STATE;
3384 }
3385
3386 status = psa_sign_verify_check_alg(0, alg);
3387 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003388 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003389 return status;
3390 }
3391
3392 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3393 PSA_KEY_USAGE_VERIFY_HASH,
3394 alg);
3395
3396 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003397 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003398 return status;
3399 }
3400
3401 psa_key_attributes_t attributes = {
3402 .core = slot->attr
3403 };
3404
Paul Elliott296ede92022-12-15 17:00:30 +00003405 /* Ensure ops count gets reset, in case of operation re-use. */
3406 operation->num_ops = 0;
3407
Paul Elliott9fe12f62022-11-30 19:16:02 +00003408 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3409 slot->key.data,
3410 slot->key.bytes,
3411 alg, hash, hash_length,
3412 signature, signature_length);
3413
3414 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003415 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003416 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417 }
3418
3419 unlock_status = psa_unlock_key_slot(slot);
3420
Paul Elliottc9774412023-02-06 15:14:07 +00003421 if (unlock_status != PSA_SUCCESS) {
3422 operation->error_occurred = 1;
3423 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003424
Paul Elliottc9774412023-02-06 15:14:07 +00003425 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003426}
3427
3428psa_status_t psa_verify_hash_complete(
3429 psa_verify_hash_interruptible_operation_t *operation)
3430{
3431 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3432
Paul Elliottc9774412023-02-06 15:14:07 +00003433 /* Check that start has been called first, and that operation has not
3434 * previously errored. */
3435 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003436 status = PSA_ERROR_BAD_STATE;
3437 goto exit;
3438 }
3439
3440 status = psa_driver_wrapper_verify_hash_complete(operation);
3441
Paul Elliott296ede92022-12-15 17:00:30 +00003442 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003443 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003444 operation);
3445
Paul Elliottebe225c2023-02-10 14:32:53 +00003446exit:
3447
Paul Elliott9fe12f62022-11-30 19:16:02 +00003448 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003449 if (status != PSA_SUCCESS) {
3450 operation->error_occurred = 1;
3451 }
3452
Paul Elliott59ad9452022-12-18 15:09:02 +00003453 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003454 }
3455
3456 return status;
3457}
3458
3459psa_status_t psa_verify_hash_abort(
3460 psa_verify_hash_interruptible_operation_t *operation)
3461{
Paul Elliott59ad9452022-12-18 15:09:02 +00003462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003463
Paul Elliott59ad9452022-12-18 15:09:02 +00003464 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003465
Paul Elliott59ad9452022-12-18 15:09:02 +00003466 /* We clear the number of ops done here, so that it is not cleared when
3467 * the operation fails or succeeds, only on manual abort. */
3468 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003469
Paul Elliottc9774412023-02-06 15:14:07 +00003470 /* Likewise, failure state. */
3471 operation->error_occurred = 0;
3472
Paul Elliott59ad9452022-12-18 15:09:02 +00003473 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003474}
3475
Paul Elliott588f8ed2022-12-02 18:10:26 +00003476/****************************************************************/
3477/* Asymmetric interruptible cryptography internal */
3478/* implementations */
3479/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003480
Paul Elliott588f8ed2022-12-02 18:10:26 +00003481void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3482{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003483
Paul Elliott588f8ed2022-12-02 18:10:26 +00003484#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3485 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3486 defined(MBEDTLS_ECP_RESTARTABLE)
3487
3488 /* Internal implementation uses zero to indicate infinite number max ops,
3489 * therefore avoid this value, and set to minimum possible. */
3490 if (max_ops == 0) {
3491 max_ops = 1;
3492 }
3493
Paul Elliott588f8ed2022-12-02 18:10:26 +00003494 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003495#else
3496 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003497#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3498 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3499 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3500}
3501
Paul Elliott588f8ed2022-12-02 18:10:26 +00003502uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003503 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003504{
3505#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3506 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3507 defined(MBEDTLS_ECP_RESTARTABLE)
3508
Paul Elliott93d9ca82023-02-15 18:14:21 +00003509 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003510#else
3511 (void) operation;
3512 return 0;
3513#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3514 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3515 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3516}
3517
3518uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003519 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003520{
3521 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3522 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3523 defined(MBEDTLS_ECP_RESTARTABLE)
3524
Paul Elliott93d9ca82023-02-15 18:14:21 +00003525 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003526#else
3527 (void) operation;
3528 return 0;
3529#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3530 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3531 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3532}
3533
3534psa_status_t mbedtls_psa_sign_hash_start(
3535 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3536 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3537 size_t key_buffer_size, psa_algorithm_t alg,
3538 const uint8_t *hash, size_t hash_length)
3539{
3540 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003541 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003542
Paul Elliott068fe072023-01-16 13:59:15 +00003543 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3544 return PSA_ERROR_NOT_SUPPORTED;
3545 }
3546
3547 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003548 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003549 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003550
3551#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003552 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3553 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003554
Paul Elliotta1c94092023-02-15 16:38:04 +00003555 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3556
Paul Elliott93d9ca82023-02-15 18:14:21 +00003557 /* Ensure num_ops is zero'ed in case of context re-use. */
3558 operation->num_ops = 0;
3559
Paul Elliott068fe072023-01-16 13:59:15 +00003560 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3561 attributes->core.bits,
3562 key_buffer,
3563 key_buffer_size,
3564 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003565
Paul Elliott068fe072023-01-16 13:59:15 +00003566 if (status != PSA_SUCCESS) {
3567 return status;
3568 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003569
Paul Elliott1bc59df2023-02-05 13:41:57 +00003570 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003571 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003572
Paul Elliott068fe072023-01-16 13:59:15 +00003573 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3574 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3575 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003576
Paul Elliott0290a762023-02-09 14:30:24 +00003577 /* We only need to store the same length of hash as the private key size
3578 * here, it would be truncated by the internal implementation anyway. */
3579 required_hash_length = (hash_length < operation->coordinate_bytes ?
3580 hash_length : operation->coordinate_bytes);
3581
Paul Elliottba70ad42023-02-15 18:23:53 +00003582 if (required_hash_length > sizeof(operation->hash)) {
3583 /* Shouldn't happen, but better safe than sorry. */
3584 return PSA_ERROR_CORRUPTION_DETECTED;
3585 }
3586
Paul Elliott0290a762023-02-09 14:30:24 +00003587 memcpy(operation->hash, hash, required_hash_length);
3588 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003589
3590 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003591
3592#else
Paul Elliott068fe072023-01-16 13:59:15 +00003593 (void) operation;
3594 (void) key_buffer;
3595 (void) key_buffer_size;
3596 (void) alg;
3597 (void) hash;
3598 (void) hash_length;
3599 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003600 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003601
Paul Elliott068fe072023-01-16 13:59:15 +00003602 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003603#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3604 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3605 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003606}
3607
3608psa_status_t mbedtls_psa_sign_hash_complete(
3609 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3610 uint8_t *signature, size_t signature_size,
3611 size_t *signature_length)
3612{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003613#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3614 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3615 defined(MBEDTLS_ECP_RESTARTABLE)
3616
Paul Elliotta1c94092023-02-15 16:38:04 +00003617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003618 mbedtls_mpi r;
3619 mbedtls_mpi s;
3620
Paul Elliott46845252023-02-03 14:59:11 +00003621 mbedtls_mpi_init(&r);
3622 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003623
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003624 /* Ensure max_ops is set to the current value (or default). */
3625 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3626
Paul Elliotta1c94092023-02-15 16:38:04 +00003627 if (signature_size < 2 * operation->coordinate_bytes) {
3628 status = PSA_ERROR_BUFFER_TOO_SMALL;
3629 goto exit;
3630 }
3631
Paul Elliott588f8ed2022-12-02 18:10:26 +00003632 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003633
Paul Elliott588f8ed2022-12-02 18:10:26 +00003634#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3635 status = mbedtls_to_psa_error(
3636 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003637 &r,
3638 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003639 &operation->ctx->d,
3640 operation->hash,
3641 operation->hash_length,
3642 operation->md_alg,
3643 mbedtls_psa_get_random,
3644 MBEDTLS_PSA_RANDOM_STATE,
3645 &operation->restart_ctx));
3646#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003647 status = PSA_ERROR_NOT_SUPPORTED;
3648 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003649#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3650 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003651 status = mbedtls_to_psa_error(
3652 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003653 &r,
3654 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003655 &operation->ctx->d,
3656 operation->hash,
3657 operation->hash_length,
3658 mbedtls_psa_get_random,
3659 MBEDTLS_PSA_RANDOM_STATE,
3660 mbedtls_psa_get_random,
3661 MBEDTLS_PSA_RANDOM_STATE,
3662 &operation->restart_ctx));
3663 }
3664
Paul Elliottf8e5b562023-02-19 18:43:45 +00003665 /* Hide the fact that the restart context only holds a delta of number of
3666 * ops done during the last operation, not an absolute value. */
3667 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3668
Paul Elliott724bd252023-02-08 12:35:08 +00003669 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003670 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003671 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003673 operation->coordinate_bytes)
3674 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675
3676 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003677 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678 }
3679
3680 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003681 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003683 operation->coordinate_bytes,
3684 operation->coordinate_bytes)
3685 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686
3687 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003688 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689 }
3690
Paul Elliott1bc59df2023-02-05 13:41:57 +00003691 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692
Paul Elliott724bd252023-02-08 12:35:08 +00003693 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003694 }
Paul Elliott724bd252023-02-08 12:35:08 +00003695
3696exit:
3697
3698 mbedtls_mpi_free(&r);
3699 mbedtls_mpi_free(&s);
3700 return status;
3701
Paul Elliott588f8ed2022-12-02 18:10:26 +00003702 #else
3703
3704 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003705 (void) signature;
3706 (void) signature_size;
3707 (void) signature_length;
3708
3709 return PSA_ERROR_NOT_SUPPORTED;
3710
3711#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3712 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3713 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3714}
3715
3716psa_status_t mbedtls_psa_sign_hash_abort(
3717 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3718{
3719
3720#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3721 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3722 defined(MBEDTLS_ECP_RESTARTABLE)
3723
3724 if (operation->ctx) {
3725 mbedtls_ecdsa_free(operation->ctx);
3726 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003727 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003728 }
3729
3730 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3731
Paul Elliott93d9ca82023-02-15 18:14:21 +00003732 operation->num_ops = 0;
3733
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734 return PSA_SUCCESS;
3735
3736#else
3737
3738 (void) operation;
3739
3740 return PSA_ERROR_NOT_SUPPORTED;
3741
3742#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3743 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3744 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3745}
3746
3747psa_status_t mbedtls_psa_verify_hash_start(
3748 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3749 const psa_key_attributes_t *attributes,
3750 const uint8_t *key_buffer, size_t key_buffer_size,
3751 psa_algorithm_t alg,
3752 const uint8_t *hash, size_t hash_length,
3753 const uint8_t *signature, size_t signature_length)
3754{
3755 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003756 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003757 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003758
Paul Elliott068fe072023-01-16 13:59:15 +00003759 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3760 return PSA_ERROR_NOT_SUPPORTED;
3761 }
3762
3763 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003764 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003765 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003766
3767#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003768 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3769 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770
Paul Elliotta1c94092023-02-15 16:38:04 +00003771 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3772 mbedtls_mpi_init(&operation->r);
3773 mbedtls_mpi_init(&operation->s);
3774
Paul Elliott93d9ca82023-02-15 18:14:21 +00003775 /* Ensure num_ops is zero'ed in case of context re-use. */
3776 operation->num_ops = 0;
3777
Paul Elliott068fe072023-01-16 13:59:15 +00003778 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3779 attributes->core.bits,
3780 key_buffer,
3781 key_buffer_size,
3782 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783
Paul Elliott068fe072023-01-16 13:59:15 +00003784 if (status != PSA_SUCCESS) {
3785 return status;
3786 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787
Paul Elliottc569fc22023-02-10 13:02:54 +00003788 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003789
Paul Elliott1bc59df2023-02-05 13:41:57 +00003790 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003791 return PSA_ERROR_INVALID_SIGNATURE;
3792 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003793
Paul Elliott068fe072023-01-16 13:59:15 +00003794 status = mbedtls_to_psa_error(
3795 mbedtls_mpi_read_binary(&operation->r,
3796 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003797 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798
Paul Elliott068fe072023-01-16 13:59:15 +00003799 if (status != PSA_SUCCESS) {
3800 return status;
3801 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802
Paul Elliott068fe072023-01-16 13:59:15 +00003803 status = mbedtls_to_psa_error(
3804 mbedtls_mpi_read_binary(&operation->s,
3805 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003806 coordinate_bytes,
3807 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003808
Paul Elliott068fe072023-01-16 13:59:15 +00003809 if (status != PSA_SUCCESS) {
3810 return status;
3811 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003812
Paul Elliott2c9843f2023-02-15 17:32:42 +00003813 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003814
Paul Elliott2c9843f2023-02-15 17:32:42 +00003815 if (status != PSA_SUCCESS) {
3816 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003817 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003818
Paul Elliott0290a762023-02-09 14:30:24 +00003819 /* We only need to store the same length of hash as the private key size
3820 * here, it would be truncated by the internal implementation anyway. */
3821 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3822 coordinate_bytes);
3823
Paul Elliottba70ad42023-02-15 18:23:53 +00003824 if (required_hash_length > sizeof(operation->hash)) {
3825 /* Shouldn't happen, but better safe than sorry. */
3826 return PSA_ERROR_CORRUPTION_DETECTED;
3827 }
3828
Paul Elliott0290a762023-02-09 14:30:24 +00003829 memcpy(operation->hash, hash, required_hash_length);
3830 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003831
3832 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003833#else
Paul Elliott068fe072023-01-16 13:59:15 +00003834 (void) operation;
3835 (void) key_buffer;
3836 (void) key_buffer_size;
3837 (void) alg;
3838 (void) hash;
3839 (void) hash_length;
3840 (void) signature;
3841 (void) signature_length;
3842 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003843 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003844 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845
Paul Elliott068fe072023-01-16 13:59:15 +00003846 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003847#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3848 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3849 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850}
3851
3852psa_status_t mbedtls_psa_verify_hash_complete(
3853 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3854{
3855
3856#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3857 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3858 defined(MBEDTLS_ECP_RESTARTABLE)
3859
Paul Elliottf8e5b562023-02-19 18:43:45 +00003860 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3861
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003862 /* Ensure max_ops is set to the current value (or default). */
3863 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3864
Paul Elliottf8e5b562023-02-19 18:43:45 +00003865 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003866 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3867 operation->hash,
3868 operation->hash_length,
3869 &operation->ctx->Q,
3870 &operation->r,
3871 &operation->s,
3872 &operation->restart_ctx));
3873
Paul Elliottf8e5b562023-02-19 18:43:45 +00003874 /* Hide the fact that the restart context only holds a delta of number of
3875 * ops done during the last operation, not an absolute value. */
3876 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3877
3878 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003879#else
3880 (void) operation;
3881
3882 return PSA_ERROR_NOT_SUPPORTED;
3883
3884#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3885 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3886 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3887}
3888
3889psa_status_t mbedtls_psa_verify_hash_abort(
3890 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3891{
3892
3893#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3894 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3895 defined(MBEDTLS_ECP_RESTARTABLE)
3896
3897 if (operation->ctx) {
3898 mbedtls_ecdsa_free(operation->ctx);
3899 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003900 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003901 }
3902
3903 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3904
Paul Elliott93d9ca82023-02-15 18:14:21 +00003905 operation->num_ops = 0;
3906
Paul Elliott588f8ed2022-12-02 18:10:26 +00003907 mbedtls_mpi_free(&operation->r);
3908 mbedtls_mpi_free(&operation->s);
3909
3910 return PSA_SUCCESS;
3911
3912#else
3913 (void) operation;
3914
3915 return PSA_ERROR_NOT_SUPPORTED;
3916
3917#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3918 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3919 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3920}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003921
mohammad1603503973b2018-03-12 15:59:30 +02003922/****************************************************************/
3923/* Symmetric cryptography */
3924/****************************************************************/
3925
Gilles Peskine449bd832023-01-11 14:50:10 +01003926static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3927 mbedtls_svc_key_id_t key,
3928 psa_algorithm_t alg,
3929 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003930{
3931 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3932 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003933 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3935 PSA_KEY_USAGE_ENCRYPT :
3936 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003937
3938 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003940 status = PSA_ERROR_BAD_STATE;
3941 goto exit;
3942 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003943
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003945 status = PSA_ERROR_INVALID_ARGUMENT;
3946 goto exit;
3947 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003948
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3950 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003953
Ronald Cron49fafa92021-03-10 08:34:23 +01003954 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003955 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003956 * so we only set it (in the driver wrapper) after resources have been
3957 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003958 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003959 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003960 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003961 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003962 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 }
3964 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003965
Ronald Crona4af55f2020-12-14 14:36:06 +01003966 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003968 };
3969
Ronald Cronab99ac22020-10-01 16:38:28 +02003970 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 if (cipher_operation == MBEDTLS_ENCRYPT) {
3972 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3973 &attributes,
3974 slot->key.data,
3975 slot->key.bytes,
3976 alg);
3977 } else {
3978 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3979 &attributes,
3980 slot->key.data,
3981 slot->key.bytes,
3982 alg);
3983 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003984
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003985exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 if (status != PSA_SUCCESS) {
3987 psa_cipher_abort(operation);
3988 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003989
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003993}
3994
Gilles Peskine449bd832023-01-11 14:50:10 +01003995psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3996 mbedtls_svc_key_id_t key,
3997 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003998{
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004000}
4001
Gilles Peskine449bd832023-01-11 14:50:10 +01004002psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4003 mbedtls_svc_key_id_t key,
4004 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004005{
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004007}
4008
Gilles Peskine449bd832023-01-11 14:50:10 +01004009psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4010 uint8_t *iv,
4011 size_t iv_size,
4012 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004013{
Ronald Cron6d051732020-10-01 14:10:20 +02004014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004015 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4016 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004017
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004019 status = PSA_ERROR_BAD_STATE;
4020 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004021 }
4022
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004024 status = PSA_ERROR_BAD_STATE;
4025 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004026 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004027
Ronald Cron2fb90522021-07-05 12:31:44 +02004028 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004030 status = PSA_ERROR_BUFFER_TOO_SMALL;
4031 goto exit;
4032 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004033
Gilles Peskine449bd832023-01-11 14:50:10 +01004034 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004035 status = PSA_ERROR_GENERIC_ERROR;
4036 goto exit;
4037 }
4038
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 status = psa_generate_random(local_iv, default_iv_length);
4040 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004041 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 }
Ronald Cron5618a392021-03-26 09:52:26 +01004043
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 status = psa_driver_wrapper_cipher_set_iv(operation,
4045 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004046
4047exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 if (status == PSA_SUCCESS) {
4049 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004050 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004051 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004053 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004055 }
Ronald Cron6d051732020-10-01 14:10:20 +02004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004058}
4059
Gilles Peskine449bd832023-01-11 14:50:10 +01004060psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4061 const uint8_t *iv,
4062 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004063{
Ronald Cron6d051732020-10-01 14:10:20 +02004064 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004065
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004067 status = PSA_ERROR_BAD_STATE;
4068 goto exit;
4069 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004070
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004072 status = PSA_ERROR_BAD_STATE;
4073 goto exit;
4074 }
Ronald Crona0d68172021-03-26 10:15:08 +01004075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004077 status = PSA_ERROR_INVALID_ARGUMENT;
4078 goto exit;
4079 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 status = psa_driver_wrapper_cipher_set_iv(operation,
4082 iv,
4083 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004084
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004085exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004087 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 } else {
4089 psa_cipher_abort(operation);
4090 }
4091 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004092}
4093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4095 const uint8_t *input,
4096 size_t input_length,
4097 uint8_t *output,
4098 size_t output_size,
4099 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004100{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004102
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004104 status = PSA_ERROR_BAD_STATE;
4105 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004106 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004107
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004109 status = PSA_ERROR_BAD_STATE;
4110 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004111 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 status = psa_driver_wrapper_cipher_update(operation,
4114 input,
4115 input_length,
4116 output,
4117 output_size,
4118 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004119
4120exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 if (status != PSA_SUCCESS) {
4122 psa_cipher_abort(operation);
4123 }
Ronald Cron6d051732020-10-01 14:10:20 +02004124
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004126}
4127
Gilles Peskine449bd832023-01-11 14:50:10 +01004128psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4129 uint8_t *output,
4130 size_t output_size,
4131 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004132{
David Saadab4ecc272019-02-14 13:48:10 +02004133 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004134
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004136 status = PSA_ERROR_BAD_STATE;
4137 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004138 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004141 status = PSA_ERROR_BAD_STATE;
4142 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004143 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 status = psa_driver_wrapper_cipher_finish(operation,
4146 output,
4147 output_size,
4148 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004149
4150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 if (status == PSA_SUCCESS) {
4152 return psa_cipher_abort(operation);
4153 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004154 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004158 }
mohammad1603503973b2018-03-12 15:59:30 +02004159}
4160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004162{
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004164 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004165 * in use. It's ok to call abort on such an object, and there's
4166 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004168 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004171
Ronald Cron49fafa92021-03-10 08:34:23 +01004172 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004173 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004174 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004177}
4178
Gilles Peskine449bd832023-01-11 14:50:10 +01004179psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4180 psa_algorithm_t alg,
4181 const uint8_t *input,
4182 size_t input_length,
4183 uint8_t *output,
4184 size_t output_size,
4185 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004186{
4187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004188 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004189 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004190 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4191 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004194 status = PSA_ERROR_INVALID_ARGUMENT;
4195 goto exit;
4196 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004197
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4199 PSA_KEY_USAGE_ENCRYPT,
4200 alg);
4201 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004202 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004204
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004205 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004207 };
4208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4210 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004211 status = PSA_ERROR_GENERIC_ERROR;
4212 goto exit;
4213 }
4214
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 if (default_iv_length > 0) {
4216 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004217 status = PSA_ERROR_BUFFER_TOO_SMALL;
4218 goto exit;
4219 }
4220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 status = psa_generate_random(local_iv, default_iv_length);
4222 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004223 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004225 }
4226
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004227 status = psa_driver_wrapper_cipher_encrypt(
4228 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004229 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 mbedtls_buffer_offset(output, default_iv_length),
4231 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004232
4233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 unlock_status = psa_unlock_key_slot(slot);
4235 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004236 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004237 }
Ronald Cron23919522021-07-08 19:00:07 +02004238
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 if (status == PSA_SUCCESS) {
4240 if (default_iv_length > 0) {
4241 memcpy(output, local_iv, default_iv_length);
4242 }
4243 *output_length += default_iv_length;
4244 } else {
4245 *output_length = 0;
4246 }
4247
4248 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004249}
4250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4252 psa_algorithm_t alg,
4253 const uint8_t *input,
4254 size_t input_length,
4255 uint8_t *output,
4256 size_t output_size,
4257 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004258{
4259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004260 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004261 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004264 status = PSA_ERROR_INVALID_ARGUMENT;
4265 goto exit;
4266 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4269 PSA_KEY_USAGE_DECRYPT,
4270 alg);
4271 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004272 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004274
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004275 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004277 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4280 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004281 status = PSA_ERROR_INVALID_ARGUMENT;
4282 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004284 status = PSA_ERROR_INVALID_ARGUMENT;
4285 goto exit;
4286 }
4287
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004288 status = psa_driver_wrapper_cipher_decrypt(
4289 &attributes, slot->key.data, slot->key.bytes,
4290 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004292
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 unlock_status = psa_unlock_key_slot(slot);
4295 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004296 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004298
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004300 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 }
Ronald Cron23919522021-07-08 19:00:07 +02004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004304}
4305
4306
mohammad16035955c982018-04-26 00:53:03 +03004307/****************************************************************/
4308/* AEAD */
4309/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004310
Paul Elliottbaff51c2021-09-28 17:44:45 +01004311/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004312static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004313{
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004315}
4316
4317/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004318static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4319 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004320{
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004322
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004324#if defined(PSA_WANT_ALG_GCM)
4325 case PSA_ALG_GCM:
4326 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 * arbitrarily large nonces. Please note that we do not generally
4328 * recommend the usage of nonces of greater length than
4329 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4330 * size, which can then lead to collisions if you encrypt a very
4331 * large number of messages.*/
4332 if (nonce_length != 0) {
4333 return PSA_SUCCESS;
4334 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004335 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004336#endif /* PSA_WANT_ALG_GCM */
4337#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004338 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 if (nonce_length >= 7 && nonce_length <= 13) {
4340 return PSA_SUCCESS;
4341 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004342 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004343#endif /* PSA_WANT_ALG_CCM */
4344#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004345 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 if (nonce_length == 12) {
4347 return PSA_SUCCESS;
4348 } else if (nonce_length == 8) {
4349 return PSA_ERROR_NOT_SUPPORTED;
4350 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004351 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004352#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004353 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004354 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004356 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004359}
4360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004362{
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4364 return PSA_ERROR_INVALID_ARGUMENT;
4365 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004368}
4369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4371 psa_algorithm_t alg,
4372 const uint8_t *nonce,
4373 size_t nonce_length,
4374 const uint8_t *additional_data,
4375 size_t additional_data_length,
4376 const uint8_t *plaintext,
4377 size_t plaintext_length,
4378 uint8_t *ciphertext,
4379 size_t ciphertext_size,
4380 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004381{
Ronald Cron215633c2021-03-16 17:15:37 +01004382 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4383 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004384
mohammad1603f08a5502018-06-03 15:05:47 +03004385 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 status = psa_aead_check_algorithm(alg);
4388 if (status != PSA_SUCCESS) {
4389 return status;
4390 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004391
Ronald Cron9a986162021-03-26 12:40:07 +01004392 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4394 if (status != PSA_SUCCESS) {
4395 return status;
4396 }
mohammad16035955c982018-04-26 00:53:03 +03004397
Ronald Cron215633c2021-03-16 17:15:37 +01004398 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004400 };
mohammad16035955c982018-04-26 00:53:03 +03004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 status = psa_aead_check_nonce_length(alg, nonce_length);
4403 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004404 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004406
Ronald Cronde822812021-03-17 16:08:20 +01004407 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004408 &attributes, slot->key.data, slot->key.bytes,
4409 alg,
4410 nonce, nonce_length,
4411 additional_data, additional_data_length,
4412 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4416 memset(ciphertext, 0, ciphertext_size);
4417 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004418
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004419exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004423}
4424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4426 psa_algorithm_t alg,
4427 const uint8_t *nonce,
4428 size_t nonce_length,
4429 const uint8_t *additional_data,
4430 size_t additional_data_length,
4431 const uint8_t *ciphertext,
4432 size_t ciphertext_length,
4433 uint8_t *plaintext,
4434 size_t plaintext_size,
4435 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004436{
Ronald Cron215633c2021-03-16 17:15:37 +01004437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4438 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004439
Gilles Peskineee652a32018-06-01 19:23:52 +02004440 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 status = psa_aead_check_algorithm(alg);
4443 if (status != PSA_SUCCESS) {
4444 return status;
4445 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004446
Ronald Cron9a986162021-03-26 12:40:07 +01004447 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4449 if (status != PSA_SUCCESS) {
4450 return status;
4451 }
mohammad16035955c982018-04-26 00:53:03 +03004452
Ronald Cron215633c2021-03-16 17:15:37 +01004453 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004455 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 status = psa_aead_check_nonce_length(alg, nonce_length);
4458 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004459 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004461
Ronald Cronde822812021-03-17 16:08:20 +01004462 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004463 &attributes, slot->key.data, slot->key.bytes,
4464 alg,
4465 nonce, nonce_length,
4466 additional_data, additional_data_length,
4467 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 if (status != PSA_SUCCESS && plaintext_size != 0) {
4471 memset(plaintext, 0, plaintext_size);
4472 }
mohammad160360a64d02018-06-03 17:20:42 +03004473
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004474exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 return status;
mohammad16035955c982018-04-26 00:53:03 +03004478}
4479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4481{
4482 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004483
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004485#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004487 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4489 return PSA_ERROR_INVALID_ARGUMENT;
4490 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004491 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004492#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004493
Przemek Stekiel86679c72022-10-06 17:06:56 +02004494#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004496 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4498 return PSA_ERROR_INVALID_ARGUMENT;
4499 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004500 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004501#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004502
Przemek Stekiel86679c72022-10-06 17:06:56 +02004503#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004505 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 if (tag_len != 16) {
4507 return PSA_ERROR_INVALID_ARGUMENT;
4508 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004509 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004510#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004511
4512 default:
4513 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004515 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004517}
4518
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004519/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004520static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4521 int is_encrypt,
4522 mbedtls_svc_key_id_t key,
4523 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004524{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004525 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4526 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4527 psa_key_slot_t *slot = NULL;
4528 psa_key_usage_t key_usage = 0;
4529
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 status = psa_aead_check_algorithm(alg);
4531 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004532 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004536 status = PSA_ERROR_BAD_STATE;
4537 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004538 }
4539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 if (operation->nonce_set || operation->lengths_set ||
4541 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004542 status = PSA_ERROR_BAD_STATE;
4543 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004544 }
4545
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004547 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004549 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4553 alg);
4554 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004555 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004557
4558 psa_key_attributes_t attributes = {
4559 .core = slot->attr
4560 };
4561
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004563 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 if (is_encrypt) {
4567 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4568 &attributes,
4569 slot->key.data,
4570 slot->key.bytes,
4571 alg);
4572 } else {
4573 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4574 &attributes,
4575 slot->key.data,
4576 slot->key.bytes,
4577 alg);
4578 }
4579 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004580 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004584
4585exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004589 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004591 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 } else {
4593 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004594 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004597}
4598
Paul Elliott302ff6b2021-04-20 18:10:30 +01004599/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004600psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4601 mbedtls_svc_key_id_t key,
4602 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004603{
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004605}
4606
4607/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004608psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4609 mbedtls_svc_key_id_t key,
4610 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004611{
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004613}
4614
4615/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004616psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4617 uint8_t *nonce,
4618 size_t nonce_size,
4619 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004620{
4621 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004622 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004623 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004624
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004625 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004628 status = PSA_ERROR_BAD_STATE;
4629 goto exit;
4630 }
4631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004633 status = PSA_ERROR_BAD_STATE;
4634 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004635 }
4636
Paul Elliotte193ea82021-10-01 13:00:16 +01004637 /* For CCM, this size may not be correct according to the PSA
4638 * specification. The PSA Crypto 1.0.1 specification states:
4639 *
4640 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4641 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4642 *
4643 * However this restriction that L has to be the smallest integer is not
4644 * applied in practice, and it is not implementable here since the
4645 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4647 operation->alg);
4648 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004649 status = PSA_ERROR_BUFFER_TOO_SMALL;
4650 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004651 }
4652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 status = psa_generate_random(local_nonce, required_nonce_size);
4654 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004655 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004657
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004659
Paul Elliott39dc6b82021-05-11 19:16:09 +01004660exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 if (status == PSA_SUCCESS) {
4662 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004663 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 } else {
4665 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004666 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004669}
4670
4671/* Set the nonce for a multipart authenticated encryption or decryption
4672 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004673psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4674 const uint8_t *nonce,
4675 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004676{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004677 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004680 status = PSA_ERROR_BAD_STATE;
4681 goto exit;
4682 }
4683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004685 status = PSA_ERROR_BAD_STATE;
4686 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004687 }
4688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4690 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004691 status = PSA_ERROR_INVALID_ARGUMENT;
4692 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004693 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4696 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004697
Paul Elliott39dc6b82021-05-11 19:16:09 +01004698exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004700 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 } else {
4702 psa_aead_abort(operation);
4703 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004706}
4707
4708/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004709psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4710 size_t ad_length,
4711 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004712{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004713 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004716 status = PSA_ERROR_BAD_STATE;
4717 goto exit;
4718 }
4719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (operation->lengths_set || operation->ad_started ||
4721 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004722 status = PSA_ERROR_BAD_STATE;
4723 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004724 }
4725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004727#if defined(PSA_WANT_ALG_GCM)
4728 case PSA_ALG_GCM:
4729 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 * bits. Without the guard this code will generate warnings on 32bit
4731 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004732#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 if (((uint64_t) ad_length) >> 61 != 0 ||
4734 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004735 status = PSA_ERROR_INVALID_ARGUMENT;
4736 goto exit;
4737 }
Paul Elliott325d3742021-09-27 17:56:28 +01004738#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004739 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004740#endif /* PSA_WANT_ALG_GCM */
4741#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004742 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004744 status = PSA_ERROR_INVALID_ARGUMENT;
4745 goto exit;
4746 }
4747 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004748#endif /* PSA_WANT_ALG_CCM */
4749#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004750 case PSA_ALG_CHACHA20_POLY1305:
4751 /* No length restrictions for ChaChaPoly. */
4752 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004753#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004754 default:
4755 break;
4756 }
Paul Elliott325d3742021-09-27 17:56:28 +01004757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4759 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004760
Paul Elliott39dc6b82021-05-11 19:16:09 +01004761exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004763 operation->ad_remaining = ad_length;
4764 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004765 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 } else {
4767 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004768 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004772
4773/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004774psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4775 const uint8_t *input,
4776 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004777{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004778 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4779
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004781 status = PSA_ERROR_BAD_STATE;
4782 goto exit;
4783 }
4784
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004786 status = PSA_ERROR_BAD_STATE;
4787 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004788 }
4789
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 if (operation->lengths_set) {
4791 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004792 status = PSA_ERROR_INVALID_ARGUMENT;
4793 goto exit;
4794 }
4795
4796 operation->ad_remaining -= input_length;
4797 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004798#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004800 status = PSA_ERROR_BAD_STATE;
4801 goto exit;
4802 }
4803#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 status = psa_driver_wrapper_aead_update_ad(operation, input,
4806 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004807
Paul Elliott39dc6b82021-05-11 19:16:09 +01004808exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004810 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 } else {
4812 psa_aead_abort(operation);
4813 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004816}
4817
4818/* Encrypt or decrypt a message fragment in an active multipart AEAD
4819 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004820psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4821 const uint8_t *input,
4822 size_t input_length,
4823 uint8_t *output,
4824 size_t output_size,
4825 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004826{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004827 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004828
4829 *output_length = 0;
4830
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004832 status = PSA_ERROR_BAD_STATE;
4833 goto exit;
4834 }
4835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004837 status = PSA_ERROR_BAD_STATE;
4838 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004839 }
4840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004842 /* Additional data length was supplied, but not all the additional
4843 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004845 status = PSA_ERROR_INVALID_ARGUMENT;
4846 goto exit;
4847 }
4848
4849 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004851 status = PSA_ERROR_INVALID_ARGUMENT;
4852 goto exit;
4853 }
4854
4855 operation->body_remaining -= input_length;
4856 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004857#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004859 status = PSA_ERROR_BAD_STATE;
4860 goto exit;
4861 }
4862#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4865 output, output_size,
4866 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004867
Paul Elliott39dc6b82021-05-11 19:16:09 +01004868exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004870 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 } else {
4872 psa_aead_abort(operation);
4873 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004876}
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004879{
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (operation->id == 0 || !operation->nonce_set) {
4881 return PSA_ERROR_BAD_STATE;
4882 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4885 operation->body_remaining != 0)) {
4886 return PSA_ERROR_INVALID_ARGUMENT;
4887 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004890}
4891
Paul Elliott302ff6b2021-04-20 18:10:30 +01004892/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004893psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4894 uint8_t *ciphertext,
4895 size_t ciphertext_size,
4896 size_t *ciphertext_length,
4897 uint8_t *tag,
4898 size_t tag_size,
4899 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004900{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4902
Paul Elliott302ff6b2021-04-20 18:10:30 +01004903 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004904 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004905
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 status = psa_aead_final_checks(operation);
4907 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004908 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004912 status = PSA_ERROR_BAD_STATE;
4913 goto exit;
4914 }
4915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4917 ciphertext_size,
4918 ciphertext_length,
4919 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004920
Paul Elliott39dc6b82021-05-11 19:16:09 +01004921exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004922
4923
Paul Elliottf47b0952021-05-21 18:02:33 +01004924 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004925 * the zero tag size, make sure the tag is set to something implausible.
4926 * Even if the operation succeeds, make sure we clear the rest of the
4927 * buffer to prevent potential leakage of anything previously placed in
4928 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004929 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004930
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004934}
4935
4936/* Finish authenticating and decrypting a message in a multipart AEAD
4937 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004938psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4939 uint8_t *plaintext,
4940 size_t plaintext_size,
4941 size_t *plaintext_length,
4942 const uint8_t *tag,
4943 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004944{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004945 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4946
Paul Elliott302ff6b2021-04-20 18:10:30 +01004947 *plaintext_length = 0;
4948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 status = psa_aead_final_checks(operation);
4950 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004955 status = PSA_ERROR_BAD_STATE;
4956 goto exit;
4957 }
4958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4960 plaintext_size,
4961 plaintext_length,
4962 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004963
4964exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004966
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004968}
4969
4970/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004971psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004972{
4973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4974
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004976 /* The object has (apparently) been initialized but it is not (yet)
4977 * in use. It's ok to call abort on such an object, and there's
4978 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004980 }
4981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004987}
4988
Gilles Peskinee59236f2018-01-27 23:32:46 +01004989/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004990/* Generators */
4991/****************************************************************/
4992
Przemek Stekiel69c46792022-06-10 12:59:51 +02004993#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004994 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004995 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4996 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004997#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004998#endif /* At least one builtin KDF */
4999
Przemek Stekiel69c46792022-06-10 12:59:51 +02005000#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005001 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5002 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5003static psa_status_t psa_key_derivation_start_hmac(
5004 psa_mac_operation_t *operation,
5005 psa_algorithm_t hash_alg,
5006 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005008{
5009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5012 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5013 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005014
Steven Cooreman72f736a2021-05-07 14:14:37 +02005015 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005017
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 status = psa_driver_wrapper_mac_sign_setup(operation,
5019 &attributes,
5020 hmac_key, hmac_key_length,
5021 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005022
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 psa_reset_key_attributes(&attributes);
5024 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005025}
5026#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005027
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005028#define HKDF_STATE_INIT 0 /* no input yet */
5029#define HKDF_STATE_STARTED 1 /* got salt */
5030#define HKDF_STATE_KEYED 2 /* got key */
5031#define HKDF_STATE_OUTPUT 3 /* output started */
5032
Gilles Peskinecbe66502019-05-16 16:59:18 +02005033static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005035{
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5037 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5038 } else {
5039 return operation->alg;
5040 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005041}
5042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005044{
5045 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5047 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005048 /* The object has (apparently) been initialized but it is not
5049 * in use. It's ok to call abort on such an object, and there's
5050 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005052#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5054 mbedtls_free(operation->ctx.hkdf.info);
5055 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5056 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005057#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005058#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5059 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5061 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5062 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5063 if (operation->ctx.tls12_prf.secret != NULL) {
5064 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5065 operation->ctx.tls12_prf.secret_length);
5066 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005067 }
5068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 if (operation->ctx.tls12_prf.seed != NULL) {
5070 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5071 operation->ctx.tls12_prf.seed_length);
5072 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005073 }
5074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 if (operation->ctx.tls12_prf.label != NULL) {
5076 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5077 operation->ctx.tls12_prf.label_length);
5078 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005079 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005080#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 if (operation->ctx.tls12_prf.other_secret != NULL) {
5082 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5083 operation->ctx.tls12_prf.other_secret_length);
5084 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005085 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005086#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005087 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005088
5089 /* We leave the fields Ai and output_block to be erased safely by the
5090 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005092#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5093 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005094#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5096 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5097 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5098 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005099#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005100 {
5101 status = PSA_ERROR_BAD_STATE;
5102 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 mbedtls_platform_zeroize(operation, sizeof(*operation));
5104 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005105}
5106
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005107psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005109{
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005111 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005113 }
5114
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005117}
5118
Gilles Peskine449bd832023-01-11 14:50:10 +01005119psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5120 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005121{
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 if (operation->alg == 0) {
5123 return PSA_ERROR_BAD_STATE;
5124 }
5125 if (capacity > operation->capacity) {
5126 return PSA_ERROR_INVALID_ARGUMENT;
5127 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005130}
5131
Przemek Stekiel69c46792022-06-10 12:59:51 +02005132#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005133/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005134static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5135 psa_algorithm_t kdf_alg,
5136 uint8_t *output,
5137 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005138{
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5140 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005141 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005142 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005143#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005145#else
5146 const uint8_t last_block = 0xff;
5147#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 if (hkdf->state < HKDF_STATE_KEYED ||
5150 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005151#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005153#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 )) {
5155 return PSA_ERROR_BAD_STATE;
5156 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005157 hkdf->state = HKDF_STATE_OUTPUT;
5158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005160 /* Copy what remains of the current block */
5161 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005163 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 }
5165 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005166 output += n;
5167 output_length -= n;
5168 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005170 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005172 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005173 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005175 * object was corrupted or if this function is called directly
5176 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 if (hkdf->block_number == last_block) {
5178 return PSA_ERROR_BAD_STATE;
5179 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005180
5181 /* We need a new block */
5182 ++hkdf->block_number;
5183 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5186 hash_alg,
5187 hkdf->prk,
5188 hash_length);
5189 if (status != PSA_SUCCESS) {
5190 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005191 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005192
5193 if (hkdf->block_number != 1) {
5194 status = psa_mac_update(&hkdf->hmac,
5195 hkdf->output_block,
5196 hash_length);
5197 if (status != PSA_SUCCESS) {
5198 return status;
5199 }
5200 }
5201 status = psa_mac_update(&hkdf->hmac,
5202 hkdf->info,
5203 hkdf->info_length);
5204 if (status != PSA_SUCCESS) {
5205 return status;
5206 }
5207 status = psa_mac_update(&hkdf->hmac,
5208 &hkdf->block_number, 1);
5209 if (status != PSA_SUCCESS) {
5210 return status;
5211 }
5212 status = psa_mac_sign_finish(&hkdf->hmac,
5213 hkdf->output_block,
5214 sizeof(hkdf->output_block),
5215 &hmac_output_length);
5216 if (status != PSA_SUCCESS) {
5217 return status;
5218 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005219 }
5220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005222}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005223#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005224
John Durkop07cc04a2020-11-16 22:08:34 -08005225#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5226 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005227static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5228 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005230{
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5232 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005233 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5234 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005235 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005236
Janos Follath7742fee2019-06-17 12:58:10 +01005237 /* We can't be wanting more output after block 0xff, otherwise
5238 * the capacity check in psa_key_derivation_output_bytes() would have
5239 * prevented this call. It could happen only if the operation
5240 * object was corrupted or if this function is called directly
5241 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 if (tls12_prf->block_number == 0xff) {
5243 return PSA_ERROR_CORRUPTION_DETECTED;
5244 }
Janos Follath7742fee2019-06-17 12:58:10 +01005245
5246 /* We need a new block */
5247 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005248 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005249
5250 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5251 *
5252 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5253 *
5254 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5255 * HMAC_hash(secret, A(2) + seed) +
5256 * HMAC_hash(secret, A(3) + seed) + ...
5257 *
5258 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005259 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005260 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005261 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005262 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005263 * is currently extracted as `output_block` and where i is
5264 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005265 */
5266
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 status = psa_key_derivation_start_hmac(&hmac,
5268 hash_alg,
5269 tls12_prf->secret,
5270 tls12_prf->secret_length);
5271 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005272 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005274
5275 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005277 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5278 * the variable seed and in this instance means it in the context of the
5279 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 status = psa_mac_update(&hmac,
5281 tls12_prf->label,
5282 tls12_prf->label_length);
5283 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005284 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 }
5286 status = psa_mac_update(&hmac,
5287 tls12_prf->seed,
5288 tls12_prf->seed_length);
5289 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005290 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 }
5292 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005293 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5295 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005296 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005298 }
5299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 status = psa_mac_sign_finish(&hmac,
5301 tls12_prf->Ai, hash_length,
5302 &hmac_output_length);
5303 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005304 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 }
5306 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005307 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005309
5310 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 status = psa_key_derivation_start_hmac(&hmac,
5312 hash_alg,
5313 tls12_prf->secret,
5314 tls12_prf->secret_length);
5315 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005316 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 }
5318 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5319 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005320 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 }
5322 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5323 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005324 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 }
5326 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5327 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005328 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 }
5330 status = psa_mac_sign_finish(&hmac,
5331 tls12_prf->output_block, hash_length,
5332 &hmac_output_length);
5333 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005334 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005336
Janos Follath7742fee2019-06-17 12:58:10 +01005337
5338cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 cleanup_status = psa_mac_abort(&hmac);
5340 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005341 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005343
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005345}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005346
Janos Follath844eb0e2019-06-19 12:10:49 +01005347static psa_status_t psa_key_derivation_tls12_prf_read(
5348 psa_tls12_prf_key_derivation_t *tls12_prf,
5349 psa_algorithm_t alg,
5350 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005352{
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5354 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005355 psa_status_t status;
5356 uint8_t offset, length;
5357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005359 case PSA_TLS12_PRF_STATE_LABEL_SET:
5360 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5361 break;
5362 case PSA_TLS12_PRF_STATE_OUTPUT:
5363 break;
5364 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005366 }
5367
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005369 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 if (tls12_prf->left_in_block == 0) {
5371 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5372 alg);
5373 if (status != PSA_SUCCESS) {
5374 return status;
5375 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005376
5377 continue;
5378 }
5379
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005381 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005383 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005385
5386 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005388 output += length;
5389 output_length -= length;
5390 tls12_prf->left_in_block -= length;
5391 }
5392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005394}
John Durkop07cc04a2020-11-16 22:08:34 -08005395#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5396 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005397
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005398#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5399static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5400 psa_tls12_ecjpake_to_pms_t *ecjpake,
5401 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005403{
5404 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005405 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 if (output_length != 32) {
5408 return PSA_ERROR_INVALID_ARGUMENT;
5409 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005410
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5412 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5413 &output_size);
5414 if (status != PSA_SUCCESS) {
5415 return status;
5416 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005417
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 if (output_size != output_length) {
5419 return PSA_ERROR_GENERIC_ERROR;
5420 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005421
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005423}
5424#endif
5425
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005426psa_status_t psa_key_derivation_output_bytes(
5427 psa_key_derivation_operation_t *operation,
5428 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005430{
5431 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005435 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005437 }
5438
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005440 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005441 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005443 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005444 goto exit;
5445 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005447 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005448 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005449 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5450 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005451 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005452 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005454 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005455 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005456
Przemek Stekiel69c46792022-06-10 12:59:51 +02005457#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5459 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5460 output, output_length);
5461 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005462#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005463#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5464 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5466 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5467 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5468 kdf_alg, output,
5469 output_length);
5470 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005471#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5472 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005473#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005475 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5477 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005478#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5479
Gilles Peskineeab56e42018-07-12 17:12:33 +02005480 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005481 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005483 }
5484
5485exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005487 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 * This allows us to differentiate between exhausted operations and
5489 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5490 * operations. */
5491 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005493 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005495 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005497}
5498
David Brown7807bf72021-02-09 16:28:23 -07005499#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005500static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005501{
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 if (data_size >= 8) {
5503 mbedtls_des_key_set_parity(data);
5504 }
5505 if (data_size >= 16) {
5506 mbedtls_des_key_set_parity(data + 8);
5507 }
5508 if (data_size >= 24) {
5509 mbedtls_des_key_set_parity(data + 16);
5510 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005511}
David Brown7807bf72021-02-09 16:28:23 -07005512#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005513
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005514/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 * ECC keys on a Weierstrass elliptic curve require the generation
5516 * of a private key which is an integer
5517 * in the range [1, N - 1], where N is the boundary of the private key domain:
5518 * N is the prime p for Diffie-Hellman, or the order of the
5519 * curve’s base point for ECC.
5520 *
5521 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5522 * This function generates the private key using the following process:
5523 *
5524 * 1. Draw a byte string of length ceiling(m/8) bytes.
5525 * 2. If m is not a multiple of 8, set the most significant
5526 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5527 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5528 * 4. If k > N - 2, discard the result and return to step 1.
5529 * 5. Output k + 1 as the private key.
5530 *
5531 * This method allows compliance to NIST standards, specifically the methods titled
5532 * Key-Pair Generation by Testing Candidates in the following publications:
5533 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5534 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5535 * Diffie-Hellman keys.
5536 *
5537 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5538 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5539 *
5540 * Note: Function allocates memory for *data buffer, so given *data should be
5541 * always NULL.
5542 */
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005543#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5544 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5545 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5546 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5547 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005548static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005549 psa_key_slot_t *slot,
5550 size_t bits,
5551 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005552 uint8_t **data
5553 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005554{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005555 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005556 mbedtls_mpi k;
5557 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005560
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 mbedtls_mpi_init(&k);
5562 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005563
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005564 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005566 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005570 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005571 goto cleanup;
5572 }
5573
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005574 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005576
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005578
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005579 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005580 /* Let m be the bit size of N. */
5581 size_t m = ecp_group.nbits;
5582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005584
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005585 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005587
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005588 /* Note: This function is always called with *data == NULL and it
5589 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 *data = mbedtls_calloc(1, m_bytes);
5591 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005592 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005593 goto cleanup;
5594 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005597 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005599 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005601
5602 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005604 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005605 * (8 * ceiling(m/8) - m) bits of the first byte in
5606 * the string to zero.
5607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005609 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005610 }
5611
5612 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 * big-endian byte string.
5614 */
5615 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005616
5617 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 * Result of comparison is returned. When it indicates error
5619 * then this function is called again.
5620 */
5621 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005622 }
5623
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005624 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5626 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005627cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 if (ret != 0) {
5629 status = mbedtls_to_psa_error(ret);
5630 }
5631 if (status != PSA_SUCCESS) {
5632 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005633 *data = NULL;
5634 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 mbedtls_mpi_free(&k);
5636 mbedtls_mpi_free(&diff_N_2);
5637 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005638}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005639
5640/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5641 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5642 *
5643 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5644 * draw a 32-byte string and process it as specified in
5645 * Elliptic Curves for Security [RFC7748] §5.
5646 *
5647 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5648 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5649 *
5650 * Note: Function allocates memory for *data buffer, so given *data should be
5651 * always NULL.
5652 */
5653
5654static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5655 size_t bits,
5656 psa_key_derivation_operation_t *operation,
5657 uint8_t **data
5658 )
5659{
5660 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005661 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005662
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005664 case 255:
5665 output_length = 32;
5666 break;
5667 case 448:
5668 output_length = 56;
5669 break;
5670 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005672 break;
5673 }
5674
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005676
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 if (*data == NULL) {
5678 return PSA_ERROR_INSUFFICIENT_MEMORY;
5679 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005680
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005682
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005684 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005688 case 255:
5689 (*data)[0] &= 248;
5690 (*data)[31] &= 127;
5691 (*data)[31] |= 64;
5692 break;
5693 case 448:
5694 (*data)[0] &= 252;
5695 (*data)[55] |= 128;
5696 break;
5697 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005699 break;
5700 }
5701
5702 return status;
5703}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005704#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5705 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5706 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5707 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5708 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005709
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005710static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005711 psa_key_slot_t *slot,
5712 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005714{
5715 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305717 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5721 return PSA_ERROR_INVALID_ARGUMENT;
5722 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005723
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005724#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5725 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5726 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5727 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5728 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5730 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5731 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005732 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5734 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005735 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 }
5737 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005738 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5740 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005741 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005743 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005744 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005745#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5746 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5747 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5748 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5749 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 if (key_type_is_raw_bytes(slot->attr.type)) {
5751 if (bits % 8 != 0) {
5752 return PSA_ERROR_INVALID_ARGUMENT;
5753 }
5754 data = mbedtls_calloc(1, bytes);
5755 if (data == NULL) {
5756 return PSA_ERROR_INSUFFICIENT_MEMORY;
5757 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005758
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 status = psa_key_derivation_output_bytes(operation, data, bytes);
5760 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005761 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 }
David Brown12ca5032021-02-11 11:02:00 -07005763#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5765 psa_des_set_key_parity(data, bytes);
5766 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005767#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 } else {
5769 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005770 }
Ronald Crondd04d422020-11-28 15:14:42 +01005771
Ronald Cronbf33c932020-11-28 18:06:53 +01005772 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005773 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005775 };
5776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5778 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5779 &storage_size);
5780 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305781 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 }
Archanad8a83dc2021-06-14 10:04:16 +05305783 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 status = psa_allocate_buffer_to_slot(slot, storage_size);
5785 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305786 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 }
Archanad8a83dc2021-06-14 10:04:16 +05305788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 status = psa_driver_wrapper_import_key(&attributes,
5790 data, bytes,
5791 slot->key.data,
5792 slot->key.bytes,
5793 &slot->key.bytes, &bits);
5794 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005795 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005797
5798exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 mbedtls_free(data);
5800 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005801}
5802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5804 psa_key_derivation_operation_t *operation,
5805 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005806{
5807 psa_status_t status;
5808 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005809 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005810
Ronald Cron81709fc2020-11-14 12:10:32 +01005811 *key = MBEDTLS_SVC_KEY_ID_INIT;
5812
Gilles Peskine0f84d622019-09-12 19:03:13 +02005813 /* Reject any attempt to create a zero-length key so that we don't
5814 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 if (psa_get_key_bits(attributes) == 0) {
5816 return PSA_ERROR_INVALID_ARGUMENT;
5817 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005818
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 if (operation->alg == PSA_ALG_NONE) {
5820 return PSA_ERROR_BAD_STATE;
5821 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005822
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 if (!operation->can_output_key) {
5824 return PSA_ERROR_NOT_PERMITTED;
5825 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5828 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005829#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005831 /* Deriving a key in a secure element is not implemented yet. */
5832 status = PSA_ERROR_NOT_SUPPORTED;
5833 }
5834#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 if (status == PSA_SUCCESS) {
5836 status = psa_generate_derived_key_internal(slot,
5837 attributes->core.bits,
5838 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005839 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 if (status == PSA_SUCCESS) {
5841 status = psa_finish_key_creation(slot, driver, key);
5842 }
5843 if (status != PSA_SUCCESS) {
5844 psa_fail_key_creation(slot, driver);
5845 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005848}
5849
Gilles Peskineeab56e42018-07-12 17:12:33 +02005850
5851
5852/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005853/* Key derivation */
5854/****************************************************************/
5855
Steven Cooreman932ffb72021-02-15 12:14:32 +01005856#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005857static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005858{
5859#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5861 return 1;
5862 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005863#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005864#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5866 return 1;
5867 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005868#endif
5869#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5871 return 1;
5872 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005873#endif
5874#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5876 return 1;
5877 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005878#endif
5879#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5881 return 1;
5882 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005883#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005884#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5886 return 1;
5887 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005888#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005890}
5891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005893{
5894 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 psa_status_t status = psa_hash_setup(&operation, alg);
5896 psa_hash_abort(&operation);
5897 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005898}
5899
Gilles Peskine969c5d62019-01-16 15:53:06 +01005900static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005901 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005903{
Janos Follath5fe19732019-06-20 15:09:30 +01005904 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5905 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005907
Gilles Peskine969c5d62019-01-16 15:53:06 +01005908 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 if (!is_kdf_alg_supported(kdf_alg)) {
5910 return PSA_ERROR_NOT_SUPPORTED;
5911 }
John Durkop07cc04a2020-11-16 22:08:34 -08005912
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005913 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005914 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5916 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5917 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5918 if (hash_size == 0) {
5919 return PSA_ERROR_NOT_SUPPORTED;
5920 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005921
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005922 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5923 * we might fail later, which is somewhat unfriendly and potentially
5924 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 psa_status_t status = psa_hash_try_support(hash_alg);
5926 if (status != PSA_SUCCESS) {
5927 return status;
5928 }
5929 } else {
5930 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005931 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5934 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5935 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5936 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005937 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005938#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005939 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5941 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005942 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005944#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5945 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 operation->capacity = 255 * hash_size;
5947 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005948}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005949
Gilles Peskine449bd832023-01-11 14:50:10 +01005950static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005951{
5952#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 if (alg == PSA_ALG_ECDH) {
5954 return PSA_SUCCESS;
5955 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005956#endif
5957 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005959}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005960
5961static int psa_key_derivation_allows_free_form_secret_input(
5962 psa_algorithm_t kdf_alg)
5963{
5964#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5965 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5966 return 0;
5967 }
5968#endif
5969 (void) kdf_alg;
5970 return 1;
5971}
John Durkop07cc04a2020-11-16 22:08:34 -08005972#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5975 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005976{
5977 psa_status_t status;
5978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 if (operation->alg != 0) {
5980 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005981 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5984 return PSA_ERROR_INVALID_ARGUMENT;
5985 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5986#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5987 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5988 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5989 status = psa_key_agreement_try_support(ka_alg);
5990 if (status != PSA_SUCCESS) {
5991 return status;
5992 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005993 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
5994 return PSA_ERROR_INVALID_ARGUMENT;
5995 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
5997#else
5998 return PSA_ERROR_NOT_SUPPORTED;
5999#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6000 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6001#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6002 status = psa_key_derivation_setup_kdf(operation, alg);
6003#else
6004 return PSA_ERROR_NOT_SUPPORTED;
6005#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6006 } else {
6007 return PSA_ERROR_INVALID_ARGUMENT;
6008 }
6009
6010 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006011 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 }
6013 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006014}
6015
Przemek Stekiel69c46792022-06-10 12:59:51 +02006016#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006017static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6018 psa_algorithm_t kdf_alg,
6019 psa_key_derivation_step_t step,
6020 const uint8_t *data,
6021 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006022{
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006024 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006026 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006027#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6029 return PSA_ERROR_INVALID_ARGUMENT;
6030 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006031#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 if (hkdf->state != HKDF_STATE_INIT) {
6033 return PSA_ERROR_BAD_STATE;
6034 } else {
6035 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6036 hash_alg,
6037 data, data_length);
6038 if (status != PSA_SUCCESS) {
6039 return status;
6040 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006041 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006043 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006044 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006045#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006047 /* We shouldn't be in different state as HKDF_EXPAND only allows
6048 * two inputs: SECRET (this case) and INFO which does not modify
6049 * the state. It could happen only if the hkdf
6050 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 if (hkdf->state != HKDF_STATE_INIT) {
6052 return PSA_ERROR_BAD_STATE;
6053 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006054
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006055 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6057 return PSA_ERROR_INVALID_ARGUMENT;
6058 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 memcpy(hkdf->prk, data, data_length);
6061 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006062#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006063 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006064 /* HKDF: If no salt was provided, use an empty salt.
6065 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006067#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6069 return PSA_ERROR_BAD_STATE;
6070 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006071#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6073 hash_alg,
6074 NULL, 0);
6075 if (status != PSA_SUCCESS) {
6076 return status;
6077 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006078 hkdf->state = HKDF_STATE_STARTED;
6079 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (hkdf->state != HKDF_STATE_STARTED) {
6081 return PSA_ERROR_BAD_STATE;
6082 }
6083 status = psa_mac_update(&hkdf->hmac,
6084 data, data_length);
6085 if (status != PSA_SUCCESS) {
6086 return status;
6087 }
6088 status = psa_mac_sign_finish(&hkdf->hmac,
6089 hkdf->prk,
6090 sizeof(hkdf->prk),
6091 &data_length);
6092 if (status != PSA_SUCCESS) {
6093 return status;
6094 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006095 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006096
Gilles Peskine2b522db2019-04-12 15:11:49 +02006097 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006098 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006099#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006101 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006103 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006105#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006106 {
6107 /* Block 0 is empty, and the next block will be
6108 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006110 }
6111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006113 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006114#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6116 return PSA_ERROR_INVALID_ARGUMENT;
6117 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006118#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006119#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6121 hkdf->state == HKDF_STATE_INIT) {
6122 return PSA_ERROR_BAD_STATE;
6123 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006124#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 if (hkdf->state == HKDF_STATE_OUTPUT) {
6126 return PSA_ERROR_BAD_STATE;
6127 }
6128 if (hkdf->info_set) {
6129 return PSA_ERROR_BAD_STATE;
6130 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006131 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 if (data_length != 0) {
6133 hkdf->info = mbedtls_calloc(1, data_length);
6134 if (hkdf->info == NULL) {
6135 return PSA_ERROR_INSUFFICIENT_MEMORY;
6136 }
6137 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006138 }
6139 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006141 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006143 }
6144}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006145#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006146
John Durkop07cc04a2020-11-16 22:08:34 -08006147#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6148 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006149static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6150 const uint8_t *data,
6151 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006152{
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6154 return PSA_ERROR_BAD_STATE;
6155 }
Janos Follathf08e2652019-06-13 09:05:41 +01006156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 if (data_length != 0) {
6158 prf->seed = mbedtls_calloc(1, data_length);
6159 if (prf->seed == NULL) {
6160 return PSA_ERROR_INSUFFICIENT_MEMORY;
6161 }
Janos Follathf08e2652019-06-13 09:05:41 +01006162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006164 prf->seed_length = data_length;
6165 }
Janos Follathf08e2652019-06-13 09:05:41 +01006166
Gilles Peskine43f958b2020-12-13 14:55:14 +01006167 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006168
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006170}
6171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6173 const uint8_t *data,
6174 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006175{
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6177 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6178 return PSA_ERROR_BAD_STATE;
6179 }
Janos Follath81550542019-06-13 14:26:34 +01006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 if (data_length != 0) {
6182 prf->secret = mbedtls_calloc(1, data_length);
6183 if (prf->secret == NULL) {
6184 return PSA_ERROR_INSUFFICIENT_MEMORY;
6185 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006186
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006188 prf->secret_length = data_length;
6189 }
Janos Follath81550542019-06-13 14:26:34 +01006190
Gilles Peskine43f958b2020-12-13 14:55:14 +01006191 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006194}
6195
Gilles Peskine449bd832023-01-11 14:50:10 +01006196static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6197 const uint8_t *data,
6198 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006199{
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6201 return PSA_ERROR_BAD_STATE;
6202 }
Janos Follath63028dd2019-06-13 09:15:47 +01006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 if (data_length != 0) {
6205 prf->label = mbedtls_calloc(1, data_length);
6206 if (prf->label == NULL) {
6207 return PSA_ERROR_INSUFFICIENT_MEMORY;
6208 }
Janos Follath63028dd2019-06-13 09:15:47 +01006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006211 prf->label_length = data_length;
6212 }
Janos Follath63028dd2019-06-13 09:15:47 +01006213
Gilles Peskine43f958b2020-12-13 14:55:14 +01006214 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006217}
6218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6220 psa_key_derivation_step_t step,
6221 const uint8_t *data,
6222 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006223{
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006225 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006227 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006229 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006231 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006233 }
6234}
John Durkop07cc04a2020-11-16 22:08:34 -08006235#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6236 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6237
6238#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6239static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6240 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006241 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006243{
6244 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6246 4 + data_length + prf->other_secret_length :
6247 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6250 return PSA_ERROR_INVALID_ARGUMENT;
6251 }
John Durkop07cc04a2020-11-16 22:08:34 -08006252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 uint8_t *pms = mbedtls_calloc(1, pms_len);
6254 if (pms == NULL) {
6255 return PSA_ERROR_INSUFFICIENT_MEMORY;
6256 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006257 uint8_t *cur = pms;
6258
6259 /* pure-PSK:
6260 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006261 *
6262 * The premaster secret is formed as follows: if the PSK is N octets
6263 * long, concatenate a uint16 with the value N, N zero octets, a second
6264 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006265 *
6266 * mixed-PSK:
6267 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6268 * follows: concatenate a uint16 with the length of the other secret,
6269 * the other secret itself, uint16 with the length of PSK, and the
6270 * PSK itself.
6271 * For details please check:
6272 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6273 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6274 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006275 */
6276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6278 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6279 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6280 if (prf->other_secret_length != 0) {
6281 memcpy(cur, prf->other_secret, prf->other_secret_length);
6282 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006283 cur += prf->other_secret_length;
6284 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 } else {
6286 *cur++ = MBEDTLS_BYTE_1(data_length);
6287 *cur++ = MBEDTLS_BYTE_0(data_length);
6288 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006289 cur += data_length;
6290 }
6291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 *cur++ = MBEDTLS_BYTE_1(data_length);
6293 *cur++ = MBEDTLS_BYTE_0(data_length);
6294 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006295 cur += data_length;
6296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 mbedtls_platform_zeroize(pms, pms_len);
6300 mbedtls_free(pms);
6301 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006302}
Janos Follath6660f0e2019-06-17 08:44:03 +01006303
Przemek Stekiele47201b2022-04-19 13:53:28 +02006304static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6305 psa_tls12_prf_key_derivation_t *prf,
6306 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006308{
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6310 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006311 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006312
6313 if (data_length != 0) {
6314 prf->other_secret = mbedtls_calloc(1, data_length);
6315 if (prf->other_secret == NULL) {
6316 return PSA_ERROR_INSUFFICIENT_MEMORY;
6317 }
6318
6319 memcpy(prf->other_secret, data, data_length);
6320 prf->other_secret_length = data_length;
6321 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006322 prf->other_secret_length = 0;
6323 }
6324
6325 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006328}
6329
Janos Follath6660f0e2019-06-17 08:44:03 +01006330static psa_status_t psa_tls12_prf_psk_to_ms_input(
6331 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006332 psa_key_derivation_step_t step,
6333 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006335{
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006337 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 return psa_tls12_prf_psk_to_ms_set_key(prf,
6339 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006340 break;
6341 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6343 data,
6344 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006345 break;
6346 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006348 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006349
Przemek Stekiele47201b2022-04-19 13:53:28 +02006350 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006351}
John Durkop07cc04a2020-11-16 22:08:34 -08006352#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006353
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006354#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6355static psa_status_t psa_tls12_ecjpake_to_pms_input(
6356 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006357 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006358 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006360{
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6362 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6363 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006364 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006365
6366 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 if (data[0] != 0x04) {
6368 return PSA_ERROR_INVALID_ARGUMENT;
6369 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006370
6371 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006373
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006375}
6376#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006377/** Check whether the given key type is acceptable for the given
6378 * input step of a key derivation.
6379 *
6380 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6381 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6382 * Both secret and non-secret inputs can alternatively have the type
6383 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6384 * that the input was passed as a buffer rather than via a key object.
6385 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006386static int psa_key_derivation_check_input_type(
6387 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006389{
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006391 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 if (key_type == PSA_KEY_TYPE_DERIVE) {
6393 return PSA_SUCCESS;
6394 }
6395 if (key_type == PSA_KEY_TYPE_NONE) {
6396 return PSA_SUCCESS;
6397 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006398 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006399 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 if (key_type == PSA_KEY_TYPE_DERIVE) {
6401 return PSA_SUCCESS;
6402 }
6403 if (key_type == PSA_KEY_TYPE_NONE) {
6404 return PSA_SUCCESS;
6405 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006406 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006407 case PSA_KEY_DERIVATION_INPUT_LABEL:
6408 case PSA_KEY_DERIVATION_INPUT_SALT:
6409 case PSA_KEY_DERIVATION_INPUT_INFO:
6410 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6412 return PSA_SUCCESS;
6413 }
6414 if (key_type == PSA_KEY_TYPE_NONE) {
6415 return PSA_SUCCESS;
6416 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006417 break;
6418 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006420}
6421
Janos Follathb80a94e2019-06-12 15:54:46 +01006422static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006423 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006424 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006425 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006426 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006428{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006429 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 status = psa_key_derivation_check_input_type(step, key_type);
6433 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006434 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006436
Przemek Stekiel69c46792022-06-10 12:59:51 +02006437#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6439 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6440 step, data, data_length);
6441 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006442#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006443#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6445 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6446 step, data, data_length);
6447 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006448#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6449#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6451 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6452 step, data, data_length);
6453 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006454#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006455#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006457 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6459 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006460#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006461 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006462 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006463 (void) data;
6464 (void) data_length;
6465 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006467 }
6468
Gilles Peskine224b0d62019-09-23 18:13:17 +02006469exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 if (status != PSA_SUCCESS) {
6471 psa_key_derivation_abort(operation);
6472 }
6473 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006474}
6475
Janos Follath51f4a0f2019-06-14 11:35:55 +01006476psa_status_t psa_key_derivation_input_bytes(
6477 psa_key_derivation_operation_t *operation,
6478 psa_key_derivation_step_t step,
6479 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006481{
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 return psa_key_derivation_input_internal(operation, step,
6483 PSA_KEY_TYPE_NONE,
6484 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006485}
6486
Janos Follath51f4a0f2019-06-14 11:35:55 +01006487psa_status_t psa_key_derivation_input_key(
6488 psa_key_derivation_operation_t *operation,
6489 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006491{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006492 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006493 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006494 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006495
Ronald Cron5c522922020-11-14 16:35:34 +01006496 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6498 if (status != PSA_SUCCESS) {
6499 psa_key_derivation_abort(operation);
6500 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006501 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006502
6503 /* Passing a key object as a SECRET input unlocks the permission
6504 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006506 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 status = psa_key_derivation_input_internal(operation,
6510 step, slot->attr.type,
6511 slot->key.data,
6512 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006513
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006517}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006518
6519
6520
6521/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006522/* Key agreement */
6523/****************************************************************/
6524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6526 const uint8_t *key_buffer,
6527 size_t key_buffer_size,
6528 psa_algorithm_t alg,
6529 const uint8_t *peer_key,
6530 size_t peer_key_length,
6531 uint8_t *shared_secret,
6532 size_t shared_secret_size,
6533 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006534{
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006536#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006537 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6539 key_buffer_size, alg,
6540 peer_key, peer_key_length,
6541 shared_secret,
6542 shared_secret_size,
6543 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006544#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006545 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006546 (void) attributes;
6547 (void) key_buffer;
6548 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006549 (void) peer_key;
6550 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006551 (void) shared_secret;
6552 (void) shared_secret_size;
6553 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006555 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006556}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006557
Aditya Deshpande17845b82022-10-13 17:21:01 +01006558/** Internal function for raw key agreement
6559 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006560 * to the driver's implementation if a driver is present.
6561 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006562 * (psa_key_agreement_raw_builtin).
6563 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006564static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6565 psa_key_slot_t *private_key,
6566 const uint8_t *peer_key,
6567 size_t peer_key_length,
6568 uint8_t *shared_secret,
6569 size_t shared_secret_size,
6570 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006571{
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6573 return PSA_ERROR_NOT_SUPPORTED;
6574 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006575
6576 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006578 };
6579
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 return psa_driver_wrapper_key_agreement(&attributes,
6581 private_key->key.data,
6582 private_key->key.bytes, alg,
6583 peer_key, peer_key_length,
6584 shared_secret,
6585 shared_secret_size,
6586 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006587}
6588
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006589/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006590 * to potentially free embedded data structures and wipe confidential data.
6591 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006592static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6593 psa_key_derivation_step_t step,
6594 psa_key_slot_t *private_key,
6595 const uint8_t *peer_key,
6596 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006597{
6598 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006599 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006600 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006602
6603 /* Step 1: run the secret agreement algorithm to generate the shared
6604 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 status = psa_key_agreement_raw_internal(ka_alg,
6606 private_key,
6607 peer_key, peer_key_length,
6608 shared_secret,
6609 sizeof(shared_secret),
6610 &shared_secret_length);
6611 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006612 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006614
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006615 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006616 * the shared secret. A shared secret is permitted wherever a key
6617 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 status = psa_key_derivation_input_internal(operation, step,
6619 PSA_KEY_TYPE_DERIVE,
6620 shared_secret,
6621 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006622exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6624 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006625}
6626
Gilles Peskine449bd832023-01-11 14:50:10 +01006627psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6628 psa_key_derivation_step_t step,
6629 mbedtls_svc_key_id_t private_key,
6630 const uint8_t *peer_key,
6631 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006632{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006633 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006634 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006635 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006636
Gilles Peskine449bd832023-01-11 14:50:10 +01006637 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6638 return PSA_ERROR_INVALID_ARGUMENT;
6639 }
Ronald Cron5c522922020-11-14 16:35:34 +01006640 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6642 if (status != PSA_SUCCESS) {
6643 return status;
6644 }
6645 status = psa_key_agreement_internal(operation, step,
6646 slot,
6647 peer_key, peer_key_length);
6648 if (status != PSA_SUCCESS) {
6649 psa_key_derivation_abort(operation);
6650 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006651 /* If a private key has been added as SECRET, we allow the derived
6652 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006654 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006656 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006657
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006659
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006661}
6662
Gilles Peskine449bd832023-01-11 14:50:10 +01006663psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6664 mbedtls_svc_key_id_t private_key,
6665 const uint8_t *peer_key,
6666 size_t peer_key_length,
6667 uint8_t *output,
6668 size_t output_size,
6669 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006670{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006671 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006672 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006673 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006674
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006676 status = PSA_ERROR_INVALID_ARGUMENT;
6677 goto exit;
6678 }
Ronald Cron5c522922020-11-14 16:35:34 +01006679 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6681 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006682 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006684
Gilles Peskine3e561302022-04-14 00:17:15 +02006685 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6686 * for the output size. The PSA specification only guarantees that this
6687 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6688 * but it might be nice to allow smaller buffers if the output fits.
6689 * At the time of writing this comment, with only ECDH implemented,
6690 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6691 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6692 * be exact for it as well. */
6693 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6695 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006696 status = PSA_ERROR_BUFFER_TOO_SMALL;
6697 goto exit;
6698 }
6699
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 status = psa_key_agreement_raw_internal(alg, slot,
6701 peer_key, peer_key_length,
6702 output, output_size,
6703 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006704
6705exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006706 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006707 /* If an error happens and is not handled properly, the output
6708 * may be used as a key to protect sensitive data. Arrange for such
6709 * a key to be random, which is likely to result in decryption or
6710 * verification errors. This is better than filling the buffer with
6711 * some constant data such as zeros, which would result in the data
6712 * being protected with a reproducible, easily knowable key.
6713 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006715 *output_length = output_size;
6716 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006717
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006719
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006721}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006722
6723
Gilles Peskine30524eb2020-11-13 17:02:26 +01006724
Gilles Peskine86a440b2018-11-12 18:39:40 +01006725/****************************************************************/
6726/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006727/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006728
Gilles Peskine30524eb2020-11-13 17:02:26 +01006729/** Initialize the PSA random generator.
6730 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006731static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006732{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006733#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006735#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6736
Gilles Peskine30524eb2020-11-13 17:02:26 +01006737 /* Set default configuration if
6738 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006740 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 }
6742 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006743 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006745
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006747#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6748 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6749 /* The PSA entropy injection feature depends on using NV seed as an entropy
6750 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 mbedtls_entropy_add_source(&rng->entropy,
6752 mbedtls_nv_seed_poll, NULL,
6753 MBEDTLS_ENTROPY_BLOCK_SIZE,
6754 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006755#endif
6756
Gilles Peskine449bd832023-01-11 14:50:10 +01006757 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006758#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006759}
6760
6761/** Deinitialize the PSA random generator.
6762 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006763static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006764{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006765#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006767#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6769 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006770#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006771}
6772
6773/** Seed the PSA random generator.
6774 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006775static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006776{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006777#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6778 /* Do nothing: the external RNG seeds itself. */
6779 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006780 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006781#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006782 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6784 drbg_seed, sizeof(drbg_seed) - 1);
6785 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006786#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006787}
6788
Gilles Peskine449bd832023-01-11 14:50:10 +01006789psa_status_t psa_generate_random(uint8_t *output,
6790 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006791{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006792 GUARD_MODULE_INITIALIZED;
6793
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006794#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6795
6796 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6798 output, output_size,
6799 &output_length);
6800 if (status != PSA_SUCCESS) {
6801 return status;
6802 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006803 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006804 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 if (output_length != output_size) {
6806 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6807 }
6808 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006809
6810#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6811
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006813 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6815 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6816 output_size);
6817 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6818 output, request_size);
6819 if (ret != 0) {
6820 return mbedtls_to_psa_error(ret);
6821 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006822 output_size -= request_size;
6823 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006824 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006826#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006827}
6828
Gilles Peskine8814fc42020-12-14 15:33:44 +01006829/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006830 *
6831 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6832 * `psa_generate_random(...)`. The state parameter is ignored since the
6833 * PSA API doesn't support passing an explicit state.
6834 *
6835 * In the non-external case, psa_generate_random() calls an
6836 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6837 * and semantics as mbedtls_psa_get_random(). As an optimization,
6838 * instead of doing this back-and-forth between the PSA API and the
6839 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6840 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006841 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006842#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6843int mbedtls_psa_get_random(void *p_rng,
6844 unsigned char *output,
6845 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006846{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006847 /* This function takes a pointer to the RNG state because that's what
6848 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6849 * its own state internally and doesn't let the caller access that state.
6850 * So we just ignore the state parameter, and in practice we'll pass
6851 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006852 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 psa_status_t status = psa_generate_random(output, output_size);
6854 if (status == PSA_SUCCESS) {
6855 return 0;
6856 } else {
6857 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6858 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006859}
6860#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6861
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006862#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006863#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006864
Gilles Peskine449bd832023-01-11 14:50:10 +01006865psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6866 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006867{
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 if (global_data.initialized) {
6869 return PSA_ERROR_NOT_PERMITTED;
6870 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006871
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6873 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6874 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6875 return PSA_ERROR_INVALID_ARGUMENT;
6876 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006877
Gilles Peskine449bd832023-01-11 14:50:10 +01006878 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006879}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006880#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006881
Ronald Cron3772afe2021-02-08 16:10:05 +01006882/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006883 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006884 * \param type The key type
6885 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006886 *
6887 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006888 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006889 * \retval #PSA_ERROR_INVALID_ARGUMENT
6890 * The size in bits of the key is not valid.
6891 * \retval #PSA_ERROR_NOT_SUPPORTED
6892 * The type and/or the size in bits of the key or the combination of
6893 * the two is not supported.
6894 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006895static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006897{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006898 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006899
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 if (key_type_is_raw_bytes(type)) {
6901 status = psa_validate_unstructured_key_bit_size(type, bits);
6902 if (status != PSA_SUCCESS) {
6903 return status;
6904 }
6905 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006906#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6908 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6909 return PSA_ERROR_NOT_SUPPORTED;
6910 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006911
Ronald Cron01b2aba2020-10-05 09:42:02 +02006912 /* Accept only byte-aligned keys, for the same reasons as
6913 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 if (bits % 8 != 0) {
6915 return PSA_ERROR_NOT_SUPPORTED;
6916 }
6917 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006918#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006919
Ronald Cron5c4d3862020-12-07 11:07:24 +01006920#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006922 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006923 return PSA_SUCCESS;
6924 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006925#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006926 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006928 }
6929
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006931}
6932
Ronald Cron55ed0592020-10-05 10:30:40 +02006933psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006934 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006936{
6937 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006938 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006939
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 if ((attributes->domain_parameters == NULL) &&
6941 (attributes->domain_parameters_size != 0)) {
6942 return PSA_ERROR_INVALID_ARGUMENT;
6943 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 if (key_type_is_raw_bytes(type)) {
6946 status = psa_generate_random(key_buffer, key_buffer_size);
6947 if (status != PSA_SUCCESS) {
6948 return status;
6949 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006950
David Brown12ca5032021-02-11 11:02:00 -07006951#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 if (type == PSA_KEY_TYPE_DES) {
6953 psa_des_set_key_parity(key_buffer, key_buffer_size);
6954 }
David Brown8107e312021-02-12 08:08:46 -07006955#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006957
Jaeden Amero424fa932021-05-14 08:34:32 +01006958#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6959 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6961 return mbedtls_psa_rsa_generate_key(attributes,
6962 key_buffer,
6963 key_buffer_size,
6964 key_buffer_length);
6965 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006966#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6967 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006968
John Durkop9814fa22020-11-04 12:28:15 -08006969#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6971 return mbedtls_psa_ecp_generate_key(attributes,
6972 key_buffer,
6973 key_buffer_size,
6974 key_buffer_length);
6975 } else
John Durkop9814fa22020-11-04 12:28:15 -08006976#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006977 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 (void) key_buffer_length;
6979 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006980 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006981
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006983}
Darryl Green0c6575a2018-11-07 16:05:30 +00006984
Gilles Peskine449bd832023-01-11 14:50:10 +01006985psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6986 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006987{
6988 psa_status_t status;
6989 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006990 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006991 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006992
Ronald Cron81709fc2020-11-14 12:10:32 +01006993 *key = MBEDTLS_SVC_KEY_ID_INIT;
6994
Gilles Peskine0f84d622019-09-12 19:03:13 +02006995 /* Reject any attempt to create a zero-length key so that we don't
6996 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 if (psa_get_key_bits(attributes) == 0) {
6998 return PSA_ERROR_INVALID_ARGUMENT;
6999 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007000
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007001 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007002 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7003 return PSA_ERROR_INVALID_ARGUMENT;
7004 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007005
Gilles Peskine449bd832023-01-11 14:50:10 +01007006 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7007 &slot, &driver);
7008 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007009 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 }
Gilles Peskine11792082019-08-06 18:36:36 +02007011
Ronald Cron977c2472020-10-13 08:32:21 +02007012 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307013 * storage ( thus not in the case of generating a key in a secure element
7014 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7015 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 if (slot->key.data == NULL) {
7017 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7018 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007019 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 attributes->core.type, attributes->core.bits);
7021 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007022 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007024
7025 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 attributes->core.type,
7027 attributes->core.bits);
7028 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007029 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007030 attributes, &key_buffer_size);
7031 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007032 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 }
Ronald Cron977c2472020-10-13 08:32:21 +02007034 }
Ronald Cron977c2472020-10-13 08:32:21 +02007035
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7037 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007038 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 }
Ronald Cron977c2472020-10-13 08:32:21 +02007040 }
7041
Gilles Peskine449bd832023-01-11 14:50:10 +01007042 status = psa_driver_wrapper_generate_key(attributes,
7043 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007044
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 if (status != PSA_SUCCESS) {
7046 psa_remove_key_data_from_memory(slot);
7047 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007048
Gilles Peskine11792082019-08-06 18:36:36 +02007049exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 if (status == PSA_SUCCESS) {
7051 status = psa_finish_key_creation(slot, driver, key);
7052 }
7053 if (status != PSA_SUCCESS) {
7054 psa_fail_key_creation(slot, driver);
7055 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007056
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007058}
7059
Gilles Peskinee59236f2018-01-27 23:32:46 +01007060/****************************************************************/
7061/* Module setup */
7062/****************************************************************/
7063
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007064#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007065psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 void (* entropy_init)(mbedtls_entropy_context *ctx),
7067 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007068{
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7070 return PSA_ERROR_BAD_STATE;
7071 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007072 global_data.rng.entropy_init = entropy_init;
7073 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007075}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007076#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007077
Gilles Peskine449bd832023-01-11 14:50:10 +01007078void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007079{
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 psa_wipe_all_key_slots();
7081 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7082 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007083 }
7084 /* Wipe all remaining data, including configuration.
7085 * In particular, this sets all state indicator to the value
7086 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007088
7089 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007091}
7092
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007093#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7094/** Recover a transaction that was interrupted by a power failure.
7095 *
7096 * This function is called during initialization, before psa_crypto_init()
7097 * returns. If this function returns a failure status, the initialization
7098 * fails.
7099 */
7100static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007102{
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007104 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7105 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 /* TODO - fall through to the failure case until this
7107 * is implemented.
7108 * https://github.com/ARMmbed/mbed-crypto/issues/218
7109 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007110 default:
7111 /* We found an unsupported transaction in the storage.
7112 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007114 }
7115}
7116#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7117
Gilles Peskine449bd832023-01-11 14:50:10 +01007118psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007119{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007120 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007121
Gilles Peskinec6b69072018-11-20 21:42:52 +01007122 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 if (global_data.initialized != 0) {
7124 return PSA_SUCCESS;
7125 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007126
Gilles Peskine30524eb2020-11-13 17:02:26 +01007127 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007129 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 status = mbedtls_psa_random_seed(&global_data.rng);
7131 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007133 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007134 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007135
Gilles Peskine449bd832023-01-11 14:50:10 +01007136 status = psa_initialize_key_slots();
7137 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007140
Ronald Cron9ba76912021-04-10 16:57:30 +02007141 /* Init drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007142 status = psa_driver_wrapper_init();
7143 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02007144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 }
Gilles Peskined9348f22019-10-01 15:22:29 +02007146
Gilles Peskine4b734222019-07-24 15:56:31 +02007147#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 status = psa_crypto_load_transaction();
7149 if (status == PSA_SUCCESS) {
7150 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7151 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 }
7154 status = psa_crypto_stop_transaction();
7155 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007156 /* There's no transaction to complete. It's all good. */
7157 status = PSA_SUCCESS;
7158 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007159#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007160
Gilles Peskinec6b69072018-11-20 21:42:52 +01007161 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007162 global_data.initialized = 1;
7163
7164exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007165 if (status != PSA_SUCCESS) {
7166 mbedtls_psa_crypto_free();
7167 }
7168 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007169}
7170
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007171psa_status_t psa_crypto_driver_pake_get_password_len(
7172 const psa_crypto_driver_pake_inputs_t *inputs,
7173 size_t *password_len)
7174{
7175 if (inputs->password_len == 0) {
7176 return PSA_ERROR_BAD_STATE;
7177 }
7178
7179 *password_len = inputs->password_len;
7180
7181 return PSA_SUCCESS;
7182}
7183
7184psa_status_t psa_crypto_driver_pake_get_password(
7185 const psa_crypto_driver_pake_inputs_t *inputs,
7186 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7187{
7188 if (inputs->password_len == 0) {
7189 return PSA_ERROR_BAD_STATE;
7190 }
7191
7192 if (buffer_size < inputs->password_len) {
7193 return PSA_ERROR_BUFFER_TOO_SMALL;
7194 }
7195
7196 memcpy(buffer, inputs->password, inputs->password_len);
7197 *buffer_length = inputs->password_len;
7198
7199 return PSA_SUCCESS;
7200}
7201
7202psa_status_t psa_crypto_driver_pake_get_role(
7203 const psa_crypto_driver_pake_inputs_t *inputs,
7204 psa_pake_role_t *role)
7205{
7206 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7207 return PSA_ERROR_BAD_STATE;
7208 }
7209
7210 *role = inputs->role;
7211
7212 return PSA_SUCCESS;
7213}
7214
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007215psa_status_t psa_crypto_driver_pake_get_user_len(
7216 const psa_crypto_driver_pake_inputs_t *inputs,
7217 size_t *user_len)
7218{
7219 if (inputs->user_len == 0) {
7220 return PSA_ERROR_BAD_STATE;
7221 }
7222
7223 *user_len = inputs->user_len;
7224
7225 return PSA_SUCCESS;
7226}
7227
7228psa_status_t psa_crypto_driver_pake_get_user(
7229 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007230 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007231{
7232 if (inputs->user_len == 0) {
7233 return PSA_ERROR_BAD_STATE;
7234 }
7235
Przemek Stekielc0e62502023-03-14 11:49:36 +01007236 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007237 return PSA_ERROR_BUFFER_TOO_SMALL;
7238 }
7239
Przemek Stekielc0e62502023-03-14 11:49:36 +01007240 memcpy(user_id, inputs->user, inputs->user_len);
7241 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007242
7243 return PSA_SUCCESS;
7244}
7245
7246psa_status_t psa_crypto_driver_pake_get_peer_len(
7247 const psa_crypto_driver_pake_inputs_t *inputs,
7248 size_t *peer_len)
7249{
7250 if (inputs->peer_len == 0) {
7251 return PSA_ERROR_BAD_STATE;
7252 }
7253
7254 *peer_len = inputs->peer_len;
7255
7256 return PSA_SUCCESS;
7257}
7258
7259psa_status_t psa_crypto_driver_pake_get_peer(
7260 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007261 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007262{
7263 if (inputs->peer_len == 0) {
7264 return PSA_ERROR_BAD_STATE;
7265 }
7266
Przemek Stekielc0e62502023-03-14 11:49:36 +01007267 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007268 return PSA_ERROR_BUFFER_TOO_SMALL;
7269 }
7270
Przemek Stekielc0e62502023-03-14 11:49:36 +01007271 memcpy(peer_id, inputs->peer, inputs->peer_len);
7272 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007273
7274 return PSA_SUCCESS;
7275}
7276
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007277psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7278 const psa_crypto_driver_pake_inputs_t *inputs,
7279 psa_pake_cipher_suite_t *cipher_suite)
7280{
7281 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7282 return PSA_ERROR_BAD_STATE;
7283 }
7284
7285 *cipher_suite = inputs->cipher_suite;
7286
7287 return PSA_SUCCESS;
7288}
7289
Neil Armstronga7d08c32022-06-01 18:21:20 +02007290psa_status_t psa_pake_setup(
7291 psa_pake_operation_t *operation,
7292 const psa_pake_cipher_suite_t *cipher_suite)
7293{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007294 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007295
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007296 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007297 status = PSA_ERROR_BAD_STATE;
7298 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007299 }
7300
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007301 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007302 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007303 status = PSA_ERROR_INVALID_ARGUMENT;
7304 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007305 }
7306
Przemek Stekiel51eac532022-12-07 11:04:51 +01007307 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7308
Przemek Stekiele12ed362022-12-21 12:54:46 +01007309 operation->alg = cipher_suite->algorithm;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007310 operation->data.inputs.cipher_suite = *cipher_suite;
7311
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007312#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007313 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007314 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007315 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007316
Przemek Stekiele12ed362022-12-21 12:54:46 +01007317 computation_stage->state = PSA_PAKE_STATE_SETUP;
7318 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7319 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7320 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007321 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007322#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007323 {
7324 status = PSA_ERROR_NOT_SUPPORTED;
7325 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007326 }
7327
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007328 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7329
Przemek Stekiel51eac532022-12-07 11:04:51 +01007330 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007331exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007332 psa_pake_abort(operation);
7333 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007334}
7335
7336psa_status_t psa_pake_set_password_key(
7337 psa_pake_operation_t *operation,
7338 mbedtls_svc_key_id_t password)
7339{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007340 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007341 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7342 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007343
Przemek Stekiel51eac532022-12-07 11:04:51 +01007344 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007345 status = PSA_ERROR_BAD_STATE;
7346 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007347 }
7348
Przemek Stekiel6c764412022-11-22 14:05:12 +01007349 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7350 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007351 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007352 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007353 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007354 }
7355
Przemek Stekiel6c764412022-11-22 14:05:12 +01007356 psa_key_attributes_t attributes = {
7357 .core = slot->attr
7358 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007359
Przemek Stekiel51eac532022-12-07 11:04:51 +01007360 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007361
Przemek Stekiel51eac532022-12-07 11:04:51 +01007362 if (type != PSA_KEY_TYPE_PASSWORD &&
7363 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7364 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007365 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007366 }
7367
Przemek Stekiel51eac532022-12-07 11:04:51 +01007368 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7369 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007370 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007371 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007372 }
7373
7374 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7375 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007376 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007377exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007378 if (status != PSA_SUCCESS) {
7379 psa_pake_abort(operation);
7380 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007381 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007382 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007383}
7384
7385psa_status_t psa_pake_set_user(
7386 psa_pake_operation_t *operation,
7387 const uint8_t *user_id,
7388 size_t user_id_len)
7389{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007390 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007391
7392 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007393 status = PSA_ERROR_BAD_STATE;
7394 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007395 }
7396
Przemek Stekiel51eac532022-12-07 11:04:51 +01007397 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007398 status = PSA_ERROR_INVALID_ARGUMENT;
7399 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007400 }
7401
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007402 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007403 status = PSA_ERROR_BAD_STATE;
7404 goto exit;
7405 }
7406
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007407 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007408 if ((user_id_len != sizeof(jpake_server_id) ||
7409 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7410 (user_id_len != sizeof(jpake_client_id) ||
7411 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007412 status = PSA_ERROR_NOT_SUPPORTED;
7413 goto exit;
7414 }
7415
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007416 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7417 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007418 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7419 goto exit;
7420 }
7421
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007422 memcpy(operation->data.inputs.user, user_id, user_id_len);
7423 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007424
7425 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007426exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007427 psa_pake_abort(operation);
7428 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007429}
7430
7431psa_status_t psa_pake_set_peer(
7432 psa_pake_operation_t *operation,
7433 const uint8_t *peer_id,
7434 size_t peer_id_len)
7435{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007436 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007437
7438 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007439 status = PSA_ERROR_BAD_STATE;
7440 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007441 }
7442
Przemek Stekiel51eac532022-12-07 11:04:51 +01007443 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007444 status = PSA_ERROR_INVALID_ARGUMENT;
7445 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007446 }
7447
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007448 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007449 status = PSA_ERROR_BAD_STATE;
7450 goto exit;
7451 }
7452
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007453 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007454 if ((peer_id_len != sizeof(jpake_server_id) ||
7455 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7456 (peer_id_len != sizeof(jpake_client_id) ||
7457 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007458 status = PSA_ERROR_NOT_SUPPORTED;
7459 goto exit;
7460 }
7461
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007462 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7463 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007464 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7465 goto exit;
7466 }
7467
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007468 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7469 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007470
7471 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007472exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007473 psa_pake_abort(operation);
7474 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007475}
7476
7477psa_status_t psa_pake_set_role(
7478 psa_pake_operation_t *operation,
7479 psa_pake_role_t role)
7480{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007481 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007482
Przemek Stekiel51eac532022-12-07 11:04:51 +01007483 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007484 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007485 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007486 }
7487
Przemek Stekiel09104b82023-03-06 14:11:51 +01007488 switch (operation->alg) {
7489#if defined(PSA_WANT_ALG_JPAKE)
7490 case PSA_ALG_JPAKE:
7491 if (role == PSA_PAKE_ROLE_NONE) {
7492 return PSA_SUCCESS;
7493 }
7494 status = PSA_ERROR_INVALID_ARGUMENT;
7495 break;
7496#endif
7497 default:
7498 (void) role;
7499 status = PSA_ERROR_NOT_SUPPORTED;
7500 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007501 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007502exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007503 psa_pake_abort(operation);
7504 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007505}
7506
Przemek Stekielb09c4872023-01-17 12:05:38 +01007507/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007508#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007509static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007510 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007511{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007512 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007513 case PSA_PAKE_OUTPUT_X1_X2:
7514 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007515 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007516 case PSA_PAKE_X1_STEP_KEY_SHARE:
7517 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007518 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7519 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007520 case PSA_PAKE_X1_STEP_ZK_PROOF:
7521 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007522 case PSA_PAKE_X2_STEP_KEY_SHARE:
7523 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007524 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7525 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007526 case PSA_PAKE_X2_STEP_ZK_PROOF:
7527 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007528 default:
7529 return PSA_JPAKE_STEP_INVALID;
7530 }
7531 break;
7532 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007533 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007534 case PSA_PAKE_X1_STEP_KEY_SHARE:
7535 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007536 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7537 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007538 case PSA_PAKE_X1_STEP_ZK_PROOF:
7539 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007540 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007541 return PSA_JPAKE_STEP_INVALID;
7542 }
7543 break;
7544 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007545 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007546 case PSA_PAKE_X1_STEP_KEY_SHARE:
7547 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007548 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7549 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007550 case PSA_PAKE_X1_STEP_ZK_PROOF:
7551 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007552 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007553 return PSA_JPAKE_STEP_INVALID;
7554 }
7555 break;
7556 default:
7557 return PSA_JPAKE_STEP_INVALID;
7558 }
7559 return PSA_JPAKE_STEP_INVALID;
7560}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007561#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007562
Przemek Stekiel2797d372022-12-22 11:19:22 +01007563static psa_status_t psa_pake_complete_inputs(
7564 psa_pake_operation_t *operation)
7565{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007566 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007567 /* Create copy of the inputs on stack as inputs share memory
7568 with the driver context which will be setup by the driver. */
7569 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007570
Przemek Stekielfde11282023-03-13 16:06:09 +01007571 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007572 return PSA_ERROR_BAD_STATE;
7573 }
7574
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007575 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007576 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7577 return PSA_ERROR_BAD_STATE;
7578 }
7579 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7580 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007581 inputs.role = PSA_PAKE_ROLE_CLIENT;
7582 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007583 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7584 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007585 inputs.role = PSA_PAKE_ROLE_SERVER;
7586 }
7587
7588 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7589 inputs.role != PSA_PAKE_ROLE_SERVER) {
7590 return PSA_ERROR_NOT_SUPPORTED;
7591 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007592 }
7593
Przemek Stekiel18620a32023-01-17 16:34:52 +01007594 /* Clear driver context */
7595 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7596
7597 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007598
7599 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007600 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7601 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007602
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007603 /* User and peer are translated to role. */
7604 mbedtls_free(inputs.user);
7605 mbedtls_free(inputs.peer);
7606
Przemek Stekiel2797d372022-12-22 11:19:22 +01007607 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007608#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007609 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007610 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007611 psa_jpake_computation_stage_t *computation_stage =
7612 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007613 computation_stage->state = PSA_PAKE_STATE_READY;
7614 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7615 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7616 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007617 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007618#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007619 {
7620 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007621 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007622 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007623 return status;
7624}
7625
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007626#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007627static psa_status_t psa_jpake_output_prologue(
7628 psa_pake_operation_t *operation,
7629 psa_pake_step_t step)
7630{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007631 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7632 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7633 step != PSA_PAKE_STEP_ZK_PROOF) {
7634 return PSA_ERROR_INVALID_ARGUMENT;
7635 }
7636
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007637 psa_jpake_computation_stage_t *computation_stage =
7638 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007639
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007640 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7641 return PSA_ERROR_BAD_STATE;
7642 }
7643
7644 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7645 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7646 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7647 return PSA_ERROR_BAD_STATE;
7648 }
7649
7650 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7651 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007652 return PSA_ERROR_BAD_STATE;
7653 }
7654
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007655 switch (computation_stage->output_step) {
7656 case PSA_PAKE_STEP_X1_X2:
7657 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007658 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007659 case PSA_PAKE_STEP_X2S:
7660 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007661 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007662 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007663 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007664 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007665
7666 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7667 }
7668
7669 /* Check if step matches current sequence */
7670 switch (computation_stage->sequence) {
7671 case PSA_PAKE_X1_STEP_KEY_SHARE:
7672 case PSA_PAKE_X2_STEP_KEY_SHARE:
7673 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7674 return PSA_ERROR_BAD_STATE;
7675 }
7676 break;
7677
7678 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7679 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7680 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7681 return PSA_ERROR_BAD_STATE;
7682 }
7683 break;
7684
7685 case PSA_PAKE_X1_STEP_ZK_PROOF:
7686 case PSA_PAKE_X2_STEP_ZK_PROOF:
7687 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7688 return PSA_ERROR_BAD_STATE;
7689 }
7690 break;
7691
7692 default:
7693 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007694 }
7695
7696 return PSA_SUCCESS;
7697}
7698
7699static psa_status_t psa_jpake_output_epilogue(
7700 psa_pake_operation_t *operation)
7701{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007702 psa_jpake_computation_stage_t *computation_stage =
7703 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007704
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007705 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007706 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007707 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007708 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007709 computation_stage->state = PSA_PAKE_STATE_READY;
7710 computation_stage->output_step++;
7711 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7712 } else {
7713 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007714 }
7715
7716 return PSA_SUCCESS;
7717}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007718#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007719
Neil Armstronga7d08c32022-06-01 18:21:20 +02007720psa_status_t psa_pake_output(
7721 psa_pake_operation_t *operation,
7722 psa_pake_step_t step,
7723 uint8_t *output,
7724 size_t output_size,
7725 size_t *output_length)
7726{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007727 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007728 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007729 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007730
7731 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007732 status = psa_pake_complete_inputs(operation);
7733 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007734 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007735 }
7736 }
7737
7738 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007739 status = PSA_ERROR_BAD_STATE;
7740 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007741 }
7742
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007743 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007744 status = PSA_ERROR_INVALID_ARGUMENT;
7745 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007746 }
7747
Przemek Stekiele12ed362022-12-21 12:54:46 +01007748 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007749#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007750 case PSA_ALG_JPAKE:
7751 status = psa_jpake_output_prologue(operation, step);
7752 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007753 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007754 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007755 driver_step = convert_jpake_computation_stage_to_driver_step(
7756 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007757 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007758#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007759 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007760 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007761 status = PSA_ERROR_NOT_SUPPORTED;
7762 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007763 }
7764
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007765 status = psa_driver_wrapper_pake_output(operation, driver_step,
7766 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007767
7768 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007769 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007770 }
7771
7772 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007773#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007774 case PSA_ALG_JPAKE:
7775 status = psa_jpake_output_epilogue(operation);
7776 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007777 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007778 }
7779 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007780#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007781 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007782 status = PSA_ERROR_NOT_SUPPORTED;
7783 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007784 }
7785
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007786 return PSA_SUCCESS;
7787exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007788 psa_pake_abort(operation);
7789 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007790}
7791
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007792#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007793static psa_status_t psa_jpake_input_prologue(
7794 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007795 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007796{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007797 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7798 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7799 step != PSA_PAKE_STEP_ZK_PROOF) {
7800 return PSA_ERROR_INVALID_ARGUMENT;
7801 }
7802
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007803 psa_jpake_computation_stage_t *computation_stage =
7804 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007805
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007806 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7807 return PSA_ERROR_BAD_STATE;
7808 }
7809
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007810 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7811 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7812 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7813 return PSA_ERROR_BAD_STATE;
7814 }
7815
7816 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7817 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007818 return PSA_ERROR_BAD_STATE;
7819 }
7820
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007821 switch (computation_stage->input_step) {
7822 case PSA_PAKE_STEP_X1_X2:
7823 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007824 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007825 case PSA_PAKE_STEP_X2S:
7826 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007827 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007828 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007829 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007830 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007831
7832 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7833 }
7834
7835 /* Check if step matches current sequence */
7836 switch (computation_stage->sequence) {
7837 case PSA_PAKE_X1_STEP_KEY_SHARE:
7838 case PSA_PAKE_X2_STEP_KEY_SHARE:
7839 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7840 return PSA_ERROR_BAD_STATE;
7841 }
7842 break;
7843
7844 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7845 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7846 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7847 return PSA_ERROR_BAD_STATE;
7848 }
7849 break;
7850
7851 case PSA_PAKE_X1_STEP_ZK_PROOF:
7852 case PSA_PAKE_X2_STEP_ZK_PROOF:
7853 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7854 return PSA_ERROR_BAD_STATE;
7855 }
7856 break;
7857
7858 default:
7859 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007860 }
7861
7862 return PSA_SUCCESS;
7863}
7864
Przemek Stekiele12ed362022-12-21 12:54:46 +01007865static psa_status_t psa_jpake_input_epilogue(
7866 psa_pake_operation_t *operation)
7867{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007868 psa_jpake_computation_stage_t *computation_stage =
7869 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007870
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007871 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007872 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007873 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007874 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007875 computation_stage->state = PSA_PAKE_STATE_READY;
7876 computation_stage->input_step++;
7877 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7878 } else {
7879 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007880 }
7881
7882 return PSA_SUCCESS;
7883}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007884#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007885
Neil Armstronga7d08c32022-06-01 18:21:20 +02007886psa_status_t psa_pake_input(
7887 psa_pake_operation_t *operation,
7888 psa_pake_step_t step,
7889 const uint8_t *input,
7890 size_t input_length)
7891{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007892 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007893 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007894
7895 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007896 status = psa_pake_complete_inputs(operation);
7897 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007898 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007899 }
7900 }
7901
7902 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007903 status = PSA_ERROR_BAD_STATE;
7904 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007905 }
7906
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007907 if (input_length == 0 || input_length > PSA_PAKE_INPUT_MAX_SIZE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007908 status = PSA_ERROR_INVALID_ARGUMENT;
7909 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007910 }
7911
Przemek Stekiele12ed362022-12-21 12:54:46 +01007912 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007913#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007914 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007915 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007916 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007917 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007918 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007919 driver_step = convert_jpake_computation_stage_to_driver_step(
7920 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007921 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007922#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007923 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007924 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007925 status = PSA_ERROR_NOT_SUPPORTED;
7926 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007927 }
7928
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007929 status = psa_driver_wrapper_pake_input(operation, driver_step,
7930 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007931
7932 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007933 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007934 }
7935
7936 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007937#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007938 case PSA_ALG_JPAKE:
7939 status = psa_jpake_input_epilogue(operation);
7940 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007941 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007942 }
7943 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007944#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007945 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007946 status = PSA_ERROR_NOT_SUPPORTED;
7947 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007948 }
7949
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007950 return PSA_SUCCESS;
7951exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007952 psa_pake_abort(operation);
7953 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007954}
7955
7956psa_status_t psa_pake_get_implicit_key(
7957 psa_pake_operation_t *operation,
7958 psa_key_derivation_operation_t *output)
7959{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007960 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007961 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007962 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01007963 size_t shared_key_len = 0;
7964
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007965 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007966 status = PSA_ERROR_BAD_STATE;
7967 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007968 }
7969
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007970#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007971 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007972 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01007973 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007974 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
7975 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007976 status = PSA_ERROR_BAD_STATE;
7977 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007978 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01007979 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007980#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007981 {
7982 status = PSA_ERROR_NOT_SUPPORTED;
7983 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007984 }
7985
Przemek Stekiel0c781802022-11-29 14:53:13 +01007986 status = psa_driver_wrapper_pake_get_implicit_key(operation,
7987 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01007988 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01007989 &shared_key_len);
7990
7991 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007992 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01007993 }
7994
7995 status = psa_key_derivation_input_bytes(output,
7996 PSA_KEY_DERIVATION_INPUT_SECRET,
7997 shared_key,
7998 shared_key_len);
7999
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008000 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008001exit:
8002 abort_status = psa_pake_abort(operation);
8003 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008004}
8005
8006psa_status_t psa_pake_abort(
8007 psa_pake_operation_t *operation)
8008{
Przemek Stekield69dca92023-01-31 19:59:20 +01008009 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008010
Przemek Stekield69dca92023-01-31 19:59:20 +01008011 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008012 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008013 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008014
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008015 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8016 if (operation->data.inputs.password != NULL) {
8017 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008018 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008019 mbedtls_free(operation->data.inputs.password);
8020 }
8021 if (operation->data.inputs.user != NULL) {
8022 mbedtls_free(operation->data.inputs.user);
8023 }
8024 if (operation->data.inputs.peer != NULL) {
8025 mbedtls_free(operation->data.inputs.peer);
8026 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008027 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008028 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008029
Przemek Stekield69dca92023-01-31 19:59:20 +01008030 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008031}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008032
Gilles Peskinee59236f2018-01-27 23:32:46 +01008033#endif /* MBEDTLS_PSA_CRYPTO_C */