blob: d9ba543e3759606d5007b889509dab935ae2cc6f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010038#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010039#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010040#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010042#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020044#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020045#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010046#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010047/* Include internal declarations that are useful for implementing persistently
48 * stored keys. */
49#include "psa_crypto_storage.h"
50
Gilles Peskineb2b64d32020-12-14 16:43:58 +010051#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010052
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020062#include "mbedtls/chacha20.h"
63#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020068#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/error.h"
72#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010074#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000075#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010076#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000077#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010078#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000079#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/sha1.h"
83#include "mbedtls/sha256.h"
84#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000085#include "hash_info.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020088
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020089#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
91 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020092#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020093#endif
94
Przemek Stekielfde11282023-03-13 16:06:09 +010095/* The only two JPAKE user/peer identifiers supported for the time being. */
96static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
97static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
Przemek Stekiel18cd6c92023-03-06 15:40:35 +010098
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099/****************************************************************/
100/* Global data, support functions and library management */
101/****************************************************************/
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104{
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106}
107
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100108/* Values for psa_global_data_t::rng_state */
109#define RNG_NOT_INITIALIZED 0
110#define RNG_INITIALIZED 1
111#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100115 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100116 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100117 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100122#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
123mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
124 &global_data.rng.drbg;
125#endif
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (global_data.initialized == 0) \
129 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300130
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100131int psa_can_do_hash(psa_algorithm_t hash_alg)
132{
133 (void) hash_alg;
134 return global_data.drivers_initialized;
135}
136
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200137static int psa_is_dh_key_size_valid(size_t bits) {
138 if (bits != 2048 && bits != 3072 && bits != 4096 &&
139 bits != 6144 && bits != 8192) {
140 return 0;
141 }
142
143 return 1;
144}
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100148 /* Mbed TLS error codes can combine a high-level error code and a
149 * low-level error code. The low-level error usually reflects the
150 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 int low_level_ret = -(-ret & 0x007f);
152 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100153 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100155
156 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
157 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200159 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
160 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
161 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
162 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
163 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200165 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200167 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100175
176 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180
Gilles Peskine26869f22019-05-06 15:25:00 +0200181 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200183
184 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200188
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
205 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100206 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
207 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
211 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100215#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100216
217 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100219
Gilles Peskinee59236f2018-01-27 23:32:46 +0100220 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
221 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
222 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
225 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200227 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100234 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
235 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100236 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100238 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
239 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100241 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243#endif
244
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200294 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
311 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200319
itayzafrir5c753392018-05-08 11:18:38 +0300320 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300321 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
328 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300330 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100332 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100334
Paul Elliott588f8ed2022-12-02 18:10:26 +0000335 case MBEDTLS_ERR_ECP_IN_PROGRESS:
336 return PSA_OPERATION_INCOMPLETE;
337
Janos Follatha13b9052019-11-22 12:48:59 +0000338 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300340
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100343 }
344}
345
Paul Elliottdc42ca82023-02-24 18:11:59 +0000346/**
347 * \brief For output buffers which contain "tags"
348 * (outputs that may be checked for validity like
349 * hashes, MACs and signatures), fill the unused
350 * part of the output buffer (the whole buffer on
351 * error, the trailing part on success) with
352 * something that isn't a valid tag (barring an
353 * attack on the tag and deliberately-crafted
354 * input), in case the caller doesn't check the
355 * return status properly.
356 *
357 * \param output_buffer Pointer to buffer to wipe. May not be NULL
358 * unless \p output_buffer_size is zero.
359 * \param status Status of function called to generate
360 * output_buffer originally
361 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
362 * could be NULL.
363 * \param output_buffer_length Length of data written to output_buffer, must be
364 * less than \p output_buffer_size
365 */
366static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000367 size_t output_buffer_size, size_t output_buffer_length)
368{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000369 size_t offset = 0;
370
371 if (output_buffer_size == 0) {
372 /* If output_buffer_size is 0 then we have nothing to do. We must not
373 call memset because output_buffer may be NULL in this case */
374 return;
375 }
376
377 if (status == PSA_SUCCESS) {
378 offset = output_buffer_length;
379 }
380
381 memset(output_buffer + offset, '!', output_buffer_size - offset);
382}
383
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200384
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200385
386
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100387/****************************************************************/
388/* Key management */
389/****************************************************************/
390
Valerio Settid4a5d462023-04-05 18:19:01 +0200391#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100392mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
393 size_t bits,
394 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200395{
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100397 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100399#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100400 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100402#endif
403#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100404 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100406#endif
407#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100408 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100410#endif
411#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100412 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100414#endif
415#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100416 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100418 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (bits_is_sloppy) {
420 return MBEDTLS_ECP_DP_SECP521R1;
421 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100422 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100423#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 }
425 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100426
Paul Elliott8ff510a2020-06-02 17:19:28 +0100427 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100429#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100430 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100432#endif
433#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100434 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100436#endif
437#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100438 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100440#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100441 }
442 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100443
Paul Elliott8ff510a2020-06-02 17:19:28 +0100444 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100446#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (bits_is_sloppy) {
451 return MBEDTLS_ECP_DP_CURVE25519;
452 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100453 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100454#endif
455#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100456 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100458#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100459 }
460 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100461
Paul Elliott8ff510a2020-06-02 17:19:28 +0100462 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100464#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100467#endif
468#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100471#endif
472#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100475#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100476 }
477 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200478 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100479
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100480 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200482}
Valerio Settid4a5d462023-04-05 18:19:01 +0200483#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
486 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487{
488 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200492 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200493 case PSA_KEY_TYPE_PASSWORD:
494 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200495 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100496#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (bits != 128 && bits != 192 && bits != 256) {
499 return PSA_ERROR_INVALID_ARGUMENT;
500 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501 break;
502#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200503#if defined(PSA_WANT_KEY_TYPE_ARIA)
504 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (bits != 128 && bits != 192 && bits != 256) {
506 return PSA_ERROR_INVALID_ARGUMENT;
507 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200508 break;
509#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100510#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (bits != 128 && bits != 192 && bits != 256) {
513 return PSA_ERROR_INVALID_ARGUMENT;
514 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200515 break;
516#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100517#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200518 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (bits != 64 && bits != 128 && bits != 192) {
520 return PSA_ERROR_INVALID_ARGUMENT;
521 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 break;
523#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100524#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200525 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (bits != 256) {
527 return PSA_ERROR_INVALID_ARGUMENT;
528 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200529 break;
530#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200531 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (bits % 8 != 0) {
535 return PSA_ERROR_INVALID_ARGUMENT;
536 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539}
540
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100541/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100542 *
Steven Cooremancd640932021-03-08 12:00:27 +0100543 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
544 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100545 *
546 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100547 * \param[in] key_type The key type of the key to be used with the
548 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 *
550 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100551 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100552 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100553 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100555MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100556 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100558{
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (PSA_ALG_IS_HMAC(algorithm)) {
560 if (key_type == PSA_KEY_TYPE_HMAC) {
561 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100562 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100563 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
566 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
567 * key. */
568 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
569 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
570 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
571 * the block length (larger than 1) for block ciphers. */
572 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
573 return PSA_SUCCESS;
574 }
575 }
576 }
577
578 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100579}
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
582 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200583{
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (slot->key.data != NULL) {
585 return PSA_ERROR_ALREADY_EXISTS;
586 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 slot->key.data = mbedtls_calloc(1, buffer_length);
589 if (slot->key.data == NULL) {
590 return PSA_ERROR_INSUFFICIENT_MEMORY;
591 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200592
Ronald Cronea0f8a62020-11-25 17:52:23 +0100593 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200595}
596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
598 const uint8_t *data,
599 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200600{
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 psa_status_t status = psa_allocate_buffer_to_slot(slot,
602 data_length);
603 if (status != PSA_SUCCESS) {
604 return status;
605 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 memcpy(slot->key.data, data, data_length);
608 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200609}
610
Ronald Crona0fe59f2020-11-29 11:14:53 +0100611psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100612 const psa_key_attributes_t *attributes,
613 const uint8_t *data, size_t data_length,
614 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100616{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
618 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200620 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (data_length == 0) {
622 return PSA_ERROR_NOT_SUPPORTED;
623 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 if (key_type_is_raw_bytes(type)) {
626 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
629 *bits);
630 if (status != PSA_SUCCESS) {
631 return status;
632 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200633
Ronald Crondd04d422020-11-28 15:14:42 +0100634 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100636 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return PSA_SUCCESS;
640 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100641#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
642 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
643 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200644 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100645 return PSA_ERROR_INVALID_ARGUMENT;
646 }
647
648 /* Copy the key material. */
649 memcpy(key_buffer, data, data_length);
650 *key_buffer_length = data_length;
651 *bits = PSA_BYTES_TO_BITS(data_length);
652 (void) key_buffer_size;
653
654 return PSA_SUCCESS;
655 }
656#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) ||
657 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY) */
John Durkop9814fa22020-11-04 12:28:15 -0800658#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
660 if (PSA_KEY_TYPE_IS_ECC(type)) {
661 return mbedtls_psa_ecp_import_key(attributes,
662 data, data_length,
663 key_buffer, key_buffer_size,
664 key_buffer_length,
665 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100666 }
John Durkop9814fa22020-11-04 12:28:15 -0800667#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
668 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700669#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
671 if (PSA_KEY_TYPE_IS_RSA(type)) {
672 return mbedtls_psa_rsa_import_key(attributes,
673 data, data_length,
674 key_buffer, key_buffer_size,
675 key_buffer_length,
676 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200677 }
John Durkop9814fa22020-11-04 12:28:15 -0800678#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
679 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100680 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100683}
684
Gilles Peskinef603c712019-01-19 13:40:11 +0100685/** Calculate the intersection of two algorithm usage policies.
686 *
687 * Return 0 (which allows no operation) on incompatibility.
688 */
689static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100690 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100691 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100693{
Gilles Peskine549ea862019-05-22 11:45:59 +0200694 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (alg1 == alg2) {
696 return alg1;
697 }
Steven Cooreman03488022021-02-18 12:07:20 +0100698 /* If the policies are from the same hash-and-sign family, check
699 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
701 PSA_ALG_IS_SIGN_HASH(alg2) &&
702 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
703 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
704 return alg2;
705 }
706 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
707 return alg1;
708 }
Steven Cooreman03488022021-02-18 12:07:20 +0100709 }
710 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100711 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100712 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
714 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
715 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
716 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
717 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100718 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100719
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100720 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
722 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
723 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
724 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100725 }
726 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
728 (alg1_len <= alg2_len)) {
729 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100730 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
732 (alg2_len <= alg1_len)) {
733 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100734 }
735 }
736 /* If the policies are from the same MAC family, check whether one
737 * of them is a minimum-MAC-length policy. Calculate the most
738 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
740 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
741 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100742 /* Validate the combination of key type and algorithm. Since the base
743 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
745 return 0;
746 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100747
Steven Cooreman31a876d2021-03-03 20:47:40 +0100748 /* Get the (exact or at-least) output lengths for both sides of the
749 * requested intersection. None of the currently supported algorithms
750 * have an output length dependent on the actual key size, so setting it
751 * to a bogus value of 0 is currently OK.
752 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100753 * Note that for at-least-this-length wildcard algorithms, the output
754 * length is set to the shortest allowed length, which allows us to
755 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
757 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100758 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100759
Steven Cooremana1d83222021-02-25 10:20:29 +0100760 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
762 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
763 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100764 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100765
766 /* If only one is an at-least-this-length policy, the intersection would
767 * be the other (fixed-length) policy as long as said fixed length is
768 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
770 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100771 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
773 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100774 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100775
Steven Cooremancd640932021-03-08 12:00:27 +0100776 /* If none of them are wildcards, check whether they define the same tag
777 * length. This is still possible here when one is default-length and
778 * the other specific-length. Ensure to always return the
779 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (alg1_len == alg2_len) {
781 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
782 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100783 }
784 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100786}
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788static int psa_key_algorithm_permits(psa_key_type_t key_type,
789 psa_algorithm_t policy_alg,
790 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200791{
Gilles Peskine549ea862019-05-22 11:45:59 +0200792 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (requested_alg == policy_alg) {
794 return 1;
795 }
Steven Cooreman03488022021-02-18 12:07:20 +0100796 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
797 * and requested_alg is the same hash-and-sign family with any hash,
798 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
800 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
801 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
802 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100803 }
804 /* If policy_alg is a wildcard AEAD algorithm of the same base as
805 * the requested algorithm, check the requested tag length to be
806 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (PSA_ALG_IS_AEAD(policy_alg) &&
808 PSA_ALG_IS_AEAD(requested_alg) &&
809 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
810 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
811 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
812 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
813 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100814 }
Steven Cooremancd640932021-03-08 12:00:27 +0100815 /* If policy_alg is a MAC algorithm of the same base as the requested
816 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (PSA_ALG_IS_MAC(policy_alg) &&
818 PSA_ALG_IS_MAC(requested_alg) &&
819 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
820 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100821 /* Validate the combination of key type and algorithm. Since the policy
822 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
824 return 0;
825 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100826
Steven Cooreman31a876d2021-03-03 20:47:40 +0100827 /* Get both the requested output length for the algorithm which is to be
828 * verified, and the default output length for the base algorithm.
829 * Note that none of the currently supported algorithms have an output
830 * length dependent on actual key size, so setting it to a bogus value
831 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100832 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100834 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 key_type, 0,
836 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100837
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100838 /* If the policy is default-length, only allow an algorithm with
839 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
841 return requested_output_length == default_output_length;
842 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100843
844 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100845 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
847 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
848 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100849 }
850
Steven Cooremancd640932021-03-08 12:00:27 +0100851 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
852 * check for the requested MAC length to be equal to or longer than the
853 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
855 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
856 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100857 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200858 }
Steven Cooremance48e852020-10-05 16:02:45 +0200859 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200860 * a key derivation with that key agreement should also be allowed. This
861 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
863 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
864 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
865 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200866 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100867 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200869}
870
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100871/** Test whether a policy permits an algorithm.
872 *
873 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100874 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100875 * \note This function requires providing the key type for which the policy is
876 * being validated, since some algorithm policy definitions (e.g. MAC)
877 * have different properties depending on what kind of cipher it is
878 * combined with.
879 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100880 * \retval PSA_SUCCESS When \p alg is a specific algorithm
881 * allowed by the \p policy.
882 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
883 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
884 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100885 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
887 psa_key_type_t key_type,
888 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100889{
Steven Cooremand788fab2021-02-25 11:29:17 +0100890 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (alg == 0) {
892 return PSA_ERROR_INVALID_ARGUMENT;
893 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100894
Steven Cooreman7e39f052021-02-23 14:18:32 +0100895 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (PSA_ALG_IS_WILDCARD(alg)) {
897 return PSA_ERROR_INVALID_ARGUMENT;
898 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
901 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
902 return PSA_SUCCESS;
903 } else {
904 return PSA_ERROR_NOT_PERMITTED;
905 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100906}
907
Gilles Peskinef603c712019-01-19 13:40:11 +0100908/** Restrict a key policy based on a constraint.
909 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100910 * \note This function requires providing the key type for which the policy is
911 * being restricted, since some algorithm policy definitions (e.g. MAC)
912 * have different properties depending on what kind of cipher it is
913 * combined with.
914 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100915 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100916 * \param[in,out] policy The policy to restrict.
917 * \param[in] constraint The policy constraint to apply.
918 *
919 * \retval #PSA_SUCCESS
920 * \c *policy contains the intersection of the original value of
921 * \c *policy and \c *constraint.
922 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100923 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100924 * \c *policy is unchanged.
925 */
926static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100927 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100928 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100930{
931 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 psa_key_policy_algorithm_intersection(key_type, policy->alg,
933 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200934 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
936 constraint->alg2);
937 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
938 return PSA_ERROR_INVALID_ARGUMENT;
939 }
940 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
941 return PSA_ERROR_INVALID_ARGUMENT;
942 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100943 policy->usage &= constraint->usage;
944 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200945 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100947}
948
Przemek Stekiel061f6942022-11-30 10:51:35 +0100949/** Get the description of a key given its identifier and policy constraints
950 * and lock it.
951 *
952 * The key must have allow all the usage flags set in \p usage. If \p alg is
953 * nonzero, the key must allow operations with this algorithm. If \p alg is
954 * zero, the algorithm is not checked.
955 *
956 * In case of a persistent key, the function loads the description of the key
957 * into a key slot if not already done.
958 *
959 * On success, the returned key slot is locked. It is the responsibility of
960 * the caller to unlock the key slot when it does not access it anymore.
961 */
962static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100963 mbedtls_svc_key_id_t key,
964 psa_key_slot_t **p_slot,
965 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100967{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100969 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 status = psa_get_and_lock_key_slot(key, p_slot);
972 if (status != PSA_SUCCESS) {
973 return status;
974 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200975 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100976
977 /* Enforce that usage policy for the key slot contains all the flags
978 * required by the usage parameter. There is one exception: public
979 * keys can always be exported, so we treat public key objects as
980 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100982 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100986 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200987 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100988 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100989
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800990 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if (alg != 0) {
992 status = psa_key_policy_permits(&slot->attr.policy,
993 slot->attr.type,
994 alg);
995 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100996 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100998 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001
1002error:
1003 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001007}
Darryl Green940d72c2018-07-13 13:18:51 +01001008
Ronald Cron5c522922020-11-14 16:35:34 +01001009/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001010 *
1011 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001012 * available, as opposed to a key in a secure element and/or to be used
1013 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001015 * This is a temporary function that may be used instead of
1016 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1017 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001018 *
Ronald Cron5c522922020-11-14 16:35:34 +01001019 * On success, the returned key slot is locked. It is the responsibility of the
1020 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001021 */
Ronald Cron5c522922020-11-14 16:35:34 +01001022static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1023 mbedtls_svc_key_id_t key,
1024 psa_key_slot_t **p_slot,
1025 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001027{
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1029 usage, alg);
1030 if (status != PSA_SUCCESS) {
1031 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001032 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1035 psa_unlock_key_slot(*p_slot);
1036 *p_slot = NULL;
1037 return PSA_ERROR_NOT_SUPPORTED;
1038 }
1039
1040 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001041}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001044{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001045 /* Data pointer will always be either a valid pointer or NULL in an
1046 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (slot->key.data != NULL) {
1048 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1049 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001052 slot->key.data = NULL;
1053 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001056}
1057
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001058/** Completely wipe a slot in memory, including its policy.
1059 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001061{
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 /*
1065 * As the return error code may not be handled in case of multiple errors,
1066 * do our best to report an unexpected lock counter. Assert with
1067 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1068 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1069 * function is called as part of the execution of a test suite, the
1070 * execution of the test suite is stopped in error if the assertion fails.
1071 */
1072 if (slot->lock_count != 1) {
1073 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001074 status = PSA_ERROR_CORRUPTION_DETECTED;
1075 }
1076
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001077 /* Multipart operations may still be using the key. This is safe
1078 * because all multipart operation objects are independent from
1079 * the key slot: if they need to access the key after the setup
1080 * phase, they have a copy of the key. Note that this means that
1081 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001082 /* At this point, key material and other type-specific content has
1083 * been wiped. Clear remaining metadata. We can call memset and not
1084 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 memset(slot, 0, sizeof(*slot));
1086 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001087}
1088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001090{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001091 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001092 psa_status_t status; /* status of the last operation */
1093 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001094#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1095 psa_se_drv_table_entry_t *driver;
1096#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (mbedtls_svc_key_id_is_null(key)) {
1099 return PSA_SUCCESS;
1100 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001101
Ronald Cronf2911112020-10-29 17:51:10 +01001102 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001103 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001104 * key, this will load the key description from persistent memory if not
1105 * done yet. We cannot avoid this loading as without it we don't know if
1106 * the key is operated by an SE or not and this information is needed by
1107 * the current implementation.
1108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 status = psa_get_and_lock_key_slot(key, &slot);
1110 if (status != PSA_SUCCESS) {
1111 return status;
1112 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001113
Ronald Cronf2911112020-10-29 17:51:10 +01001114 /*
1115 * If the key slot containing the key description is under access by the
1116 * library (apart from the present access), the key cannot be destroyed
1117 * yet. For the time being, just return in error. Eventually (to be
1118 * implemented), the key should be destroyed when all accesses have
1119 * stopped.
1120 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (slot->lock_count > 1) {
1122 psa_unlock_key_slot(slot);
1123 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001124 }
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001127 /* Refuse the destruction of a read-only key (which may or may not work
1128 * if we attempt it, depending on whether the key is merely read-only
1129 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001130 * Just do the best we can, which is to wipe the copy in memory
1131 * (done in this function's cleanup code). */
1132 overall_status = PSA_ERROR_NOT_PERMITTED;
1133 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001134 }
1135
Gilles Peskine354f7672019-07-12 23:46:38 +02001136#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1138 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001139 /* For a key in a secure element, we need to do three things:
1140 * remove the key file in internal storage, destroy the
1141 * key inside the secure element, and update the driver's
1142 * persistent data. Start a transaction that will encompass these
1143 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001145 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001147 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 status = psa_crypto_save_transaction();
1149 if (status != PSA_SUCCESS) {
1150 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001151 /* We should still try to destroy the key in the secure
1152 * element and the key metadata in storage. This is especially
1153 * important if the error is that the storage is full.
1154 * But how to do it exactly without risking an inconsistent
1155 * state after a reset?
1156 * https://github.com/ARMmbed/mbed-crypto/issues/215
1157 */
1158 overall_status = status;
1159 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001160 }
1161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 status = psa_destroy_se_key(driver,
1163 psa_key_slot_get_slot_number(slot));
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 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001168#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1169
Darryl Greend49a4992018-06-18 17:27:26 +01001170#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1172 status = psa_destroy_persistent_key(slot->attr.id);
1173 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001174 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001176
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001177 /* TODO: other slots may have a copy of the same key. We should
1178 * invalidate them.
1179 * https://github.com/ARMmbed/mbed-crypto/issues/214
1180 */
Darryl Greend49a4992018-06-18 17:27:26 +01001181 }
1182#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001183
Gilles Peskinefc762652019-07-22 19:30:34 +02001184#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if (driver != NULL) {
1186 status = psa_save_se_persistent_data(driver);
1187 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001188 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 }
1190 status = psa_crypto_stop_transaction();
1191 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001192 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001194 }
1195#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1196
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001199 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001201 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 }
1203 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001204}
1205
John Durkop07cc04a2020-11-16 22:08:34 -08001206#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001207 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001208static psa_status_t psa_get_rsa_public_exponent(
1209 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001211{
1212 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001214 uint8_t *buffer = NULL;
1215 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1219 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 }
1222 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001223 /* It's the default value, which is reported as an empty string,
1224 * so there's nothing to do. */
1225 goto exit;
1226 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 buflen = mbedtls_mpi_size(&mpi);
1229 buffer = mbedtls_calloc(1, buflen);
1230 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1232 goto exit;
1233 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1235 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001238 attributes->domain_parameters = buffer;
1239 attributes->domain_parameters_size = buflen;
1240
1241exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 mbedtls_mpi_free(&mpi);
1243 if (ret != 0) {
1244 mbedtls_free(buffer);
1245 }
1246 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001247}
John Durkop07cc04a2020-11-16 22:08:34 -08001248#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001249 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001250
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001251/** Retrieve all the publicly-accessible attributes of a key.
1252 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001253psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1254 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001255{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001256 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001257 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001258 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1263 if (status != PSA_SUCCESS) {
1264 return status;
1265 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001266
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001267 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1269 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001270
1271#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1273 psa_set_key_slot_number(attributes,
1274 psa_key_slot_get_slot_number(slot));
1275 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001276#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001279#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001280 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001281 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001282 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001283 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001284 * is not yet implemented.
1285 * https://github.com/ARMmbed/mbed-crypto/issues/216
1286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001288 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001289
Ronald Cron90857082020-11-25 14:58:33 +01001290 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 slot->attr.type,
1292 slot->key.data,
1293 slot->key.bytes,
1294 &rsa);
1295 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001296 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 status = psa_get_rsa_public_exponent(rsa,
1300 attributes);
1301 mbedtls_rsa_free(rsa);
1302 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001303 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001304 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001305#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001306 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001307 default:
1308 /* Nothing else to do. */
1309 break;
1310 }
1311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (status != PSA_SUCCESS) {
1313 psa_reset_key_attributes(attributes);
1314 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001319}
1320
Gilles Peskinec8000c02019-08-02 20:15:51 +02001321#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1322psa_status_t psa_get_key_slot_number(
1323 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001325{
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001327 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 return PSA_SUCCESS;
1329 } else {
1330 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001331 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001332}
1333#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1336 size_t key_buffer_size,
1337 uint8_t *data,
1338 size_t data_size,
1339 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001340{
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 if (key_buffer_size > data_size) {
1342 return PSA_ERROR_BUFFER_TOO_SMALL;
1343 }
1344 memcpy(data, key_buffer, key_buffer_size);
1345 memset(data + key_buffer_size, 0,
1346 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001347 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001349}
1350
Ronald Cron7285cda2020-11-26 14:46:37 +01001351psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001352 const psa_key_attributes_t *attributes,
1353 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355{
Ronald Crond18b5f82020-11-25 16:46:05 +01001356 psa_key_type_t type = attributes->core.type;
1357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (key_type_is_raw_bytes(type) ||
1359 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001360 PSA_KEY_TYPE_IS_ECC(type) ||
1361 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return psa_export_key_buffer_internal(
1363 key_buffer, key_buffer_size,
1364 data, data_size, data_length);
1365 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001366 /* This shouldn't happen in the reference implementation, but
1367 it is valid for a special-purpose implementation to omit
1368 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001370 }
1371}
1372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1374 uint8_t *data,
1375 size_t data_size,
1376 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001377{
1378 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1379 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1380 psa_key_slot_t *slot;
1381
Ronald Crone907e552021-01-18 13:22:38 +01001382 /* Reject a zero-length output buffer now, since this can never be a
1383 * valid key representation. This way we know that data must be a valid
1384 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if (data_size == 0) {
1386 return PSA_ERROR_BUFFER_TOO_SMALL;
1387 }
Ronald Crone907e552021-01-18 13:22:38 +01001388
Ronald Cron9486f9d2020-11-26 13:36:23 +01001389 /* Set the key to empty now, so that even when there are errors, we always
1390 * set data_length to a value between 0 and data_size. On error, setting
1391 * the key to empty is a good choice because an empty key representation is
1392 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001393 *data_length = 0;
1394
Ronald Cron9486f9d2020-11-26 13:36:23 +01001395 /* Export requires the EXPORT flag. There is an exception for public keys,
1396 * which don't require any flag, but
1397 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1398 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1400 PSA_KEY_USAGE_EXPORT, 0);
1401 if (status != PSA_SUCCESS) {
1402 return status;
1403 }
mohammad160306e79202018-03-28 13:17:44 +03001404
Ronald Crond18b5f82020-11-25 16:46:05 +01001405 psa_key_attributes_t attributes = {
1406 .core = slot->attr
1407 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 status = psa_driver_wrapper_export_key(&attributes,
1409 slot->key.data, slot->key.bytes,
1410 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001411
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001415}
1416
Ronald Cron7285cda2020-11-26 14:46:37 +01001417psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001418 const psa_key_attributes_t *attributes,
1419 const uint8_t *key_buffer,
1420 size_t key_buffer_size,
1421 uint8_t *data,
1422 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001424{
Ronald Crond18b5f82020-11-25 16:46:05 +01001425 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001426
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001427 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
1428 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1429 PSA_KEY_TYPE_IS_DH(type))) {
1430 /* Exporting public -> public */
1431 return psa_export_key_buffer_internal(
1432 key_buffer, key_buffer_size,
1433 data, data_size, data_length);
1434 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001435#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1437 return mbedtls_psa_rsa_export_public_key(attributes,
1438 key_buffer,
1439 key_buffer_size,
1440 data,
1441 data_size,
1442 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001443#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001444 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001446#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1447 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001448 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
John Durkop6ba40d12020-11-10 08:50:04 -08001449#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1451 return mbedtls_psa_ecp_export_public_key(attributes,
1452 key_buffer,
1453 key_buffer_size,
1454 data,
1455 data_size,
1456 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001457#else
1458 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001460#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1461 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001462 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001463#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) || \
1464 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY)
Przemek Stekiel359f4622022-12-05 14:11:55 +01001465 return mbedtls_psa_export_ffdh_public_key(attributes,
1466 key_buffer,
1467 key_buffer_size,
1468 data, data_size,
1469 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001470#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001471 return PSA_ERROR_NOT_SUPPORTED;
1472#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) ||
1473 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001476 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001477}
Ronald Crond18b5f82020-11-25 16:46:05 +01001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1480 uint8_t *data,
1481 size_t data_size,
1482 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001483{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001484 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001485 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001486 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001487
Ronald Crone907e552021-01-18 13:22:38 +01001488 /* Reject a zero-length output buffer now, since this can never be a
1489 * valid key representation. This way we know that data must be a valid
1490 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (data_size == 0) {
1492 return PSA_ERROR_BUFFER_TOO_SMALL;
1493 }
Ronald Crone907e552021-01-18 13:22:38 +01001494
Darryl Greendd8fb772018-11-07 16:00:44 +00001495 /* Set the key to empty now, so that even when there are errors, we always
1496 * set data_length to a value between 0 and data_size. On error, setting
1497 * the key to empty is a good choice because an empty key representation is
1498 * unlikely to be accepted anywhere. */
1499 *data_length = 0;
1500
1501 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1503 if (status != PSA_SUCCESS) {
1504 return status;
1505 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1508 status = PSA_ERROR_INVALID_ARGUMENT;
1509 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001510 }
1511
Ronald Crond18b5f82020-11-25 16:46:05 +01001512 psa_key_attributes_t attributes = {
1513 .core = slot->attr
1514 };
Ronald Cron67227982020-11-26 15:16:05 +01001515 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001516 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001518
1519exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001523}
1524
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001525MBEDTLS_STATIC_ASSERT(
1526 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1527 "One or more key attribute flag is listed as both external-only and dual-use")
1528MBEDTLS_STATIC_ASSERT(
1529 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1530 "One or more key attribute flag is listed as both internal-only and dual-use")
1531MBEDTLS_STATIC_ASSERT(
1532 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1533 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001534
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001535/** Validate that a key policy is internally well-formed.
1536 *
1537 * This function only rejects invalid policies. It does not validate the
1538 * consistency of the policy with respect to other attributes of the key
1539 * such as the key type.
1540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001542{
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1544 PSA_KEY_USAGE_COPY |
1545 PSA_KEY_USAGE_ENCRYPT |
1546 PSA_KEY_USAGE_DECRYPT |
1547 PSA_KEY_USAGE_SIGN_MESSAGE |
1548 PSA_KEY_USAGE_VERIFY_MESSAGE |
1549 PSA_KEY_USAGE_SIGN_HASH |
1550 PSA_KEY_USAGE_VERIFY_HASH |
1551 PSA_KEY_USAGE_VERIFY_DERIVATION |
1552 PSA_KEY_USAGE_DERIVE)) != 0) {
1553 return PSA_ERROR_INVALID_ARGUMENT;
1554 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001557}
1558
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001559/** Validate the internal consistency of key attributes.
1560 *
1561 * This function only rejects invalid attribute values. If does not
1562 * validate the consistency of the attributes with any key data that may
1563 * be involved in the creation of the key.
1564 *
1565 * Call this function early in the key creation process.
1566 *
1567 * \param[in] attributes Key attributes for the new key.
1568 * \param[out] p_drv On any return, the driver for the key, if any.
1569 * NULL for a transparent key.
1570 *
1571 */
1572static psa_status_t psa_validate_key_attributes(
1573 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001575{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001576 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1578 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 status = psa_validate_key_location(lifetime, p_drv);
1581 if (status != PSA_SUCCESS) {
1582 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001583 }
1584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 status = psa_validate_key_persistence(lifetime);
1586 if (status != PSA_SUCCESS) {
1587 return status;
1588 }
1589
1590 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1591 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1592 return PSA_ERROR_INVALID_ARGUMENT;
1593 }
1594 } else {
1595 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1596 return PSA_ERROR_INVALID_ARGUMENT;
1597 }
1598 }
1599
1600 status = psa_validate_key_policy(&attributes->core.policy);
1601 if (status != PSA_SUCCESS) {
1602 return status;
1603 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001604
1605 /* Refuse to create overly large keys.
1606 * Note that this doesn't trigger on import if the attributes don't
1607 * explicitly specify a size (so psa_get_key_bits returns 0), so
1608 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1610 return PSA_ERROR_NOT_SUPPORTED;
1611 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001612
Gilles Peskine91e8c332019-08-02 19:19:39 +02001613 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1615 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1616 return PSA_ERROR_INVALID_ARGUMENT;
1617 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001620}
1621
Gilles Peskine4747d192019-04-17 15:05:45 +02001622/** Prepare a key slot to receive key material.
1623 *
1624 * This function allocates a key slot and sets its metadata.
1625 *
1626 * If this function fails, call psa_fail_key_creation().
1627 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001628 * This function is intended to be used as follows:
1629 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001630 * it with the specified attributes, and in case of a volatile key assign it
1631 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001632 * -# Populate the slot with the key material.
1633 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1634 * In case of failure at any step, stop the sequence and call
1635 * psa_fail_key_creation().
1636 *
Ronald Cron5c522922020-11-14 16:35:34 +01001637 * On success, the key slot is locked. It is the responsibility of the caller
1638 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001639 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001640 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001641 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001642 * \param[out] p_slot On success, a pointer to the prepared slot.
1643 * \param[out] p_drv On any return, the driver for the key, if any.
1644 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001645 *
1646 * \retval #PSA_SUCCESS
1647 * The key slot is ready to receive key material.
1648 * \return If this function fails, the key slot is an invalid state.
1649 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001650 */
1651static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001652 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001653 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001654 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001656{
1657 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001658 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001659 psa_key_slot_t *slot;
1660
Gilles Peskinedf179142019-07-15 22:02:14 +02001661 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001662 *p_drv = NULL;
1663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 status = psa_validate_key_attributes(attributes, p_drv);
1665 if (status != PSA_SUCCESS) {
1666 return status;
1667 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1670 if (status != PSA_SUCCESS) {
1671 return status;
1672 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001673 slot = *p_slot;
1674
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001675 /* We're storing the declared bit-size of the key. It's up to each
1676 * creation mechanism to verify that this information is correct.
1677 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001678 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001679 * is optional (import, copy). In case of a volatile key, assign it the
1680 * volatile key identifier associated to the slot returned to contain its
1681 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001682
1683 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001685#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1686 slot->attr.id = volatile_key_id;
1687#else
1688 slot->attr.id.key_id = volatile_key_id;
1689#endif
1690 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001691
Gilles Peskine91e8c332019-08-02 19:19:39 +02001692 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001693 * external-only flags, query `attributes`. Thanks to the check
1694 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001695 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001696 * may have set. */
1697 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001698
Gilles Peskinecbaff462019-07-12 23:46:04 +02001699#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001700 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001701 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001702 * create the key file in internal storage, create the
1703 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001704 * persistent data. This is done by starting a transaction that will
1705 * encompass these three actions.
1706 * For registering a volatile key, we just need to find an appropriate
1707 * slot number inside the SE. Since the key is designated volatile, creating
1708 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001709 /* The first thing to do is to find a slot number for the new key.
1710 * We save the slot number in persistent storage as part of the
1711 * transaction data. It will be needed to recover if the power
1712 * fails during the key creation process, to clean up on the secure
1713 * element side after restarting. Obtaining a slot number from the
1714 * secure element driver updates its persistent state, but we do not yet
1715 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001716 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001718 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1720 &slot_number);
1721 if (status != PSA_SUCCESS) {
1722 return status;
1723 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1726 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001727 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001728 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001729 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 status = psa_crypto_save_transaction();
1731 if (status != PSA_SUCCESS) {
1732 (void) psa_crypto_stop_transaction();
1733 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001734 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001735 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001736
1737 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001739 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001742 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001744 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001745#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001748}
Gilles Peskine4747d192019-04-17 15:05:45 +02001749
1750/** Finalize the creation of a key once its key material has been set.
1751 *
1752 * This entails writing the key to persistent storage.
1753 *
1754 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001755 * See the documentation of psa_start_key_creation() for the intended use
1756 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001757 *
Ronald Cron5c522922020-11-14 16:35:34 +01001758 * If the finalization succeeds, the function unlocks the key slot (it was
1759 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1760 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001761 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001762 * \param[in,out] slot Pointer to the slot with key material.
1763 * \param[in] driver The secure element driver for the key,
1764 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001765 * \param[out] key On success, identifier of the key. Note that the
1766 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001767 *
1768 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001769 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001770 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1771 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1772 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1773 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1774 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1775 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001776 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001777 * \return If this function fails, the key slot is an invalid state.
1778 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001779 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001780static psa_status_t psa_finish_key_creation(
1781 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001782 psa_se_drv_table_entry_t *driver,
1783 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001784{
1785 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001786 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001787 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001788
1789#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001791#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001793 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001794 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001796
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001797 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1798 sizeof(data.slot_number),
1799 "Slot number size does not match psa_se_key_data_storage_t");
1800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1802 status = psa_save_persistent_key(&slot->attr,
1803 (uint8_t *) &data,
1804 sizeof(data));
1805 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001806#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1807 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001808 /* Key material is saved in export representation in the slot, so
1809 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 status = psa_save_persistent_key(&slot->attr,
1811 slot->key.data,
1812 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001813 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001814 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001815#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1816
Gilles Peskinecbaff462019-07-12 23:46:04 +02001817#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001818 /* Finish the transaction for a key creation. This does not
1819 * happen when registering an existing key. Detect this case
1820 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001821 * creation of a persistent key in a secure element requires a transaction,
1822 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (driver != NULL &&
1824 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1825 status = psa_save_se_persistent_data(driver);
1826 if (status != PSA_SUCCESS) {
1827 psa_destroy_persistent_key(slot->attr.id);
1828 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001829 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001831 }
1832#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001835 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 status = psa_unlock_key_slot(slot);
1837 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001838 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001840 }
Ronald Cron50972942020-11-14 11:28:25 +01001841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001843}
1844
1845/** Abort the creation of a key.
1846 *
1847 * You may call this function after calling psa_start_key_creation(),
1848 * or after psa_finish_key_creation() fails. In other circumstances, this
1849 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001850 * See the documentation of psa_start_key_creation() for the intended use
1851 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001852 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001853 * \param[in,out] slot Pointer to the slot with key material.
1854 * \param[in] driver The secure element driver for the key,
1855 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001856 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857static void psa_fail_key_creation(psa_key_slot_t *slot,
1858 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001859{
Gilles Peskine011e4282019-06-26 18:34:38 +02001860 (void) driver;
1861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001863 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001865
1866#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001867 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001868 * element, and the failure happened later (when saving metadata
1869 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001870 * element.
1871 * https://github.com/ARMmbed/mbed-crypto/issues/217
1872 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001873
Gilles Peskined7729582019-08-05 15:55:54 +02001874 /* Abort the ongoing transaction if any (there may not be one if
1875 * the creation process failed before starting one, or if the
1876 * key creation is a registration of a key in a secure element).
1877 * Earlier functions must already have done what it takes to undo any
1878 * partial creation. All that's left is to update the transaction data
1879 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001881#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001884}
1885
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001886/** Validate optional attributes during key creation.
1887 *
1888 * Some key attributes are optional during key creation. If they are
1889 * specified in the attributes structure, check that they are consistent
1890 * with the data in the slot.
1891 *
1892 * This function should be called near the end of key creation, after
1893 * the slot in memory is fully populated but before saving persistent data.
1894 */
1895static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898{
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 if (attributes->core.type != 0) {
1900 if (attributes->core.type != slot->attr.type) {
1901 return PSA_ERROR_INVALID_ARGUMENT;
1902 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001903 }
1904
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001906#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1908 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001909 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001910 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001912
Ronald Cron90857082020-11-25 14:58:33 +01001913 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 slot->attr.type,
1915 slot->key.data,
1916 slot->key.bytes,
1917 &rsa);
1918 if (status != PSA_SUCCESS) {
1919 return status;
1920 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 mbedtls_mpi_init(&actual);
1923 mbedtls_mpi_init(&required);
1924 ret = mbedtls_rsa_export(rsa,
1925 NULL, NULL, NULL, NULL, &actual);
1926 mbedtls_rsa_free(rsa);
1927 mbedtls_free(rsa);
1928 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001929 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 }
1931 ret = mbedtls_mpi_read_binary(&required,
1932 attributes->domain_parameters,
1933 attributes->domain_parameters_size);
1934 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001935 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 }
1937 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001938 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 }
1940rsa_exit:
1941 mbedtls_mpi_free(&actual);
1942 mbedtls_mpi_free(&required);
1943 if (ret != 0) {
1944 return mbedtls_to_psa_error(ret);
1945 }
1946 } else
John Durkop9814fa22020-11-04 12:28:15 -08001947#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1948 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 }
1952 }
1953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 if (attributes->core.bits != 0) {
1955 if (attributes->core.bits != slot->attr.bits) {
1956 return PSA_ERROR_INVALID_ARGUMENT;
1957 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001958 }
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001961}
1962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1964 const uint8_t *data,
1965 size_t data_length,
1966 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001967{
1968 psa_status_t status;
1969 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001970 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001971 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301972 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001973
Ronald Cron81709fc2020-11-14 12:10:32 +01001974 *key = MBEDTLS_SVC_KEY_ID_INIT;
1975
Gilles Peskine0f84d622019-09-12 19:03:13 +02001976 /* Reject zero-length symmetric keys (including raw data key objects).
1977 * This also rejects any key which might be encoded as an empty string,
1978 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (data_length == 0) {
1980 return PSA_ERROR_INVALID_ARGUMENT;
1981 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001982
Archana449608b2021-09-08 15:36:05 +05301983 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (data_length > SIZE_MAX / 8) {
1985 return PSA_ERROR_NOT_SUPPORTED;
1986 }
Archana6ed4bda2021-08-04 10:47:15 +05301987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1989 &slot, &driver);
1990 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001993
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001994 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301995 * storage ( thus not in the case of importing a key in a secure element
1996 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1997 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 if (slot->key.data == NULL) {
1999 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302000 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 attributes, data, data_length, &storage_size);
2002 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302003 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 }
Archanad8a83dc2021-06-14 10:04:16 +05302005 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 status = psa_allocate_buffer_to_slot(slot, storage_size);
2007 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002008 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002010 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002011
2012 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 status = psa_driver_wrapper_import_key(attributes,
2014 data, data_length,
2015 slot->key.data,
2016 slot->key.bytes,
2017 &slot->key.bytes, &bits);
2018 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002019 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002023 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002025 status = PSA_ERROR_INVALID_ARGUMENT;
2026 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002027 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002028
Archana6ed4bda2021-08-04 10:47:15 +05302029 /* Enforce a size limit, and in particular ensure that the bit
2030 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302032 status = PSA_ERROR_NOT_SUPPORTED;
2033 goto exit;
2034 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 status = psa_validate_optional_attributes(slot, attributes);
2036 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002037 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 if (status != PSA_SUCCESS) {
2043 psa_fail_key_creation(slot, driver);
2044 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002047}
2048
Gilles Peskined7729582019-08-05 15:55:54 +02002049#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2050psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002052{
2053 psa_status_t status;
2054 psa_key_slot_t *slot = NULL;
2055 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002056 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002057
2058 /* Leaving attributes unspecified is not currently supported.
2059 * It could make sense to query the key type and size from the
2060 * secure element, but not all secure elements support this
2061 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2063 return PSA_ERROR_NOT_SUPPORTED;
2064 }
2065 if (psa_get_key_bits(attributes) == 0) {
2066 return PSA_ERROR_NOT_SUPPORTED;
2067 }
Gilles Peskined7729582019-08-05 15:55:54 +02002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2070 &slot, &driver);
2071 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002072 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 }
Gilles Peskined7729582019-08-05 15:55:54 +02002074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002076
2077exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (status != PSA_SUCCESS) {
2079 psa_fail_key_creation(slot, driver);
2080 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002081
Gilles Peskined7729582019-08-05 15:55:54 +02002082 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 psa_close_key(key);
2084 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002085}
2086#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2089 const psa_key_attributes_t *specified_attributes,
2090 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002091{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002093 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002094 psa_key_slot_t *source_slot = NULL;
2095 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002096 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002097 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302098 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002099
Ronald Cron81709fc2020-11-14 12:10:32 +01002100 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2101
Archana8a180362021-07-05 02:18:48 +05302102 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2104 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002105 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 status = psa_validate_optional_attributes(source_slot,
2109 specified_attributes);
2110 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002113
Archana9d17bf42021-09-10 06:22:44 +05302114 /* The target key type and number of bits have been validated by
2115 * psa_validate_optional_attributes() to be either equal to zero or
2116 * equal to the ones of the source key. So it is safe to inherit
2117 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302118 * */
Archana9d17bf42021-09-10 06:22:44 +05302119 actual_attributes.core.bits = source_slot->attr.bits;
2120 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302121
2122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 status = psa_restrict_key_policy(source_slot->attr.type,
2124 &actual_attributes.core.policy,
2125 &source_slot->attr.policy);
2126 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2131 &target_slot, &driver);
2132 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 }
2135 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2136 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302137 /*
Archana9d17bf42021-09-10 06:22:44 +05302138 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302139 * the source key would need to be exported as plaintext and re-imported
2140 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302141 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302142 * appropriate API invocations from the application, if needed.
2143 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002144 status = PSA_ERROR_NOT_SUPPORTED;
2145 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002146 }
Archana8a180362021-07-05 02:18:48 +05302147 /*
2148 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302149 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302150 * - For opaque keys this translates to an invocation of the drivers'
2151 * copy_key entry point through the dispatch layer.
2152 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2154 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2155 &storage_size);
2156 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302157 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 }
Archana374fe5b2021-09-08 15:50:28 +05302159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2161 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 }
Archana374fe5b2021-09-08 15:50:28 +05302164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 status = psa_driver_wrapper_copy_key(&actual_attributes,
2166 source_slot->key.data,
2167 source_slot->key.bytes,
2168 target_slot->key.data,
2169 target_slot->key.bytes,
2170 &target_slot->key.bytes);
2171 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302172 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 }
2174 } else {
2175 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302176 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 source_slot->key.bytes);
2178 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302179 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 }
Archana8a180362021-07-05 02:18:48 +05302181 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (status != PSA_SUCCESS) {
2185 psa_fail_key_creation(target_slot, driver);
2186 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002191}
2192
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002193
2194
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002195/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002196/* Message digests */
2197/****************************************************************/
2198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002200{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002201 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (operation->id == 0) {
2203 return PSA_SUCCESS;
2204 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002207 operation->id = 0;
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002210}
2211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2213 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002214{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002215 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002216
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002217 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002219 status = PSA_ERROR_BAD_STATE;
2220 goto exit;
2221 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002224 status = PSA_ERROR_INVALID_ARGUMENT;
2225 goto exit;
2226 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002227
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002228 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2229 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002233
2234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 if (status != PSA_SUCCESS) {
2236 psa_hash_abort(operation);
2237 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002238
2239 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002240}
2241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2243 const uint8_t *input,
2244 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002249 status = PSA_ERROR_BAD_STATE;
2250 goto exit;
2251 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002252
Steven Cooremanc8288352021-03-04 14:02:19 +01002253 /* Don't require hash implementations to behave correctly on a
2254 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if (input_length == 0) {
2256 return PSA_SUCCESS;
2257 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002260
2261exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (status != PSA_SUCCESS) {
2263 psa_hash_abort(operation);
2264 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267}
2268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2270 uint8_t *hash,
2271 size_t hash_size,
2272 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002274 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 if (operation->id == 0) {
2276 return PSA_ERROR_BAD_STATE;
2277 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278
Steven Cooreman1e582352021-02-18 17:24:37 +01002279 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 operation, hash, hash_size, hash_length);
2281 psa_hash_abort(operation);
2282 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002283}
2284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2286 const uint8_t *hash,
2287 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288{
Ronald Cron69a63422021-10-18 09:47:58 +02002289 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002291 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 operation,
2293 actual_hash, sizeof(actual_hash),
2294 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002297 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002301 status = PSA_ERROR_INVALID_SIGNATURE;
2302 goto exit;
2303 }
2304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002306 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002308
2309exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2311 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002312 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316}
2317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318psa_status_t psa_hash_compute(psa_algorithm_t alg,
2319 const uint8_t *input, size_t input_length,
2320 uint8_t *hash, size_t hash_size,
2321 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002322{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002323 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 if (!PSA_ALG_IS_HASH(alg)) {
2325 return PSA_ERROR_INVALID_ARGUMENT;
2326 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2329 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002330}
2331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332psa_status_t psa_hash_compare(psa_algorithm_t alg,
2333 const uint8_t *input, size_t input_length,
2334 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002335{
Ronald Cron69a63422021-10-18 09:47:58 +02002336 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002337 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 if (!PSA_ALG_IS_HASH(alg)) {
2340 return PSA_ERROR_INVALID_ARGUMENT;
2341 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002342
Steven Cooreman1e582352021-02-18 17:24:37 +01002343 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 alg, input, input_length,
2345 actual_hash, sizeof(actual_hash),
2346 &actual_hash_length);
2347 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002348 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 }
2350 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002351 status = PSA_ERROR_INVALID_SIGNATURE;
2352 goto exit;
2353 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002355 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002357
2358exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2360 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002361}
2362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2364 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002365{
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 if (source_operation->id == 0 ||
2367 target_operation->id != 0) {
2368 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002369 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2372 target_operation);
2373 if (status != PSA_SUCCESS) {
2374 psa_hash_abort(target_operation);
2375 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002378}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002379
2380
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002382/* MAC */
2383/****************************************************************/
2384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002386{
Steven Cooremane6804192021-03-19 18:28:56 +01002387 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 if (operation->id == 0) {
2389 return PSA_SUCCESS;
2390 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002393 operation->mac_size = 0;
2394 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002395 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002398}
2399
Ronald Cron2dff3b22021-06-17 16:33:22 +02002400static psa_status_t psa_mac_finalize_alg_and_key_validation(
2401 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002402 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002405 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 psa_key_type_t key_type = psa_get_key_type(attributes);
2407 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 if (!PSA_ALG_IS_MAC(alg)) {
2410 return PSA_ERROR_INVALID_ARGUMENT;
2411 }
Steven Cooremane6804192021-03-19 18:28:56 +01002412
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002413 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 status = psa_mac_key_can_do(alg, key_type);
2415 if (status != PSA_SUCCESS) {
2416 return status;
2417 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002418
Steven Cooremand1a68f12021-05-10 11:13:41 +02002419 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002423 /* A very short MAC is too short for security since it can be
2424 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2425 * so we make this our minimum, even though 32 bits is still
2426 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002428 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2431 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002432 /* It's impossible to "truncate" to a larger length than the full length
2433 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002435 }
2436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002438 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2439 * that is disabled in the compile-time configuration. The result can
2440 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2441 * configuration into account. In this case, force a return of
2442 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2443 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2444 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2445 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2446 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002448 }
2449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002451}
2452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2454 mbedtls_svc_key_id_t key,
2455 psa_algorithm_t alg,
2456 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002457{
2458 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2459 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002460 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461
2462 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002464 status = PSA_ERROR_BAD_STATE;
2465 goto exit;
2466 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002467
2468 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 key,
2470 &slot,
2471 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2472 alg);
2473 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002474 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002476
2477 psa_key_attributes_t attributes = {
2478 .core = slot->attr
2479 };
2480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2482 &operation->mac_size);
2483 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486
2487 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002488 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 if (is_sign) {
2490 status = psa_driver_wrapper_mac_sign_setup(operation,
2491 &attributes,
2492 slot->key.data,
2493 slot->key.bytes,
2494 alg);
2495 } else {
2496 status = psa_driver_wrapper_mac_verify_setup(operation,
2497 &attributes,
2498 slot->key.data,
2499 slot->key.bytes,
2500 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002502
Gilles Peskinefbfac682018-07-08 20:51:54 +02002503exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 if (status != PSA_SUCCESS) {
2505 psa_mac_abort(operation);
2506 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002511}
2512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2514 mbedtls_svc_key_id_t key,
2515 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002516{
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002518}
2519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2521 mbedtls_svc_key_id_t key,
2522 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002523{
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002525}
2526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2528 const uint8_t *input,
2529 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002530{
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (operation->id == 0) {
2532 return PSA_ERROR_BAD_STATE;
2533 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002535 /* Don't require hash implementations to behave correctly on a
2536 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 if (input_length == 0) {
2538 return PSA_SUCCESS;
2539 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2542 input, input_length);
2543 if (status != PSA_SUCCESS) {
2544 psa_mac_abort(operation);
2545 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548}
2549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2551 uint8_t *mac,
2552 size_t mac_size,
2553 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002554{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002555 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2556 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002559 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002560 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002561 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002564 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002565 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002566 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002567
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002568 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2569 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002571 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002572 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002573 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002576 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002577 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002578 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 status = psa_driver_wrapper_mac_sign_finish(operation,
2581 mac, operation->mac_size,
2582 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002583
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002584exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002585 /* In case of success, set the potential excess room in the output buffer
2586 * to an invalid value, to avoid potentially leaking a longer MAC.
2587 * In case of error, set the output length and content to a safe default,
2588 * such that in case the caller misses an error check, the output would be
2589 * an unachievable MAC.
2590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002592 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002593 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002594 }
mohammad16036df908f2018-04-02 08:34:15 -07002595
Paul Elliottdc42ca82023-02-24 18:11:59 +00002596 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002601}
2602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2604 const uint8_t *mac,
2605 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002607 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2608 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002611 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002612 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002613 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002616 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002617 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002618 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002621 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002622 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002623 }
2624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 status = psa_driver_wrapper_mac_verify_finish(operation,
2626 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002627
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002628exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632}
2633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2635 psa_algorithm_t alg,
2636 const uint8_t *input,
2637 size_t input_length,
2638 uint8_t *mac,
2639 size_t mac_size,
2640 size_t *mac_length,
2641 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002642{
2643 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002644 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2645 psa_key_slot_t *slot;
2646 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002647
Ronald Cron51131b52021-06-17 17:17:20 +02002648 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 key,
2650 &slot,
2651 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2652 alg);
2653 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002654 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002656
Ronald Cron51131b52021-06-17 17:17:20 +02002657 psa_key_attributes_t attributes = {
2658 .core = slot->attr
2659 };
2660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2662 &operation_mac_size);
2663 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002664 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002668 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002669 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002670 }
2671
2672 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 &attributes,
2674 slot->key.data, slot->key.bytes,
2675 alg,
2676 input, input_length,
2677 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002678
2679exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002680 /* In case of success, set the potential excess room in the output buffer
2681 * to an invalid value, to avoid potentially leaking a longer MAC.
2682 * In case of error, set the output length and content to a safe default,
2683 * such that in case the caller misses an error check, the output would be
2684 * an unachievable MAC.
2685 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002687 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002688 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002689 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002690
2691 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002696}
2697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002699 psa_algorithm_t alg,
2700 const uint8_t *input,
2701 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 uint8_t *mac,
2703 size_t mac_size,
2704 size_t *mac_length)
2705{
2706 return psa_mac_compute_internal(key, alg,
2707 input, input_length,
2708 mac, mac_size, mac_length, 1);
2709}
2710
2711psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2712 psa_algorithm_t alg,
2713 const uint8_t *input,
2714 size_t input_length,
2715 const uint8_t *mac,
2716 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002717{
2718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002719 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2720 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 status = psa_mac_compute_internal(key, alg,
2723 input, input_length,
2724 actual_mac, sizeof(actual_mac),
2725 &actual_mac_length, 0);
2726 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002727 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002731 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002732 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002733 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002735 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002736 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002737 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002738
2739exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002743}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002744
Gilles Peskinee59236f2018-01-27 23:32:46 +01002745/****************************************************************/
2746/* Asymmetric cryptography */
2747/****************************************************************/
2748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2750 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002751{
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 if (input_is_message) {
2753 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2754 return PSA_ERROR_INVALID_ARGUMENT;
2755 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2758 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2759 return PSA_ERROR_INVALID_ARGUMENT;
2760 }
2761 }
2762 } else {
2763 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2764 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002765 }
2766 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002767
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002769}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2772 int input_is_message,
2773 psa_algorithm_t alg,
2774 const uint8_t *input,
2775 size_t input_length,
2776 uint8_t *signature,
2777 size_t signature_size,
2778 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002779{
2780 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2781 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2782 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002783
2784 *signature_length = 0;
2785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 status = psa_sign_verify_check_alg(input_is_message, alg);
2787 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002788 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002790
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002791 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002792 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002793 * buffer can in principle be empty since it doesn't actually have
2794 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 if (signature_size == 0) {
2796 return PSA_ERROR_BUFFER_TOO_SMALL;
2797 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002798
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002799 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 key, &slot,
2801 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2802 PSA_KEY_USAGE_SIGN_HASH,
2803 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002806 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002810 status = PSA_ERROR_INVALID_ARGUMENT;
2811 goto exit;
2812 }
2813
2814 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002816 };
2817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002819 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002820 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002821 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 signature, signature_size, signature_length);
2823 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002824
2825 status = psa_driver_wrapper_sign_hash(
2826 &attributes, slot->key.data, slot->key.bytes,
2827 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002829 }
2830
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002831
2832exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002833 psa_wipe_tag_output_buffer(signature, status, signature_size,
2834 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002839}
2840
Gilles Peskine449bd832023-01-11 14:50:10 +01002841static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2842 int input_is_message,
2843 psa_algorithm_t alg,
2844 const uint8_t *input,
2845 size_t input_length,
2846 const uint8_t *signature,
2847 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002848{
2849 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2850 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2851 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 status = psa_sign_verify_check_alg(input_is_message, alg);
2854 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002855 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002857
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002858 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 key, &slot,
2860 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2861 PSA_KEY_USAGE_VERIFY_HASH,
2862 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 if (status != PSA_SUCCESS) {
2865 return status;
2866 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002867
2868 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002870 };
2871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002873 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002874 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002875 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 signature, signature_length);
2877 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002878 status = psa_driver_wrapper_verify_hash(
2879 &attributes, slot->key.data, slot->key.bytes,
2880 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002882 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002883
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002887
2888}
2889
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002890psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002891 const psa_key_attributes_t *attributes,
2892 const uint8_t *key_buffer,
2893 size_t key_buffer_size,
2894 psa_algorithm_t alg,
2895 const uint8_t *input,
2896 size_t input_length,
2897 uint8_t *signature,
2898 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002900{
2901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002904 size_t hash_length;
2905 uint8_t hash[PSA_HASH_MAX_SIZE];
2906
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002907 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 PSA_ALG_SIGN_GET_HASH(alg),
2909 input, input_length,
2910 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002911
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002913 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002915
2916 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 attributes, key_buffer, key_buffer_size,
2918 alg, hash, hash_length,
2919 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002920 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002921
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002923}
2924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2926 psa_algorithm_t alg,
2927 const uint8_t *input,
2928 size_t input_length,
2929 uint8_t *signature,
2930 size_t signature_size,
2931 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002932{
2933 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002934 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002936}
2937
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002938psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002939 const psa_key_attributes_t *attributes,
2940 const uint8_t *key_buffer,
2941 size_t key_buffer_size,
2942 psa_algorithm_t alg,
2943 const uint8_t *input,
2944 size_t input_length,
2945 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002947{
2948 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002951 size_t hash_length;
2952 uint8_t hash[PSA_HASH_MAX_SIZE];
2953
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002954 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 PSA_ALG_SIGN_GET_HASH(alg),
2956 input, input_length,
2957 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002960 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002962
2963 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 attributes, key_buffer, key_buffer_size,
2965 alg, hash, hash_length,
2966 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002967 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002970}
2971
Gilles Peskine449bd832023-01-11 14:50:10 +01002972psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2973 psa_algorithm_t alg,
2974 const uint8_t *input,
2975 size_t input_length,
2976 const uint8_t *signature,
2977 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002978{
2979 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002980 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002982}
2983
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002984psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002985 const psa_key_attributes_t *attributes,
2986 const uint8_t *key_buffer, size_t key_buffer_size,
2987 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002989{
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2991 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2992 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002993#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2995 return mbedtls_psa_rsa_sign_hash(
2996 attributes,
2997 key_buffer, key_buffer_size,
2998 alg, hash, hash_length,
2999 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003000#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3001 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 } else {
3003 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003004 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3006 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003007#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3009 return mbedtls_psa_ecdsa_sign_hash(
3010 attributes,
3011 key_buffer, key_buffer_size,
3012 alg, hash, hash_length,
3013 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003014#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3015 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 } else {
3017 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003018 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003019 }
Ronald Cron4501c982021-03-19 20:09:32 +01003020
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 (void) key_buffer;
3022 (void) key_buffer_size;
3023 (void) hash;
3024 (void) hash_length;
3025 (void) signature;
3026 (void) signature_size;
3027 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003030}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3033 psa_algorithm_t alg,
3034 const uint8_t *hash,
3035 size_t hash_length,
3036 uint8_t *signature,
3037 size_t signature_size,
3038 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003039{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003040 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003041 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003043}
3044
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003045psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003046 const psa_key_attributes_t *attributes,
3047 const uint8_t *key_buffer, size_t key_buffer_size,
3048 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003050{
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3052 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3053 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003054#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3056 return mbedtls_psa_rsa_verify_hash(
3057 attributes,
3058 key_buffer, key_buffer_size,
3059 alg, hash, hash_length,
3060 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003061#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3062 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 } else {
3064 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003065 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3067 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003068#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3070 return mbedtls_psa_ecdsa_verify_hash(
3071 attributes,
3072 key_buffer, key_buffer_size,
3073 alg, hash, hash_length,
3074 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003075#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3076 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 } else {
3078 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003079 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003080 }
Ronald Cron4501c982021-03-19 20:09:32 +01003081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 (void) key_buffer;
3083 (void) key_buffer_size;
3084 (void) hash;
3085 (void) hash_length;
3086 (void) signature;
3087 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003090}
3091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3093 psa_algorithm_t alg,
3094 const uint8_t *hash,
3095 size_t hash_length,
3096 const uint8_t *signature,
3097 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003098{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003099 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003100 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003102}
3103
Gilles Peskine449bd832023-01-11 14:50:10 +01003104psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3105 psa_algorithm_t alg,
3106 const uint8_t *input,
3107 size_t input_length,
3108 const uint8_t *salt,
3109 size_t salt_length,
3110 uint8_t *output,
3111 size_t output_size,
3112 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003113{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003114 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003115 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003116 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003117
Darryl Green5cc689a2018-07-24 15:34:10 +01003118 (void) input;
3119 (void) input_length;
3120 (void) salt;
3121 (void) output;
3122 (void) output_size;
3123
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003124 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3127 return PSA_ERROR_INVALID_ARGUMENT;
3128 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003129
Ronald Cron5c522922020-11-14 16:35:34 +01003130 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3132 if (status != PSA_SUCCESS) {
3133 return status;
3134 }
3135 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3136 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003137 status = PSA_ERROR_INVALID_ARGUMENT;
3138 goto exit;
3139 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003141 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003143 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003144
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003145 status = psa_driver_wrapper_asymmetric_encrypt(
3146 &attributes, slot->key.data, slot->key.bytes,
3147 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003149exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003153}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3156 psa_algorithm_t alg,
3157 const uint8_t *input,
3158 size_t input_length,
3159 const uint8_t *salt,
3160 size_t salt_length,
3161 uint8_t *output,
3162 size_t output_size,
3163 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003164{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003166 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003167 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003168
Darryl Green5cc689a2018-07-24 15:34:10 +01003169 (void) input;
3170 (void) input_length;
3171 (void) salt;
3172 (void) output;
3173 (void) output_size;
3174
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003175 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003176
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3178 return PSA_ERROR_INVALID_ARGUMENT;
3179 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003180
Ronald Cron5c522922020-11-14 16:35:34 +01003181 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3183 if (status != PSA_SUCCESS) {
3184 return status;
3185 }
3186 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003187 status = PSA_ERROR_INVALID_ARGUMENT;
3188 goto exit;
3189 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003191 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003193 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003195 status = psa_driver_wrapper_asymmetric_decrypt(
3196 &attributes, slot->key.data, slot->key.bytes,
3197 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003199
3200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003204}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003205
Paul Elliott9fe12f62022-11-30 19:16:02 +00003206/****************************************************************/
3207/* Asymmetric interruptible cryptography */
3208/****************************************************************/
3209
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003210static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3211
Paul Elliott9fe12f62022-11-30 19:16:02 +00003212void psa_interruptible_set_max_ops(uint32_t max_ops)
3213{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003214 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003215}
3216
3217uint32_t psa_interruptible_get_max_ops(void)
3218{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003219 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003220}
3221
Paul Elliott9fe12f62022-11-30 19:16:02 +00003222uint32_t psa_sign_hash_get_num_ops(
3223 const psa_sign_hash_interruptible_operation_t *operation)
3224{
Paul Elliott296ede92022-12-15 17:00:30 +00003225 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003226}
3227
3228uint32_t psa_verify_hash_get_num_ops(
3229 const psa_verify_hash_interruptible_operation_t *operation)
3230{
Paul Elliott296ede92022-12-15 17:00:30 +00003231 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003232}
3233
Paul Elliott59ad9452022-12-18 15:09:02 +00003234static psa_status_t psa_sign_hash_abort_internal(
3235 psa_sign_hash_interruptible_operation_t *operation)
3236{
3237 if (operation->id == 0) {
3238 /* The object has (apparently) been initialized but it is not (yet)
3239 * in use. It's ok to call abort on such an object, and there's
3240 * nothing to do. */
3241 return PSA_SUCCESS;
3242 }
3243
3244 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3245
3246 status = psa_driver_wrapper_sign_hash_abort(operation);
3247
3248 operation->id = 0;
3249
Paul Elliotte6145dc2023-02-07 12:51:21 +00003250 /* Do not clear either the error_occurred or num_ops elements here as they
3251 * only want to be cleared by the application calling abort, not by abort
3252 * being called at completion of an operation. */
3253
Paul Elliott59ad9452022-12-18 15:09:02 +00003254 return status;
3255}
3256
Paul Elliott9fe12f62022-11-30 19:16:02 +00003257psa_status_t psa_sign_hash_start(
3258 psa_sign_hash_interruptible_operation_t *operation,
3259 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3260 const uint8_t *hash, size_t hash_length)
3261{
3262 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3263 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3264 psa_key_slot_t *slot;
3265
Paul Elliottc9774412023-02-06 15:14:07 +00003266 /* Check that start has not been previously called, or operation has not
3267 * previously errored. */
3268 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003269 return PSA_ERROR_BAD_STATE;
3270 }
3271
Paul Elliott9fe12f62022-11-30 19:16:02 +00003272 status = psa_sign_verify_check_alg(0, alg);
3273 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003274 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003275 return status;
3276 }
3277
3278 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3279 PSA_KEY_USAGE_SIGN_HASH,
3280 alg);
3281
3282 if (status != PSA_SUCCESS) {
3283 goto exit;
3284 }
3285
3286 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3287 status = PSA_ERROR_INVALID_ARGUMENT;
3288 goto exit;
3289 }
3290
3291 psa_key_attributes_t attributes = {
3292 .core = slot->attr
3293 };
3294
Paul Elliott296ede92022-12-15 17:00:30 +00003295 /* Ensure ops count gets reset, in case of operation re-use. */
3296 operation->num_ops = 0;
3297
Paul Elliott9fe12f62022-11-30 19:16:02 +00003298 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3299 slot->key.data,
3300 slot->key.bytes, alg,
3301 hash, hash_length);
3302exit:
3303
3304 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003305 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003306 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003307 }
3308
3309 unlock_status = psa_unlock_key_slot(slot);
3310
Paul Elliottc9774412023-02-06 15:14:07 +00003311 if (unlock_status != PSA_SUCCESS) {
3312 operation->error_occurred = 1;
3313 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003314
Paul Elliottc9774412023-02-06 15:14:07 +00003315 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003316}
3317
3318
3319psa_status_t psa_sign_hash_complete(
3320 psa_sign_hash_interruptible_operation_t *operation,
3321 uint8_t *signature, size_t signature_size,
3322 size_t *signature_length)
3323{
3324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3325
3326 *signature_length = 0;
3327
Paul Elliottc9774412023-02-06 15:14:07 +00003328 /* Check that start has been called first, and that operation has not
3329 * previously errored. */
3330 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003331 status = PSA_ERROR_BAD_STATE;
3332 goto exit;
3333 }
3334
Paul Elliott096abc42023-02-03 18:33:23 +00003335 /* Immediately reject a zero-length signature buffer. This guarantees that
3336 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003337 if (signature_size == 0) {
3338 status = PSA_ERROR_BUFFER_TOO_SMALL;
3339 goto exit;
3340 }
3341
3342 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3343 signature_size,
3344 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003345
Paul Elliott296ede92022-12-15 17:00:30 +00003346 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003347 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003348
Paul Elliottebe225c2023-02-10 14:32:53 +00003349exit:
3350
Paul Elliott59200a22023-02-21 15:07:40 +00003351 psa_wipe_tag_output_buffer(signature, status, signature_size,
3352 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003353
Paul Elliott53bb3122023-02-10 14:22:22 +00003354 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003355 if (status != PSA_SUCCESS) {
3356 operation->error_occurred = 1;
3357 }
3358
Paul Elliott59ad9452022-12-18 15:09:02 +00003359 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003360 }
3361
3362 return status;
3363}
3364
3365psa_status_t psa_sign_hash_abort(
3366 psa_sign_hash_interruptible_operation_t *operation)
3367{
Paul Elliott59ad9452022-12-18 15:09:02 +00003368 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3369
3370 status = psa_sign_hash_abort_internal(operation);
3371
3372 /* We clear the number of ops done here, so that it is not cleared when
3373 * the operation fails or succeeds, only on manual abort. */
3374 operation->num_ops = 0;
3375
Paul Elliottc9774412023-02-06 15:14:07 +00003376 /* Likewise, failure state. */
3377 operation->error_occurred = 0;
3378
Paul Elliott59ad9452022-12-18 15:09:02 +00003379 return status;
3380}
3381
3382static psa_status_t psa_verify_hash_abort_internal(
3383 psa_verify_hash_interruptible_operation_t *operation)
3384{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003385 if (operation->id == 0) {
3386 /* The object has (apparently) been initialized but it is not (yet)
3387 * in use. It's ok to call abort on such an object, and there's
3388 * nothing to do. */
3389 return PSA_SUCCESS;
3390 }
3391
Paul Elliott59ad9452022-12-18 15:09:02 +00003392 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3393
3394 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003395
3396 operation->id = 0;
3397
Paul Elliotte6145dc2023-02-07 12:51:21 +00003398 /* Do not clear either the error_occurred or num_ops elements here as they
3399 * only want to be cleared by the application calling abort, not by abort
3400 * being called at completion of an operation. */
3401
Paul Elliott59ad9452022-12-18 15:09:02 +00003402 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003403}
3404
3405psa_status_t psa_verify_hash_start(
3406 psa_verify_hash_interruptible_operation_t *operation,
3407 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3408 const uint8_t *hash, size_t hash_length,
3409 const uint8_t *signature, size_t signature_length)
3410{
3411 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3412 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3413 psa_key_slot_t *slot;
3414
Paul Elliottc9774412023-02-06 15:14:07 +00003415 /* Check that start has not been previously called, or operation has not
3416 * previously errored. */
3417 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003418 return PSA_ERROR_BAD_STATE;
3419 }
3420
3421 status = psa_sign_verify_check_alg(0, alg);
3422 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003423 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003424 return status;
3425 }
3426
3427 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3428 PSA_KEY_USAGE_VERIFY_HASH,
3429 alg);
3430
3431 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003432 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003433 return status;
3434 }
3435
3436 psa_key_attributes_t attributes = {
3437 .core = slot->attr
3438 };
3439
Paul Elliott296ede92022-12-15 17:00:30 +00003440 /* Ensure ops count gets reset, in case of operation re-use. */
3441 operation->num_ops = 0;
3442
Paul Elliott9fe12f62022-11-30 19:16:02 +00003443 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3444 slot->key.data,
3445 slot->key.bytes,
3446 alg, hash, hash_length,
3447 signature, signature_length);
3448
3449 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003450 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003451 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003452 }
3453
3454 unlock_status = psa_unlock_key_slot(slot);
3455
Paul Elliottc9774412023-02-06 15:14:07 +00003456 if (unlock_status != PSA_SUCCESS) {
3457 operation->error_occurred = 1;
3458 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459
Paul Elliottc9774412023-02-06 15:14:07 +00003460 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461}
3462
3463psa_status_t psa_verify_hash_complete(
3464 psa_verify_hash_interruptible_operation_t *operation)
3465{
3466 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3467
Paul Elliottc9774412023-02-06 15:14:07 +00003468 /* Check that start has been called first, and that operation has not
3469 * previously errored. */
3470 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003471 status = PSA_ERROR_BAD_STATE;
3472 goto exit;
3473 }
3474
3475 status = psa_driver_wrapper_verify_hash_complete(operation);
3476
Paul Elliott296ede92022-12-15 17:00:30 +00003477 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003478 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003479 operation);
3480
Paul Elliottebe225c2023-02-10 14:32:53 +00003481exit:
3482
Paul Elliott9fe12f62022-11-30 19:16:02 +00003483 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003484 if (status != PSA_SUCCESS) {
3485 operation->error_occurred = 1;
3486 }
3487
Paul Elliott59ad9452022-12-18 15:09:02 +00003488 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003489 }
3490
3491 return status;
3492}
3493
3494psa_status_t psa_verify_hash_abort(
3495 psa_verify_hash_interruptible_operation_t *operation)
3496{
Paul Elliott59ad9452022-12-18 15:09:02 +00003497 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003498
Paul Elliott59ad9452022-12-18 15:09:02 +00003499 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003500
Paul Elliott59ad9452022-12-18 15:09:02 +00003501 /* We clear the number of ops done here, so that it is not cleared when
3502 * the operation fails or succeeds, only on manual abort. */
3503 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003504
Paul Elliottc9774412023-02-06 15:14:07 +00003505 /* Likewise, failure state. */
3506 operation->error_occurred = 0;
3507
Paul Elliott59ad9452022-12-18 15:09:02 +00003508 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003509}
3510
Paul Elliott588f8ed2022-12-02 18:10:26 +00003511/****************************************************************/
3512/* Asymmetric interruptible cryptography internal */
3513/* implementations */
3514/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003515
Paul Elliott588f8ed2022-12-02 18:10:26 +00003516void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3517{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003518
Paul Elliott588f8ed2022-12-02 18:10:26 +00003519#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3520 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3521 defined(MBEDTLS_ECP_RESTARTABLE)
3522
3523 /* Internal implementation uses zero to indicate infinite number max ops,
3524 * therefore avoid this value, and set to minimum possible. */
3525 if (max_ops == 0) {
3526 max_ops = 1;
3527 }
3528
Paul Elliott588f8ed2022-12-02 18:10:26 +00003529 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003530#else
3531 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003532#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3533 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3534 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3535}
3536
Paul Elliott588f8ed2022-12-02 18:10:26 +00003537uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003538 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003539{
3540#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3541 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3542 defined(MBEDTLS_ECP_RESTARTABLE)
3543
Paul Elliott93d9ca82023-02-15 18:14:21 +00003544 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003545#else
3546 (void) operation;
3547 return 0;
3548#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3549 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3550 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3551}
3552
3553uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003554 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003555{
3556 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3557 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3558 defined(MBEDTLS_ECP_RESTARTABLE)
3559
Paul Elliott93d9ca82023-02-15 18:14:21 +00003560 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003561#else
3562 (void) operation;
3563 return 0;
3564#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3565 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3566 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3567}
3568
3569psa_status_t mbedtls_psa_sign_hash_start(
3570 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3571 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3572 size_t key_buffer_size, psa_algorithm_t alg,
3573 const uint8_t *hash, size_t hash_length)
3574{
3575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003576 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003577
Paul Elliott068fe072023-01-16 13:59:15 +00003578 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3579 return PSA_ERROR_NOT_SUPPORTED;
3580 }
3581
3582 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003583 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003584 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003585
3586#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003587 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3588 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003589
Paul Elliotta1c94092023-02-15 16:38:04 +00003590 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3591
Paul Elliott93d9ca82023-02-15 18:14:21 +00003592 /* Ensure num_ops is zero'ed in case of context re-use. */
3593 operation->num_ops = 0;
3594
Paul Elliott068fe072023-01-16 13:59:15 +00003595 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3596 attributes->core.bits,
3597 key_buffer,
3598 key_buffer_size,
3599 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003600
Paul Elliott068fe072023-01-16 13:59:15 +00003601 if (status != PSA_SUCCESS) {
3602 return status;
3603 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003604
Paul Elliott1bc59df2023-02-05 13:41:57 +00003605 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003606 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003607
Paul Elliott068fe072023-01-16 13:59:15 +00003608 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3609 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3610 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003611
Paul Elliott0290a762023-02-09 14:30:24 +00003612 /* We only need to store the same length of hash as the private key size
3613 * here, it would be truncated by the internal implementation anyway. */
3614 required_hash_length = (hash_length < operation->coordinate_bytes ?
3615 hash_length : operation->coordinate_bytes);
3616
Paul Elliottba70ad42023-02-15 18:23:53 +00003617 if (required_hash_length > sizeof(operation->hash)) {
3618 /* Shouldn't happen, but better safe than sorry. */
3619 return PSA_ERROR_CORRUPTION_DETECTED;
3620 }
3621
Paul Elliott0290a762023-02-09 14:30:24 +00003622 memcpy(operation->hash, hash, required_hash_length);
3623 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003624
3625 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003626
3627#else
Paul Elliott068fe072023-01-16 13:59:15 +00003628 (void) operation;
3629 (void) key_buffer;
3630 (void) key_buffer_size;
3631 (void) alg;
3632 (void) hash;
3633 (void) hash_length;
3634 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003635 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003636
Paul Elliott068fe072023-01-16 13:59:15 +00003637 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3639 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3640 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641}
3642
3643psa_status_t mbedtls_psa_sign_hash_complete(
3644 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3645 uint8_t *signature, size_t signature_size,
3646 size_t *signature_length)
3647{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003648#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3649 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3650 defined(MBEDTLS_ECP_RESTARTABLE)
3651
Paul Elliotta1c94092023-02-15 16:38:04 +00003652 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003653 mbedtls_mpi r;
3654 mbedtls_mpi s;
3655
Paul Elliott46845252023-02-03 14:59:11 +00003656 mbedtls_mpi_init(&r);
3657 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003658
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003659 /* Ensure max_ops is set to the current value (or default). */
3660 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3661
Paul Elliotta1c94092023-02-15 16:38:04 +00003662 if (signature_size < 2 * operation->coordinate_bytes) {
3663 status = PSA_ERROR_BUFFER_TOO_SMALL;
3664 goto exit;
3665 }
3666
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003668
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3670 status = mbedtls_to_psa_error(
3671 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003672 &r,
3673 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674 &operation->ctx->d,
3675 operation->hash,
3676 operation->hash_length,
3677 operation->md_alg,
3678 mbedtls_psa_get_random,
3679 MBEDTLS_PSA_RANDOM_STATE,
3680 &operation->restart_ctx));
3681#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003682 status = PSA_ERROR_NOT_SUPPORTED;
3683 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003684#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3685 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686 status = mbedtls_to_psa_error(
3687 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003688 &r,
3689 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690 &operation->ctx->d,
3691 operation->hash,
3692 operation->hash_length,
3693 mbedtls_psa_get_random,
3694 MBEDTLS_PSA_RANDOM_STATE,
3695 mbedtls_psa_get_random,
3696 MBEDTLS_PSA_RANDOM_STATE,
3697 &operation->restart_ctx));
3698 }
3699
Paul Elliottf8e5b562023-02-19 18:43:45 +00003700 /* Hide the fact that the restart context only holds a delta of number of
3701 * ops done during the last operation, not an absolute value. */
3702 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3703
Paul Elliott724bd252023-02-08 12:35:08 +00003704 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003705 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003706 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003707 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003708 operation->coordinate_bytes)
3709 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003710
3711 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003712 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003713 }
3714
3715 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003716 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003718 operation->coordinate_bytes,
3719 operation->coordinate_bytes)
3720 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003721
3722 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003723 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724 }
3725
Paul Elliott1bc59df2023-02-05 13:41:57 +00003726 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727
Paul Elliott724bd252023-02-08 12:35:08 +00003728 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729 }
Paul Elliott724bd252023-02-08 12:35:08 +00003730
3731exit:
3732
3733 mbedtls_mpi_free(&r);
3734 mbedtls_mpi_free(&s);
3735 return status;
3736
Paul Elliott588f8ed2022-12-02 18:10:26 +00003737 #else
3738
3739 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003740 (void) signature;
3741 (void) signature_size;
3742 (void) signature_length;
3743
3744 return PSA_ERROR_NOT_SUPPORTED;
3745
3746#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3747 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3748 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3749}
3750
3751psa_status_t mbedtls_psa_sign_hash_abort(
3752 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3753{
3754
3755#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3756 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3757 defined(MBEDTLS_ECP_RESTARTABLE)
3758
3759 if (operation->ctx) {
3760 mbedtls_ecdsa_free(operation->ctx);
3761 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003762 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003763 }
3764
3765 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3766
Paul Elliott93d9ca82023-02-15 18:14:21 +00003767 operation->num_ops = 0;
3768
Paul Elliott588f8ed2022-12-02 18:10:26 +00003769 return PSA_SUCCESS;
3770
3771#else
3772
3773 (void) operation;
3774
3775 return PSA_ERROR_NOT_SUPPORTED;
3776
3777#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3778 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3779 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3780}
3781
3782psa_status_t mbedtls_psa_verify_hash_start(
3783 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3784 const psa_key_attributes_t *attributes,
3785 const uint8_t *key_buffer, size_t key_buffer_size,
3786 psa_algorithm_t alg,
3787 const uint8_t *hash, size_t hash_length,
3788 const uint8_t *signature, size_t signature_length)
3789{
3790 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003791 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003792 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003793
Paul Elliott068fe072023-01-16 13:59:15 +00003794 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3795 return PSA_ERROR_NOT_SUPPORTED;
3796 }
3797
3798 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003799 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003800 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003801
3802#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003803 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3804 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805
Paul Elliotta1c94092023-02-15 16:38:04 +00003806 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3807 mbedtls_mpi_init(&operation->r);
3808 mbedtls_mpi_init(&operation->s);
3809
Paul Elliott93d9ca82023-02-15 18:14:21 +00003810 /* Ensure num_ops is zero'ed in case of context re-use. */
3811 operation->num_ops = 0;
3812
Paul Elliott068fe072023-01-16 13:59:15 +00003813 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3814 attributes->core.bits,
3815 key_buffer,
3816 key_buffer_size,
3817 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003818
Paul Elliott068fe072023-01-16 13:59:15 +00003819 if (status != PSA_SUCCESS) {
3820 return status;
3821 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003822
Paul Elliottc569fc22023-02-10 13:02:54 +00003823 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003824
Paul Elliott1bc59df2023-02-05 13:41:57 +00003825 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003826 return PSA_ERROR_INVALID_SIGNATURE;
3827 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003828
Paul Elliott068fe072023-01-16 13:59:15 +00003829 status = mbedtls_to_psa_error(
3830 mbedtls_mpi_read_binary(&operation->r,
3831 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003832 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003833
Paul Elliott068fe072023-01-16 13:59:15 +00003834 if (status != PSA_SUCCESS) {
3835 return status;
3836 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837
Paul Elliott068fe072023-01-16 13:59:15 +00003838 status = mbedtls_to_psa_error(
3839 mbedtls_mpi_read_binary(&operation->s,
3840 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003841 coordinate_bytes,
3842 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843
Paul Elliott068fe072023-01-16 13:59:15 +00003844 if (status != PSA_SUCCESS) {
3845 return status;
3846 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003847
Paul Elliott2c9843f2023-02-15 17:32:42 +00003848 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849
Paul Elliott2c9843f2023-02-15 17:32:42 +00003850 if (status != PSA_SUCCESS) {
3851 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003852 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853
Paul Elliott0290a762023-02-09 14:30:24 +00003854 /* We only need to store the same length of hash as the private key size
3855 * here, it would be truncated by the internal implementation anyway. */
3856 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3857 coordinate_bytes);
3858
Paul Elliottba70ad42023-02-15 18:23:53 +00003859 if (required_hash_length > sizeof(operation->hash)) {
3860 /* Shouldn't happen, but better safe than sorry. */
3861 return PSA_ERROR_CORRUPTION_DETECTED;
3862 }
3863
Paul Elliott0290a762023-02-09 14:30:24 +00003864 memcpy(operation->hash, hash, required_hash_length);
3865 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003866
3867 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003868#else
Paul Elliott068fe072023-01-16 13:59:15 +00003869 (void) operation;
3870 (void) key_buffer;
3871 (void) key_buffer_size;
3872 (void) alg;
3873 (void) hash;
3874 (void) hash_length;
3875 (void) signature;
3876 (void) signature_length;
3877 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003878 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003879 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003880
Paul Elliott068fe072023-01-16 13:59:15 +00003881 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3883 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3884 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885}
3886
3887psa_status_t mbedtls_psa_verify_hash_complete(
3888 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3889{
3890
3891#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3892 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3893 defined(MBEDTLS_ECP_RESTARTABLE)
3894
Paul Elliottf8e5b562023-02-19 18:43:45 +00003895 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3896
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003897 /* Ensure max_ops is set to the current value (or default). */
3898 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3899
Paul Elliottf8e5b562023-02-19 18:43:45 +00003900 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003901 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3902 operation->hash,
3903 operation->hash_length,
3904 &operation->ctx->Q,
3905 &operation->r,
3906 &operation->s,
3907 &operation->restart_ctx));
3908
Paul Elliottf8e5b562023-02-19 18:43:45 +00003909 /* Hide the fact that the restart context only holds a delta of number of
3910 * ops done during the last operation, not an absolute value. */
3911 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3912
3913 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003914#else
3915 (void) operation;
3916
3917 return PSA_ERROR_NOT_SUPPORTED;
3918
3919#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3920 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3921 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3922}
3923
3924psa_status_t mbedtls_psa_verify_hash_abort(
3925 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3926{
3927
3928#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3929 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3930 defined(MBEDTLS_ECP_RESTARTABLE)
3931
3932 if (operation->ctx) {
3933 mbedtls_ecdsa_free(operation->ctx);
3934 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003935 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003936 }
3937
3938 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3939
Paul Elliott93d9ca82023-02-15 18:14:21 +00003940 operation->num_ops = 0;
3941
Paul Elliott588f8ed2022-12-02 18:10:26 +00003942 mbedtls_mpi_free(&operation->r);
3943 mbedtls_mpi_free(&operation->s);
3944
3945 return PSA_SUCCESS;
3946
3947#else
3948 (void) operation;
3949
3950 return PSA_ERROR_NOT_SUPPORTED;
3951
3952#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3953 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3954 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3955}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003956
mohammad1603503973b2018-03-12 15:59:30 +02003957/****************************************************************/
3958/* Symmetric cryptography */
3959/****************************************************************/
3960
Gilles Peskine449bd832023-01-11 14:50:10 +01003961static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3962 mbedtls_svc_key_id_t key,
3963 psa_algorithm_t alg,
3964 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003965{
3966 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3967 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003968 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3970 PSA_KEY_USAGE_ENCRYPT :
3971 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003972
3973 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003975 status = PSA_ERROR_BAD_STATE;
3976 goto exit;
3977 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003978
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003980 status = PSA_ERROR_INVALID_ARGUMENT;
3981 goto exit;
3982 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003983
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3985 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003986 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003988
Ronald Cron49fafa92021-03-10 08:34:23 +01003989 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003990 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003991 * so we only set it (in the driver wrapper) after resources have been
3992 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003993 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003995 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003997 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 }
3999 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004000
Ronald Crona4af55f2020-12-14 14:36:06 +01004001 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004003 };
4004
Ronald Cronab99ac22020-10-01 16:38:28 +02004005 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 if (cipher_operation == MBEDTLS_ENCRYPT) {
4007 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4008 &attributes,
4009 slot->key.data,
4010 slot->key.bytes,
4011 alg);
4012 } else {
4013 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4014 &attributes,
4015 slot->key.data,
4016 slot->key.bytes,
4017 alg);
4018 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004019
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004020exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 if (status != PSA_SUCCESS) {
4022 psa_cipher_abort(operation);
4023 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004024
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004028}
4029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4031 mbedtls_svc_key_id_t key,
4032 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004033{
Gilles Peskine449bd832023-01-11 14:50:10 +01004034 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004035}
4036
Gilles Peskine449bd832023-01-11 14:50:10 +01004037psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4038 mbedtls_svc_key_id_t key,
4039 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004040{
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004042}
4043
Gilles Peskine449bd832023-01-11 14:50:10 +01004044psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4045 uint8_t *iv,
4046 size_t iv_size,
4047 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004048{
Ronald Cron6d051732020-10-01 14:10:20 +02004049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004050 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4051 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004052
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004054 status = PSA_ERROR_BAD_STATE;
4055 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004056 }
4057
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004059 status = PSA_ERROR_BAD_STATE;
4060 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004061 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004062
Ronald Cron2fb90522021-07-05 12:31:44 +02004063 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004065 status = PSA_ERROR_BUFFER_TOO_SMALL;
4066 goto exit;
4067 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004068
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004070 status = PSA_ERROR_GENERIC_ERROR;
4071 goto exit;
4072 }
4073
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 status = psa_generate_random(local_iv, default_iv_length);
4075 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 }
Ronald Cron5618a392021-03-26 09:52:26 +01004078
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 status = psa_driver_wrapper_cipher_set_iv(operation,
4080 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004081
4082exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 if (status == PSA_SUCCESS) {
4084 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004085 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004086 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004088 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004090 }
Ronald Cron6d051732020-10-01 14:10:20 +02004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004093}
4094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4096 const uint8_t *iv,
4097 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004098{
Ronald Cron6d051732020-10-01 14:10:20 +02004099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004102 status = PSA_ERROR_BAD_STATE;
4103 goto exit;
4104 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004105
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004107 status = PSA_ERROR_BAD_STATE;
4108 goto exit;
4109 }
Ronald Crona0d68172021-03-26 10:15:08 +01004110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004112 status = PSA_ERROR_INVALID_ARGUMENT;
4113 goto exit;
4114 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004115
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 status = psa_driver_wrapper_cipher_set_iv(operation,
4117 iv,
4118 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004119
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004120exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004122 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 } else {
4124 psa_cipher_abort(operation);
4125 }
4126 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004127}
4128
Gilles Peskine449bd832023-01-11 14:50:10 +01004129psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4130 const uint8_t *input,
4131 size_t input_length,
4132 uint8_t *output,
4133 size_t output_size,
4134 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004135{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004136 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004137
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004139 status = PSA_ERROR_BAD_STATE;
4140 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004141 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004144 status = PSA_ERROR_BAD_STATE;
4145 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004146 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004147
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 status = psa_driver_wrapper_cipher_update(operation,
4149 input,
4150 input_length,
4151 output,
4152 output_size,
4153 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004154
4155exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 if (status != PSA_SUCCESS) {
4157 psa_cipher_abort(operation);
4158 }
Ronald Cron6d051732020-10-01 14:10:20 +02004159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004161}
4162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4164 uint8_t *output,
4165 size_t output_size,
4166 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004167{
David Saadab4ecc272019-02-14 13:48:10 +02004168 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004171 status = PSA_ERROR_BAD_STATE;
4172 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004173 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004176 status = PSA_ERROR_BAD_STATE;
4177 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004178 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 status = psa_driver_wrapper_cipher_finish(operation,
4181 output,
4182 output_size,
4183 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004184
4185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (status == PSA_SUCCESS) {
4187 return psa_cipher_abort(operation);
4188 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004189 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004193 }
mohammad1603503973b2018-03-12 15:59:30 +02004194}
4195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004197{
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004199 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004200 * in use. It's ok to call abort on such an object, and there's
4201 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004203 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004204
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004206
Ronald Cron49fafa92021-03-10 08:34:23 +01004207 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004208 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004209 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004210
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004212}
4213
Gilles Peskine449bd832023-01-11 14:50:10 +01004214psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4215 psa_algorithm_t alg,
4216 const uint8_t *input,
4217 size_t input_length,
4218 uint8_t *output,
4219 size_t output_size,
4220 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004221{
4222 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004223 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004224 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004225 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4226 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004229 status = PSA_ERROR_INVALID_ARGUMENT;
4230 goto exit;
4231 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4234 PSA_KEY_USAGE_ENCRYPT,
4235 alg);
4236 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004239
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004240 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004242 };
4243
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4245 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004246 status = PSA_ERROR_GENERIC_ERROR;
4247 goto exit;
4248 }
4249
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 if (default_iv_length > 0) {
4251 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004252 status = PSA_ERROR_BUFFER_TOO_SMALL;
4253 goto exit;
4254 }
4255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 status = psa_generate_random(local_iv, default_iv_length);
4257 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004258 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004260 }
4261
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004262 status = psa_driver_wrapper_cipher_encrypt(
4263 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004264 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004265 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004267
4268exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 unlock_status = psa_unlock_key_slot(slot);
4270 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004271 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004272 }
Ronald Cron23919522021-07-08 19:00:07 +02004273
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 if (status == PSA_SUCCESS) {
4275 if (default_iv_length > 0) {
4276 memcpy(output, local_iv, default_iv_length);
4277 }
4278 *output_length += default_iv_length;
4279 } else {
4280 *output_length = 0;
4281 }
4282
4283 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004284}
4285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4287 psa_algorithm_t alg,
4288 const uint8_t *input,
4289 size_t input_length,
4290 uint8_t *output,
4291 size_t output_size,
4292 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004293{
4294 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004295 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004296 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004297
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004299 status = PSA_ERROR_INVALID_ARGUMENT;
4300 goto exit;
4301 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4304 PSA_KEY_USAGE_DECRYPT,
4305 alg);
4306 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004307 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004309
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004310 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004312 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004313
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4315 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004316 status = PSA_ERROR_INVALID_ARGUMENT;
4317 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004319 status = PSA_ERROR_INVALID_ARGUMENT;
4320 goto exit;
4321 }
4322
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004323 status = psa_driver_wrapper_cipher_decrypt(
4324 &attributes, slot->key.data, slot->key.bytes,
4325 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004327
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004328exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 unlock_status = psa_unlock_key_slot(slot);
4330 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004331 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004333
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004335 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 }
Ronald Cron23919522021-07-08 19:00:07 +02004337
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004339}
4340
4341
mohammad16035955c982018-04-26 00:53:03 +03004342/****************************************************************/
4343/* AEAD */
4344/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004345
Paul Elliottbaff51c2021-09-28 17:44:45 +01004346/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004347static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004348{
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004350}
4351
4352/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004353static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4354 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004355{
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004359#if defined(PSA_WANT_ALG_GCM)
4360 case PSA_ALG_GCM:
4361 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 * arbitrarily large nonces. Please note that we do not generally
4363 * recommend the usage of nonces of greater length than
4364 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4365 * size, which can then lead to collisions if you encrypt a very
4366 * large number of messages.*/
4367 if (nonce_length != 0) {
4368 return PSA_SUCCESS;
4369 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004370 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004371#endif /* PSA_WANT_ALG_GCM */
4372#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004373 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 if (nonce_length >= 7 && nonce_length <= 13) {
4375 return PSA_SUCCESS;
4376 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004377 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004378#endif /* PSA_WANT_ALG_CCM */
4379#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004380 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 if (nonce_length == 12) {
4382 return PSA_SUCCESS;
4383 } else if (nonce_length == 8) {
4384 return PSA_ERROR_NOT_SUPPORTED;
4385 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004386 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004387#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004388 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004389 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004391 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004392
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004394}
4395
Gilles Peskine449bd832023-01-11 14:50:10 +01004396static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004397{
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4399 return PSA_ERROR_INVALID_ARGUMENT;
4400 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004403}
4404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4406 psa_algorithm_t alg,
4407 const uint8_t *nonce,
4408 size_t nonce_length,
4409 const uint8_t *additional_data,
4410 size_t additional_data_length,
4411 const uint8_t *plaintext,
4412 size_t plaintext_length,
4413 uint8_t *ciphertext,
4414 size_t ciphertext_size,
4415 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004416{
Ronald Cron215633c2021-03-16 17:15:37 +01004417 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4418 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004419
mohammad1603f08a5502018-06-03 15:05:47 +03004420 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 status = psa_aead_check_algorithm(alg);
4423 if (status != PSA_SUCCESS) {
4424 return status;
4425 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004426
Ronald Cron9a986162021-03-26 12:40:07 +01004427 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4429 if (status != PSA_SUCCESS) {
4430 return status;
4431 }
mohammad16035955c982018-04-26 00:53:03 +03004432
Ronald Cron215633c2021-03-16 17:15:37 +01004433 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004435 };
mohammad16035955c982018-04-26 00:53:03 +03004436
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 status = psa_aead_check_nonce_length(alg, nonce_length);
4438 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004439 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004441
Ronald Cronde822812021-03-17 16:08:20 +01004442 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004443 &attributes, slot->key.data, slot->key.bytes,
4444 alg,
4445 nonce, nonce_length,
4446 additional_data, additional_data_length,
4447 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4451 memset(ciphertext, 0, ciphertext_size);
4452 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004453
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004454exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004458}
4459
Gilles Peskine449bd832023-01-11 14:50:10 +01004460psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4461 psa_algorithm_t alg,
4462 const uint8_t *nonce,
4463 size_t nonce_length,
4464 const uint8_t *additional_data,
4465 size_t additional_data_length,
4466 const uint8_t *ciphertext,
4467 size_t ciphertext_length,
4468 uint8_t *plaintext,
4469 size_t plaintext_size,
4470 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004471{
Ronald Cron215633c2021-03-16 17:15:37 +01004472 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4473 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004474
Gilles Peskineee652a32018-06-01 19:23:52 +02004475 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 status = psa_aead_check_algorithm(alg);
4478 if (status != PSA_SUCCESS) {
4479 return status;
4480 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004481
Ronald Cron9a986162021-03-26 12:40:07 +01004482 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4484 if (status != PSA_SUCCESS) {
4485 return status;
4486 }
mohammad16035955c982018-04-26 00:53:03 +03004487
Ronald Cron215633c2021-03-16 17:15:37 +01004488 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004490 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004491
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 status = psa_aead_check_nonce_length(alg, nonce_length);
4493 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004494 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004496
Ronald Cronde822812021-03-17 16:08:20 +01004497 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004498 &attributes, slot->key.data, slot->key.bytes,
4499 alg,
4500 nonce, nonce_length,
4501 additional_data, additional_data_length,
4502 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004504
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 if (status != PSA_SUCCESS && plaintext_size != 0) {
4506 memset(plaintext, 0, plaintext_size);
4507 }
mohammad160360a64d02018-06-03 17:20:42 +03004508
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004509exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 return status;
mohammad16035955c982018-04-26 00:53:03 +03004513}
4514
Gilles Peskine449bd832023-01-11 14:50:10 +01004515static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4516{
4517 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004520#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004522 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4524 return PSA_ERROR_INVALID_ARGUMENT;
4525 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004526 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004527#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004528
Przemek Stekiel86679c72022-10-06 17:06:56 +02004529#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004531 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4533 return PSA_ERROR_INVALID_ARGUMENT;
4534 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004535 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004536#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004537
Przemek Stekiel86679c72022-10-06 17:06:56 +02004538#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004540 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 if (tag_len != 16) {
4542 return PSA_ERROR_INVALID_ARGUMENT;
4543 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004544 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004545#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004546
4547 default:
4548 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004550 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004552}
4553
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004554/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004555static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4556 int is_encrypt,
4557 mbedtls_svc_key_id_t key,
4558 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004559{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4561 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4562 psa_key_slot_t *slot = NULL;
4563 psa_key_usage_t key_usage = 0;
4564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 status = psa_aead_check_algorithm(alg);
4566 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004567 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004571 status = PSA_ERROR_BAD_STATE;
4572 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004573 }
4574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 if (operation->nonce_set || operation->lengths_set ||
4576 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004577 status = PSA_ERROR_BAD_STATE;
4578 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004579 }
4580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004582 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004584 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4588 alg);
4589 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004590 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004592
4593 psa_key_attributes_t attributes = {
4594 .core = slot->attr
4595 };
4596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004598 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004600
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 if (is_encrypt) {
4602 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4603 &attributes,
4604 slot->key.data,
4605 slot->key.bytes,
4606 alg);
4607 } else {
4608 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4609 &attributes,
4610 slot->key.data,
4611 slot->key.bytes,
4612 alg);
4613 }
4614 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004615 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004617
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004619
4620exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004624 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004626 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 } else {
4628 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004629 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004632}
4633
Paul Elliott302ff6b2021-04-20 18:10:30 +01004634/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004635psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4636 mbedtls_svc_key_id_t key,
4637 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004638{
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004640}
4641
4642/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004643psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4644 mbedtls_svc_key_id_t key,
4645 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004646{
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004648}
4649
4650/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004651psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4652 uint8_t *nonce,
4653 size_t nonce_size,
4654 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004655{
4656 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004657 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004658 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004659
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004660 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004661
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004663 status = PSA_ERROR_BAD_STATE;
4664 goto exit;
4665 }
4666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004668 status = PSA_ERROR_BAD_STATE;
4669 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004670 }
4671
Paul Elliotte193ea82021-10-01 13:00:16 +01004672 /* For CCM, this size may not be correct according to the PSA
4673 * specification. The PSA Crypto 1.0.1 specification states:
4674 *
4675 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4676 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4677 *
4678 * However this restriction that L has to be the smallest integer is not
4679 * applied in practice, and it is not implementable here since the
4680 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4682 operation->alg);
4683 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004684 status = PSA_ERROR_BUFFER_TOO_SMALL;
4685 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004686 }
4687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 status = psa_generate_random(local_nonce, required_nonce_size);
4689 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004690 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004692
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004694
Paul Elliott39dc6b82021-05-11 19:16:09 +01004695exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 if (status == PSA_SUCCESS) {
4697 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004698 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 } else {
4700 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004701 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004704}
4705
4706/* Set the nonce for a multipart authenticated encryption or decryption
4707 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004708psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4709 const uint8_t *nonce,
4710 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004711{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004712 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004715 status = PSA_ERROR_BAD_STATE;
4716 goto exit;
4717 }
4718
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004720 status = PSA_ERROR_BAD_STATE;
4721 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004722 }
4723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4725 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004726 status = PSA_ERROR_INVALID_ARGUMENT;
4727 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004728 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4731 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004732
Paul Elliott39dc6b82021-05-11 19:16:09 +01004733exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004735 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 } else {
4737 psa_aead_abort(operation);
4738 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004741}
4742
4743/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004744psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4745 size_t ad_length,
4746 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004747{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004748 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4749
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004751 status = PSA_ERROR_BAD_STATE;
4752 goto exit;
4753 }
4754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 if (operation->lengths_set || operation->ad_started ||
4756 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004757 status = PSA_ERROR_BAD_STATE;
4758 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004759 }
4760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004762#if defined(PSA_WANT_ALG_GCM)
4763 case PSA_ALG_GCM:
4764 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 * bits. Without the guard this code will generate warnings on 32bit
4766 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004767#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 if (((uint64_t) ad_length) >> 61 != 0 ||
4769 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004770 status = PSA_ERROR_INVALID_ARGUMENT;
4771 goto exit;
4772 }
Paul Elliott325d3742021-09-27 17:56:28 +01004773#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004774 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004775#endif /* PSA_WANT_ALG_GCM */
4776#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004777 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004779 status = PSA_ERROR_INVALID_ARGUMENT;
4780 goto exit;
4781 }
4782 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004783#endif /* PSA_WANT_ALG_CCM */
4784#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004785 case PSA_ALG_CHACHA20_POLY1305:
4786 /* No length restrictions for ChaChaPoly. */
4787 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004788#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004789 default:
4790 break;
4791 }
Paul Elliott325d3742021-09-27 17:56:28 +01004792
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4794 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004795
Paul Elliott39dc6b82021-05-11 19:16:09 +01004796exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004798 operation->ad_remaining = ad_length;
4799 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004800 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 } else {
4802 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004803 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004806}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004807
4808/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004809psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4810 const uint8_t *input,
4811 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004812{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004816 status = PSA_ERROR_BAD_STATE;
4817 goto exit;
4818 }
4819
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004821 status = PSA_ERROR_BAD_STATE;
4822 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823 }
4824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 if (operation->lengths_set) {
4826 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004827 status = PSA_ERROR_INVALID_ARGUMENT;
4828 goto exit;
4829 }
4830
4831 operation->ad_remaining -= input_length;
4832 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004833#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004835 status = PSA_ERROR_BAD_STATE;
4836 goto exit;
4837 }
4838#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 status = psa_driver_wrapper_aead_update_ad(operation, input,
4841 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004842
Paul Elliott39dc6b82021-05-11 19:16:09 +01004843exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004845 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 } else {
4847 psa_aead_abort(operation);
4848 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004851}
4852
4853/* Encrypt or decrypt a message fragment in an active multipart AEAD
4854 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004855psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4856 const uint8_t *input,
4857 size_t input_length,
4858 uint8_t *output,
4859 size_t output_size,
4860 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004861{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004863
4864 *output_length = 0;
4865
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004867 status = PSA_ERROR_BAD_STATE;
4868 goto exit;
4869 }
4870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004872 status = PSA_ERROR_BAD_STATE;
4873 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004874 }
4875
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004877 /* Additional data length was supplied, but not all the additional
4878 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004880 status = PSA_ERROR_INVALID_ARGUMENT;
4881 goto exit;
4882 }
4883
4884 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004886 status = PSA_ERROR_INVALID_ARGUMENT;
4887 goto exit;
4888 }
4889
4890 operation->body_remaining -= input_length;
4891 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004892#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004894 status = PSA_ERROR_BAD_STATE;
4895 goto exit;
4896 }
4897#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004898
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4900 output, output_size,
4901 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004902
Paul Elliott39dc6b82021-05-11 19:16:09 +01004903exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004905 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 } else {
4907 psa_aead_abort(operation);
4908 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004911}
4912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004914{
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 if (operation->id == 0 || !operation->nonce_set) {
4916 return PSA_ERROR_BAD_STATE;
4917 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4920 operation->body_remaining != 0)) {
4921 return PSA_ERROR_INVALID_ARGUMENT;
4922 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004925}
4926
Paul Elliott302ff6b2021-04-20 18:10:30 +01004927/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004928psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4929 uint8_t *ciphertext,
4930 size_t ciphertext_size,
4931 size_t *ciphertext_length,
4932 uint8_t *tag,
4933 size_t tag_size,
4934 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004935{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004936 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4937
Paul Elliott302ff6b2021-04-20 18:10:30 +01004938 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004939 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004940
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 status = psa_aead_final_checks(operation);
4942 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004943 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004945
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004947 status = PSA_ERROR_BAD_STATE;
4948 goto exit;
4949 }
4950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4952 ciphertext_size,
4953 ciphertext_length,
4954 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004955
Paul Elliott39dc6b82021-05-11 19:16:09 +01004956exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004957
4958
Paul Elliottf47b0952021-05-21 18:02:33 +01004959 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004960 * the zero tag size, make sure the tag is set to something implausible.
4961 * Even if the operation succeeds, make sure we clear the rest of the
4962 * buffer to prevent potential leakage of anything previously placed in
4963 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004964 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004965
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004969}
4970
4971/* Finish authenticating and decrypting a message in a multipart AEAD
4972 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004973psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4974 uint8_t *plaintext,
4975 size_t plaintext_size,
4976 size_t *plaintext_length,
4977 const uint8_t *tag,
4978 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004979{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004980 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4981
Paul Elliott302ff6b2021-04-20 18:10:30 +01004982 *plaintext_length = 0;
4983
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 status = psa_aead_final_checks(operation);
4985 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004986 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004988
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004990 status = PSA_ERROR_BAD_STATE;
4991 goto exit;
4992 }
4993
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4995 plaintext_size,
4996 plaintext_length,
4997 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004998
4999exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005001
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005003}
5004
5005/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005006psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005007{
5008 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5009
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005011 /* The object has (apparently) been initialized but it is not (yet)
5012 * in use. It's ok to call abort on such an object, and there's
5013 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005015 }
5016
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005022}
5023
Gilles Peskinee59236f2018-01-27 23:32:46 +01005024/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005025/* Generators */
5026/****************************************************************/
5027
Przemek Stekiel69c46792022-06-10 12:59:51 +02005028#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005029 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005030 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
5031 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08005032#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005033#endif /* At least one builtin KDF */
5034
Przemek Stekiel69c46792022-06-10 12:59:51 +02005035#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005036 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5037 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5038static psa_status_t psa_key_derivation_start_hmac(
5039 psa_mac_operation_t *operation,
5040 psa_algorithm_t hash_alg,
5041 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005043{
5044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5045 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5047 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5048 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005049
Steven Cooreman72f736a2021-05-07 14:14:37 +02005050 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005052
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 status = psa_driver_wrapper_mac_sign_setup(operation,
5054 &attributes,
5055 hmac_key, hmac_key_length,
5056 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 psa_reset_key_attributes(&attributes);
5059 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005060}
5061#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005062
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005063#define HKDF_STATE_INIT 0 /* no input yet */
5064#define HKDF_STATE_STARTED 1 /* got salt */
5065#define HKDF_STATE_KEYED 2 /* got key */
5066#define HKDF_STATE_OUTPUT 3 /* output started */
5067
Gilles Peskinecbe66502019-05-16 16:59:18 +02005068static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005070{
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5072 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5073 } else {
5074 return operation->alg;
5075 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005076}
5077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005079{
5080 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5082 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005083 /* The object has (apparently) been initialized but it is not
5084 * in use. It's ok to call abort on such an object, and there's
5085 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005087#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5089 mbedtls_free(operation->ctx.hkdf.info);
5090 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5091 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005092#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005093#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5094 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5096 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5097 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5098 if (operation->ctx.tls12_prf.secret != NULL) {
5099 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5100 operation->ctx.tls12_prf.secret_length);
5101 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005102 }
5103
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 if (operation->ctx.tls12_prf.seed != NULL) {
5105 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5106 operation->ctx.tls12_prf.seed_length);
5107 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005108 }
5109
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 if (operation->ctx.tls12_prf.label != NULL) {
5111 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5112 operation->ctx.tls12_prf.label_length);
5113 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005114 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005115#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 if (operation->ctx.tls12_prf.other_secret != NULL) {
5117 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5118 operation->ctx.tls12_prf.other_secret_length);
5119 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005120 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005121#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005122 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005123
5124 /* We leave the fields Ai and output_block to be erased safely by the
5125 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005127#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5128 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005129#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5131 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5132 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5133 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005134#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005135 {
5136 status = PSA_ERROR_BAD_STATE;
5137 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 mbedtls_platform_zeroize(operation, sizeof(*operation));
5139 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005140}
5141
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005142psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005144{
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005146 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005148 }
5149
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005150 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005152}
5153
Gilles Peskine449bd832023-01-11 14:50:10 +01005154psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5155 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005156{
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 if (operation->alg == 0) {
5158 return PSA_ERROR_BAD_STATE;
5159 }
5160 if (capacity > operation->capacity) {
5161 return PSA_ERROR_INVALID_ARGUMENT;
5162 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005163 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005165}
5166
Przemek Stekiel69c46792022-06-10 12:59:51 +02005167#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005168/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005169static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5170 psa_algorithm_t kdf_alg,
5171 uint8_t *output,
5172 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005173{
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5175 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005176 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005177 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005178#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005180#else
5181 const uint8_t last_block = 0xff;
5182#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 if (hkdf->state < HKDF_STATE_KEYED ||
5185 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005186#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005188#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 )) {
5190 return PSA_ERROR_BAD_STATE;
5191 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005192 hkdf->state = HKDF_STATE_OUTPUT;
5193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005195 /* Copy what remains of the current block */
5196 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005198 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 }
5200 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005201 output += n;
5202 output_length -= n;
5203 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005205 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005207 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005208 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005210 * object was corrupted or if this function is called directly
5211 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 if (hkdf->block_number == last_block) {
5213 return PSA_ERROR_BAD_STATE;
5214 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005215
5216 /* We need a new block */
5217 ++hkdf->block_number;
5218 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005219
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5221 hash_alg,
5222 hkdf->prk,
5223 hash_length);
5224 if (status != PSA_SUCCESS) {
5225 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005226 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005227
5228 if (hkdf->block_number != 1) {
5229 status = psa_mac_update(&hkdf->hmac,
5230 hkdf->output_block,
5231 hash_length);
5232 if (status != PSA_SUCCESS) {
5233 return status;
5234 }
5235 }
5236 status = psa_mac_update(&hkdf->hmac,
5237 hkdf->info,
5238 hkdf->info_length);
5239 if (status != PSA_SUCCESS) {
5240 return status;
5241 }
5242 status = psa_mac_update(&hkdf->hmac,
5243 &hkdf->block_number, 1);
5244 if (status != PSA_SUCCESS) {
5245 return status;
5246 }
5247 status = psa_mac_sign_finish(&hkdf->hmac,
5248 hkdf->output_block,
5249 sizeof(hkdf->output_block),
5250 &hmac_output_length);
5251 if (status != PSA_SUCCESS) {
5252 return status;
5253 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005254 }
5255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005257}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005258#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005259
John Durkop07cc04a2020-11-16 22:08:34 -08005260#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5261 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005262static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5263 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005265{
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5267 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005268 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5269 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005270 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005271
Janos Follath7742fee2019-06-17 12:58:10 +01005272 /* We can't be wanting more output after block 0xff, otherwise
5273 * the capacity check in psa_key_derivation_output_bytes() would have
5274 * prevented this call. It could happen only if the operation
5275 * object was corrupted or if this function is called directly
5276 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 if (tls12_prf->block_number == 0xff) {
5278 return PSA_ERROR_CORRUPTION_DETECTED;
5279 }
Janos Follath7742fee2019-06-17 12:58:10 +01005280
5281 /* We need a new block */
5282 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005283 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005284
5285 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5286 *
5287 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5288 *
5289 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5290 * HMAC_hash(secret, A(2) + seed) +
5291 * HMAC_hash(secret, A(3) + seed) + ...
5292 *
5293 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005294 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005295 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005296 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005297 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005298 * is currently extracted as `output_block` and where i is
5299 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005300 */
5301
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 status = psa_key_derivation_start_hmac(&hmac,
5303 hash_alg,
5304 tls12_prf->secret,
5305 tls12_prf->secret_length);
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 A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005312 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5313 * the variable seed and in this instance means it in the context of the
5314 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 status = psa_mac_update(&hmac,
5316 tls12_prf->label,
5317 tls12_prf->label_length);
5318 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005319 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 }
5321 status = psa_mac_update(&hmac,
5322 tls12_prf->seed,
5323 tls12_prf->seed_length);
5324 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005325 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 }
5327 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005328 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5330 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005331 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005333 }
5334
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 status = psa_mac_sign_finish(&hmac,
5336 tls12_prf->Ai, hash_length,
5337 &hmac_output_length);
5338 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005339 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 }
5341 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005342 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005344
5345 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 status = psa_key_derivation_start_hmac(&hmac,
5347 hash_alg,
5348 tls12_prf->secret,
5349 tls12_prf->secret_length);
5350 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005351 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 }
5353 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5354 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005355 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 }
5357 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5358 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005359 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 }
5361 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5362 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005363 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 }
5365 status = psa_mac_sign_finish(&hmac,
5366 tls12_prf->output_block, hash_length,
5367 &hmac_output_length);
5368 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005369 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005371
Janos Follath7742fee2019-06-17 12:58:10 +01005372
5373cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 cleanup_status = psa_mac_abort(&hmac);
5375 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005376 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005378
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005380}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005381
Janos Follath844eb0e2019-06-19 12:10:49 +01005382static psa_status_t psa_key_derivation_tls12_prf_read(
5383 psa_tls12_prf_key_derivation_t *tls12_prf,
5384 psa_algorithm_t alg,
5385 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005387{
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5389 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005390 psa_status_t status;
5391 uint8_t offset, length;
5392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005394 case PSA_TLS12_PRF_STATE_LABEL_SET:
5395 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5396 break;
5397 case PSA_TLS12_PRF_STATE_OUTPUT:
5398 break;
5399 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005401 }
5402
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005404 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 if (tls12_prf->left_in_block == 0) {
5406 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5407 alg);
5408 if (status != PSA_SUCCESS) {
5409 return status;
5410 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005411
5412 continue;
5413 }
5414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005416 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005418 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005420
5421 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005423 output += length;
5424 output_length -= length;
5425 tls12_prf->left_in_block -= length;
5426 }
5427
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005429}
John Durkop07cc04a2020-11-16 22:08:34 -08005430#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5431 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005432
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005433#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5434static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5435 psa_tls12_ecjpake_to_pms_t *ecjpake,
5436 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005438{
5439 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005440 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005441
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 if (output_length != 32) {
5443 return PSA_ERROR_INVALID_ARGUMENT;
5444 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005445
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5447 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5448 &output_size);
5449 if (status != PSA_SUCCESS) {
5450 return status;
5451 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005452
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 if (output_size != output_length) {
5454 return PSA_ERROR_GENERIC_ERROR;
5455 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005458}
5459#endif
5460
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005461psa_status_t psa_key_derivation_output_bytes(
5462 psa_key_derivation_operation_t *operation,
5463 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005465{
5466 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005468
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005472 }
5473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005475 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005476 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005477 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005478 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005479 goto exit;
5480 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005482 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005483 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005484 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5485 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005486 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005487 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005489 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005490 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005491
Przemek Stekiel69c46792022-06-10 12:59:51 +02005492#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5494 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5495 output, output_length);
5496 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005497#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005498#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5499 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5501 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5502 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5503 kdf_alg, output,
5504 output_length);
5505 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005506#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5507 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005508#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005510 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5512 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005513#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5514
Gilles Peskineeab56e42018-07-12 17:12:33 +02005515 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005516 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005518 }
5519
5520exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005522 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 * This allows us to differentiate between exhausted operations and
5524 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5525 * operations. */
5526 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005528 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005530 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005532}
5533
David Brown7807bf72021-02-09 16:28:23 -07005534#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005535static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005536{
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 if (data_size >= 8) {
5538 mbedtls_des_key_set_parity(data);
5539 }
5540 if (data_size >= 16) {
5541 mbedtls_des_key_set_parity(data + 8);
5542 }
5543 if (data_size >= 24) {
5544 mbedtls_des_key_set_parity(data + 16);
5545 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005546}
David Brown7807bf72021-02-09 16:28:23 -07005547#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005548
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005549/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 * ECC keys on a Weierstrass elliptic curve require the generation
5551 * of a private key which is an integer
5552 * in the range [1, N - 1], where N is the boundary of the private key domain:
5553 * N is the prime p for Diffie-Hellman, or the order of the
5554 * curve’s base point for ECC.
5555 *
5556 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5557 * This function generates the private key using the following process:
5558 *
5559 * 1. Draw a byte string of length ceiling(m/8) bytes.
5560 * 2. If m is not a multiple of 8, set the most significant
5561 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5562 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5563 * 4. If k > N - 2, discard the result and return to step 1.
5564 * 5. Output k + 1 as the private key.
5565 *
5566 * This method allows compliance to NIST standards, specifically the methods titled
5567 * Key-Pair Generation by Testing Candidates in the following publications:
5568 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5569 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5570 * Diffie-Hellman keys.
5571 *
5572 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5573 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5574 *
5575 * Note: Function allocates memory for *data buffer, so given *data should be
5576 * always NULL.
5577 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005578#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5579 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005580 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5581 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5582 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005583static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005584 psa_key_slot_t *slot,
5585 size_t bits,
5586 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005587 uint8_t **data
5588 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005589{
Valerio Setti52789862023-04-07 12:13:11 +02005590#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005591 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005592 mbedtls_mpi k;
5593 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 mbedtls_mpi_init(&k);
5598 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005599
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005600 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005602 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005604
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005606 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005607 goto cleanup;
5608 }
5609
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005610 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005612
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005614
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005615 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005616 /* Let m be the bit size of N. */
5617 size_t m = ecp_group.nbits;
5618
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005620
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005621 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005623
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005624 /* Note: This function is always called with *data == NULL and it
5625 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 *data = mbedtls_calloc(1, m_bytes);
5627 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005628 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005629 goto cleanup;
5630 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005633 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005635 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005637
5638 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005640 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005641 * (8 * ceiling(m/8) - m) bits of the first byte in
5642 * the string to zero.
5643 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005645 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005646 }
5647
5648 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 * big-endian byte string.
5650 */
5651 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005652
5653 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 * Result of comparison is returned. When it indicates error
5655 * then this function is called again.
5656 */
5657 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005658 }
5659
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005660 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5662 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005663cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 if (ret != 0) {
5665 status = mbedtls_to_psa_error(ret);
5666 }
5667 if (status != PSA_SUCCESS) {
5668 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005669 *data = NULL;
5670 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 mbedtls_mpi_free(&k);
5672 mbedtls_mpi_free(&diff_N_2);
5673 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005674#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005675 (void) slot;
5676 (void) bits;
5677 (void) operation;
5678 (void) data;
5679 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005680#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005681}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005682
5683/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5684 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5685 *
5686 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5687 * draw a 32-byte string and process it as specified in
5688 * Elliptic Curves for Security [RFC7748] §5.
5689 *
5690 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5691 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5692 *
5693 * Note: Function allocates memory for *data buffer, so given *data should be
5694 * always NULL.
5695 */
5696
5697static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5698 size_t bits,
5699 psa_key_derivation_operation_t *operation,
5700 uint8_t **data
5701 )
5702{
5703 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005707 case 255:
5708 output_length = 32;
5709 break;
5710 case 448:
5711 output_length = 56;
5712 break;
5713 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005715 break;
5716 }
5717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 if (*data == NULL) {
5721 return PSA_ERROR_INSUFFICIENT_MEMORY;
5722 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005727 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005729
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005731 case 255:
5732 (*data)[0] &= 248;
5733 (*data)[31] &= 127;
5734 (*data)[31] |= 64;
5735 break;
5736 case 448:
5737 (*data)[0] &= 252;
5738 (*data)[55] |= 128;
5739 break;
5740 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005742 break;
5743 }
5744
5745 return status;
5746}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005747#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5748 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005749 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5750 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5751 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005752
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005753static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005754 psa_key_slot_t *slot,
5755 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005757{
5758 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305760 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005761 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005762
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5764 return PSA_ERROR_INVALID_ARGUMENT;
5765 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005766
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005767#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5768 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005769 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5770 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5771 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5773 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5774 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005775 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5777 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005778 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 }
5780 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005781 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5783 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005784 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005786 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005787 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005788#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5789 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005790 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5791 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5792 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 if (key_type_is_raw_bytes(slot->attr.type)) {
5794 if (bits % 8 != 0) {
5795 return PSA_ERROR_INVALID_ARGUMENT;
5796 }
5797 data = mbedtls_calloc(1, bytes);
5798 if (data == NULL) {
5799 return PSA_ERROR_INSUFFICIENT_MEMORY;
5800 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 status = psa_key_derivation_output_bytes(operation, data, bytes);
5803 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005804 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 }
David Brown12ca5032021-02-11 11:02:00 -07005806#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5808 psa_des_set_key_parity(data, bytes);
5809 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005810#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 } else {
5812 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005813 }
Ronald Crondd04d422020-11-28 15:14:42 +01005814
Ronald Cronbf33c932020-11-28 18:06:53 +01005815 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005816 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005818 };
5819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5821 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5822 &storage_size);
5823 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305824 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 }
Archanad8a83dc2021-06-14 10:04:16 +05305826 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 status = psa_allocate_buffer_to_slot(slot, storage_size);
5828 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305829 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 }
Archanad8a83dc2021-06-14 10:04:16 +05305831
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 status = psa_driver_wrapper_import_key(&attributes,
5833 data, bytes,
5834 slot->key.data,
5835 slot->key.bytes,
5836 &slot->key.bytes, &bits);
5837 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005838 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005840
5841exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 mbedtls_free(data);
5843 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005844}
5845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5847 psa_key_derivation_operation_t *operation,
5848 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005849{
5850 psa_status_t status;
5851 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005852 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005853
Ronald Cron81709fc2020-11-14 12:10:32 +01005854 *key = MBEDTLS_SVC_KEY_ID_INIT;
5855
Gilles Peskine0f84d622019-09-12 19:03:13 +02005856 /* Reject any attempt to create a zero-length key so that we don't
5857 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 if (psa_get_key_bits(attributes) == 0) {
5859 return PSA_ERROR_INVALID_ARGUMENT;
5860 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 if (operation->alg == PSA_ALG_NONE) {
5863 return PSA_ERROR_BAD_STATE;
5864 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 if (!operation->can_output_key) {
5867 return PSA_ERROR_NOT_PERMITTED;
5868 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005869
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5871 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005872#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005874 /* Deriving a key in a secure element is not implemented yet. */
5875 status = PSA_ERROR_NOT_SUPPORTED;
5876 }
5877#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 if (status == PSA_SUCCESS) {
5879 status = psa_generate_derived_key_internal(slot,
5880 attributes->core.bits,
5881 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005882 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 if (status == PSA_SUCCESS) {
5884 status = psa_finish_key_creation(slot, driver, key);
5885 }
5886 if (status != PSA_SUCCESS) {
5887 psa_fail_key_creation(slot, driver);
5888 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005889
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005891}
5892
Gilles Peskineeab56e42018-07-12 17:12:33 +02005893
5894
5895/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005896/* Key derivation */
5897/****************************************************************/
5898
Steven Cooreman932ffb72021-02-15 12:14:32 +01005899#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005900static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005901{
5902#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5904 return 1;
5905 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005906#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005907#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5909 return 1;
5910 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005911#endif
5912#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5914 return 1;
5915 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005916#endif
5917#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5919 return 1;
5920 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005921#endif
5922#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5924 return 1;
5925 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005926#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005927#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5929 return 1;
5930 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005931#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005933}
5934
Gilles Peskine449bd832023-01-11 14:50:10 +01005935static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005936{
5937 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 psa_status_t status = psa_hash_setup(&operation, alg);
5939 psa_hash_abort(&operation);
5940 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005941}
5942
Gilles Peskine969c5d62019-01-16 15:53:06 +01005943static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005944 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005946{
Janos Follath5fe19732019-06-20 15:09:30 +01005947 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5948 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005950
Gilles Peskine969c5d62019-01-16 15:53:06 +01005951 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 if (!is_kdf_alg_supported(kdf_alg)) {
5953 return PSA_ERROR_NOT_SUPPORTED;
5954 }
John Durkop07cc04a2020-11-16 22:08:34 -08005955
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005956 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005957 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5959 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5960 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5961 if (hash_size == 0) {
5962 return PSA_ERROR_NOT_SUPPORTED;
5963 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005964
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005965 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5966 * we might fail later, which is somewhat unfriendly and potentially
5967 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 psa_status_t status = psa_hash_try_support(hash_alg);
5969 if (status != PSA_SUCCESS) {
5970 return status;
5971 }
5972 } else {
5973 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005974 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5977 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5978 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5979 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005980 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005981#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005982 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5984 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005985 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005987#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5988 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 operation->capacity = 255 * hash_size;
5990 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005991}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005994{
5995#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 if (alg == PSA_ALG_ECDH) {
5997 return PSA_SUCCESS;
5998 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005999#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006000#if defined(PSA_WANT_ALG_FFDH)
6001 if (alg == PSA_ALG_FFDH) {
6002 return PSA_SUCCESS;
6003 }
6004#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006005 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006007}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006008
6009static int psa_key_derivation_allows_free_form_secret_input(
6010 psa_algorithm_t kdf_alg)
6011{
6012#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6013 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6014 return 0;
6015 }
6016#endif
6017 (void) kdf_alg;
6018 return 1;
6019}
John Durkop07cc04a2020-11-16 22:08:34 -08006020#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6023 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006024{
6025 psa_status_t status;
6026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 if (operation->alg != 0) {
6028 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006029 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006030
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6032 return PSA_ERROR_INVALID_ARGUMENT;
6033 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6034#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6035 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6036 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6037 status = psa_key_agreement_try_support(ka_alg);
6038 if (status != PSA_SUCCESS) {
6039 return status;
6040 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006041 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6042 return PSA_ERROR_INVALID_ARGUMENT;
6043 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6045#else
6046 return PSA_ERROR_NOT_SUPPORTED;
6047#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6048 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6049#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6050 status = psa_key_derivation_setup_kdf(operation, alg);
6051#else
6052 return PSA_ERROR_NOT_SUPPORTED;
6053#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6054 } else {
6055 return PSA_ERROR_INVALID_ARGUMENT;
6056 }
6057
6058 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006059 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 }
6061 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006062}
6063
Przemek Stekiel69c46792022-06-10 12:59:51 +02006064#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006065static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6066 psa_algorithm_t kdf_alg,
6067 psa_key_derivation_step_t step,
6068 const uint8_t *data,
6069 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006070{
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006072 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006074 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6077 return PSA_ERROR_INVALID_ARGUMENT;
6078 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006079#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (hkdf->state != HKDF_STATE_INIT) {
6081 return PSA_ERROR_BAD_STATE;
6082 } else {
6083 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6084 hash_alg,
6085 data, data_length);
6086 if (status != PSA_SUCCESS) {
6087 return status;
6088 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006089 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006091 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006092 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006093#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006095 /* We shouldn't be in different state as HKDF_EXPAND only allows
6096 * two inputs: SECRET (this case) and INFO which does not modify
6097 * the state. It could happen only if the hkdf
6098 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 if (hkdf->state != HKDF_STATE_INIT) {
6100 return PSA_ERROR_BAD_STATE;
6101 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006102
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006103 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6105 return PSA_ERROR_INVALID_ARGUMENT;
6106 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 memcpy(hkdf->prk, data, data_length);
6109 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006110#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006111 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006112 /* HKDF: If no salt was provided, use an empty salt.
6113 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006115#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6117 return PSA_ERROR_BAD_STATE;
6118 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006119#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6121 hash_alg,
6122 NULL, 0);
6123 if (status != PSA_SUCCESS) {
6124 return status;
6125 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006126 hkdf->state = HKDF_STATE_STARTED;
6127 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 if (hkdf->state != HKDF_STATE_STARTED) {
6129 return PSA_ERROR_BAD_STATE;
6130 }
6131 status = psa_mac_update(&hkdf->hmac,
6132 data, data_length);
6133 if (status != PSA_SUCCESS) {
6134 return status;
6135 }
6136 status = psa_mac_sign_finish(&hkdf->hmac,
6137 hkdf->prk,
6138 sizeof(hkdf->prk),
6139 &data_length);
6140 if (status != PSA_SUCCESS) {
6141 return status;
6142 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006143 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006144
Gilles Peskine2b522db2019-04-12 15:11:49 +02006145 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006146 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006147#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006149 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006151 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006153#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006154 {
6155 /* Block 0 is empty, and the next block will be
6156 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006158 }
6159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006161 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006162#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6164 return PSA_ERROR_INVALID_ARGUMENT;
6165 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006166#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006167#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6169 hkdf->state == HKDF_STATE_INIT) {
6170 return PSA_ERROR_BAD_STATE;
6171 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006172#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 if (hkdf->state == HKDF_STATE_OUTPUT) {
6174 return PSA_ERROR_BAD_STATE;
6175 }
6176 if (hkdf->info_set) {
6177 return PSA_ERROR_BAD_STATE;
6178 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006179 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 if (data_length != 0) {
6181 hkdf->info = mbedtls_calloc(1, data_length);
6182 if (hkdf->info == NULL) {
6183 return PSA_ERROR_INSUFFICIENT_MEMORY;
6184 }
6185 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006186 }
6187 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006189 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006191 }
6192}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006193#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006194
John Durkop07cc04a2020-11-16 22:08:34 -08006195#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6196 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006197static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6198 const uint8_t *data,
6199 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006200{
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6202 return PSA_ERROR_BAD_STATE;
6203 }
Janos Follathf08e2652019-06-13 09:05:41 +01006204
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 if (data_length != 0) {
6206 prf->seed = mbedtls_calloc(1, data_length);
6207 if (prf->seed == NULL) {
6208 return PSA_ERROR_INSUFFICIENT_MEMORY;
6209 }
Janos Follathf08e2652019-06-13 09:05:41 +01006210
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006212 prf->seed_length = data_length;
6213 }
Janos Follathf08e2652019-06-13 09:05:41 +01006214
Gilles Peskine43f958b2020-12-13 14:55:14 +01006215 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006218}
6219
Gilles Peskine449bd832023-01-11 14:50:10 +01006220static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6221 const uint8_t *data,
6222 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006223{
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6225 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6226 return PSA_ERROR_BAD_STATE;
6227 }
Janos Follath81550542019-06-13 14:26:34 +01006228
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 if (data_length != 0) {
6230 prf->secret = mbedtls_calloc(1, data_length);
6231 if (prf->secret == NULL) {
6232 return PSA_ERROR_INSUFFICIENT_MEMORY;
6233 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006236 prf->secret_length = data_length;
6237 }
Janos Follath81550542019-06-13 14:26:34 +01006238
Gilles Peskine43f958b2020-12-13 14:55:14 +01006239 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006240
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006242}
6243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6245 const uint8_t *data,
6246 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006247{
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6249 return PSA_ERROR_BAD_STATE;
6250 }
Janos Follath63028dd2019-06-13 09:15:47 +01006251
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 if (data_length != 0) {
6253 prf->label = mbedtls_calloc(1, data_length);
6254 if (prf->label == NULL) {
6255 return PSA_ERROR_INSUFFICIENT_MEMORY;
6256 }
Janos Follath63028dd2019-06-13 09:15:47 +01006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006259 prf->label_length = data_length;
6260 }
Janos Follath63028dd2019-06-13 09:15:47 +01006261
Gilles Peskine43f958b2020-12-13 14:55:14 +01006262 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006265}
6266
Gilles Peskine449bd832023-01-11 14:50:10 +01006267static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6268 psa_key_derivation_step_t step,
6269 const uint8_t *data,
6270 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006271{
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006273 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006275 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006277 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006279 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006281 }
6282}
John Durkop07cc04a2020-11-16 22:08:34 -08006283#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6284 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6285
6286#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6287static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6288 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006289 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006291{
6292 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6294 4 + data_length + prf->other_secret_length :
6295 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6298 return PSA_ERROR_INVALID_ARGUMENT;
6299 }
John Durkop07cc04a2020-11-16 22:08:34 -08006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 uint8_t *pms = mbedtls_calloc(1, pms_len);
6302 if (pms == NULL) {
6303 return PSA_ERROR_INSUFFICIENT_MEMORY;
6304 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006305 uint8_t *cur = pms;
6306
6307 /* pure-PSK:
6308 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006309 *
6310 * The premaster secret is formed as follows: if the PSK is N octets
6311 * long, concatenate a uint16 with the value N, N zero octets, a second
6312 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006313 *
6314 * mixed-PSK:
6315 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6316 * follows: concatenate a uint16 with the length of the other secret,
6317 * the other secret itself, uint16 with the length of PSK, and the
6318 * PSK itself.
6319 * For details please check:
6320 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6321 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6322 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006323 */
6324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6326 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6327 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6328 if (prf->other_secret_length != 0) {
6329 memcpy(cur, prf->other_secret, prf->other_secret_length);
6330 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006331 cur += prf->other_secret_length;
6332 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 } else {
6334 *cur++ = MBEDTLS_BYTE_1(data_length);
6335 *cur++ = MBEDTLS_BYTE_0(data_length);
6336 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006337 cur += data_length;
6338 }
6339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 *cur++ = MBEDTLS_BYTE_1(data_length);
6341 *cur++ = MBEDTLS_BYTE_0(data_length);
6342 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006343 cur += data_length;
6344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 mbedtls_platform_zeroize(pms, pms_len);
6348 mbedtls_free(pms);
6349 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006350}
Janos Follath6660f0e2019-06-17 08:44:03 +01006351
Przemek Stekiele47201b2022-04-19 13:53:28 +02006352static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6353 psa_tls12_prf_key_derivation_t *prf,
6354 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006356{
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6358 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006359 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006360
6361 if (data_length != 0) {
6362 prf->other_secret = mbedtls_calloc(1, data_length);
6363 if (prf->other_secret == NULL) {
6364 return PSA_ERROR_INSUFFICIENT_MEMORY;
6365 }
6366
6367 memcpy(prf->other_secret, data, data_length);
6368 prf->other_secret_length = data_length;
6369 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006370 prf->other_secret_length = 0;
6371 }
6372
6373 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006376}
6377
Janos Follath6660f0e2019-06-17 08:44:03 +01006378static psa_status_t psa_tls12_prf_psk_to_ms_input(
6379 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006380 psa_key_derivation_step_t step,
6381 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006383{
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006385 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 return psa_tls12_prf_psk_to_ms_set_key(prf,
6387 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006388 break;
6389 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6391 data,
6392 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006393 break;
6394 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006396 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006397
Przemek Stekiele47201b2022-04-19 13:53:28 +02006398 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006399}
John Durkop07cc04a2020-11-16 22:08:34 -08006400#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006401
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006402#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6403static psa_status_t psa_tls12_ecjpake_to_pms_input(
6404 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006405 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006406 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006408{
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6410 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6411 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006412 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006413
6414 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 if (data[0] != 0x04) {
6416 return PSA_ERROR_INVALID_ARGUMENT;
6417 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006418
6419 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006423}
6424#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006425/** Check whether the given key type is acceptable for the given
6426 * input step of a key derivation.
6427 *
6428 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6429 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6430 * Both secret and non-secret inputs can alternatively have the type
6431 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6432 * that the input was passed as a buffer rather than via a key object.
6433 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006434static int psa_key_derivation_check_input_type(
6435 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006437{
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006439 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 if (key_type == PSA_KEY_TYPE_DERIVE) {
6441 return PSA_SUCCESS;
6442 }
6443 if (key_type == PSA_KEY_TYPE_NONE) {
6444 return PSA_SUCCESS;
6445 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006446 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006447 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 if (key_type == PSA_KEY_TYPE_DERIVE) {
6449 return PSA_SUCCESS;
6450 }
6451 if (key_type == PSA_KEY_TYPE_NONE) {
6452 return PSA_SUCCESS;
6453 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006454 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006455 case PSA_KEY_DERIVATION_INPUT_LABEL:
6456 case PSA_KEY_DERIVATION_INPUT_SALT:
6457 case PSA_KEY_DERIVATION_INPUT_INFO:
6458 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6460 return PSA_SUCCESS;
6461 }
6462 if (key_type == PSA_KEY_TYPE_NONE) {
6463 return PSA_SUCCESS;
6464 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006465 break;
6466 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006468}
6469
Janos Follathb80a94e2019-06-12 15:54:46 +01006470static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006471 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006472 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006473 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006474 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006476{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006477 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006479
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 status = psa_key_derivation_check_input_type(step, key_type);
6481 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006482 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006484
Przemek Stekiel69c46792022-06-10 12:59:51 +02006485#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6487 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6488 step, data, data_length);
6489 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006490#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006491#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6493 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6494 step, data, data_length);
6495 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006496#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6497#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6499 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6500 step, data, data_length);
6501 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006502#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006503#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006505 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6507 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006508#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006509 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006510 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006511 (void) data;
6512 (void) data_length;
6513 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006515 }
6516
Gilles Peskine224b0d62019-09-23 18:13:17 +02006517exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 if (status != PSA_SUCCESS) {
6519 psa_key_derivation_abort(operation);
6520 }
6521 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006522}
6523
Janos Follath51f4a0f2019-06-14 11:35:55 +01006524psa_status_t psa_key_derivation_input_bytes(
6525 psa_key_derivation_operation_t *operation,
6526 psa_key_derivation_step_t step,
6527 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006528 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006529{
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 return psa_key_derivation_input_internal(operation, step,
6531 PSA_KEY_TYPE_NONE,
6532 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006533}
6534
Janos Follath51f4a0f2019-06-14 11:35:55 +01006535psa_status_t psa_key_derivation_input_key(
6536 psa_key_derivation_operation_t *operation,
6537 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006539{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006540 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006541 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006542 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006543
Ronald Cron5c522922020-11-14 16:35:34 +01006544 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6546 if (status != PSA_SUCCESS) {
6547 psa_key_derivation_abort(operation);
6548 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006549 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006550
6551 /* Passing a key object as a SECRET input unlocks the permission
6552 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006554 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006556
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 status = psa_key_derivation_input_internal(operation,
6558 step, slot->attr.type,
6559 slot->key.data,
6560 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006561
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006563
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006565}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006566
6567
6568
6569/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006570/* Key agreement */
6571/****************************************************************/
6572
Gilles Peskine449bd832023-01-11 14:50:10 +01006573psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6574 const uint8_t *key_buffer,
6575 size_t key_buffer_size,
6576 psa_algorithm_t alg,
6577 const uint8_t *peer_key,
6578 size_t peer_key_length,
6579 uint8_t *shared_secret,
6580 size_t shared_secret_size,
6581 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006582{
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006584#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006585 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6587 key_buffer_size, alg,
6588 peer_key, peer_key_length,
6589 shared_secret,
6590 shared_secret_size,
6591 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006592#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006593
6594#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6595 case PSA_ALG_FFDH:
Przemek Stekiel359f4622022-12-05 14:11:55 +01006596 return mbedtls_psa_key_agreement_ffdh(attributes,
6597 peer_key,
6598 peer_key_length,
6599 key_buffer,
6600 key_buffer_size,
6601 shared_secret,
6602 shared_secret_size,
6603 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006604#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6605
Gilles Peskine01d718c2018-09-18 12:01:02 +02006606 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006607 (void) attributes;
6608 (void) key_buffer;
6609 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006610 (void) peer_key;
6611 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006612 (void) shared_secret;
6613 (void) shared_secret_size;
6614 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006615 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006616 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006617}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006618
Aditya Deshpande17845b82022-10-13 17:21:01 +01006619/** Internal function for raw key agreement
6620 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006621 * to the driver's implementation if a driver is present.
6622 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006623 * (psa_key_agreement_raw_builtin).
6624 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006625static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6626 psa_key_slot_t *private_key,
6627 const uint8_t *peer_key,
6628 size_t peer_key_length,
6629 uint8_t *shared_secret,
6630 size_t shared_secret_size,
6631 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006632{
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6634 return PSA_ERROR_NOT_SUPPORTED;
6635 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006636
6637 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006639 };
6640
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 return psa_driver_wrapper_key_agreement(&attributes,
6642 private_key->key.data,
6643 private_key->key.bytes, alg,
6644 peer_key, peer_key_length,
6645 shared_secret,
6646 shared_secret_size,
6647 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006648}
6649
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006650/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006651 * to potentially free embedded data structures and wipe confidential data.
6652 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006653static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6654 psa_key_derivation_step_t step,
6655 psa_key_slot_t *private_key,
6656 const uint8_t *peer_key,
6657 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006658{
6659 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006660 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006661 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006663
6664 /* Step 1: run the secret agreement algorithm to generate the shared
6665 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 status = psa_key_agreement_raw_internal(ka_alg,
6667 private_key,
6668 peer_key, peer_key_length,
6669 shared_secret,
6670 sizeof(shared_secret),
6671 &shared_secret_length);
6672 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006673 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006675
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006676 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006677 * the shared secret. A shared secret is permitted wherever a key
6678 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006679 status = psa_key_derivation_input_internal(operation, step,
6680 PSA_KEY_TYPE_DERIVE,
6681 shared_secret,
6682 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006683exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6685 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006686}
6687
Gilles Peskine449bd832023-01-11 14:50:10 +01006688psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6689 psa_key_derivation_step_t step,
6690 mbedtls_svc_key_id_t private_key,
6691 const uint8_t *peer_key,
6692 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006693{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006694 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006695 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006696 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006697
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6699 return PSA_ERROR_INVALID_ARGUMENT;
6700 }
Ronald Cron5c522922020-11-14 16:35:34 +01006701 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6703 if (status != PSA_SUCCESS) {
6704 return status;
6705 }
6706 status = psa_key_agreement_internal(operation, step,
6707 slot,
6708 peer_key, peer_key_length);
6709 if (status != PSA_SUCCESS) {
6710 psa_key_derivation_abort(operation);
6711 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006712 /* If a private key has been added as SECRET, we allow the derived
6713 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006715 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006717 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006718
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006720
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006722}
6723
Gilles Peskine449bd832023-01-11 14:50:10 +01006724psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6725 mbedtls_svc_key_id_t private_key,
6726 const uint8_t *peer_key,
6727 size_t peer_key_length,
6728 uint8_t *output,
6729 size_t output_size,
6730 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006731{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006732 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006733 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006734 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006735
Gilles Peskine449bd832023-01-11 14:50:10 +01006736 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006737 status = PSA_ERROR_INVALID_ARGUMENT;
6738 goto exit;
6739 }
Ronald Cron5c522922020-11-14 16:35:34 +01006740 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6742 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006743 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006745
Gilles Peskine3e561302022-04-14 00:17:15 +02006746 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6747 * for the output size. The PSA specification only guarantees that this
6748 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6749 * but it might be nice to allow smaller buffers if the output fits.
6750 * At the time of writing this comment, with only ECDH implemented,
6751 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6752 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6753 * be exact for it as well. */
6754 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6756 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006757 status = PSA_ERROR_BUFFER_TOO_SMALL;
6758 goto exit;
6759 }
6760
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 status = psa_key_agreement_raw_internal(alg, slot,
6762 peer_key, peer_key_length,
6763 output, output_size,
6764 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006765
6766exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006768 /* If an error happens and is not handled properly, the output
6769 * may be used as a key to protect sensitive data. Arrange for such
6770 * a key to be random, which is likely to result in decryption or
6771 * verification errors. This is better than filling the buffer with
6772 * some constant data such as zeros, which would result in the data
6773 * being protected with a reproducible, easily knowable key.
6774 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006776 *output_length = output_size;
6777 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006778
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006780
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006782}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006783
6784
Gilles Peskine30524eb2020-11-13 17:02:26 +01006785
Gilles Peskine86a440b2018-11-12 18:39:40 +01006786/****************************************************************/
6787/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006788/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006789
Gilles Peskine30524eb2020-11-13 17:02:26 +01006790/** Initialize the PSA random generator.
6791 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006792static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006793{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006794#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006796#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6797
Gilles Peskine30524eb2020-11-13 17:02:26 +01006798 /* Set default configuration if
6799 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006801 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 }
6803 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006804 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006806
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006808#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6809 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6810 /* The PSA entropy injection feature depends on using NV seed as an entropy
6811 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 mbedtls_entropy_add_source(&rng->entropy,
6813 mbedtls_nv_seed_poll, NULL,
6814 MBEDTLS_ENTROPY_BLOCK_SIZE,
6815 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006816#endif
6817
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006819#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006820}
6821
6822/** Deinitialize the PSA random generator.
6823 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006824static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006825{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006826#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006828#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6830 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006831#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006832}
6833
6834/** Seed the PSA random generator.
6835 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006836static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006837{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006838#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6839 /* Do nothing: the external RNG seeds itself. */
6840 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006841 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006842#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006843 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6845 drbg_seed, sizeof(drbg_seed) - 1);
6846 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006847#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006848}
6849
Gilles Peskine449bd832023-01-11 14:50:10 +01006850psa_status_t psa_generate_random(uint8_t *output,
6851 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006852{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006853 GUARD_MODULE_INITIALIZED;
6854
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006855#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6856
6857 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6859 output, output_size,
6860 &output_length);
6861 if (status != PSA_SUCCESS) {
6862 return status;
6863 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006864 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006865 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 if (output_length != output_size) {
6867 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6868 }
6869 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006870
6871#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6872
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006874 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6876 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6877 output_size);
6878 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6879 output, request_size);
6880 if (ret != 0) {
6881 return mbedtls_to_psa_error(ret);
6882 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006883 output_size -= request_size;
6884 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006885 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006887#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006888}
6889
Gilles Peskine8814fc42020-12-14 15:33:44 +01006890/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006891 *
6892 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6893 * `psa_generate_random(...)`. The state parameter is ignored since the
6894 * PSA API doesn't support passing an explicit state.
6895 *
6896 * In the non-external case, psa_generate_random() calls an
6897 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6898 * and semantics as mbedtls_psa_get_random(). As an optimization,
6899 * instead of doing this back-and-forth between the PSA API and the
6900 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6901 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006902 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006903#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6904int mbedtls_psa_get_random(void *p_rng,
6905 unsigned char *output,
6906 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006907{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006908 /* This function takes a pointer to the RNG state because that's what
6909 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6910 * its own state internally and doesn't let the caller access that state.
6911 * So we just ignore the state parameter, and in practice we'll pass
6912 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006913 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 psa_status_t status = psa_generate_random(output, output_size);
6915 if (status == PSA_SUCCESS) {
6916 return 0;
6917 } else {
6918 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6919 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006920}
6921#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6922
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006923#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006924#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006925
Gilles Peskine449bd832023-01-11 14:50:10 +01006926psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6927 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006928{
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 if (global_data.initialized) {
6930 return PSA_ERROR_NOT_PERMITTED;
6931 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006932
Gilles Peskine449bd832023-01-11 14:50:10 +01006933 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6934 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6935 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6936 return PSA_ERROR_INVALID_ARGUMENT;
6937 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006938
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006940}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006941#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006942
Ronald Cron3772afe2021-02-08 16:10:05 +01006943/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006944 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006945 * \param type The key type
6946 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006947 *
6948 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006949 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006950 * \retval #PSA_ERROR_INVALID_ARGUMENT
6951 * The size in bits of the key is not valid.
6952 * \retval #PSA_ERROR_NOT_SUPPORTED
6953 * The type and/or the size in bits of the key or the combination of
6954 * the two is not supported.
6955 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006956static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006958{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006960
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 if (key_type_is_raw_bytes(type)) {
6962 status = psa_validate_unstructured_key_bit_size(type, bits);
6963 if (status != PSA_SUCCESS) {
6964 return status;
6965 }
6966 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006967#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6969 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6970 return PSA_ERROR_NOT_SUPPORTED;
6971 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006972
Ronald Cron01b2aba2020-10-05 09:42:02 +02006973 /* Accept only byte-aligned keys, for the same reasons as
6974 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 if (bits % 8 != 0) {
6976 return PSA_ERROR_NOT_SUPPORTED;
6977 }
6978 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006979#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006980
Ronald Cron5c4d3862020-12-07 11:07:24 +01006981#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006983 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 return PSA_SUCCESS;
6985 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006986#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01006987
6988#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR)
6989 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02006990 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01006991 return PSA_ERROR_NOT_SUPPORTED;
6992 }
6993 } else
6994#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006995 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006996 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006997 }
6998
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007000}
7001
Ronald Cron55ed0592020-10-05 10:30:40 +02007002psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007003 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007005{
7006 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007007 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007008
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 if ((attributes->domain_parameters == NULL) &&
7010 (attributes->domain_parameters_size != 0)) {
7011 return PSA_ERROR_INVALID_ARGUMENT;
7012 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007013
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 if (key_type_is_raw_bytes(type)) {
7015 status = psa_generate_random(key_buffer, key_buffer_size);
7016 if (status != PSA_SUCCESS) {
7017 return status;
7018 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007019
David Brown12ca5032021-02-11 11:02:00 -07007020#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 if (type == PSA_KEY_TYPE_DES) {
7022 psa_des_set_key_parity(key_buffer, key_buffer_size);
7023 }
David Brown8107e312021-02-12 08:08:46 -07007024#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007026
Jaeden Amero424fa932021-05-14 08:34:32 +01007027#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
7028 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7030 return mbedtls_psa_rsa_generate_key(attributes,
7031 key_buffer,
7032 key_buffer_size,
7033 key_buffer_length);
7034 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007035#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7036 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007037
John Durkop9814fa22020-11-04 12:28:15 -08007038#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7040 return mbedtls_psa_ecp_generate_key(attributes,
7041 key_buffer,
7042 key_buffer_size,
7043 key_buffer_length);
7044 } else
John Durkop9814fa22020-11-04 12:28:15 -08007045#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007046
7047#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR)
7048 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7049 return mbedtls_psa_ffdh_generate_key(attributes,
7050 key_buffer,
7051 key_buffer_size,
7052 key_buffer_length);
7053 } else
7054#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_FFDH_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007055 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 (void) key_buffer_length;
7057 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007058 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007061}
Darryl Green0c6575a2018-11-07 16:05:30 +00007062
Gilles Peskine449bd832023-01-11 14:50:10 +01007063psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7064 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007065{
7066 psa_status_t status;
7067 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007068 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007069 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007070
Ronald Cron81709fc2020-11-14 12:10:32 +01007071 *key = MBEDTLS_SVC_KEY_ID_INIT;
7072
Gilles Peskine0f84d622019-09-12 19:03:13 +02007073 /* Reject any attempt to create a zero-length key so that we don't
7074 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 if (psa_get_key_bits(attributes) == 0) {
7076 return PSA_ERROR_INVALID_ARGUMENT;
7077 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007078
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007079 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7081 return PSA_ERROR_INVALID_ARGUMENT;
7082 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007083
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7085 &slot, &driver);
7086 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007087 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 }
Gilles Peskine11792082019-08-06 18:36:36 +02007089
Ronald Cron977c2472020-10-13 08:32:21 +02007090 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307091 * storage ( thus not in the case of generating a key in a secure element
7092 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7093 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 if (slot->key.data == NULL) {
7095 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7096 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007097 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 attributes->core.type, attributes->core.bits);
7099 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007100 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007102
7103 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 attributes->core.type,
7105 attributes->core.bits);
7106 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007107 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 attributes, &key_buffer_size);
7109 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 }
Ronald Cron977c2472020-10-13 08:32:21 +02007112 }
Ronald Cron977c2472020-10-13 08:32:21 +02007113
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7115 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 }
Ronald Cron977c2472020-10-13 08:32:21 +02007118 }
7119
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 status = psa_driver_wrapper_generate_key(attributes,
7121 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007122
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 if (status != PSA_SUCCESS) {
7124 psa_remove_key_data_from_memory(slot);
7125 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007126
Gilles Peskine11792082019-08-06 18:36:36 +02007127exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 if (status == PSA_SUCCESS) {
7129 status = psa_finish_key_creation(slot, driver, key);
7130 }
7131 if (status != PSA_SUCCESS) {
7132 psa_fail_key_creation(slot, driver);
7133 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007134
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007136}
7137
Gilles Peskinee59236f2018-01-27 23:32:46 +01007138/****************************************************************/
7139/* Module setup */
7140/****************************************************************/
7141
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007142#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007143psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 void (* entropy_init)(mbedtls_entropy_context *ctx),
7145 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007146{
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7148 return PSA_ERROR_BAD_STATE;
7149 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007150 global_data.rng.entropy_init = entropy_init;
7151 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007153}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007154#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007155
Gilles Peskine449bd832023-01-11 14:50:10 +01007156void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007157{
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 psa_wipe_all_key_slots();
7159 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7160 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007161 }
7162 /* Wipe all remaining data, including configuration.
7163 * In particular, this sets all state indicator to the value
7164 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007165 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007166
7167 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007168 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007169}
7170
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007171#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7172/** Recover a transaction that was interrupted by a power failure.
7173 *
7174 * This function is called during initialization, before psa_crypto_init()
7175 * returns. If this function returns a failure status, the initialization
7176 * fails.
7177 */
7178static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007180{
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007182 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7183 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 /* TODO - fall through to the failure case until this
7185 * is implemented.
7186 * https://github.com/ARMmbed/mbed-crypto/issues/218
7187 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007188 default:
7189 /* We found an unsupported transaction in the storage.
7190 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007192 }
7193}
7194#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7195
Gilles Peskine449bd832023-01-11 14:50:10 +01007196psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007197{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007198 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007199
Gilles Peskinec6b69072018-11-20 21:42:52 +01007200 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 if (global_data.initialized != 0) {
7202 return PSA_SUCCESS;
7203 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007204
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007205 /* Init drivers */
7206 status = psa_driver_wrapper_init();
7207 if (status != PSA_SUCCESS) {
7208 goto exit;
7209 }
7210 global_data.drivers_initialized = 1;
7211
Gilles Peskine30524eb2020-11-13 17:02:26 +01007212 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007214 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 status = mbedtls_psa_random_seed(&global_data.rng);
7216 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007219 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 status = psa_initialize_key_slots();
7222 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007223 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007225
Gilles Peskine4b734222019-07-24 15:56:31 +02007226#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 status = psa_crypto_load_transaction();
7228 if (status == PSA_SUCCESS) {
7229 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7230 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007231 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 }
7233 status = psa_crypto_stop_transaction();
7234 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007235 /* There's no transaction to complete. It's all good. */
7236 status = PSA_SUCCESS;
7237 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007238#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007239
Gilles Peskinec6b69072018-11-20 21:42:52 +01007240 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007241 global_data.initialized = 1;
7242
7243exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 if (status != PSA_SUCCESS) {
7245 mbedtls_psa_crypto_free();
7246 }
7247 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007248}
7249
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007250psa_status_t psa_crypto_driver_pake_get_password_len(
7251 const psa_crypto_driver_pake_inputs_t *inputs,
7252 size_t *password_len)
7253{
7254 if (inputs->password_len == 0) {
7255 return PSA_ERROR_BAD_STATE;
7256 }
7257
7258 *password_len = inputs->password_len;
7259
7260 return PSA_SUCCESS;
7261}
7262
7263psa_status_t psa_crypto_driver_pake_get_password(
7264 const psa_crypto_driver_pake_inputs_t *inputs,
7265 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7266{
7267 if (inputs->password_len == 0) {
7268 return PSA_ERROR_BAD_STATE;
7269 }
7270
7271 if (buffer_size < inputs->password_len) {
7272 return PSA_ERROR_BUFFER_TOO_SMALL;
7273 }
7274
7275 memcpy(buffer, inputs->password, inputs->password_len);
7276 *buffer_length = inputs->password_len;
7277
7278 return PSA_SUCCESS;
7279}
7280
7281psa_status_t psa_crypto_driver_pake_get_role(
7282 const psa_crypto_driver_pake_inputs_t *inputs,
7283 psa_pake_role_t *role)
7284{
7285 if (inputs->role == PSA_PAKE_ROLE_NONE) {
7286 return PSA_ERROR_BAD_STATE;
7287 }
7288
7289 *role = inputs->role;
7290
7291 return PSA_SUCCESS;
7292}
7293
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007294psa_status_t psa_crypto_driver_pake_get_user_len(
7295 const psa_crypto_driver_pake_inputs_t *inputs,
7296 size_t *user_len)
7297{
7298 if (inputs->user_len == 0) {
7299 return PSA_ERROR_BAD_STATE;
7300 }
7301
7302 *user_len = inputs->user_len;
7303
7304 return PSA_SUCCESS;
7305}
7306
7307psa_status_t psa_crypto_driver_pake_get_user(
7308 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007309 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007310{
7311 if (inputs->user_len == 0) {
7312 return PSA_ERROR_BAD_STATE;
7313 }
7314
Przemek Stekielc0e62502023-03-14 11:49:36 +01007315 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007316 return PSA_ERROR_BUFFER_TOO_SMALL;
7317 }
7318
Przemek Stekielc0e62502023-03-14 11:49:36 +01007319 memcpy(user_id, inputs->user, inputs->user_len);
7320 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007321
7322 return PSA_SUCCESS;
7323}
7324
7325psa_status_t psa_crypto_driver_pake_get_peer_len(
7326 const psa_crypto_driver_pake_inputs_t *inputs,
7327 size_t *peer_len)
7328{
7329 if (inputs->peer_len == 0) {
7330 return PSA_ERROR_BAD_STATE;
7331 }
7332
7333 *peer_len = inputs->peer_len;
7334
7335 return PSA_SUCCESS;
7336}
7337
7338psa_status_t psa_crypto_driver_pake_get_peer(
7339 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007340 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007341{
7342 if (inputs->peer_len == 0) {
7343 return PSA_ERROR_BAD_STATE;
7344 }
7345
Przemek Stekielc0e62502023-03-14 11:49:36 +01007346 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007347 return PSA_ERROR_BUFFER_TOO_SMALL;
7348 }
7349
Przemek Stekielc0e62502023-03-14 11:49:36 +01007350 memcpy(peer_id, inputs->peer, inputs->peer_len);
7351 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007352
7353 return PSA_SUCCESS;
7354}
7355
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007356psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7357 const psa_crypto_driver_pake_inputs_t *inputs,
7358 psa_pake_cipher_suite_t *cipher_suite)
7359{
7360 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7361 return PSA_ERROR_BAD_STATE;
7362 }
7363
7364 *cipher_suite = inputs->cipher_suite;
7365
7366 return PSA_SUCCESS;
7367}
7368
Neil Armstronga7d08c32022-06-01 18:21:20 +02007369psa_status_t psa_pake_setup(
7370 psa_pake_operation_t *operation,
7371 const psa_pake_cipher_suite_t *cipher_suite)
7372{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007374
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007375 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007376 status = PSA_ERROR_BAD_STATE;
7377 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007378 }
7379
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007380 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007381 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007382 status = PSA_ERROR_INVALID_ARGUMENT;
7383 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007384 }
7385
Przemek Stekiel51eac532022-12-07 11:04:51 +01007386 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7387
Przemek Stekiele12ed362022-12-21 12:54:46 +01007388 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007389 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7390 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007391 operation->data.inputs.cipher_suite = *cipher_suite;
7392
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007393#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007394 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007395 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007396 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007397
Przemek Stekiele12ed362022-12-21 12:54:46 +01007398 computation_stage->state = PSA_PAKE_STATE_SETUP;
7399 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7400 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7401 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007402 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007403#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007404 {
7405 status = PSA_ERROR_NOT_SUPPORTED;
7406 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007407 }
7408
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007409 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7410
Przemek Stekiel51eac532022-12-07 11:04:51 +01007411 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007412exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007413 psa_pake_abort(operation);
7414 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007415}
7416
7417psa_status_t psa_pake_set_password_key(
7418 psa_pake_operation_t *operation,
7419 mbedtls_svc_key_id_t password)
7420{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007422 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7423 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007424
Przemek Stekiel51eac532022-12-07 11:04:51 +01007425 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007426 status = PSA_ERROR_BAD_STATE;
7427 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007428 }
7429
Przemek Stekiel6c764412022-11-22 14:05:12 +01007430 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7431 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007432 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007433 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007434 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007435 }
7436
Przemek Stekiel6c764412022-11-22 14:05:12 +01007437 psa_key_attributes_t attributes = {
7438 .core = slot->attr
7439 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007440
Przemek Stekiel51eac532022-12-07 11:04:51 +01007441 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007442
Przemek Stekiel51eac532022-12-07 11:04:51 +01007443 if (type != PSA_KEY_TYPE_PASSWORD &&
7444 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7445 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007446 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007447 }
7448
Przemek Stekiel51eac532022-12-07 11:04:51 +01007449 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7450 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007451 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007452 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007453 }
7454
7455 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7456 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007457 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007458exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007459 if (status != PSA_SUCCESS) {
7460 psa_pake_abort(operation);
7461 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007462 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007463 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007464}
7465
7466psa_status_t psa_pake_set_user(
7467 psa_pake_operation_t *operation,
7468 const uint8_t *user_id,
7469 size_t user_id_len)
7470{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007471 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007472
7473 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007474 status = PSA_ERROR_BAD_STATE;
7475 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007476 }
7477
Przemek Stekiel51eac532022-12-07 11:04:51 +01007478 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007479 status = PSA_ERROR_INVALID_ARGUMENT;
7480 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007481 }
7482
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007483 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007484 status = PSA_ERROR_BAD_STATE;
7485 goto exit;
7486 }
7487
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007488 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007489 if ((user_id_len != sizeof(jpake_server_id) ||
7490 memcmp(user_id, jpake_server_id, user_id_len) != 0) &&
7491 (user_id_len != sizeof(jpake_client_id) ||
7492 memcmp(user_id, jpake_client_id, user_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007493 status = PSA_ERROR_NOT_SUPPORTED;
7494 goto exit;
7495 }
7496
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007497 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7498 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007499 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7500 goto exit;
7501 }
7502
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007503 memcpy(operation->data.inputs.user, user_id, user_id_len);
7504 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007505
7506 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007507exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007508 psa_pake_abort(operation);
7509 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007510}
7511
7512psa_status_t psa_pake_set_peer(
7513 psa_pake_operation_t *operation,
7514 const uint8_t *peer_id,
7515 size_t peer_id_len)
7516{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007517 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007518
7519 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007520 status = PSA_ERROR_BAD_STATE;
7521 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007522 }
7523
Przemek Stekiel51eac532022-12-07 11:04:51 +01007524 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007525 status = PSA_ERROR_INVALID_ARGUMENT;
7526 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007527 }
7528
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007529 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007530 status = PSA_ERROR_BAD_STATE;
7531 goto exit;
7532 }
7533
Przemek Stekield7f6ad72023-03-06 13:39:52 +01007534 /* Allow only "client" or "server" values (temporary restriction). */
Przemek Stekielfde11282023-03-13 16:06:09 +01007535 if ((peer_id_len != sizeof(jpake_server_id) ||
7536 memcmp(peer_id, jpake_server_id, peer_id_len) != 0) &&
7537 (peer_id_len != sizeof(jpake_client_id) ||
7538 memcmp(peer_id, jpake_client_id, peer_id_len) != 0)) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007539 status = PSA_ERROR_NOT_SUPPORTED;
7540 goto exit;
7541 }
7542
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007543 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7544 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007545 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7546 goto exit;
7547 }
7548
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007549 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7550 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007551
7552 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007553exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007554 psa_pake_abort(operation);
7555 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007556}
7557
7558psa_status_t psa_pake_set_role(
7559 psa_pake_operation_t *operation,
7560 psa_pake_role_t role)
7561{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007563
Przemek Stekiel51eac532022-12-07 11:04:51 +01007564 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007565 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007566 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007567 }
7568
Przemek Stekiel09104b82023-03-06 14:11:51 +01007569 switch (operation->alg) {
7570#if defined(PSA_WANT_ALG_JPAKE)
7571 case PSA_ALG_JPAKE:
7572 if (role == PSA_PAKE_ROLE_NONE) {
7573 return PSA_SUCCESS;
7574 }
7575 status = PSA_ERROR_INVALID_ARGUMENT;
7576 break;
7577#endif
7578 default:
7579 (void) role;
7580 status = PSA_ERROR_NOT_SUPPORTED;
7581 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007582 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007583exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007584 psa_pake_abort(operation);
7585 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007586}
7587
Przemek Stekielb09c4872023-01-17 12:05:38 +01007588/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007589#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007590static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007591 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007592{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007593 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007594 case PSA_PAKE_OUTPUT_X1_X2:
7595 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007596 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007597 case PSA_PAKE_X1_STEP_KEY_SHARE:
7598 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007599 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7600 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007601 case PSA_PAKE_X1_STEP_ZK_PROOF:
7602 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007603 case PSA_PAKE_X2_STEP_KEY_SHARE:
7604 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007605 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7606 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007607 case PSA_PAKE_X2_STEP_ZK_PROOF:
7608 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007609 default:
7610 return PSA_JPAKE_STEP_INVALID;
7611 }
7612 break;
7613 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007614 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007615 case PSA_PAKE_X1_STEP_KEY_SHARE:
7616 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007617 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7618 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007619 case PSA_PAKE_X1_STEP_ZK_PROOF:
7620 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007621 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007622 return PSA_JPAKE_STEP_INVALID;
7623 }
7624 break;
7625 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007626 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007627 case PSA_PAKE_X1_STEP_KEY_SHARE:
7628 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007629 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7630 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007631 case PSA_PAKE_X1_STEP_ZK_PROOF:
7632 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007633 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007634 return PSA_JPAKE_STEP_INVALID;
7635 }
7636 break;
7637 default:
7638 return PSA_JPAKE_STEP_INVALID;
7639 }
7640 return PSA_JPAKE_STEP_INVALID;
7641}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007642#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007643
Przemek Stekiel2797d372022-12-22 11:19:22 +01007644static psa_status_t psa_pake_complete_inputs(
7645 psa_pake_operation_t *operation)
7646{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007647 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007648 /* Create copy of the inputs on stack as inputs share memory
7649 with the driver context which will be setup by the driver. */
7650 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007651
Przemek Stekielfde11282023-03-13 16:06:09 +01007652 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007653 return PSA_ERROR_BAD_STATE;
7654 }
7655
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007656 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007657 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7658 return PSA_ERROR_BAD_STATE;
7659 }
7660 if (memcmp(inputs.user, jpake_client_id, inputs.user_len) == 0 &&
7661 memcmp(inputs.peer, jpake_server_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007662 inputs.role = PSA_PAKE_ROLE_CLIENT;
7663 } else
Przemek Stekielfde11282023-03-13 16:06:09 +01007664 if (memcmp(inputs.user, jpake_server_id, inputs.user_len) == 0 &&
7665 memcmp(inputs.peer, jpake_client_id, inputs.peer_len) == 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007666 inputs.role = PSA_PAKE_ROLE_SERVER;
7667 }
7668
7669 if (inputs.role != PSA_PAKE_ROLE_CLIENT &&
7670 inputs.role != PSA_PAKE_ROLE_SERVER) {
7671 return PSA_ERROR_NOT_SUPPORTED;
7672 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007673 }
7674
Przemek Stekiel18620a32023-01-17 16:34:52 +01007675 /* Clear driver context */
7676 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7677
7678 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007679
7680 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007681 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7682 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007683
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007684 /* User and peer are translated to role. */
7685 mbedtls_free(inputs.user);
7686 mbedtls_free(inputs.peer);
7687
Przemek Stekiel2797d372022-12-22 11:19:22 +01007688 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007689#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007690 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007691 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007692 psa_jpake_computation_stage_t *computation_stage =
7693 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007694 computation_stage->state = PSA_PAKE_STATE_READY;
7695 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7696 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7697 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007698 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007699#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007700 {
7701 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007702 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007703 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007704 return status;
7705}
7706
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007707#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007708static psa_status_t psa_jpake_output_prologue(
7709 psa_pake_operation_t *operation,
7710 psa_pake_step_t step)
7711{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007712 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7713 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7714 step != PSA_PAKE_STEP_ZK_PROOF) {
7715 return PSA_ERROR_INVALID_ARGUMENT;
7716 }
7717
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007718 psa_jpake_computation_stage_t *computation_stage =
7719 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007720
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007721 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7722 return PSA_ERROR_BAD_STATE;
7723 }
7724
7725 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7726 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7727 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7728 return PSA_ERROR_BAD_STATE;
7729 }
7730
7731 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7732 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007733 return PSA_ERROR_BAD_STATE;
7734 }
7735
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007736 switch (computation_stage->output_step) {
7737 case PSA_PAKE_STEP_X1_X2:
7738 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007739 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007740 case PSA_PAKE_STEP_X2S:
7741 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007742 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007743 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007744 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007745 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007746
7747 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7748 }
7749
7750 /* Check if step matches current sequence */
7751 switch (computation_stage->sequence) {
7752 case PSA_PAKE_X1_STEP_KEY_SHARE:
7753 case PSA_PAKE_X2_STEP_KEY_SHARE:
7754 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7755 return PSA_ERROR_BAD_STATE;
7756 }
7757 break;
7758
7759 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7760 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7761 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7762 return PSA_ERROR_BAD_STATE;
7763 }
7764 break;
7765
7766 case PSA_PAKE_X1_STEP_ZK_PROOF:
7767 case PSA_PAKE_X2_STEP_ZK_PROOF:
7768 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7769 return PSA_ERROR_BAD_STATE;
7770 }
7771 break;
7772
7773 default:
7774 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007775 }
7776
7777 return PSA_SUCCESS;
7778}
7779
7780static psa_status_t psa_jpake_output_epilogue(
7781 psa_pake_operation_t *operation)
7782{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007783 psa_jpake_computation_stage_t *computation_stage =
7784 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007785
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007786 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007787 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007788 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007789 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007790 computation_stage->state = PSA_PAKE_STATE_READY;
7791 computation_stage->output_step++;
7792 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7793 } else {
7794 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007795 }
7796
7797 return PSA_SUCCESS;
7798}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007799#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007800
Neil Armstronga7d08c32022-06-01 18:21:20 +02007801psa_status_t psa_pake_output(
7802 psa_pake_operation_t *operation,
7803 psa_pake_step_t step,
7804 uint8_t *output,
7805 size_t output_size,
7806 size_t *output_length)
7807{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007808 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007809 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007810 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007811
7812 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007813 status = psa_pake_complete_inputs(operation);
7814 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007815 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007816 }
7817 }
7818
7819 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007820 status = PSA_ERROR_BAD_STATE;
7821 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007822 }
7823
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007824 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007825 status = PSA_ERROR_INVALID_ARGUMENT;
7826 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007827 }
7828
Przemek Stekiele12ed362022-12-21 12:54:46 +01007829 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007830#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007831 case PSA_ALG_JPAKE:
7832 status = psa_jpake_output_prologue(operation, step);
7833 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007834 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007835 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007836 driver_step = convert_jpake_computation_stage_to_driver_step(
7837 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007838 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007839#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007840 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007841 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007842 status = PSA_ERROR_NOT_SUPPORTED;
7843 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007844 }
7845
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007846 status = psa_driver_wrapper_pake_output(operation, driver_step,
7847 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007848
7849 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007850 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007851 }
7852
7853 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007854#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007855 case PSA_ALG_JPAKE:
7856 status = psa_jpake_output_epilogue(operation);
7857 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007858 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007859 }
7860 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007861#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007862 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007863 status = PSA_ERROR_NOT_SUPPORTED;
7864 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007865 }
7866
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007867 return PSA_SUCCESS;
7868exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007869 psa_pake_abort(operation);
7870 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007871}
7872
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007873#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007874static psa_status_t psa_jpake_input_prologue(
7875 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007876 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007877{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007878 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7879 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7880 step != PSA_PAKE_STEP_ZK_PROOF) {
7881 return PSA_ERROR_INVALID_ARGUMENT;
7882 }
7883
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007884 psa_jpake_computation_stage_t *computation_stage =
7885 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007886
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007887 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7888 return PSA_ERROR_BAD_STATE;
7889 }
7890
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007891 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7892 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7893 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7894 return PSA_ERROR_BAD_STATE;
7895 }
7896
7897 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7898 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007899 return PSA_ERROR_BAD_STATE;
7900 }
7901
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007902 switch (computation_stage->input_step) {
7903 case PSA_PAKE_STEP_X1_X2:
7904 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007905 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007906 case PSA_PAKE_STEP_X2S:
7907 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007908 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007909 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007910 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007911 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007912
7913 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7914 }
7915
7916 /* Check if step matches current sequence */
7917 switch (computation_stage->sequence) {
7918 case PSA_PAKE_X1_STEP_KEY_SHARE:
7919 case PSA_PAKE_X2_STEP_KEY_SHARE:
7920 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7921 return PSA_ERROR_BAD_STATE;
7922 }
7923 break;
7924
7925 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7926 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7927 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7928 return PSA_ERROR_BAD_STATE;
7929 }
7930 break;
7931
7932 case PSA_PAKE_X1_STEP_ZK_PROOF:
7933 case PSA_PAKE_X2_STEP_ZK_PROOF:
7934 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7935 return PSA_ERROR_BAD_STATE;
7936 }
7937 break;
7938
7939 default:
7940 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007941 }
7942
7943 return PSA_SUCCESS;
7944}
7945
Przemek Stekiele12ed362022-12-21 12:54:46 +01007946static psa_status_t psa_jpake_input_epilogue(
7947 psa_pake_operation_t *operation)
7948{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007949 psa_jpake_computation_stage_t *computation_stage =
7950 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007951
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007952 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007953 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007954 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007955 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007956 computation_stage->state = PSA_PAKE_STATE_READY;
7957 computation_stage->input_step++;
7958 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7959 } else {
7960 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007961 }
7962
7963 return PSA_SUCCESS;
7964}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007965#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007966
Neil Armstronga7d08c32022-06-01 18:21:20 +02007967psa_status_t psa_pake_input(
7968 psa_pake_operation_t *operation,
7969 psa_pake_step_t step,
7970 const uint8_t *input,
7971 size_t input_length)
7972{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007974 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007975 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
7976 operation->primitive,
7977 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007978
7979 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007980 status = psa_pake_complete_inputs(operation);
7981 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007982 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007983 }
7984 }
7985
7986 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007987 status = PSA_ERROR_BAD_STATE;
7988 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007989 }
7990
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007991 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007992 status = PSA_ERROR_INVALID_ARGUMENT;
7993 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007994 }
7995
Przemek Stekiele12ed362022-12-21 12:54:46 +01007996 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007997#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007998 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007999 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008000 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008001 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008002 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008003 driver_step = convert_jpake_computation_stage_to_driver_step(
8004 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008005 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008006#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008007 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008008 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008009 status = PSA_ERROR_NOT_SUPPORTED;
8010 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008011 }
8012
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008013 status = psa_driver_wrapper_pake_input(operation, driver_step,
8014 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008015
8016 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008017 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008018 }
8019
8020 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008021#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008022 case PSA_ALG_JPAKE:
8023 status = psa_jpake_input_epilogue(operation);
8024 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008025 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008026 }
8027 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008028#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008029 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008030 status = PSA_ERROR_NOT_SUPPORTED;
8031 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008032 }
8033
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008034 return PSA_SUCCESS;
8035exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008036 psa_pake_abort(operation);
8037 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008038}
8039
8040psa_status_t psa_pake_get_implicit_key(
8041 psa_pake_operation_t *operation,
8042 psa_key_derivation_operation_t *output)
8043{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008045 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008046 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008047 size_t shared_key_len = 0;
8048
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008049 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008050 status = PSA_ERROR_BAD_STATE;
8051 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008052 }
8053
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008054#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008055 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008056 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008057 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008058 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
8059 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008060 status = PSA_ERROR_BAD_STATE;
8061 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008062 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008063 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008064#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008065 {
8066 status = PSA_ERROR_NOT_SUPPORTED;
8067 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008068 }
8069
Przemek Stekiel0c781802022-11-29 14:53:13 +01008070 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8071 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008072 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008073 &shared_key_len);
8074
8075 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008076 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008077 }
8078
8079 status = psa_key_derivation_input_bytes(output,
8080 PSA_KEY_DERIVATION_INPUT_SECRET,
8081 shared_key,
8082 shared_key_len);
8083
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008084 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008085exit:
8086 abort_status = psa_pake_abort(operation);
8087 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008088}
8089
8090psa_status_t psa_pake_abort(
8091 psa_pake_operation_t *operation)
8092{
Przemek Stekield69dca92023-01-31 19:59:20 +01008093 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008094
Przemek Stekield69dca92023-01-31 19:59:20 +01008095 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008096 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008097 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008098
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008099 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8100 if (operation->data.inputs.password != NULL) {
8101 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008102 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008103 mbedtls_free(operation->data.inputs.password);
8104 }
8105 if (operation->data.inputs.user != NULL) {
8106 mbedtls_free(operation->data.inputs.user);
8107 }
8108 if (operation->data.inputs.peer != NULL) {
8109 mbedtls_free(operation->data.inputs.peer);
8110 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008111 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008112 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008113
Przemek Stekield69dca92023-01-31 19:59:20 +01008114 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008115}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008116
Gilles Peskinee59236f2018-01-27 23:32:46 +01008117#endif /* MBEDTLS_PSA_CRYPTO_C */