blob: 6c7f2b0ece32d6c9d8e8b0be44d177136b8747c0 [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"
Steven Cooreman5f88e772021-03-15 11:07:12 +010038#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010039#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010040#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010041#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020042#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020043#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020044#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010045#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010046/* Include internal declarations that are useful for implementing persistently
47 * stored keys. */
48#include "psa_crypto_storage.h"
49
Gilles Peskineb2b64d32020-12-14 16:43:58 +010050#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010051
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010056#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020057#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000058#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020059#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000074#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000076#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000084#include "hash_info.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020087
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020088#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020091#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020092#endif
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/****************************************************************/
95/* Global data, support functions and library management */
96/****************************************************************/
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020099{
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200101}
102
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100103/* Values for psa_global_data_t::rng_state */
104#define RNG_NOT_INITIALIZED 0
105#define RNG_INITIALIZED 1
106#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100109 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100111 unsigned drivers_initialized : 1;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100112 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100113} psa_global_data_t;
114
115static psa_global_data_t global_data;
116
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100117#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
118mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
119 &global_data.rng.drbg;
120#endif
121
itayzafrir0adf0fc2018-09-06 16:24:41 +0300122#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (global_data.initialized == 0) \
124 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300125
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100126int psa_can_do_hash(psa_algorithm_t hash_alg)
127{
128 (void) hash_alg;
129 return global_data.drivers_initialized;
130}
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100134 /* Mbed TLS error codes can combine a high-level error code and a
135 * low-level error code. The low-level error usually reflects the
136 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 int low_level_ret = -(-ret & 0x007f);
138 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100139 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100141
142 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
143 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200145 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
146 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
147 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
148 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
149 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200151 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200153 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200155
Jaeden Amero93e21112019-02-20 13:57:28 +0000156#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000157 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100159 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100161
162 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100166
Gilles Peskine26869f22019-05-06 15:25:00 +0200167 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200169
170 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200172 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200174
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100179 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100187 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
191 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100192 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
193 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
197 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100201#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100202
203 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100205
Gilles Peskinee59236f2018-01-27 23:32:46 +0100206 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
207 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
208 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210
211 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200213 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100217
Gilles Peskine82e57d12020-11-13 21:31:17 +0100218#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100220 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
221 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100222 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100224 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
225 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100227 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100229#endif
230
Gilles Peskinea5905292018-02-07 20:59:33 +0100231 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100233 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100239
Gilles Peskinef76aa772018-10-29 19:24:33 +0100240 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100242 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100244 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100246 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100248 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100250 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100252 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100257 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100259 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
260 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100264 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
265 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100269 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
270 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_INVALID_ALG:
275 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
276 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200280 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100282
Gilles Peskineff2d2002019-05-06 15:26:23 +0200283 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200285 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200287
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100290 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
297 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100301 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200305
itayzafrir5c753392018-05-08 11:18:38 +0300306 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300307 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300309 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300311 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300313 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
314 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300316 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100318 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100320
Paul Elliott588f8ed2022-12-02 18:10:26 +0000321 case MBEDTLS_ERR_ECP_IN_PROGRESS:
322 return PSA_OPERATION_INCOMPLETE;
323
Janos Follatha13b9052019-11-22 12:48:59 +0000324 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300326
Gilles Peskinee59236f2018-01-27 23:32:46 +0100327 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100329 }
330}
331
Paul Elliottdc42ca82023-02-24 18:11:59 +0000332/**
333 * \brief For output buffers which contain "tags"
334 * (outputs that may be checked for validity like
335 * hashes, MACs and signatures), fill the unused
336 * part of the output buffer (the whole buffer on
337 * error, the trailing part on success) with
338 * something that isn't a valid tag (barring an
339 * attack on the tag and deliberately-crafted
340 * input), in case the caller doesn't check the
341 * return status properly.
342 *
343 * \param output_buffer Pointer to buffer to wipe. May not be NULL
344 * unless \p output_buffer_size is zero.
345 * \param status Status of function called to generate
346 * output_buffer originally
347 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
348 * could be NULL.
349 * \param output_buffer_length Length of data written to output_buffer, must be
350 * less than \p output_buffer_size
351 */
352static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000353 size_t output_buffer_size, size_t output_buffer_length)
354{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000355 size_t offset = 0;
356
357 if (output_buffer_size == 0) {
358 /* If output_buffer_size is 0 then we have nothing to do. We must not
359 call memset because output_buffer may be NULL in this case */
360 return;
361 }
362
363 if (status == PSA_SUCCESS) {
364 offset = output_buffer_length;
365 }
366
367 memset(output_buffer + offset, '!', output_buffer_size - offset);
368}
369
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200370
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200371
372
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100373/****************************************************************/
374/* Key management */
375/****************************************************************/
376
Valerio Settid4a5d462023-04-05 18:19:01 +0200377#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100378mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
379 size_t bits,
380 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200381{
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100383 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100385#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100386 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100388#endif
389#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100392#endif
393#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100396#endif
397#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100398 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100400#endif
401#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100402 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100404 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if (bits_is_sloppy) {
406 return MBEDTLS_ECP_DP_SECP521R1;
407 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100408 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100409#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100410 }
411 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100412
Paul Elliott8ff510a2020-06-02 17:19:28 +0100413 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100415#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100416 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100418#endif
419#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100422#endif
423#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100424 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100426#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100427 }
428 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100429
Paul Elliott8ff510a2020-06-02 17:19:28 +0100430 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100432#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (bits_is_sloppy) {
437 return MBEDTLS_ECP_DP_CURVE25519;
438 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100439 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100440#endif
441#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100442 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100444#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100445 }
446 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100447
Paul Elliott8ff510a2020-06-02 17:19:28 +0100448 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#endif
454#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#endif
458#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100461#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100462 }
463 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200464 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100466 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200468}
Valerio Settid4a5d462023-04-05 18:19:01 +0200469#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
472 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200473{
474 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200476 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200478 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200479 case PSA_KEY_TYPE_PASSWORD:
480 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200481 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100482#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200483 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (bits != 128 && bits != 192 && bits != 256) {
485 return PSA_ERROR_INVALID_ARGUMENT;
486 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 break;
488#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200489#if defined(PSA_WANT_KEY_TYPE_ARIA)
490 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (bits != 128 && bits != 192 && bits != 256) {
492 return PSA_ERROR_INVALID_ARGUMENT;
493 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200494 break;
495#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100496#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497 case PSA_KEY_TYPE_CAMELLIA:
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
Ronald Cron1f0db802021-03-12 09:59:30 +0100503#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (bits != 64 && bits != 128 && bits != 192) {
506 return PSA_ERROR_INVALID_ARGUMENT;
507 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508 break;
509#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100510#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200511 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (bits != 256) {
513 return PSA_ERROR_INVALID_ARGUMENT;
514 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200515 break;
516#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200519 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (bits % 8 != 0) {
521 return PSA_ERROR_INVALID_ARGUMENT;
522 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200525}
526
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100527/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100528 *
Steven Cooremancd640932021-03-08 12:00:27 +0100529 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
530 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100531 *
532 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100533 * \param[in] key_type The key type of the key to be used with the
534 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100535 *
536 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100537 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100538 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100539 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100540 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100541MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100542 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100544{
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (PSA_ALG_IS_HMAC(algorithm)) {
546 if (key_type == PSA_KEY_TYPE_HMAC) {
547 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
552 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
553 * key. */
554 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
555 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
556 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
557 * the block length (larger than 1) for block ciphers. */
558 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
559 return PSA_SUCCESS;
560 }
561 }
562 }
563
564 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100565}
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
568 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200569{
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (slot->key.data != NULL) {
571 return PSA_ERROR_ALREADY_EXISTS;
572 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 slot->key.data = mbedtls_calloc(1, buffer_length);
575 if (slot->key.data == NULL) {
576 return PSA_ERROR_INSUFFICIENT_MEMORY;
577 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200578
Ronald Cronea0f8a62020-11-25 17:52:23 +0100579 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200581}
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
584 const uint8_t *data,
585 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200586{
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 psa_status_t status = psa_allocate_buffer_to_slot(slot,
588 data_length);
589 if (status != PSA_SUCCESS) {
590 return status;
591 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 memcpy(slot->key.data, data, data_length);
594 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200595}
596
Ronald Crona0fe59f2020-11-29 11:14:53 +0100597psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100598 const psa_key_attributes_t *attributes,
599 const uint8_t *data, size_t data_length,
600 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100602{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100603 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
604 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200606 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (data_length == 0) {
608 return PSA_ERROR_NOT_SUPPORTED;
609 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (key_type_is_raw_bytes(type)) {
612 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
615 *bits);
616 if (status != PSA_SUCCESS) {
617 return status;
618 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200619
Ronald Crondd04d422020-11-28 15:14:42 +0100620 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100622 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 return PSA_SUCCESS;
626 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800627#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
629 if (PSA_KEY_TYPE_IS_ECC(type)) {
630 return mbedtls_psa_ecp_import_key(attributes,
631 data, data_length,
632 key_buffer, key_buffer_size,
633 key_buffer_length,
634 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100635 }
John Durkop9814fa22020-11-04 12:28:15 -0800636#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
637 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700638#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
640 if (PSA_KEY_TYPE_IS_RSA(type)) {
641 return mbedtls_psa_rsa_import_key(attributes,
642 data, data_length,
643 key_buffer, key_buffer_size,
644 key_buffer_length,
645 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200646 }
John Durkop9814fa22020-11-04 12:28:15 -0800647#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
648 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100649 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100652}
653
Gilles Peskinef603c712019-01-19 13:40:11 +0100654/** Calculate the intersection of two algorithm usage policies.
655 *
656 * Return 0 (which allows no operation) on incompatibility.
657 */
658static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100659 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100660 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100662{
Gilles Peskine549ea862019-05-22 11:45:59 +0200663 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (alg1 == alg2) {
665 return alg1;
666 }
Steven Cooreman03488022021-02-18 12:07:20 +0100667 /* If the policies are from the same hash-and-sign family, check
668 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
670 PSA_ALG_IS_SIGN_HASH(alg2) &&
671 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
672 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
673 return alg2;
674 }
675 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
676 return alg1;
677 }
Steven Cooreman03488022021-02-18 12:07:20 +0100678 }
679 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100680 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100681 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
683 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
684 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
685 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
686 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100687 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100688
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100689 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
691 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
692 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
693 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100694 }
695 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
697 (alg1_len <= alg2_len)) {
698 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100699 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
701 (alg2_len <= alg1_len)) {
702 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100703 }
704 }
705 /* If the policies are from the same MAC family, check whether one
706 * of them is a minimum-MAC-length policy. Calculate the most
707 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
709 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
710 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100711 /* Validate the combination of key type and algorithm. Since the base
712 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
714 return 0;
715 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100716
Steven Cooreman31a876d2021-03-03 20:47:40 +0100717 /* Get the (exact or at-least) output lengths for both sides of the
718 * requested intersection. None of the currently supported algorithms
719 * have an output length dependent on the actual key size, so setting it
720 * to a bogus value of 0 is currently OK.
721 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100722 * Note that for at-least-this-length wildcard algorithms, the output
723 * length is set to the shortest allowed length, which allows us to
724 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
726 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100727 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100728
Steven Cooremana1d83222021-02-25 10:20:29 +0100729 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
731 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
732 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100733 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100734
735 /* If only one is an at-least-this-length policy, the intersection would
736 * be the other (fixed-length) policy as long as said fixed length is
737 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
739 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100740 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
742 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100743 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100744
Steven Cooremancd640932021-03-08 12:00:27 +0100745 /* If none of them are wildcards, check whether they define the same tag
746 * length. This is still possible here when one is default-length and
747 * the other specific-length. Ensure to always return the
748 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (alg1_len == alg2_len) {
750 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
751 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100752 }
753 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100755}
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757static int psa_key_algorithm_permits(psa_key_type_t key_type,
758 psa_algorithm_t policy_alg,
759 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200760{
Gilles Peskine549ea862019-05-22 11:45:59 +0200761 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (requested_alg == policy_alg) {
763 return 1;
764 }
Steven Cooreman03488022021-02-18 12:07:20 +0100765 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
766 * and requested_alg is the same hash-and-sign family with any hash,
767 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
769 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
770 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
771 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100772 }
773 /* If policy_alg is a wildcard AEAD algorithm of the same base as
774 * the requested algorithm, check the requested tag length to be
775 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (PSA_ALG_IS_AEAD(policy_alg) &&
777 PSA_ALG_IS_AEAD(requested_alg) &&
778 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
779 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
780 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
781 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
782 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100783 }
Steven Cooremancd640932021-03-08 12:00:27 +0100784 /* If policy_alg is a MAC algorithm of the same base as the requested
785 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (PSA_ALG_IS_MAC(policy_alg) &&
787 PSA_ALG_IS_MAC(requested_alg) &&
788 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
789 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100790 /* Validate the combination of key type and algorithm. Since the policy
791 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
793 return 0;
794 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100795
Steven Cooreman31a876d2021-03-03 20:47:40 +0100796 /* Get both the requested output length for the algorithm which is to be
797 * verified, and the default output length for the base algorithm.
798 * Note that none of the currently supported algorithms have an output
799 * length dependent on actual key size, so setting it to a bogus value
800 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100801 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100803 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 key_type, 0,
805 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100806
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100807 /* If the policy is default-length, only allow an algorithm with
808 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
810 return requested_output_length == default_output_length;
811 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100812
813 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100814 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
816 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
817 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100818 }
819
Steven Cooremancd640932021-03-08 12:00:27 +0100820 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
821 * check for the requested MAC length to be equal to or longer than the
822 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
824 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
825 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100826 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200827 }
Steven Cooremance48e852020-10-05 16:02:45 +0200828 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200829 * a key derivation with that key agreement should also be allowed. This
830 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
832 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
833 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
834 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200835 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100836 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200838}
839
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100840/** Test whether a policy permits an algorithm.
841 *
842 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100843 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100844 * \note This function requires providing the key type for which the policy is
845 * being validated, since some algorithm policy definitions (e.g. MAC)
846 * have different properties depending on what kind of cipher it is
847 * combined with.
848 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100849 * \retval PSA_SUCCESS When \p alg is a specific algorithm
850 * allowed by the \p policy.
851 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
852 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
853 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100854 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
856 psa_key_type_t key_type,
857 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100858{
Steven Cooremand788fab2021-02-25 11:29:17 +0100859 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (alg == 0) {
861 return PSA_ERROR_INVALID_ARGUMENT;
862 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100863
Steven Cooreman7e39f052021-02-23 14:18:32 +0100864 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (PSA_ALG_IS_WILDCARD(alg)) {
866 return PSA_ERROR_INVALID_ARGUMENT;
867 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
870 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
871 return PSA_SUCCESS;
872 } else {
873 return PSA_ERROR_NOT_PERMITTED;
874 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100875}
876
Gilles Peskinef603c712019-01-19 13:40:11 +0100877/** Restrict a key policy based on a constraint.
878 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100879 * \note This function requires providing the key type for which the policy is
880 * being restricted, since some algorithm policy definitions (e.g. MAC)
881 * have different properties depending on what kind of cipher it is
882 * combined with.
883 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100884 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100885 * \param[in,out] policy The policy to restrict.
886 * \param[in] constraint The policy constraint to apply.
887 *
888 * \retval #PSA_SUCCESS
889 * \c *policy contains the intersection of the original value of
890 * \c *policy and \c *constraint.
891 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100892 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100893 * \c *policy is unchanged.
894 */
895static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100896 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100897 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100899{
900 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 psa_key_policy_algorithm_intersection(key_type, policy->alg,
902 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200903 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
905 constraint->alg2);
906 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
907 return PSA_ERROR_INVALID_ARGUMENT;
908 }
909 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
910 return PSA_ERROR_INVALID_ARGUMENT;
911 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100912 policy->usage &= constraint->usage;
913 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200914 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100916}
917
Przemek Stekiel061f6942022-11-30 10:51:35 +0100918/** Get the description of a key given its identifier and policy constraints
919 * and lock it.
920 *
921 * The key must have allow all the usage flags set in \p usage. If \p alg is
922 * nonzero, the key must allow operations with this algorithm. If \p alg is
923 * zero, the algorithm is not checked.
924 *
925 * In case of a persistent key, the function loads the description of the key
926 * into a key slot if not already done.
927 *
928 * On success, the returned key slot is locked. It is the responsibility of
929 * the caller to unlock the key slot when it does not access it anymore.
930 */
931static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100932 mbedtls_svc_key_id_t key,
933 psa_key_slot_t **p_slot,
934 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100936{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200937 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100938 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 status = psa_get_and_lock_key_slot(key, p_slot);
941 if (status != PSA_SUCCESS) {
942 return status;
943 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200944 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100945
946 /* Enforce that usage policy for the key slot contains all the flags
947 * required by the usage parameter. There is one exception: public
948 * keys can always be exported, so we treat public key objects as
949 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100951 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100955 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200956 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100957 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100958
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800959 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (alg != 0) {
961 status = psa_key_policy_permits(&slot->attr.policy,
962 slot->attr.type,
963 alg);
964 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100965 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100967 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200970
971error:
972 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100976}
Darryl Green940d72c2018-07-13 13:18:51 +0100977
Ronald Cron5c522922020-11-14 16:35:34 +0100978/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200979 *
980 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200981 * available, as opposed to a key in a secure element and/or to be used
982 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200983 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200984 * This is a temporary function that may be used instead of
985 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
986 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200987 *
Ronald Cron5c522922020-11-14 16:35:34 +0100988 * On success, the returned key slot is locked. It is the responsibility of the
989 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200990 */
Ronald Cron5c522922020-11-14 16:35:34 +0100991static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
992 mbedtls_svc_key_id_t key,
993 psa_key_slot_t **p_slot,
994 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200996{
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
998 usage, alg);
999 if (status != PSA_SUCCESS) {
1000 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001001 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1004 psa_unlock_key_slot(*p_slot);
1005 *p_slot = NULL;
1006 return PSA_ERROR_NOT_SUPPORTED;
1007 }
1008
1009 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001010}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001013{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001014 /* Data pointer will always be either a valid pointer or NULL in an
1015 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if (slot->key.data != NULL) {
1017 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1018 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001021 slot->key.data = NULL;
1022 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001025}
1026
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001027/** Completely wipe a slot in memory, including its policy.
1028 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001030{
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 /*
1034 * As the return error code may not be handled in case of multiple errors,
1035 * do our best to report an unexpected lock counter. Assert with
1036 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1037 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1038 * function is called as part of the execution of a test suite, the
1039 * execution of the test suite is stopped in error if the assertion fails.
1040 */
1041 if (slot->lock_count != 1) {
1042 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001043 status = PSA_ERROR_CORRUPTION_DETECTED;
1044 }
1045
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001046 /* Multipart operations may still be using the key. This is safe
1047 * because all multipart operation objects are independent from
1048 * the key slot: if they need to access the key after the setup
1049 * phase, they have a copy of the key. Note that this means that
1050 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001051 /* At this point, key material and other type-specific content has
1052 * been wiped. Clear remaining metadata. We can call memset and not
1053 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 memset(slot, 0, sizeof(*slot));
1055 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001056}
1057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001059{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001060 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001061 psa_status_t status; /* status of the last operation */
1062 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001063#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1064 psa_se_drv_table_entry_t *driver;
1065#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (mbedtls_svc_key_id_is_null(key)) {
1068 return PSA_SUCCESS;
1069 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001070
Ronald Cronf2911112020-10-29 17:51:10 +01001071 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001072 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001073 * key, this will load the key description from persistent memory if not
1074 * done yet. We cannot avoid this loading as without it we don't know if
1075 * the key is operated by an SE or not and this information is needed by
1076 * the current implementation.
1077 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 status = psa_get_and_lock_key_slot(key, &slot);
1079 if (status != PSA_SUCCESS) {
1080 return status;
1081 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001082
Ronald Cronf2911112020-10-29 17:51:10 +01001083 /*
1084 * If the key slot containing the key description is under access by the
1085 * library (apart from the present access), the key cannot be destroyed
1086 * yet. For the time being, just return in error. Eventually (to be
1087 * implemented), the key should be destroyed when all accesses have
1088 * stopped.
1089 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if (slot->lock_count > 1) {
1091 psa_unlock_key_slot(slot);
1092 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001093 }
1094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001096 /* Refuse the destruction of a read-only key (which may or may not work
1097 * if we attempt it, depending on whether the key is merely read-only
1098 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001099 * Just do the best we can, which is to wipe the copy in memory
1100 * (done in this function's cleanup code). */
1101 overall_status = PSA_ERROR_NOT_PERMITTED;
1102 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001103 }
1104
Gilles Peskine354f7672019-07-12 23:46:38 +02001105#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1107 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001108 /* For a key in a secure element, we need to do three things:
1109 * remove the key file in internal storage, destroy the
1110 * key inside the secure element, and update the driver's
1111 * persistent data. Start a transaction that will encompass these
1112 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001114 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001116 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 status = psa_crypto_save_transaction();
1118 if (status != PSA_SUCCESS) {
1119 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001120 /* We should still try to destroy the key in the secure
1121 * element and the key metadata in storage. This is especially
1122 * important if the error is that the storage is full.
1123 * But how to do it exactly without risking an inconsistent
1124 * state after a reset?
1125 * https://github.com/ARMmbed/mbed-crypto/issues/215
1126 */
1127 overall_status = status;
1128 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001129 }
1130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 status = psa_destroy_se_key(driver,
1132 psa_key_slot_get_slot_number(slot));
1133 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001134 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001136 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001137#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1138
Darryl Greend49a4992018-06-18 17:27:26 +01001139#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1141 status = psa_destroy_persistent_key(slot->attr.id);
1142 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001143 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001145
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001146 /* TODO: other slots may have a copy of the same key. We should
1147 * invalidate them.
1148 * https://github.com/ARMmbed/mbed-crypto/issues/214
1149 */
Darryl Greend49a4992018-06-18 17:27:26 +01001150 }
1151#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001152
Gilles Peskinefc762652019-07-22 19:30:34 +02001153#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (driver != NULL) {
1155 status = psa_save_se_persistent_data(driver);
1156 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001157 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
1159 status = psa_crypto_stop_transaction();
1160 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001161 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001163 }
1164#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1165
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001166exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001168 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001170 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 }
1172 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001173}
1174
John Durkop07cc04a2020-11-16 22:08:34 -08001175#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001176 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001177static psa_status_t psa_get_rsa_public_exponent(
1178 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001180{
1181 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001183 uint8_t *buffer = NULL;
1184 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1188 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001189 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 }
1191 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001192 /* It's the default value, which is reported as an empty string,
1193 * so there's nothing to do. */
1194 goto exit;
1195 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 buflen = mbedtls_mpi_size(&mpi);
1198 buffer = mbedtls_calloc(1, buflen);
1199 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001200 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1201 goto exit;
1202 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1204 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001207 attributes->domain_parameters = buffer;
1208 attributes->domain_parameters_size = buflen;
1209
1210exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 mbedtls_mpi_free(&mpi);
1212 if (ret != 0) {
1213 mbedtls_free(buffer);
1214 }
1215 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001216}
John Durkop07cc04a2020-11-16 22:08:34 -08001217#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001218 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001219
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001220/** Retrieve all the publicly-accessible attributes of a key.
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1223 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001224{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001225 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001226 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001227 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1232 if (status != PSA_SUCCESS) {
1233 return status;
1234 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001235
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001236 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1238 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001239
1240#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1242 psa_set_key_slot_number(attributes,
1243 psa_key_slot_get_slot_number(slot));
1244 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001245#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001248#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001249 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001250 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001251 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001252 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001253 * is not yet implemented.
1254 * https://github.com/ARMmbed/mbed-crypto/issues/216
1255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001257 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001258
Ronald Cron90857082020-11-25 14:58:33 +01001259 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 slot->attr.type,
1261 slot->key.data,
1262 slot->key.bytes,
1263 &rsa);
1264 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001265 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 status = psa_get_rsa_public_exponent(rsa,
1269 attributes);
1270 mbedtls_rsa_free(rsa);
1271 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001272 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001273 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001274#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001275 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001276 default:
1277 /* Nothing else to do. */
1278 break;
1279 }
1280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 if (status != PSA_SUCCESS) {
1282 psa_reset_key_attributes(attributes);
1283 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001288}
1289
Gilles Peskinec8000c02019-08-02 20:15:51 +02001290#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1291psa_status_t psa_get_key_slot_number(
1292 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001294{
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001296 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 return PSA_SUCCESS;
1298 } else {
1299 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001300 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001301}
1302#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1305 size_t key_buffer_size,
1306 uint8_t *data,
1307 size_t data_size,
1308 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001309{
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if (key_buffer_size > data_size) {
1311 return PSA_ERROR_BUFFER_TOO_SMALL;
1312 }
1313 memcpy(data, key_buffer, key_buffer_size);
1314 memset(data + key_buffer_size, 0,
1315 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001316 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001318}
1319
Ronald Cron7285cda2020-11-26 14:46:37 +01001320psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001321 const psa_key_attributes_t *attributes,
1322 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001324{
Ronald Crond18b5f82020-11-25 16:46:05 +01001325 psa_key_type_t type = attributes->core.type;
1326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 if (key_type_is_raw_bytes(type) ||
1328 PSA_KEY_TYPE_IS_RSA(type) ||
1329 PSA_KEY_TYPE_IS_ECC(type)) {
1330 return psa_export_key_buffer_internal(
1331 key_buffer, key_buffer_size,
1332 data, data_size, data_length);
1333 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001334 /* This shouldn't happen in the reference implementation, but
1335 it is valid for a special-purpose implementation to omit
1336 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001338 }
1339}
1340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1342 uint8_t *data,
1343 size_t data_size,
1344 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001345{
1346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1347 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1348 psa_key_slot_t *slot;
1349
Ronald Crone907e552021-01-18 13:22:38 +01001350 /* Reject a zero-length output buffer now, since this can never be a
1351 * valid key representation. This way we know that data must be a valid
1352 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (data_size == 0) {
1354 return PSA_ERROR_BUFFER_TOO_SMALL;
1355 }
Ronald Crone907e552021-01-18 13:22:38 +01001356
Ronald Cron9486f9d2020-11-26 13:36:23 +01001357 /* Set the key to empty now, so that even when there are errors, we always
1358 * set data_length to a value between 0 and data_size. On error, setting
1359 * the key to empty is a good choice because an empty key representation is
1360 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001361 *data_length = 0;
1362
Ronald Cron9486f9d2020-11-26 13:36:23 +01001363 /* Export requires the EXPORT flag. There is an exception for public keys,
1364 * which don't require any flag, but
1365 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1368 PSA_KEY_USAGE_EXPORT, 0);
1369 if (status != PSA_SUCCESS) {
1370 return status;
1371 }
mohammad160306e79202018-03-28 13:17:44 +03001372
Ronald Crond18b5f82020-11-25 16:46:05 +01001373 psa_key_attributes_t attributes = {
1374 .core = slot->attr
1375 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 status = psa_driver_wrapper_export_key(&attributes,
1377 slot->key.data, slot->key.bytes,
1378 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001383}
1384
Ronald Cron7285cda2020-11-26 14:46:37 +01001385psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001386 const psa_key_attributes_t *attributes,
1387 const uint8_t *key_buffer,
1388 size_t key_buffer_size,
1389 uint8_t *data,
1390 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001392{
Ronald Crond18b5f82020-11-25 16:46:05 +01001393 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1396 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001397 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 return psa_export_key_buffer_internal(
1399 key_buffer, key_buffer_size,
1400 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001401 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001404#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1406 return mbedtls_psa_rsa_export_public_key(attributes,
1407 key_buffer,
1408 key_buffer_size,
1409 data,
1410 data_size,
1411 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001412#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001413 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001415#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1416 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001418#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1420 return mbedtls_psa_ecp_export_public_key(attributes,
1421 key_buffer,
1422 key_buffer_size,
1423 data,
1424 data_size,
1425 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001426#else
1427 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001429#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1430 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001431 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001433 /* This shouldn't happen in the reference implementation, but
1434 it is valid for a special-purpose implementation to omit
1435 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001437 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001438}
Ronald Crond18b5f82020-11-25 16:46:05 +01001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1441 uint8_t *data,
1442 size_t data_size,
1443 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001444{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001446 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001447 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001448
Ronald Crone907e552021-01-18 13:22:38 +01001449 /* Reject a zero-length output buffer now, since this can never be a
1450 * valid key representation. This way we know that data must be a valid
1451 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (data_size == 0) {
1453 return PSA_ERROR_BUFFER_TOO_SMALL;
1454 }
Ronald Crone907e552021-01-18 13:22:38 +01001455
Darryl Greendd8fb772018-11-07 16:00:44 +00001456 /* Set the key to empty now, so that even when there are errors, we always
1457 * set data_length to a value between 0 and data_size. On error, setting
1458 * the key to empty is a good choice because an empty key representation is
1459 * unlikely to be accepted anywhere. */
1460 *data_length = 0;
1461
1462 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1464 if (status != PSA_SUCCESS) {
1465 return status;
1466 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1469 status = PSA_ERROR_INVALID_ARGUMENT;
1470 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001471 }
1472
Ronald Crond18b5f82020-11-25 16:46:05 +01001473 psa_key_attributes_t attributes = {
1474 .core = slot->attr
1475 };
Ronald Cron67227982020-11-26 15:16:05 +01001476 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001477 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001479
1480exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001484}
1485
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001486MBEDTLS_STATIC_ASSERT(
1487 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1488 "One or more key attribute flag is listed as both external-only and dual-use")
1489MBEDTLS_STATIC_ASSERT(
1490 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1491 "One or more key attribute flag is listed as both internal-only and dual-use")
1492MBEDTLS_STATIC_ASSERT(
1493 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1494 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001495
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001496/** Validate that a key policy is internally well-formed.
1497 *
1498 * This function only rejects invalid policies. It does not validate the
1499 * consistency of the policy with respect to other attributes of the key
1500 * such as the key type.
1501 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001503{
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1505 PSA_KEY_USAGE_COPY |
1506 PSA_KEY_USAGE_ENCRYPT |
1507 PSA_KEY_USAGE_DECRYPT |
1508 PSA_KEY_USAGE_SIGN_MESSAGE |
1509 PSA_KEY_USAGE_VERIFY_MESSAGE |
1510 PSA_KEY_USAGE_SIGN_HASH |
1511 PSA_KEY_USAGE_VERIFY_HASH |
1512 PSA_KEY_USAGE_VERIFY_DERIVATION |
1513 PSA_KEY_USAGE_DERIVE)) != 0) {
1514 return PSA_ERROR_INVALID_ARGUMENT;
1515 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001518}
1519
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001520/** Validate the internal consistency of key attributes.
1521 *
1522 * This function only rejects invalid attribute values. If does not
1523 * validate the consistency of the attributes with any key data that may
1524 * be involved in the creation of the key.
1525 *
1526 * Call this function early in the key creation process.
1527 *
1528 * \param[in] attributes Key attributes for the new key.
1529 * \param[out] p_drv On any return, the driver for the key, if any.
1530 * NULL for a transparent key.
1531 *
1532 */
1533static psa_status_t psa_validate_key_attributes(
1534 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001536{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001537 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1539 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 status = psa_validate_key_location(lifetime, p_drv);
1542 if (status != PSA_SUCCESS) {
1543 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001544 }
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 status = psa_validate_key_persistence(lifetime);
1547 if (status != PSA_SUCCESS) {
1548 return status;
1549 }
1550
1551 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1552 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1553 return PSA_ERROR_INVALID_ARGUMENT;
1554 }
1555 } else {
1556 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1557 return PSA_ERROR_INVALID_ARGUMENT;
1558 }
1559 }
1560
1561 status = psa_validate_key_policy(&attributes->core.policy);
1562 if (status != PSA_SUCCESS) {
1563 return status;
1564 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001565
1566 /* Refuse to create overly large keys.
1567 * Note that this doesn't trigger on import if the attributes don't
1568 * explicitly specify a size (so psa_get_key_bits returns 0), so
1569 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1571 return PSA_ERROR_NOT_SUPPORTED;
1572 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001573
Gilles Peskine91e8c332019-08-02 19:19:39 +02001574 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1576 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1577 return PSA_ERROR_INVALID_ARGUMENT;
1578 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001581}
1582
Gilles Peskine4747d192019-04-17 15:05:45 +02001583/** Prepare a key slot to receive key material.
1584 *
1585 * This function allocates a key slot and sets its metadata.
1586 *
1587 * If this function fails, call psa_fail_key_creation().
1588 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001589 * This function is intended to be used as follows:
1590 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001591 * it with the specified attributes, and in case of a volatile key assign it
1592 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001593 * -# Populate the slot with the key material.
1594 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1595 * In case of failure at any step, stop the sequence and call
1596 * psa_fail_key_creation().
1597 *
Ronald Cron5c522922020-11-14 16:35:34 +01001598 * On success, the key slot is locked. It is the responsibility of the caller
1599 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001600 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001601 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001602 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001603 * \param[out] p_slot On success, a pointer to the prepared slot.
1604 * \param[out] p_drv On any return, the driver for the key, if any.
1605 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001606 *
1607 * \retval #PSA_SUCCESS
1608 * The key slot is ready to receive key material.
1609 * \return If this function fails, the key slot is an invalid state.
1610 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001611 */
1612static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001613 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001614 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001615 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001617{
1618 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001619 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001620 psa_key_slot_t *slot;
1621
Gilles Peskinedf179142019-07-15 22:02:14 +02001622 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001623 *p_drv = NULL;
1624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 status = psa_validate_key_attributes(attributes, p_drv);
1626 if (status != PSA_SUCCESS) {
1627 return status;
1628 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1631 if (status != PSA_SUCCESS) {
1632 return status;
1633 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001634 slot = *p_slot;
1635
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001636 /* We're storing the declared bit-size of the key. It's up to each
1637 * creation mechanism to verify that this information is correct.
1638 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001639 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001640 * is optional (import, copy). In case of a volatile key, assign it the
1641 * volatile key identifier associated to the slot returned to contain its
1642 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001643
1644 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001646#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1647 slot->attr.id = volatile_key_id;
1648#else
1649 slot->attr.id.key_id = volatile_key_id;
1650#endif
1651 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001652
Gilles Peskine91e8c332019-08-02 19:19:39 +02001653 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001654 * external-only flags, query `attributes`. Thanks to the check
1655 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001656 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001657 * may have set. */
1658 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001659
Gilles Peskinecbaff462019-07-12 23:46:04 +02001660#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001661 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001662 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001663 * create the key file in internal storage, create the
1664 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001665 * persistent data. This is done by starting a transaction that will
1666 * encompass these three actions.
1667 * For registering a volatile key, we just need to find an appropriate
1668 * slot number inside the SE. Since the key is designated volatile, creating
1669 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001670 /* The first thing to do is to find a slot number for the new key.
1671 * We save the slot number in persistent storage as part of the
1672 * transaction data. It will be needed to recover if the power
1673 * fails during the key creation process, to clean up on the secure
1674 * element side after restarting. Obtaining a slot number from the
1675 * secure element driver updates its persistent state, but we do not yet
1676 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001677 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001679 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1681 &slot_number);
1682 if (status != PSA_SUCCESS) {
1683 return status;
1684 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1687 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001688 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001689 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001690 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 status = psa_crypto_save_transaction();
1692 if (status != PSA_SUCCESS) {
1693 (void) psa_crypto_stop_transaction();
1694 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001695 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001696 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001697
1698 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001700 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001703 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001705 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001706#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001709}
Gilles Peskine4747d192019-04-17 15:05:45 +02001710
1711/** Finalize the creation of a key once its key material has been set.
1712 *
1713 * This entails writing the key to persistent storage.
1714 *
1715 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001716 * See the documentation of psa_start_key_creation() for the intended use
1717 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001718 *
Ronald Cron5c522922020-11-14 16:35:34 +01001719 * If the finalization succeeds, the function unlocks the key slot (it was
1720 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1721 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001722 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001723 * \param[in,out] slot Pointer to the slot with key material.
1724 * \param[in] driver The secure element driver for the key,
1725 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001726 * \param[out] key On success, identifier of the key. Note that the
1727 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001728 *
1729 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001730 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001731 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1732 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1733 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1734 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1735 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1736 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001737 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001738 * \return If this function fails, the key slot is an invalid state.
1739 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001740 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001741static psa_status_t psa_finish_key_creation(
1742 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001743 psa_se_drv_table_entry_t *driver,
1744 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001745{
1746 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001747 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001748 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001749
1750#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001752#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001754 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001755 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001757
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001758 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1759 sizeof(data.slot_number),
1760 "Slot number size does not match psa_se_key_data_storage_t");
1761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1763 status = psa_save_persistent_key(&slot->attr,
1764 (uint8_t *) &data,
1765 sizeof(data));
1766 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001767#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1768 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001769 /* Key material is saved in export representation in the slot, so
1770 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 status = psa_save_persistent_key(&slot->attr,
1772 slot->key.data,
1773 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001774 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001775 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001776#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1777
Gilles Peskinecbaff462019-07-12 23:46:04 +02001778#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001779 /* Finish the transaction for a key creation. This does not
1780 * happen when registering an existing key. Detect this case
1781 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001782 * creation of a persistent key in a secure element requires a transaction,
1783 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if (driver != NULL &&
1785 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1786 status = psa_save_se_persistent_data(driver);
1787 if (status != PSA_SUCCESS) {
1788 psa_destroy_persistent_key(slot->attr.id);
1789 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001790 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001792 }
1793#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001796 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 status = psa_unlock_key_slot(slot);
1798 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001799 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001801 }
Ronald Cron50972942020-11-14 11:28:25 +01001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001804}
1805
1806/** Abort the creation of a key.
1807 *
1808 * You may call this function after calling psa_start_key_creation(),
1809 * or after psa_finish_key_creation() fails. In other circumstances, this
1810 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001811 * See the documentation of psa_start_key_creation() for the intended use
1812 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001813 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001814 * \param[in,out] slot Pointer to the slot with key material.
1815 * \param[in] driver The secure element driver for the key,
1816 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001817 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001818static void psa_fail_key_creation(psa_key_slot_t *slot,
1819 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001820{
Gilles Peskine011e4282019-06-26 18:34:38 +02001821 (void) driver;
1822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001824 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001826
1827#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001828 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001829 * element, and the failure happened later (when saving metadata
1830 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001831 * element.
1832 * https://github.com/ARMmbed/mbed-crypto/issues/217
1833 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001834
Gilles Peskined7729582019-08-05 15:55:54 +02001835 /* Abort the ongoing transaction if any (there may not be one if
1836 * the creation process failed before starting one, or if the
1837 * key creation is a registration of a key in a secure element).
1838 * Earlier functions must already have done what it takes to undo any
1839 * partial creation. All that's left is to update the transaction data
1840 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001842#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001845}
1846
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001847/** Validate optional attributes during key creation.
1848 *
1849 * Some key attributes are optional during key creation. If they are
1850 * specified in the attributes structure, check that they are consistent
1851 * with the data in the slot.
1852 *
1853 * This function should be called near the end of key creation, after
1854 * the slot in memory is fully populated but before saving persistent data.
1855 */
1856static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001857 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001859{
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (attributes->core.type != 0) {
1861 if (attributes->core.type != slot->attr.type) {
1862 return PSA_ERROR_INVALID_ARGUMENT;
1863 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001864 }
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001867#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1869 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001870 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001871 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001873
Ronald Cron90857082020-11-25 14:58:33 +01001874 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 slot->attr.type,
1876 slot->key.data,
1877 slot->key.bytes,
1878 &rsa);
1879 if (status != PSA_SUCCESS) {
1880 return status;
1881 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 mbedtls_mpi_init(&actual);
1884 mbedtls_mpi_init(&required);
1885 ret = mbedtls_rsa_export(rsa,
1886 NULL, NULL, NULL, NULL, &actual);
1887 mbedtls_rsa_free(rsa);
1888 mbedtls_free(rsa);
1889 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001890 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 }
1892 ret = mbedtls_mpi_read_binary(&required,
1893 attributes->domain_parameters,
1894 attributes->domain_parameters_size);
1895 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 }
1898 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 }
1901rsa_exit:
1902 mbedtls_mpi_free(&actual);
1903 mbedtls_mpi_free(&required);
1904 if (ret != 0) {
1905 return mbedtls_to_psa_error(ret);
1906 }
1907 } else
John Durkop9814fa22020-11-04 12:28:15 -08001908#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1909 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001910 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001912 }
1913 }
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 if (attributes->core.bits != 0) {
1916 if (attributes->core.bits != slot->attr.bits) {
1917 return PSA_ERROR_INVALID_ARGUMENT;
1918 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001919 }
1920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001922}
1923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1925 const uint8_t *data,
1926 size_t data_length,
1927 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001928{
1929 psa_status_t status;
1930 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001931 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001932 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301933 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001934
Ronald Cron81709fc2020-11-14 12:10:32 +01001935 *key = MBEDTLS_SVC_KEY_ID_INIT;
1936
Gilles Peskine0f84d622019-09-12 19:03:13 +02001937 /* Reject zero-length symmetric keys (including raw data key objects).
1938 * This also rejects any key which might be encoded as an empty string,
1939 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (data_length == 0) {
1941 return PSA_ERROR_INVALID_ARGUMENT;
1942 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001943
Archana449608b2021-09-08 15:36:05 +05301944 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 if (data_length > SIZE_MAX / 8) {
1946 return PSA_ERROR_NOT_SUPPORTED;
1947 }
Archana6ed4bda2021-08-04 10:47:15 +05301948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1950 &slot, &driver);
1951 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001952 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001954
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001955 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301956 * storage ( thus not in the case of importing a key in a secure element
1957 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1958 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if (slot->key.data == NULL) {
1960 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301961 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 attributes, data, data_length, &storage_size);
1963 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301964 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 }
Archanad8a83dc2021-06-14 10:04:16 +05301966 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 status = psa_allocate_buffer_to_slot(slot, storage_size);
1968 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001969 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001971 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001972
1973 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 status = psa_driver_wrapper_import_key(attributes,
1975 data, data_length,
1976 slot->key.data,
1977 slot->key.bytes,
1978 &slot->key.bytes, &bits);
1979 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001980 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001984 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001986 status = PSA_ERROR_INVALID_ARGUMENT;
1987 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001988 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001989
Archana6ed4bda2021-08-04 10:47:15 +05301990 /* Enforce a size limit, and in particular ensure that the bit
1991 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301993 status = PSA_ERROR_NOT_SUPPORTED;
1994 goto exit;
1995 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 status = psa_validate_optional_attributes(slot, attributes);
1997 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001998 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002002exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 if (status != PSA_SUCCESS) {
2004 psa_fail_key_creation(slot, driver);
2005 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002008}
2009
Gilles Peskined7729582019-08-05 15:55:54 +02002010#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2011psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002013{
2014 psa_status_t status;
2015 psa_key_slot_t *slot = NULL;
2016 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002018
2019 /* Leaving attributes unspecified is not currently supported.
2020 * It could make sense to query the key type and size from the
2021 * secure element, but not all secure elements support this
2022 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2024 return PSA_ERROR_NOT_SUPPORTED;
2025 }
2026 if (psa_get_key_bits(attributes) == 0) {
2027 return PSA_ERROR_NOT_SUPPORTED;
2028 }
Gilles Peskined7729582019-08-05 15:55:54 +02002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2031 &slot, &driver);
2032 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002033 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 }
Gilles Peskined7729582019-08-05 15:55:54 +02002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002037
2038exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (status != PSA_SUCCESS) {
2040 psa_fail_key_creation(slot, driver);
2041 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002042
Gilles Peskined7729582019-08-05 15:55:54 +02002043 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 psa_close_key(key);
2045 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002046}
2047#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2050 const psa_key_attributes_t *specified_attributes,
2051 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002052{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002054 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002055 psa_key_slot_t *source_slot = NULL;
2056 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002057 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002058 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302059 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002060
Ronald Cron81709fc2020-11-14 12:10:32 +01002061 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2062
Archana8a180362021-07-05 02:18:48 +05302063 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2065 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002066 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 status = psa_validate_optional_attributes(source_slot,
2070 specified_attributes);
2071 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002072 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002074
Archana9d17bf42021-09-10 06:22:44 +05302075 /* The target key type and number of bits have been validated by
2076 * psa_validate_optional_attributes() to be either equal to zero or
2077 * equal to the ones of the source key. So it is safe to inherit
2078 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302079 * */
Archana9d17bf42021-09-10 06:22:44 +05302080 actual_attributes.core.bits = source_slot->attr.bits;
2081 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302082
2083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 status = psa_restrict_key_policy(source_slot->attr.type,
2085 &actual_attributes.core.policy,
2086 &source_slot->attr.policy);
2087 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002088 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2092 &target_slot, &driver);
2093 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 }
2096 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2097 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302098 /*
Archana9d17bf42021-09-10 06:22:44 +05302099 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302100 * the source key would need to be exported as plaintext and re-imported
2101 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302102 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302103 * appropriate API invocations from the application, if needed.
2104 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002105 status = PSA_ERROR_NOT_SUPPORTED;
2106 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002107 }
Archana8a180362021-07-05 02:18:48 +05302108 /*
2109 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302110 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302111 * - For opaque keys this translates to an invocation of the drivers'
2112 * copy_key entry point through the dispatch layer.
2113 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2115 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2116 &storage_size);
2117 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 }
Archana374fe5b2021-09-08 15:50:28 +05302120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2122 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 }
Archana374fe5b2021-09-08 15:50:28 +05302125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 status = psa_driver_wrapper_copy_key(&actual_attributes,
2127 source_slot->key.data,
2128 source_slot->key.bytes,
2129 target_slot->key.data,
2130 target_slot->key.bytes,
2131 &target_slot->key.bytes);
2132 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 }
2135 } else {
2136 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302137 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 source_slot->key.bytes);
2139 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 }
Archana8a180362021-07-05 02:18:48 +05302142 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002144exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (status != PSA_SUCCESS) {
2146 psa_fail_key_creation(target_slot, driver);
2147 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002152}
2153
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002154
2155
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002156/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002157/* Message digests */
2158/****************************************************************/
2159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002161{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002162 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (operation->id == 0) {
2164 return PSA_SUCCESS;
2165 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002168 operation->id = 0;
2169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002171}
2172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2174 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002175{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002176 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002177
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002178 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002180 status = PSA_ERROR_BAD_STATE;
2181 goto exit;
2182 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002185 status = PSA_ERROR_INVALID_ARGUMENT;
2186 goto exit;
2187 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002188
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002189 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2190 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002194
2195exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (status != PSA_SUCCESS) {
2197 psa_hash_abort(operation);
2198 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002199
2200 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002201}
2202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2204 const uint8_t *input,
2205 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002206{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002210 status = PSA_ERROR_BAD_STATE;
2211 goto exit;
2212 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002213
Steven Cooremanc8288352021-03-04 14:02:19 +01002214 /* Don't require hash implementations to behave correctly on a
2215 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if (input_length == 0) {
2217 return PSA_SUCCESS;
2218 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002221
2222exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if (status != PSA_SUCCESS) {
2224 psa_hash_abort(operation);
2225 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002228}
2229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2231 uint8_t *hash,
2232 size_t hash_size,
2233 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002234{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002235 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 if (operation->id == 0) {
2237 return PSA_ERROR_BAD_STATE;
2238 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239
Steven Cooreman1e582352021-02-18 17:24:37 +01002240 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 operation, hash, hash_size, hash_length);
2242 psa_hash_abort(operation);
2243 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244}
2245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2247 const uint8_t *hash,
2248 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249{
Ronald Cron69a63422021-10-18 09:47:58 +02002250 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002252 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 operation,
2254 actual_hash, sizeof(actual_hash),
2255 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002258 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002262 status = PSA_ERROR_INVALID_SIGNATURE;
2263 goto exit;
2264 }
2265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002267 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002269
2270exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2272 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002273 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002277}
2278
Gilles Peskine449bd832023-01-11 14:50:10 +01002279psa_status_t psa_hash_compute(psa_algorithm_t alg,
2280 const uint8_t *input, size_t input_length,
2281 uint8_t *hash, size_t hash_size,
2282 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002283{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002284 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 if (!PSA_ALG_IS_HASH(alg)) {
2286 return PSA_ERROR_INVALID_ARGUMENT;
2287 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2290 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002291}
2292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293psa_status_t psa_hash_compare(psa_algorithm_t alg,
2294 const uint8_t *input, size_t input_length,
2295 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002296{
Ronald Cron69a63422021-10-18 09:47:58 +02002297 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002298 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 if (!PSA_ALG_IS_HASH(alg)) {
2301 return PSA_ERROR_INVALID_ARGUMENT;
2302 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002303
Steven Cooreman1e582352021-02-18 17:24:37 +01002304 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 alg, input, input_length,
2306 actual_hash, sizeof(actual_hash),
2307 &actual_hash_length);
2308 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002309 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 }
2311 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002312 status = PSA_ERROR_INVALID_SIGNATURE;
2313 goto exit;
2314 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002316 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002318
2319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2321 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002322}
2323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2325 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002326{
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 if (source_operation->id == 0 ||
2328 target_operation->id != 0) {
2329 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002330 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2333 target_operation);
2334 if (status != PSA_SUCCESS) {
2335 psa_hash_abort(target_operation);
2336 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002339}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340
2341
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002343/* MAC */
2344/****************************************************************/
2345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347{
Steven Cooremane6804192021-03-19 18:28:56 +01002348 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if (operation->id == 0) {
2350 return PSA_SUCCESS;
2351 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002354 operation->mac_size = 0;
2355 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002356 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002359}
2360
Ronald Cron2dff3b22021-06-17 16:33:22 +02002361static psa_status_t psa_mac_finalize_alg_and_key_validation(
2362 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002363 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002365{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 psa_key_type_t key_type = psa_get_key_type(attributes);
2368 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (!PSA_ALG_IS_MAC(alg)) {
2371 return PSA_ERROR_INVALID_ARGUMENT;
2372 }
Steven Cooremane6804192021-03-19 18:28:56 +01002373
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002374 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 status = psa_mac_key_can_do(alg, key_type);
2376 if (status != PSA_SUCCESS) {
2377 return status;
2378 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002379
Steven Cooremand1a68f12021-05-10 11:13:41 +02002380 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002384 /* A very short MAC is too short for security since it can be
2385 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2386 * so we make this our minimum, even though 32 bits is still
2387 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002389 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2392 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002393 /* It's impossible to "truncate" to a larger length than the full length
2394 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002396 }
2397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002399 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2400 * that is disabled in the compile-time configuration. The result can
2401 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2402 * configuration into account. In this case, force a return of
2403 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2404 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2405 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2406 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2407 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002409 }
2410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002412}
2413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2415 mbedtls_svc_key_id_t key,
2416 psa_algorithm_t alg,
2417 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002418{
2419 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2420 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002421 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002422
2423 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002425 status = PSA_ERROR_BAD_STATE;
2426 goto exit;
2427 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002428
2429 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 key,
2431 &slot,
2432 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2433 alg);
2434 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002435 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002437
2438 psa_key_attributes_t attributes = {
2439 .core = slot->attr
2440 };
2441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2443 &operation->mac_size);
2444 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002445 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002447
2448 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002449 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (is_sign) {
2451 status = psa_driver_wrapper_mac_sign_setup(operation,
2452 &attributes,
2453 slot->key.data,
2454 slot->key.bytes,
2455 alg);
2456 } else {
2457 status = psa_driver_wrapper_mac_verify_setup(operation,
2458 &attributes,
2459 slot->key.data,
2460 slot->key.bytes,
2461 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002462 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002463
Gilles Peskinefbfac682018-07-08 20:51:54 +02002464exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 if (status != PSA_SUCCESS) {
2466 psa_mac_abort(operation);
2467 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472}
2473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2475 mbedtls_svc_key_id_t key,
2476 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002477{
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002479}
2480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2482 mbedtls_svc_key_id_t key,
2483 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002484{
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002486}
2487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2489 const uint8_t *input,
2490 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491{
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 if (operation->id == 0) {
2493 return PSA_ERROR_BAD_STATE;
2494 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002496 /* Don't require hash implementations to behave correctly on a
2497 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 if (input_length == 0) {
2499 return PSA_SUCCESS;
2500 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2503 input, input_length);
2504 if (status != PSA_SUCCESS) {
2505 psa_mac_abort(operation);
2506 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509}
2510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2512 uint8_t *mac,
2513 size_t mac_size,
2514 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002515{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002516 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2517 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002520 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002521 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002522 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002525 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002526 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002527 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002528
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002529 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2530 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002532 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002533 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002534 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002537 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002538 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002539 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 status = psa_driver_wrapper_mac_sign_finish(operation,
2542 mac, operation->mac_size,
2543 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002544
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002545exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002546 /* In case of success, set the potential excess room in the output buffer
2547 * to an invalid value, to avoid potentially leaking a longer MAC.
2548 * In case of error, set the output length and content to a safe default,
2549 * such that in case the caller misses an error check, the output would be
2550 * an unachievable MAC.
2551 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002553 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002554 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002555 }
mohammad16036df908f2018-04-02 08:34:15 -07002556
Paul Elliottdc42ca82023-02-24 18:11:59 +00002557 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002562}
2563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2565 const uint8_t *mac,
2566 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002567{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002568 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2569 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002572 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002573 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002574 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002577 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002578 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002579 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002582 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002583 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002584 }
2585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 status = psa_driver_wrapper_mac_verify_finish(operation,
2587 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002588
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002589exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593}
2594
Gilles Peskine449bd832023-01-11 14:50:10 +01002595static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2596 psa_algorithm_t alg,
2597 const uint8_t *input,
2598 size_t input_length,
2599 uint8_t *mac,
2600 size_t mac_size,
2601 size_t *mac_length,
2602 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002603{
2604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002605 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2606 psa_key_slot_t *slot;
2607 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608
Ronald Cron51131b52021-06-17 17:17:20 +02002609 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 key,
2611 &slot,
2612 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2613 alg);
2614 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002615 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002617
Ronald Cron51131b52021-06-17 17:17:20 +02002618 psa_key_attributes_t attributes = {
2619 .core = slot->attr
2620 };
2621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2623 &operation_mac_size);
2624 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002625 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002629 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002630 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002631 }
2632
2633 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 &attributes,
2635 slot->key.data, slot->key.bytes,
2636 alg,
2637 input, input_length,
2638 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002639
2640exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002641 /* In case of success, set the potential excess room in the output buffer
2642 * to an invalid value, to avoid potentially leaking a longer MAC.
2643 * In case of error, set the output length and content to a safe default,
2644 * such that in case the caller misses an error check, the output would be
2645 * an unachievable MAC.
2646 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002648 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002649 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002650 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002651
2652 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002653
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002657}
2658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002660 psa_algorithm_t alg,
2661 const uint8_t *input,
2662 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 uint8_t *mac,
2664 size_t mac_size,
2665 size_t *mac_length)
2666{
2667 return psa_mac_compute_internal(key, alg,
2668 input, input_length,
2669 mac, mac_size, mac_length, 1);
2670}
2671
2672psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2673 psa_algorithm_t alg,
2674 const uint8_t *input,
2675 size_t input_length,
2676 const uint8_t *mac,
2677 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002678{
2679 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002680 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2681 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 status = psa_mac_compute_internal(key, alg,
2684 input, input_length,
2685 actual_mac, sizeof(actual_mac),
2686 &actual_mac_length, 0);
2687 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002688 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002692 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002693 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002694 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002696 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002697 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002698 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002699
2700exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002704}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002705
Gilles Peskinee59236f2018-01-27 23:32:46 +01002706/****************************************************************/
2707/* Asymmetric cryptography */
2708/****************************************************************/
2709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2711 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002712{
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if (input_is_message) {
2714 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2715 return PSA_ERROR_INVALID_ARGUMENT;
2716 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2719 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2720 return PSA_ERROR_INVALID_ARGUMENT;
2721 }
2722 }
2723 } else {
2724 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2725 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002726 }
2727 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002730}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2733 int input_is_message,
2734 psa_algorithm_t alg,
2735 const uint8_t *input,
2736 size_t input_length,
2737 uint8_t *signature,
2738 size_t signature_size,
2739 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002740{
2741 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2742 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2743 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002744
2745 *signature_length = 0;
2746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 status = psa_sign_verify_check_alg(input_is_message, alg);
2748 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002749 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002751
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002752 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002753 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002754 * buffer can in principle be empty since it doesn't actually have
2755 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 if (signature_size == 0) {
2757 return PSA_ERROR_BUFFER_TOO_SMALL;
2758 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002759
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002760 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 key, &slot,
2762 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2763 PSA_KEY_USAGE_SIGN_HASH,
2764 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002765
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002767 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002771 status = PSA_ERROR_INVALID_ARGUMENT;
2772 goto exit;
2773 }
2774
2775 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002777 };
2778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002780 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002781 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002782 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 signature, signature_size, signature_length);
2784 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002785
2786 status = psa_driver_wrapper_sign_hash(
2787 &attributes, slot->key.data, slot->key.bytes,
2788 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002790 }
2791
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002792
2793exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002794 psa_wipe_tag_output_buffer(signature, status, signature_size,
2795 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002796
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800}
2801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2803 int input_is_message,
2804 psa_algorithm_t alg,
2805 const uint8_t *input,
2806 size_t input_length,
2807 const uint8_t *signature,
2808 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002809{
2810 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2811 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2812 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 status = psa_sign_verify_check_alg(input_is_message, alg);
2815 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002816 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002818
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002819 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 key, &slot,
2821 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2822 PSA_KEY_USAGE_VERIFY_HASH,
2823 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002824
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 if (status != PSA_SUCCESS) {
2826 return status;
2827 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002828
2829 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002831 };
2832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002834 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002835 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002836 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 signature, signature_length);
2838 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002839 status = psa_driver_wrapper_verify_hash(
2840 &attributes, slot->key.data, slot->key.bytes,
2841 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002843 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002848
2849}
2850
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002851psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002852 const psa_key_attributes_t *attributes,
2853 const uint8_t *key_buffer,
2854 size_t key_buffer_size,
2855 psa_algorithm_t alg,
2856 const uint8_t *input,
2857 size_t input_length,
2858 uint8_t *signature,
2859 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002861{
2862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002865 size_t hash_length;
2866 uint8_t hash[PSA_HASH_MAX_SIZE];
2867
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002868 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 PSA_ALG_SIGN_GET_HASH(alg),
2870 input, input_length,
2871 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002874 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002876
2877 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 attributes, key_buffer, key_buffer_size,
2879 alg, hash, hash_length,
2880 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002881 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002884}
2885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2887 psa_algorithm_t alg,
2888 const uint8_t *input,
2889 size_t input_length,
2890 uint8_t *signature,
2891 size_t signature_size,
2892 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002893{
2894 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002895 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002897}
2898
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002899psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002900 const psa_key_attributes_t *attributes,
2901 const uint8_t *key_buffer,
2902 size_t key_buffer_size,
2903 psa_algorithm_t alg,
2904 const uint8_t *input,
2905 size_t input_length,
2906 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002908{
2909 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002910
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002912 size_t hash_length;
2913 uint8_t hash[PSA_HASH_MAX_SIZE];
2914
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002915 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 PSA_ALG_SIGN_GET_HASH(alg),
2917 input, input_length,
2918 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002921 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002923
2924 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 attributes, key_buffer, key_buffer_size,
2926 alg, hash, hash_length,
2927 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002928 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002929
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002931}
2932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2934 psa_algorithm_t alg,
2935 const uint8_t *input,
2936 size_t input_length,
2937 const uint8_t *signature,
2938 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002939{
2940 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002941 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002943}
2944
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002945psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002946 const psa_key_attributes_t *attributes,
2947 const uint8_t *key_buffer, size_t key_buffer_size,
2948 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002950{
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2952 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2953 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002954#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2956 return mbedtls_psa_rsa_sign_hash(
2957 attributes,
2958 key_buffer, key_buffer_size,
2959 alg, hash, hash_length,
2960 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002961#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2962 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 } else {
2964 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002965 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2967 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002968#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2970 return mbedtls_psa_ecdsa_sign_hash(
2971 attributes,
2972 key_buffer, key_buffer_size,
2973 alg, hash, hash_length,
2974 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002975#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2976 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 } else {
2978 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002979 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002980 }
Ronald Cron4501c982021-03-19 20:09:32 +01002981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 (void) key_buffer;
2983 (void) key_buffer_size;
2984 (void) hash;
2985 (void) hash_length;
2986 (void) signature;
2987 (void) signature_size;
2988 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002989
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002991}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002992
Gilles Peskine449bd832023-01-11 14:50:10 +01002993psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2994 psa_algorithm_t alg,
2995 const uint8_t *hash,
2996 size_t hash_length,
2997 uint8_t *signature,
2998 size_t signature_size,
2999 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003000{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003001 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003002 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003004}
3005
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003006psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003007 const psa_key_attributes_t *attributes,
3008 const uint8_t *key_buffer, size_t key_buffer_size,
3009 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003011{
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3013 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3014 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003015#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3017 return mbedtls_psa_rsa_verify_hash(
3018 attributes,
3019 key_buffer, key_buffer_size,
3020 alg, hash, hash_length,
3021 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003022#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3023 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 } else {
3025 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003026 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3028 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003029#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3031 return mbedtls_psa_ecdsa_verify_hash(
3032 attributes,
3033 key_buffer, key_buffer_size,
3034 alg, hash, hash_length,
3035 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003036#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3037 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 } else {
3039 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003040 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003041 }
Ronald Cron4501c982021-03-19 20:09:32 +01003042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 (void) key_buffer;
3044 (void) key_buffer_size;
3045 (void) hash;
3046 (void) hash_length;
3047 (void) signature;
3048 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003049
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003051}
3052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3054 psa_algorithm_t alg,
3055 const uint8_t *hash,
3056 size_t hash_length,
3057 const uint8_t *signature,
3058 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003059{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003060 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003061 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003063}
3064
Gilles Peskine449bd832023-01-11 14:50:10 +01003065psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3066 psa_algorithm_t alg,
3067 const uint8_t *input,
3068 size_t input_length,
3069 const uint8_t *salt,
3070 size_t salt_length,
3071 uint8_t *output,
3072 size_t output_size,
3073 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003074{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003075 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003076 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003077 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003078
Darryl Green5cc689a2018-07-24 15:34:10 +01003079 (void) input;
3080 (void) input_length;
3081 (void) salt;
3082 (void) output;
3083 (void) output_size;
3084
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003085 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3088 return PSA_ERROR_INVALID_ARGUMENT;
3089 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003090
Ronald Cron5c522922020-11-14 16:35:34 +01003091 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3093 if (status != PSA_SUCCESS) {
3094 return status;
3095 }
3096 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3097 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003098 status = PSA_ERROR_INVALID_ARGUMENT;
3099 goto exit;
3100 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003101
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003102 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003104 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003105
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003106 status = psa_driver_wrapper_asymmetric_encrypt(
3107 &attributes, slot->key.data, slot->key.bytes,
3108 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003110exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003112
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003114}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3117 psa_algorithm_t alg,
3118 const uint8_t *input,
3119 size_t input_length,
3120 const uint8_t *salt,
3121 size_t salt_length,
3122 uint8_t *output,
3123 size_t output_size,
3124 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003126 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003127 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003128 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003129
Darryl Green5cc689a2018-07-24 15:34:10 +01003130 (void) input;
3131 (void) input_length;
3132 (void) salt;
3133 (void) output;
3134 (void) output_size;
3135
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003136 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003137
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3139 return PSA_ERROR_INVALID_ARGUMENT;
3140 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003141
Ronald Cron5c522922020-11-14 16:35:34 +01003142 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3144 if (status != PSA_SUCCESS) {
3145 return status;
3146 }
3147 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003148 status = PSA_ERROR_INVALID_ARGUMENT;
3149 goto exit;
3150 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003152 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003154 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003155
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003156 status = psa_driver_wrapper_asymmetric_decrypt(
3157 &attributes, slot->key.data, slot->key.bytes,
3158 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003160
3161exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003163
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003165}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003166
Paul Elliott9fe12f62022-11-30 19:16:02 +00003167/****************************************************************/
3168/* Asymmetric interruptible cryptography */
3169/****************************************************************/
3170
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003171static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3172
Paul Elliott9fe12f62022-11-30 19:16:02 +00003173void psa_interruptible_set_max_ops(uint32_t max_ops)
3174{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003175 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003176}
3177
3178uint32_t psa_interruptible_get_max_ops(void)
3179{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003180 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003181}
3182
Paul Elliott9fe12f62022-11-30 19:16:02 +00003183uint32_t psa_sign_hash_get_num_ops(
3184 const psa_sign_hash_interruptible_operation_t *operation)
3185{
Paul Elliott296ede92022-12-15 17:00:30 +00003186 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003187}
3188
3189uint32_t psa_verify_hash_get_num_ops(
3190 const psa_verify_hash_interruptible_operation_t *operation)
3191{
Paul Elliott296ede92022-12-15 17:00:30 +00003192 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003193}
3194
Paul Elliott59ad9452022-12-18 15:09:02 +00003195static psa_status_t psa_sign_hash_abort_internal(
3196 psa_sign_hash_interruptible_operation_t *operation)
3197{
3198 if (operation->id == 0) {
3199 /* The object has (apparently) been initialized but it is not (yet)
3200 * in use. It's ok to call abort on such an object, and there's
3201 * nothing to do. */
3202 return PSA_SUCCESS;
3203 }
3204
3205 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3206
3207 status = psa_driver_wrapper_sign_hash_abort(operation);
3208
3209 operation->id = 0;
3210
Paul Elliotte6145dc2023-02-07 12:51:21 +00003211 /* Do not clear either the error_occurred or num_ops elements here as they
3212 * only want to be cleared by the application calling abort, not by abort
3213 * being called at completion of an operation. */
3214
Paul Elliott59ad9452022-12-18 15:09:02 +00003215 return status;
3216}
3217
Paul Elliott9fe12f62022-11-30 19:16:02 +00003218psa_status_t psa_sign_hash_start(
3219 psa_sign_hash_interruptible_operation_t *operation,
3220 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3221 const uint8_t *hash, size_t hash_length)
3222{
3223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3224 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3225 psa_key_slot_t *slot;
3226
Paul Elliottc9774412023-02-06 15:14:07 +00003227 /* Check that start has not been previously called, or operation has not
3228 * previously errored. */
3229 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003230 return PSA_ERROR_BAD_STATE;
3231 }
3232
Paul Elliott9fe12f62022-11-30 19:16:02 +00003233 status = psa_sign_verify_check_alg(0, alg);
3234 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003235 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003236 return status;
3237 }
3238
3239 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3240 PSA_KEY_USAGE_SIGN_HASH,
3241 alg);
3242
3243 if (status != PSA_SUCCESS) {
3244 goto exit;
3245 }
3246
3247 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3248 status = PSA_ERROR_INVALID_ARGUMENT;
3249 goto exit;
3250 }
3251
3252 psa_key_attributes_t attributes = {
3253 .core = slot->attr
3254 };
3255
Paul Elliott296ede92022-12-15 17:00:30 +00003256 /* Ensure ops count gets reset, in case of operation re-use. */
3257 operation->num_ops = 0;
3258
Paul Elliott9fe12f62022-11-30 19:16:02 +00003259 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3260 slot->key.data,
3261 slot->key.bytes, alg,
3262 hash, hash_length);
3263exit:
3264
3265 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003266 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003267 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003268 }
3269
3270 unlock_status = psa_unlock_key_slot(slot);
3271
Paul Elliottc9774412023-02-06 15:14:07 +00003272 if (unlock_status != PSA_SUCCESS) {
3273 operation->error_occurred = 1;
3274 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003275
Paul Elliottc9774412023-02-06 15:14:07 +00003276 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003277}
3278
3279
3280psa_status_t psa_sign_hash_complete(
3281 psa_sign_hash_interruptible_operation_t *operation,
3282 uint8_t *signature, size_t signature_size,
3283 size_t *signature_length)
3284{
3285 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3286
3287 *signature_length = 0;
3288
Paul Elliottc9774412023-02-06 15:14:07 +00003289 /* Check that start has been called first, and that operation has not
3290 * previously errored. */
3291 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003292 status = PSA_ERROR_BAD_STATE;
3293 goto exit;
3294 }
3295
Paul Elliott096abc42023-02-03 18:33:23 +00003296 /* Immediately reject a zero-length signature buffer. This guarantees that
3297 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003298 if (signature_size == 0) {
3299 status = PSA_ERROR_BUFFER_TOO_SMALL;
3300 goto exit;
3301 }
3302
3303 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3304 signature_size,
3305 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003306
Paul Elliott296ede92022-12-15 17:00:30 +00003307 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003308 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003309
Paul Elliottebe225c2023-02-10 14:32:53 +00003310exit:
3311
Paul Elliott59200a22023-02-21 15:07:40 +00003312 psa_wipe_tag_output_buffer(signature, status, signature_size,
3313 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003314
Paul Elliott53bb3122023-02-10 14:22:22 +00003315 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003316 if (status != PSA_SUCCESS) {
3317 operation->error_occurred = 1;
3318 }
3319
Paul Elliott59ad9452022-12-18 15:09:02 +00003320 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003321 }
3322
3323 return status;
3324}
3325
3326psa_status_t psa_sign_hash_abort(
3327 psa_sign_hash_interruptible_operation_t *operation)
3328{
Paul Elliott59ad9452022-12-18 15:09:02 +00003329 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3330
3331 status = psa_sign_hash_abort_internal(operation);
3332
3333 /* We clear the number of ops done here, so that it is not cleared when
3334 * the operation fails or succeeds, only on manual abort. */
3335 operation->num_ops = 0;
3336
Paul Elliottc9774412023-02-06 15:14:07 +00003337 /* Likewise, failure state. */
3338 operation->error_occurred = 0;
3339
Paul Elliott59ad9452022-12-18 15:09:02 +00003340 return status;
3341}
3342
3343static psa_status_t psa_verify_hash_abort_internal(
3344 psa_verify_hash_interruptible_operation_t *operation)
3345{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346 if (operation->id == 0) {
3347 /* The object has (apparently) been initialized but it is not (yet)
3348 * in use. It's ok to call abort on such an object, and there's
3349 * nothing to do. */
3350 return PSA_SUCCESS;
3351 }
3352
Paul Elliott59ad9452022-12-18 15:09:02 +00003353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3354
3355 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003356
3357 operation->id = 0;
3358
Paul Elliotte6145dc2023-02-07 12:51:21 +00003359 /* Do not clear either the error_occurred or num_ops elements here as they
3360 * only want to be cleared by the application calling abort, not by abort
3361 * being called at completion of an operation. */
3362
Paul Elliott59ad9452022-12-18 15:09:02 +00003363 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003364}
3365
3366psa_status_t psa_verify_hash_start(
3367 psa_verify_hash_interruptible_operation_t *operation,
3368 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3369 const uint8_t *hash, size_t hash_length,
3370 const uint8_t *signature, size_t signature_length)
3371{
3372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3373 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3374 psa_key_slot_t *slot;
3375
Paul Elliottc9774412023-02-06 15:14:07 +00003376 /* Check that start has not been previously called, or operation has not
3377 * previously errored. */
3378 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003379 return PSA_ERROR_BAD_STATE;
3380 }
3381
3382 status = psa_sign_verify_check_alg(0, alg);
3383 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003384 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003385 return status;
3386 }
3387
3388 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3389 PSA_KEY_USAGE_VERIFY_HASH,
3390 alg);
3391
3392 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003393 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003394 return status;
3395 }
3396
3397 psa_key_attributes_t attributes = {
3398 .core = slot->attr
3399 };
3400
Paul Elliott296ede92022-12-15 17:00:30 +00003401 /* Ensure ops count gets reset, in case of operation re-use. */
3402 operation->num_ops = 0;
3403
Paul Elliott9fe12f62022-11-30 19:16:02 +00003404 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3405 slot->key.data,
3406 slot->key.bytes,
3407 alg, hash, hash_length,
3408 signature, signature_length);
3409
3410 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003411 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003412 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003413 }
3414
3415 unlock_status = psa_unlock_key_slot(slot);
3416
Paul Elliottc9774412023-02-06 15:14:07 +00003417 if (unlock_status != PSA_SUCCESS) {
3418 operation->error_occurred = 1;
3419 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420
Paul Elliottc9774412023-02-06 15:14:07 +00003421 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003422}
3423
3424psa_status_t psa_verify_hash_complete(
3425 psa_verify_hash_interruptible_operation_t *operation)
3426{
3427 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3428
Paul Elliottc9774412023-02-06 15:14:07 +00003429 /* Check that start has been called first, and that operation has not
3430 * previously errored. */
3431 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003432 status = PSA_ERROR_BAD_STATE;
3433 goto exit;
3434 }
3435
3436 status = psa_driver_wrapper_verify_hash_complete(operation);
3437
Paul Elliott296ede92022-12-15 17:00:30 +00003438 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003439 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003440 operation);
3441
Paul Elliottebe225c2023-02-10 14:32:53 +00003442exit:
3443
Paul Elliott9fe12f62022-11-30 19:16:02 +00003444 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003445 if (status != PSA_SUCCESS) {
3446 operation->error_occurred = 1;
3447 }
3448
Paul Elliott59ad9452022-12-18 15:09:02 +00003449 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003450 }
3451
3452 return status;
3453}
3454
3455psa_status_t psa_verify_hash_abort(
3456 psa_verify_hash_interruptible_operation_t *operation)
3457{
Paul Elliott59ad9452022-12-18 15:09:02 +00003458 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459
Paul Elliott59ad9452022-12-18 15:09:02 +00003460 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461
Paul Elliott59ad9452022-12-18 15:09:02 +00003462 /* We clear the number of ops done here, so that it is not cleared when
3463 * the operation fails or succeeds, only on manual abort. */
3464 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003465
Paul Elliottc9774412023-02-06 15:14:07 +00003466 /* Likewise, failure state. */
3467 operation->error_occurred = 0;
3468
Paul Elliott59ad9452022-12-18 15:09:02 +00003469 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003470}
3471
Paul Elliott588f8ed2022-12-02 18:10:26 +00003472/****************************************************************/
3473/* Asymmetric interruptible cryptography internal */
3474/* implementations */
3475/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003476
Paul Elliott588f8ed2022-12-02 18:10:26 +00003477void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3478{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003479
Paul Elliott588f8ed2022-12-02 18:10:26 +00003480#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3481 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3482 defined(MBEDTLS_ECP_RESTARTABLE)
3483
3484 /* Internal implementation uses zero to indicate infinite number max ops,
3485 * therefore avoid this value, and set to minimum possible. */
3486 if (max_ops == 0) {
3487 max_ops = 1;
3488 }
3489
Paul Elliott588f8ed2022-12-02 18:10:26 +00003490 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003491#else
3492 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003493#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3494 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3495 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3496}
3497
Paul Elliott588f8ed2022-12-02 18:10:26 +00003498uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003499 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003500{
3501#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3502 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3503 defined(MBEDTLS_ECP_RESTARTABLE)
3504
Paul Elliott93d9ca82023-02-15 18:14:21 +00003505 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003506#else
3507 (void) operation;
3508 return 0;
3509#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3510 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3511 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3512}
3513
3514uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003515 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003516{
3517 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3518 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3519 defined(MBEDTLS_ECP_RESTARTABLE)
3520
Paul Elliott93d9ca82023-02-15 18:14:21 +00003521 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003522#else
3523 (void) operation;
3524 return 0;
3525#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3526 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3527 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3528}
3529
3530psa_status_t mbedtls_psa_sign_hash_start(
3531 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3532 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3533 size_t key_buffer_size, psa_algorithm_t alg,
3534 const uint8_t *hash, size_t hash_length)
3535{
3536 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003537 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003538
Paul Elliott068fe072023-01-16 13:59:15 +00003539 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3540 return PSA_ERROR_NOT_SUPPORTED;
3541 }
3542
3543 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003544 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003545 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003546
3547#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003548 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3549 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003550
Paul Elliotta1c94092023-02-15 16:38:04 +00003551 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3552
Paul Elliott93d9ca82023-02-15 18:14:21 +00003553 /* Ensure num_ops is zero'ed in case of context re-use. */
3554 operation->num_ops = 0;
3555
Paul Elliott068fe072023-01-16 13:59:15 +00003556 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3557 attributes->core.bits,
3558 key_buffer,
3559 key_buffer_size,
3560 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003561
Paul Elliott068fe072023-01-16 13:59:15 +00003562 if (status != PSA_SUCCESS) {
3563 return status;
3564 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003565
Paul Elliott1bc59df2023-02-05 13:41:57 +00003566 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003567 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003568
Paul Elliott068fe072023-01-16 13:59:15 +00003569 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3570 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3571 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003572
Paul Elliott0290a762023-02-09 14:30:24 +00003573 /* We only need to store the same length of hash as the private key size
3574 * here, it would be truncated by the internal implementation anyway. */
3575 required_hash_length = (hash_length < operation->coordinate_bytes ?
3576 hash_length : operation->coordinate_bytes);
3577
Paul Elliottba70ad42023-02-15 18:23:53 +00003578 if (required_hash_length > sizeof(operation->hash)) {
3579 /* Shouldn't happen, but better safe than sorry. */
3580 return PSA_ERROR_CORRUPTION_DETECTED;
3581 }
3582
Paul Elliott0290a762023-02-09 14:30:24 +00003583 memcpy(operation->hash, hash, required_hash_length);
3584 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003585
3586 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587
3588#else
Paul Elliott068fe072023-01-16 13:59:15 +00003589 (void) operation;
3590 (void) key_buffer;
3591 (void) key_buffer_size;
3592 (void) alg;
3593 (void) hash;
3594 (void) hash_length;
3595 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003596 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003597
Paul Elliott068fe072023-01-16 13:59:15 +00003598 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003599#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3600 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3601 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602}
3603
3604psa_status_t mbedtls_psa_sign_hash_complete(
3605 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3606 uint8_t *signature, size_t signature_size,
3607 size_t *signature_length)
3608{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3610 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3611 defined(MBEDTLS_ECP_RESTARTABLE)
3612
Paul Elliotta1c94092023-02-15 16:38:04 +00003613 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003614 mbedtls_mpi r;
3615 mbedtls_mpi s;
3616
Paul Elliott46845252023-02-03 14:59:11 +00003617 mbedtls_mpi_init(&r);
3618 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003619
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003620 /* Ensure max_ops is set to the current value (or default). */
3621 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3622
Paul Elliotta1c94092023-02-15 16:38:04 +00003623 if (signature_size < 2 * operation->coordinate_bytes) {
3624 status = PSA_ERROR_BUFFER_TOO_SMALL;
3625 goto exit;
3626 }
3627
Paul Elliott588f8ed2022-12-02 18:10:26 +00003628 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003629
Paul Elliott588f8ed2022-12-02 18:10:26 +00003630#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3631 status = mbedtls_to_psa_error(
3632 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003633 &r,
3634 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003635 &operation->ctx->d,
3636 operation->hash,
3637 operation->hash_length,
3638 operation->md_alg,
3639 mbedtls_psa_get_random,
3640 MBEDTLS_PSA_RANDOM_STATE,
3641 &operation->restart_ctx));
3642#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003643 status = PSA_ERROR_NOT_SUPPORTED;
3644 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003645#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3646 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003647 status = mbedtls_to_psa_error(
3648 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003649 &r,
3650 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003651 &operation->ctx->d,
3652 operation->hash,
3653 operation->hash_length,
3654 mbedtls_psa_get_random,
3655 MBEDTLS_PSA_RANDOM_STATE,
3656 mbedtls_psa_get_random,
3657 MBEDTLS_PSA_RANDOM_STATE,
3658 &operation->restart_ctx));
3659 }
3660
Paul Elliottf8e5b562023-02-19 18:43:45 +00003661 /* Hide the fact that the restart context only holds a delta of number of
3662 * ops done during the last operation, not an absolute value. */
3663 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3664
Paul Elliott724bd252023-02-08 12:35:08 +00003665 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003666 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003667 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003668 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003669 operation->coordinate_bytes)
3670 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671
3672 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003673 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674 }
3675
3676 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003677 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003679 operation->coordinate_bytes,
3680 operation->coordinate_bytes)
3681 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682
3683 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003684 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003685 }
3686
Paul Elliott1bc59df2023-02-05 13:41:57 +00003687 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688
Paul Elliott724bd252023-02-08 12:35:08 +00003689 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690 }
Paul Elliott724bd252023-02-08 12:35:08 +00003691
3692exit:
3693
3694 mbedtls_mpi_free(&r);
3695 mbedtls_mpi_free(&s);
3696 return status;
3697
Paul Elliott588f8ed2022-12-02 18:10:26 +00003698 #else
3699
3700 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003701 (void) signature;
3702 (void) signature_size;
3703 (void) signature_length;
3704
3705 return PSA_ERROR_NOT_SUPPORTED;
3706
3707#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3708 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3709 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3710}
3711
3712psa_status_t mbedtls_psa_sign_hash_abort(
3713 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3714{
3715
3716#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3717 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3718 defined(MBEDTLS_ECP_RESTARTABLE)
3719
3720 if (operation->ctx) {
3721 mbedtls_ecdsa_free(operation->ctx);
3722 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003723 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724 }
3725
3726 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3727
Paul Elliott93d9ca82023-02-15 18:14:21 +00003728 operation->num_ops = 0;
3729
Paul Elliott588f8ed2022-12-02 18:10:26 +00003730 return PSA_SUCCESS;
3731
3732#else
3733
3734 (void) operation;
3735
3736 return PSA_ERROR_NOT_SUPPORTED;
3737
3738#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3739 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3740 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3741}
3742
3743psa_status_t mbedtls_psa_verify_hash_start(
3744 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3745 const psa_key_attributes_t *attributes,
3746 const uint8_t *key_buffer, size_t key_buffer_size,
3747 psa_algorithm_t alg,
3748 const uint8_t *hash, size_t hash_length,
3749 const uint8_t *signature, size_t signature_length)
3750{
3751 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003752 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003753 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003754
Paul Elliott068fe072023-01-16 13:59:15 +00003755 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3756 return PSA_ERROR_NOT_SUPPORTED;
3757 }
3758
3759 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003760 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003761 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003762
3763#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003764 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3765 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003766
Paul Elliotta1c94092023-02-15 16:38:04 +00003767 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3768 mbedtls_mpi_init(&operation->r);
3769 mbedtls_mpi_init(&operation->s);
3770
Paul Elliott93d9ca82023-02-15 18:14:21 +00003771 /* Ensure num_ops is zero'ed in case of context re-use. */
3772 operation->num_ops = 0;
3773
Paul Elliott068fe072023-01-16 13:59:15 +00003774 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3775 attributes->core.bits,
3776 key_buffer,
3777 key_buffer_size,
3778 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003779
Paul Elliott068fe072023-01-16 13:59:15 +00003780 if (status != PSA_SUCCESS) {
3781 return status;
3782 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783
Paul Elliottc569fc22023-02-10 13:02:54 +00003784 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003785
Paul Elliott1bc59df2023-02-05 13:41:57 +00003786 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003787 return PSA_ERROR_INVALID_SIGNATURE;
3788 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003789
Paul Elliott068fe072023-01-16 13:59:15 +00003790 status = mbedtls_to_psa_error(
3791 mbedtls_mpi_read_binary(&operation->r,
3792 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003793 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003794
Paul Elliott068fe072023-01-16 13:59:15 +00003795 if (status != PSA_SUCCESS) {
3796 return status;
3797 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798
Paul Elliott068fe072023-01-16 13:59:15 +00003799 status = mbedtls_to_psa_error(
3800 mbedtls_mpi_read_binary(&operation->s,
3801 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003802 coordinate_bytes,
3803 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003804
Paul Elliott068fe072023-01-16 13:59:15 +00003805 if (status != PSA_SUCCESS) {
3806 return status;
3807 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003808
Paul Elliott2c9843f2023-02-15 17:32:42 +00003809 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003810
Paul Elliott2c9843f2023-02-15 17:32:42 +00003811 if (status != PSA_SUCCESS) {
3812 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003813 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003814
Paul Elliott0290a762023-02-09 14:30:24 +00003815 /* We only need to store the same length of hash as the private key size
3816 * here, it would be truncated by the internal implementation anyway. */
3817 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3818 coordinate_bytes);
3819
Paul Elliottba70ad42023-02-15 18:23:53 +00003820 if (required_hash_length > sizeof(operation->hash)) {
3821 /* Shouldn't happen, but better safe than sorry. */
3822 return PSA_ERROR_CORRUPTION_DETECTED;
3823 }
3824
Paul Elliott0290a762023-02-09 14:30:24 +00003825 memcpy(operation->hash, hash, required_hash_length);
3826 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003827
3828 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003829#else
Paul Elliott068fe072023-01-16 13:59:15 +00003830 (void) operation;
3831 (void) key_buffer;
3832 (void) key_buffer_size;
3833 (void) alg;
3834 (void) hash;
3835 (void) hash_length;
3836 (void) signature;
3837 (void) signature_length;
3838 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003839 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003840 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841
Paul Elliott068fe072023-01-16 13:59:15 +00003842 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3844 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3845 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846}
3847
3848psa_status_t mbedtls_psa_verify_hash_complete(
3849 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3850{
3851
3852#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3853 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3854 defined(MBEDTLS_ECP_RESTARTABLE)
3855
Paul Elliottf8e5b562023-02-19 18:43:45 +00003856 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3857
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003858 /* Ensure max_ops is set to the current value (or default). */
3859 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3860
Paul Elliottf8e5b562023-02-19 18:43:45 +00003861 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003862 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3863 operation->hash,
3864 operation->hash_length,
3865 &operation->ctx->Q,
3866 &operation->r,
3867 &operation->s,
3868 &operation->restart_ctx));
3869
Paul Elliottf8e5b562023-02-19 18:43:45 +00003870 /* Hide the fact that the restart context only holds a delta of number of
3871 * ops done during the last operation, not an absolute value. */
3872 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3873
3874 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003875#else
3876 (void) operation;
3877
3878 return PSA_ERROR_NOT_SUPPORTED;
3879
3880#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3881 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3882 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3883}
3884
3885psa_status_t mbedtls_psa_verify_hash_abort(
3886 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3887{
3888
3889#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3890 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3891 defined(MBEDTLS_ECP_RESTARTABLE)
3892
3893 if (operation->ctx) {
3894 mbedtls_ecdsa_free(operation->ctx);
3895 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003896 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003897 }
3898
3899 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3900
Paul Elliott93d9ca82023-02-15 18:14:21 +00003901 operation->num_ops = 0;
3902
Paul Elliott588f8ed2022-12-02 18:10:26 +00003903 mbedtls_mpi_free(&operation->r);
3904 mbedtls_mpi_free(&operation->s);
3905
3906 return PSA_SUCCESS;
3907
3908#else
3909 (void) operation;
3910
3911 return PSA_ERROR_NOT_SUPPORTED;
3912
3913#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3914 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3915 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3916}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003917
mohammad1603503973b2018-03-12 15:59:30 +02003918/****************************************************************/
3919/* Symmetric cryptography */
3920/****************************************************************/
3921
Gilles Peskine449bd832023-01-11 14:50:10 +01003922static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3923 mbedtls_svc_key_id_t key,
3924 psa_algorithm_t alg,
3925 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003926{
3927 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3928 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003929 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3931 PSA_KEY_USAGE_ENCRYPT :
3932 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003933
3934 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003936 status = PSA_ERROR_BAD_STATE;
3937 goto exit;
3938 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003939
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003941 status = PSA_ERROR_INVALID_ARGUMENT;
3942 goto exit;
3943 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003944
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3946 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003947 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003949
Ronald Cron49fafa92021-03-10 08:34:23 +01003950 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003951 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003952 * so we only set it (in the driver wrapper) after resources have been
3953 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003954 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003955 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003956 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003958 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003959 }
3960 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003961
Ronald Crona4af55f2020-12-14 14:36:06 +01003962 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003964 };
3965
Ronald Cronab99ac22020-10-01 16:38:28 +02003966 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 if (cipher_operation == MBEDTLS_ENCRYPT) {
3968 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3969 &attributes,
3970 slot->key.data,
3971 slot->key.bytes,
3972 alg);
3973 } else {
3974 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3975 &attributes,
3976 slot->key.data,
3977 slot->key.bytes,
3978 alg);
3979 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003980
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003981exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 if (status != PSA_SUCCESS) {
3983 psa_cipher_abort(operation);
3984 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003987
Gilles Peskine449bd832023-01-11 14:50:10 +01003988 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003989}
3990
Gilles Peskine449bd832023-01-11 14:50:10 +01003991psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3992 mbedtls_svc_key_id_t key,
3993 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003994{
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003996}
3997
Gilles Peskine449bd832023-01-11 14:50:10 +01003998psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3999 mbedtls_svc_key_id_t key,
4000 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004001{
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004003}
4004
Gilles Peskine449bd832023-01-11 14:50:10 +01004005psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4006 uint8_t *iv,
4007 size_t iv_size,
4008 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004009{
Ronald Cron6d051732020-10-01 14:10:20 +02004010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004011 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4012 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004013
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004015 status = PSA_ERROR_BAD_STATE;
4016 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004017 }
4018
Gilles Peskine449bd832023-01-11 14:50:10 +01004019 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004020 status = PSA_ERROR_BAD_STATE;
4021 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004022 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004023
Ronald Cron2fb90522021-07-05 12:31:44 +02004024 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004026 status = PSA_ERROR_BUFFER_TOO_SMALL;
4027 goto exit;
4028 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004031 status = PSA_ERROR_GENERIC_ERROR;
4032 goto exit;
4033 }
4034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 status = psa_generate_random(local_iv, default_iv_length);
4036 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004037 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 }
Ronald Cron5618a392021-03-26 09:52:26 +01004039
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 status = psa_driver_wrapper_cipher_set_iv(operation,
4041 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004042
4043exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 if (status == PSA_SUCCESS) {
4045 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004046 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004047 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004049 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004051 }
Ronald Cron6d051732020-10-01 14:10:20 +02004052
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004054}
4055
Gilles Peskine449bd832023-01-11 14:50:10 +01004056psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4057 const uint8_t *iv,
4058 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004059{
Ronald Cron6d051732020-10-01 14:10:20 +02004060 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004061
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004063 status = PSA_ERROR_BAD_STATE;
4064 goto exit;
4065 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004066
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004068 status = PSA_ERROR_BAD_STATE;
4069 goto exit;
4070 }
Ronald Crona0d68172021-03-26 10:15:08 +01004071
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004073 status = PSA_ERROR_INVALID_ARGUMENT;
4074 goto exit;
4075 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 status = psa_driver_wrapper_cipher_set_iv(operation,
4078 iv,
4079 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004080
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004081exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004083 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 } else {
4085 psa_cipher_abort(operation);
4086 }
4087 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004088}
4089
Gilles Peskine449bd832023-01-11 14:50:10 +01004090psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4091 const uint8_t *input,
4092 size_t input_length,
4093 uint8_t *output,
4094 size_t output_size,
4095 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004096{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004098
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004100 status = PSA_ERROR_BAD_STATE;
4101 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004102 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004105 status = PSA_ERROR_BAD_STATE;
4106 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004107 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 status = psa_driver_wrapper_cipher_update(operation,
4110 input,
4111 input_length,
4112 output,
4113 output_size,
4114 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004115
4116exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 if (status != PSA_SUCCESS) {
4118 psa_cipher_abort(operation);
4119 }
Ronald Cron6d051732020-10-01 14:10:20 +02004120
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004122}
4123
Gilles Peskine449bd832023-01-11 14:50:10 +01004124psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4125 uint8_t *output,
4126 size_t output_size,
4127 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004128{
David Saadab4ecc272019-02-14 13:48:10 +02004129 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004132 status = PSA_ERROR_BAD_STATE;
4133 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004134 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004137 status = PSA_ERROR_BAD_STATE;
4138 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004139 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 status = psa_driver_wrapper_cipher_finish(operation,
4142 output,
4143 output_size,
4144 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004145
4146exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 if (status == PSA_SUCCESS) {
4148 return psa_cipher_abort(operation);
4149 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004150 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004154 }
mohammad1603503973b2018-03-12 15:59:30 +02004155}
4156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004158{
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004160 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004161 * in use. It's ok to call abort on such an object, and there's
4162 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004164 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004167
Ronald Cron49fafa92021-03-10 08:34:23 +01004168 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004169 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004170 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004173}
4174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4176 psa_algorithm_t alg,
4177 const uint8_t *input,
4178 size_t input_length,
4179 uint8_t *output,
4180 size_t output_size,
4181 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004182{
4183 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004184 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004185 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004186 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4187 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004190 status = PSA_ERROR_INVALID_ARGUMENT;
4191 goto exit;
4192 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4195 PSA_KEY_USAGE_ENCRYPT,
4196 alg);
4197 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004198 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004200
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004201 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004203 };
4204
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4206 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004207 status = PSA_ERROR_GENERIC_ERROR;
4208 goto exit;
4209 }
4210
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 if (default_iv_length > 0) {
4212 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004213 status = PSA_ERROR_BUFFER_TOO_SMALL;
4214 goto exit;
4215 }
4216
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 status = psa_generate_random(local_iv, default_iv_length);
4218 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004219 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004221 }
4222
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004223 status = psa_driver_wrapper_cipher_encrypt(
4224 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004225 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004226 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004228
4229exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 unlock_status = psa_unlock_key_slot(slot);
4231 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004232 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004233 }
Ronald Cron23919522021-07-08 19:00:07 +02004234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 if (status == PSA_SUCCESS) {
4236 if (default_iv_length > 0) {
4237 memcpy(output, local_iv, default_iv_length);
4238 }
4239 *output_length += default_iv_length;
4240 } else {
4241 *output_length = 0;
4242 }
4243
4244 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004245}
4246
Gilles Peskine449bd832023-01-11 14:50:10 +01004247psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4248 psa_algorithm_t alg,
4249 const uint8_t *input,
4250 size_t input_length,
4251 uint8_t *output,
4252 size_t output_size,
4253 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004254{
4255 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004256 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004257 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004258
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004260 status = PSA_ERROR_INVALID_ARGUMENT;
4261 goto exit;
4262 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4265 PSA_KEY_USAGE_DECRYPT,
4266 alg);
4267 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004268 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004270
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004271 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004273 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4276 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004277 status = PSA_ERROR_INVALID_ARGUMENT;
4278 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004280 status = PSA_ERROR_INVALID_ARGUMENT;
4281 goto exit;
4282 }
4283
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004284 status = psa_driver_wrapper_cipher_decrypt(
4285 &attributes, slot->key.data, slot->key.bytes,
4286 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004288
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004289exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 unlock_status = psa_unlock_key_slot(slot);
4291 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004292 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004294
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004296 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 }
Ronald Cron23919522021-07-08 19:00:07 +02004298
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004300}
4301
4302
mohammad16035955c982018-04-26 00:53:03 +03004303/****************************************************************/
4304/* AEAD */
4305/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004306
Paul Elliottbaff51c2021-09-28 17:44:45 +01004307/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004308static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004309{
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004311}
4312
4313/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004314static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4315 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004316{
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004318
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004320#if defined(PSA_WANT_ALG_GCM)
4321 case PSA_ALG_GCM:
4322 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 * arbitrarily large nonces. Please note that we do not generally
4324 * recommend the usage of nonces of greater length than
4325 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4326 * size, which can then lead to collisions if you encrypt a very
4327 * large number of messages.*/
4328 if (nonce_length != 0) {
4329 return PSA_SUCCESS;
4330 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004331 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004332#endif /* PSA_WANT_ALG_GCM */
4333#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004334 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 if (nonce_length >= 7 && nonce_length <= 13) {
4336 return PSA_SUCCESS;
4337 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004338 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004339#endif /* PSA_WANT_ALG_CCM */
4340#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004341 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 if (nonce_length == 12) {
4343 return PSA_SUCCESS;
4344 } else if (nonce_length == 8) {
4345 return PSA_ERROR_NOT_SUPPORTED;
4346 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004347 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004348#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004349 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004350 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004352 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004355}
4356
Gilles Peskine449bd832023-01-11 14:50:10 +01004357static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004358{
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4360 return PSA_ERROR_INVALID_ARGUMENT;
4361 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004364}
4365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4367 psa_algorithm_t alg,
4368 const uint8_t *nonce,
4369 size_t nonce_length,
4370 const uint8_t *additional_data,
4371 size_t additional_data_length,
4372 const uint8_t *plaintext,
4373 size_t plaintext_length,
4374 uint8_t *ciphertext,
4375 size_t ciphertext_size,
4376 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004377{
Ronald Cron215633c2021-03-16 17:15:37 +01004378 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4379 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004380
mohammad1603f08a5502018-06-03 15:05:47 +03004381 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004382
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 status = psa_aead_check_algorithm(alg);
4384 if (status != PSA_SUCCESS) {
4385 return status;
4386 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004387
Ronald Cron9a986162021-03-26 12:40:07 +01004388 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4390 if (status != PSA_SUCCESS) {
4391 return status;
4392 }
mohammad16035955c982018-04-26 00:53:03 +03004393
Ronald Cron215633c2021-03-16 17:15:37 +01004394 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004396 };
mohammad16035955c982018-04-26 00:53:03 +03004397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 status = psa_aead_check_nonce_length(alg, nonce_length);
4399 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004400 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004402
Ronald Cronde822812021-03-17 16:08:20 +01004403 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004404 &attributes, slot->key.data, slot->key.bytes,
4405 alg,
4406 nonce, nonce_length,
4407 additional_data, additional_data_length,
4408 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004410
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4412 memset(ciphertext, 0, ciphertext_size);
4413 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004414
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004415exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004419}
4420
Gilles Peskine449bd832023-01-11 14:50:10 +01004421psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4422 psa_algorithm_t alg,
4423 const uint8_t *nonce,
4424 size_t nonce_length,
4425 const uint8_t *additional_data,
4426 size_t additional_data_length,
4427 const uint8_t *ciphertext,
4428 size_t ciphertext_length,
4429 uint8_t *plaintext,
4430 size_t plaintext_size,
4431 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004432{
Ronald Cron215633c2021-03-16 17:15:37 +01004433 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4434 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004435
Gilles Peskineee652a32018-06-01 19:23:52 +02004436 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004437
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 status = psa_aead_check_algorithm(alg);
4439 if (status != PSA_SUCCESS) {
4440 return status;
4441 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004442
Ronald Cron9a986162021-03-26 12:40:07 +01004443 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4445 if (status != PSA_SUCCESS) {
4446 return status;
4447 }
mohammad16035955c982018-04-26 00:53:03 +03004448
Ronald Cron215633c2021-03-16 17:15:37 +01004449 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004451 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 status = psa_aead_check_nonce_length(alg, nonce_length);
4454 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004455 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004457
Ronald Cronde822812021-03-17 16:08:20 +01004458 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004459 &attributes, slot->key.data, slot->key.bytes,
4460 alg,
4461 nonce, nonce_length,
4462 additional_data, additional_data_length,
4463 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004465
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 if (status != PSA_SUCCESS && plaintext_size != 0) {
4467 memset(plaintext, 0, plaintext_size);
4468 }
mohammad160360a64d02018-06-03 17:20:42 +03004469
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004470exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 return status;
mohammad16035955c982018-04-26 00:53:03 +03004474}
4475
Gilles Peskine449bd832023-01-11 14:50:10 +01004476static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4477{
4478 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004481#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004483 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4485 return PSA_ERROR_INVALID_ARGUMENT;
4486 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004487 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004488#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004489
Przemek Stekiel86679c72022-10-06 17:06:56 +02004490#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004492 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4494 return PSA_ERROR_INVALID_ARGUMENT;
4495 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004496 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004497#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004498
Przemek Stekiel86679c72022-10-06 17:06:56 +02004499#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004501 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 if (tag_len != 16) {
4503 return PSA_ERROR_INVALID_ARGUMENT;
4504 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004505 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004506#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004507
4508 default:
4509 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004511 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004513}
4514
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004515/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004516static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4517 int is_encrypt,
4518 mbedtls_svc_key_id_t key,
4519 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004520{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004521 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4522 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4523 psa_key_slot_t *slot = NULL;
4524 psa_key_usage_t key_usage = 0;
4525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 status = psa_aead_check_algorithm(alg);
4527 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004528 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004530
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004532 status = PSA_ERROR_BAD_STATE;
4533 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004534 }
4535
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 if (operation->nonce_set || operation->lengths_set ||
4537 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004538 status = PSA_ERROR_BAD_STATE;
4539 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004540 }
4541
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004543 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004545 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004547
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4549 alg);
4550 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004551 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004553
4554 psa_key_attributes_t attributes = {
4555 .core = slot->attr
4556 };
4557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004559 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004561
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 if (is_encrypt) {
4563 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4564 &attributes,
4565 slot->key.data,
4566 slot->key.bytes,
4567 alg);
4568 } else {
4569 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4570 &attributes,
4571 slot->key.data,
4572 slot->key.bytes,
4573 alg);
4574 }
4575 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004576 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004580
4581exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004583
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004585 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004587 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 } else {
4589 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004590 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004593}
4594
Paul Elliott302ff6b2021-04-20 18:10:30 +01004595/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004596psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4597 mbedtls_svc_key_id_t key,
4598 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004599{
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004601}
4602
4603/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004604psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4605 mbedtls_svc_key_id_t key,
4606 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004607{
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004609}
4610
4611/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004612psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4613 uint8_t *nonce,
4614 size_t nonce_size,
4615 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004616{
4617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004618 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004619 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004620
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004621 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004624 status = PSA_ERROR_BAD_STATE;
4625 goto exit;
4626 }
4627
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004629 status = PSA_ERROR_BAD_STATE;
4630 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004631 }
4632
Paul Elliotte193ea82021-10-01 13:00:16 +01004633 /* For CCM, this size may not be correct according to the PSA
4634 * specification. The PSA Crypto 1.0.1 specification states:
4635 *
4636 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4637 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4638 *
4639 * However this restriction that L has to be the smallest integer is not
4640 * applied in practice, and it is not implementable here since the
4641 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4643 operation->alg);
4644 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004645 status = PSA_ERROR_BUFFER_TOO_SMALL;
4646 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004647 }
4648
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 status = psa_generate_random(local_nonce, required_nonce_size);
4650 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004651 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004655
Paul Elliott39dc6b82021-05-11 19:16:09 +01004656exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if (status == PSA_SUCCESS) {
4658 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004659 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 } else {
4661 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004662 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004665}
4666
4667/* Set the nonce for a multipart authenticated encryption or decryption
4668 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004669psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4670 const uint8_t *nonce,
4671 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004672{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004673 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004676 status = PSA_ERROR_BAD_STATE;
4677 goto exit;
4678 }
4679
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004681 status = PSA_ERROR_BAD_STATE;
4682 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004683 }
4684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4686 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004687 status = PSA_ERROR_INVALID_ARGUMENT;
4688 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004689 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4692 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004693
Paul Elliott39dc6b82021-05-11 19:16:09 +01004694exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004696 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 } else {
4698 psa_aead_abort(operation);
4699 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004702}
4703
4704/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004705psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4706 size_t ad_length,
4707 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004708{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004709 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004712 status = PSA_ERROR_BAD_STATE;
4713 goto exit;
4714 }
4715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (operation->lengths_set || operation->ad_started ||
4717 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004718 status = PSA_ERROR_BAD_STATE;
4719 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004720 }
4721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004723#if defined(PSA_WANT_ALG_GCM)
4724 case PSA_ALG_GCM:
4725 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 * bits. Without the guard this code will generate warnings on 32bit
4727 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004728#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 if (((uint64_t) ad_length) >> 61 != 0 ||
4730 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004731 status = PSA_ERROR_INVALID_ARGUMENT;
4732 goto exit;
4733 }
Paul Elliott325d3742021-09-27 17:56:28 +01004734#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004735 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004736#endif /* PSA_WANT_ALG_GCM */
4737#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004738 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004740 status = PSA_ERROR_INVALID_ARGUMENT;
4741 goto exit;
4742 }
4743 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004744#endif /* PSA_WANT_ALG_CCM */
4745#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004746 case PSA_ALG_CHACHA20_POLY1305:
4747 /* No length restrictions for ChaChaPoly. */
4748 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004749#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004750 default:
4751 break;
4752 }
Paul Elliott325d3742021-09-27 17:56:28 +01004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4755 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004756
Paul Elliott39dc6b82021-05-11 19:16:09 +01004757exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004759 operation->ad_remaining = ad_length;
4760 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004761 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 } else {
4763 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004764 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004765
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004767}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004768
4769/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004770psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4771 const uint8_t *input,
4772 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004773{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4775
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004777 status = PSA_ERROR_BAD_STATE;
4778 goto exit;
4779 }
4780
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004782 status = PSA_ERROR_BAD_STATE;
4783 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004784 }
4785
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (operation->lengths_set) {
4787 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004788 status = PSA_ERROR_INVALID_ARGUMENT;
4789 goto exit;
4790 }
4791
4792 operation->ad_remaining -= input_length;
4793 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004794#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004796 status = PSA_ERROR_BAD_STATE;
4797 goto exit;
4798 }
4799#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 status = psa_driver_wrapper_aead_update_ad(operation, input,
4802 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004803
Paul Elliott39dc6b82021-05-11 19:16:09 +01004804exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004806 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 } else {
4808 psa_aead_abort(operation);
4809 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004810
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004812}
4813
4814/* Encrypt or decrypt a message fragment in an active multipart AEAD
4815 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004816psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4817 const uint8_t *input,
4818 size_t input_length,
4819 uint8_t *output,
4820 size_t output_size,
4821 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004822{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004823 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004824
4825 *output_length = 0;
4826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004828 status = PSA_ERROR_BAD_STATE;
4829 goto exit;
4830 }
4831
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004833 status = PSA_ERROR_BAD_STATE;
4834 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004835 }
4836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004838 /* Additional data length was supplied, but not all the additional
4839 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004841 status = PSA_ERROR_INVALID_ARGUMENT;
4842 goto exit;
4843 }
4844
4845 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004847 status = PSA_ERROR_INVALID_ARGUMENT;
4848 goto exit;
4849 }
4850
4851 operation->body_remaining -= input_length;
4852 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004853#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004855 status = PSA_ERROR_BAD_STATE;
4856 goto exit;
4857 }
4858#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4861 output, output_size,
4862 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004863
Paul Elliott39dc6b82021-05-11 19:16:09 +01004864exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004866 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 } else {
4868 psa_aead_abort(operation);
4869 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004872}
4873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004875{
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 if (operation->id == 0 || !operation->nonce_set) {
4877 return PSA_ERROR_BAD_STATE;
4878 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4881 operation->body_remaining != 0)) {
4882 return PSA_ERROR_INVALID_ARGUMENT;
4883 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004886}
4887
Paul Elliott302ff6b2021-04-20 18:10:30 +01004888/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004889psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4890 uint8_t *ciphertext,
4891 size_t ciphertext_size,
4892 size_t *ciphertext_length,
4893 uint8_t *tag,
4894 size_t tag_size,
4895 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004896{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4898
Paul Elliott302ff6b2021-04-20 18:10:30 +01004899 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004900 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 status = psa_aead_final_checks(operation);
4903 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004904 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004906
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004908 status = PSA_ERROR_BAD_STATE;
4909 goto exit;
4910 }
4911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4913 ciphertext_size,
4914 ciphertext_length,
4915 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004916
Paul Elliott39dc6b82021-05-11 19:16:09 +01004917exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004918
4919
Paul Elliottf47b0952021-05-21 18:02:33 +01004920 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004921 * the zero tag size, make sure the tag is set to something implausible.
4922 * Even if the operation succeeds, make sure we clear the rest of the
4923 * buffer to prevent potential leakage of anything previously placed in
4924 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004925 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004930}
4931
4932/* Finish authenticating and decrypting a message in a multipart AEAD
4933 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004934psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4935 uint8_t *plaintext,
4936 size_t plaintext_size,
4937 size_t *plaintext_length,
4938 const uint8_t *tag,
4939 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004940{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4942
Paul Elliott302ff6b2021-04-20 18:10:30 +01004943 *plaintext_length = 0;
4944
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 status = psa_aead_final_checks(operation);
4946 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004947 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004951 status = PSA_ERROR_BAD_STATE;
4952 goto exit;
4953 }
4954
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4956 plaintext_size,
4957 plaintext_length,
4958 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004959
4960exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004962
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004964}
4965
4966/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004967psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004968{
4969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4970
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004972 /* The object has (apparently) been initialized but it is not (yet)
4973 * in use. It's ok to call abort on such an object, and there's
4974 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004976 }
4977
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004979
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983}
4984
Gilles Peskinee59236f2018-01-27 23:32:46 +01004985/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004986/* Generators */
4987/****************************************************************/
4988
Przemek Stekiel69c46792022-06-10 12:59:51 +02004989#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004990 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004991 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05304992 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
4993 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
John Durkop07cc04a2020-11-16 22:08:34 -08004994#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004995#endif /* At least one builtin KDF */
4996
Przemek Stekiel69c46792022-06-10 12:59:51 +02004997#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004998 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4999 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5000static psa_status_t psa_key_derivation_start_hmac(
5001 psa_mac_operation_t *operation,
5002 psa_algorithm_t hash_alg,
5003 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005005{
5006 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5009 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5010 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005011
Steven Cooreman72f736a2021-05-07 14:14:37 +02005012 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005014
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 status = psa_driver_wrapper_mac_sign_setup(operation,
5016 &attributes,
5017 hmac_key, hmac_key_length,
5018 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005019
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 psa_reset_key_attributes(&attributes);
5021 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005022}
5023#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005024
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005025#define HKDF_STATE_INIT 0 /* no input yet */
5026#define HKDF_STATE_STARTED 1 /* got salt */
5027#define HKDF_STATE_KEYED 2 /* got key */
5028#define HKDF_STATE_OUTPUT 3 /* output started */
5029
Gilles Peskinecbe66502019-05-16 16:59:18 +02005030static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005032{
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5034 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5035 } else {
5036 return operation->alg;
5037 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005038}
5039
Gilles Peskine449bd832023-01-11 14:50:10 +01005040psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005041{
5042 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5044 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005045 /* The object has (apparently) been initialized but it is not
5046 * in use. It's ok to call abort on such an object, and there's
5047 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005049#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5051 mbedtls_free(operation->ctx.hkdf.info);
5052 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5053 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005054#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005055#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5056 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5058 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5059 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5060 if (operation->ctx.tls12_prf.secret != NULL) {
5061 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5062 operation->ctx.tls12_prf.secret_length);
5063 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005064 }
5065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 if (operation->ctx.tls12_prf.seed != NULL) {
5067 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5068 operation->ctx.tls12_prf.seed_length);
5069 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005070 }
5071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 if (operation->ctx.tls12_prf.label != NULL) {
5073 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5074 operation->ctx.tls12_prf.label_length);
5075 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005076 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005077#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 if (operation->ctx.tls12_prf.other_secret != NULL) {
5079 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5080 operation->ctx.tls12_prf.other_secret_length);
5081 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005082 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005083#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005084 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005085
5086 /* We leave the fields Ai and output_block to be erased safely by the
5087 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005089#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5090 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005091#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5093 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5094 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5095 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005096#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005097 {
5098 status = PSA_ERROR_BAD_STATE;
5099 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 mbedtls_platform_zeroize(operation, sizeof(*operation));
5101 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005102}
5103
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005104psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005106{
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005110 }
5111
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005114}
5115
Gilles Peskine449bd832023-01-11 14:50:10 +01005116psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5117 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005118{
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 if (operation->alg == 0) {
5120 return PSA_ERROR_BAD_STATE;
5121 }
5122 if (capacity > operation->capacity) {
5123 return PSA_ERROR_INVALID_ARGUMENT;
5124 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005127}
5128
Przemek Stekiel69c46792022-06-10 12:59:51 +02005129#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005130/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005131static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5132 psa_algorithm_t kdf_alg,
5133 uint8_t *output,
5134 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005135{
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5137 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005138 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005139 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005140#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005142#else
5143 const uint8_t last_block = 0xff;
5144#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 if (hkdf->state < HKDF_STATE_KEYED ||
5147 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005148#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005150#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 )) {
5152 return PSA_ERROR_BAD_STATE;
5153 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005154 hkdf->state = HKDF_STATE_OUTPUT;
5155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005157 /* Copy what remains of the current block */
5158 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005160 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 }
5162 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005163 output += n;
5164 output_length -= n;
5165 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005167 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005169 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005170 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005171 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005172 * object was corrupted or if this function is called directly
5173 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 if (hkdf->block_number == last_block) {
5175 return PSA_ERROR_BAD_STATE;
5176 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005177
5178 /* We need a new block */
5179 ++hkdf->block_number;
5180 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5183 hash_alg,
5184 hkdf->prk,
5185 hash_length);
5186 if (status != PSA_SUCCESS) {
5187 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005188 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005189
5190 if (hkdf->block_number != 1) {
5191 status = psa_mac_update(&hkdf->hmac,
5192 hkdf->output_block,
5193 hash_length);
5194 if (status != PSA_SUCCESS) {
5195 return status;
5196 }
5197 }
5198 status = psa_mac_update(&hkdf->hmac,
5199 hkdf->info,
5200 hkdf->info_length);
5201 if (status != PSA_SUCCESS) {
5202 return status;
5203 }
5204 status = psa_mac_update(&hkdf->hmac,
5205 &hkdf->block_number, 1);
5206 if (status != PSA_SUCCESS) {
5207 return status;
5208 }
5209 status = psa_mac_sign_finish(&hkdf->hmac,
5210 hkdf->output_block,
5211 sizeof(hkdf->output_block),
5212 &hmac_output_length);
5213 if (status != PSA_SUCCESS) {
5214 return status;
5215 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005216 }
5217
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005219}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005220#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005221
John Durkop07cc04a2020-11-16 22:08:34 -08005222#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5223 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005224static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5225 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005227{
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5229 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005230 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5231 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005232 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005233
Janos Follath7742fee2019-06-17 12:58:10 +01005234 /* We can't be wanting more output after block 0xff, otherwise
5235 * the capacity check in psa_key_derivation_output_bytes() would have
5236 * prevented this call. It could happen only if the operation
5237 * object was corrupted or if this function is called directly
5238 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 if (tls12_prf->block_number == 0xff) {
5240 return PSA_ERROR_CORRUPTION_DETECTED;
5241 }
Janos Follath7742fee2019-06-17 12:58:10 +01005242
5243 /* We need a new block */
5244 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005245 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005246
5247 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5248 *
5249 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5250 *
5251 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5252 * HMAC_hash(secret, A(2) + seed) +
5253 * HMAC_hash(secret, A(3) + seed) + ...
5254 *
5255 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005256 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005257 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005258 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005259 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005260 * is currently extracted as `output_block` and where i is
5261 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005262 */
5263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 status = psa_key_derivation_start_hmac(&hmac,
5265 hash_alg,
5266 tls12_prf->secret,
5267 tls12_prf->secret_length);
5268 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005269 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005271
5272 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005274 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5275 * the variable seed and in this instance means it in the context of the
5276 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 status = psa_mac_update(&hmac,
5278 tls12_prf->label,
5279 tls12_prf->label_length);
5280 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005281 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 }
5283 status = psa_mac_update(&hmac,
5284 tls12_prf->seed,
5285 tls12_prf->seed_length);
5286 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005287 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 }
5289 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005290 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5292 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005293 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005295 }
5296
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 status = psa_mac_sign_finish(&hmac,
5298 tls12_prf->Ai, hash_length,
5299 &hmac_output_length);
5300 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005301 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 }
5303 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005304 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005306
5307 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 status = psa_key_derivation_start_hmac(&hmac,
5309 hash_alg,
5310 tls12_prf->secret,
5311 tls12_prf->secret_length);
5312 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005313 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 }
5315 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5316 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005317 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 }
5319 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5320 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005321 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 }
5323 status = psa_mac_update(&hmac, tls12_prf->seed, 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 status = psa_mac_sign_finish(&hmac,
5328 tls12_prf->output_block, hash_length,
5329 &hmac_output_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
Janos Follath7742fee2019-06-17 12:58:10 +01005334
5335cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 cleanup_status = psa_mac_abort(&hmac);
5337 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005338 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005340
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005342}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005343
Janos Follath844eb0e2019-06-19 12:10:49 +01005344static psa_status_t psa_key_derivation_tls12_prf_read(
5345 psa_tls12_prf_key_derivation_t *tls12_prf,
5346 psa_algorithm_t alg,
5347 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005349{
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5351 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005352 psa_status_t status;
5353 uint8_t offset, length;
5354
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005356 case PSA_TLS12_PRF_STATE_LABEL_SET:
5357 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5358 break;
5359 case PSA_TLS12_PRF_STATE_OUTPUT:
5360 break;
5361 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005363 }
5364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005366 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 if (tls12_prf->left_in_block == 0) {
5368 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5369 alg);
5370 if (status != PSA_SUCCESS) {
5371 return status;
5372 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005373
5374 continue;
5375 }
5376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005378 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005380 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005382
5383 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005385 output += length;
5386 output_length -= length;
5387 tls12_prf->left_in_block -= length;
5388 }
5389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005391}
John Durkop07cc04a2020-11-16 22:08:34 -08005392#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5393 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005394
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005395#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5396static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5397 psa_tls12_ecjpake_to_pms_t *ecjpake,
5398 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005400{
5401 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005402 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 if (output_length != 32) {
5405 return PSA_ERROR_INVALID_ARGUMENT;
5406 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5409 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5410 &output_size);
5411 if (status != PSA_SUCCESS) {
5412 return status;
5413 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 if (output_size != output_length) {
5416 return PSA_ERROR_GENERIC_ERROR;
5417 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005418
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005420}
5421#endif
5422
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005423psa_status_t psa_key_derivation_output_bytes(
5424 psa_key_derivation_operation_t *operation,
5425 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005427{
5428 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005430
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005432 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005434 }
5435
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005438 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005440 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005441 goto exit;
5442 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005444 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005445 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005446 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5447 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005448 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005449 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005451 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005453
Przemek Stekiel69c46792022-06-10 12:59:51 +02005454#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5456 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5457 output, output_length);
5458 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005459#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005460#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5461 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5463 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5464 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5465 kdf_alg, output,
5466 output_length);
5467 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005468#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5469 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005470#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005472 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5474 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005475#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5476
Gilles Peskineeab56e42018-07-12 17:12:33 +02005477 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005478 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005480 }
5481
5482exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005484 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005485 * This allows us to differentiate between exhausted operations and
5486 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5487 * operations. */
5488 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005490 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005492 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005494}
5495
David Brown7807bf72021-02-09 16:28:23 -07005496#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005497static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005498{
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 if (data_size >= 8) {
5500 mbedtls_des_key_set_parity(data);
5501 }
5502 if (data_size >= 16) {
5503 mbedtls_des_key_set_parity(data + 8);
5504 }
5505 if (data_size >= 24) {
5506 mbedtls_des_key_set_parity(data + 16);
5507 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005508}
David Brown7807bf72021-02-09 16:28:23 -07005509#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005510
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005511/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 * ECC keys on a Weierstrass elliptic curve require the generation
5513 * of a private key which is an integer
5514 * in the range [1, N - 1], where N is the boundary of the private key domain:
5515 * N is the prime p for Diffie-Hellman, or the order of the
5516 * curve’s base point for ECC.
5517 *
5518 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5519 * This function generates the private key using the following process:
5520 *
5521 * 1. Draw a byte string of length ceiling(m/8) bytes.
5522 * 2. If m is not a multiple of 8, set the most significant
5523 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5524 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5525 * 4. If k > N - 2, discard the result and return to step 1.
5526 * 5. Output k + 1 as the private key.
5527 *
5528 * This method allows compliance to NIST standards, specifically the methods titled
5529 * Key-Pair Generation by Testing Candidates in the following publications:
5530 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5531 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5532 * Diffie-Hellman keys.
5533 *
5534 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5535 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5536 *
5537 * Note: Function allocates memory for *data buffer, so given *data should be
5538 * always NULL.
5539 */
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005540#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5541 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005542 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5543 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5544 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005545static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005546 psa_key_slot_t *slot,
5547 size_t bits,
5548 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005549 uint8_t **data
5550 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005551{
Valerio Setti52789862023-04-07 12:13:11 +02005552#if defined(MBEDTLS_ECP_LIGHT)
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005553 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005554 mbedtls_mpi k;
5555 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005558
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 mbedtls_mpi_init(&k);
5560 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005561
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005562 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005564 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005566
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005568 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005569 goto cleanup;
5570 }
5571
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005572 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005574
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005576
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005577 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005578 /* Let m be the bit size of N. */
5579 size_t m = ecp_group.nbits;
5580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005582
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005583 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005585
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005586 /* Note: This function is always called with *data == NULL and it
5587 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 *data = mbedtls_calloc(1, m_bytes);
5589 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005590 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005591 goto cleanup;
5592 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005593
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005595 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005597 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005599
5600 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005602 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005603 * (8 * ceiling(m/8) - m) bits of the first byte in
5604 * the string to zero.
5605 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005607 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005608 }
5609
5610 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 * big-endian byte string.
5612 */
5613 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005614
5615 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 * Result of comparison is returned. When it indicates error
5617 * then this function is called again.
5618 */
5619 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005620 }
5621
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005622 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5624 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005625cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 if (ret != 0) {
5627 status = mbedtls_to_psa_error(ret);
5628 }
5629 if (status != PSA_SUCCESS) {
5630 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005631 *data = NULL;
5632 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 mbedtls_mpi_free(&k);
5634 mbedtls_mpi_free(&diff_N_2);
5635 return status;
Valerio Setti52789862023-04-07 12:13:11 +02005636#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard38316372023-03-17 12:18:32 +01005637 (void) slot;
5638 (void) bits;
5639 (void) operation;
5640 (void) data;
5641 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti52789862023-04-07 12:13:11 +02005642#endif /* MBEDTLS_ECP_LIGHT */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005643}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005644
5645/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5646 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5647 *
5648 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5649 * draw a 32-byte string and process it as specified in
5650 * Elliptic Curves for Security [RFC7748] §5.
5651 *
5652 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5653 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5654 *
5655 * Note: Function allocates memory for *data buffer, so given *data should be
5656 * always NULL.
5657 */
5658
5659static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5660 size_t bits,
5661 psa_key_derivation_operation_t *operation,
5662 uint8_t **data
5663 )
5664{
5665 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005667
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005669 case 255:
5670 output_length = 32;
5671 break;
5672 case 448:
5673 output_length = 56;
5674 break;
5675 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005677 break;
5678 }
5679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 if (*data == NULL) {
5683 return PSA_ERROR_INSUFFICIENT_MEMORY;
5684 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005689 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005693 case 255:
5694 (*data)[0] &= 248;
5695 (*data)[31] &= 127;
5696 (*data)[31] |= 64;
5697 break;
5698 case 448:
5699 (*data)[0] &= 252;
5700 (*data)[55] |= 128;
5701 break;
5702 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005704 break;
5705 }
5706
5707 return status;
5708}
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005709#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5710 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005711 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5712 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5713 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005714
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005715static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005716 psa_key_slot_t *slot,
5717 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005719{
5720 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305722 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005723 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5726 return PSA_ERROR_INVALID_ARGUMENT;
5727 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005728
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005729#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) || \
5730 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || \
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005731 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5732 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5733 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5735 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5736 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005737 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5739 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005740 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 }
5742 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005743 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5745 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005746 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005748 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005749 } else
Joakim Anderssonbb576fe2023-03-01 11:23:02 +01005750#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) ||
5751 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) ||
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005752 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5753 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5754 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 if (key_type_is_raw_bytes(slot->attr.type)) {
5756 if (bits % 8 != 0) {
5757 return PSA_ERROR_INVALID_ARGUMENT;
5758 }
5759 data = mbedtls_calloc(1, bytes);
5760 if (data == NULL) {
5761 return PSA_ERROR_INSUFFICIENT_MEMORY;
5762 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 status = psa_key_derivation_output_bytes(operation, data, bytes);
5765 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005766 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 }
David Brown12ca5032021-02-11 11:02:00 -07005768#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5770 psa_des_set_key_parity(data, bytes);
5771 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005772#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 } else {
5774 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005775 }
Ronald Crondd04d422020-11-28 15:14:42 +01005776
Ronald Cronbf33c932020-11-28 18:06:53 +01005777 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005778 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005780 };
5781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5783 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5784 &storage_size);
5785 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305786 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 }
Archanad8a83dc2021-06-14 10:04:16 +05305788 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 status = psa_allocate_buffer_to_slot(slot, storage_size);
5790 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305791 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 }
Archanad8a83dc2021-06-14 10:04:16 +05305793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 status = psa_driver_wrapper_import_key(&attributes,
5795 data, bytes,
5796 slot->key.data,
5797 slot->key.bytes,
5798 &slot->key.bytes, &bits);
5799 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005800 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005802
5803exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 mbedtls_free(data);
5805 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005806}
5807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5809 psa_key_derivation_operation_t *operation,
5810 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005811{
5812 psa_status_t status;
5813 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005814 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005815
Ronald Cron81709fc2020-11-14 12:10:32 +01005816 *key = MBEDTLS_SVC_KEY_ID_INIT;
5817
Gilles Peskine0f84d622019-09-12 19:03:13 +02005818 /* Reject any attempt to create a zero-length key so that we don't
5819 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 if (psa_get_key_bits(attributes) == 0) {
5821 return PSA_ERROR_INVALID_ARGUMENT;
5822 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 if (operation->alg == PSA_ALG_NONE) {
5825 return PSA_ERROR_BAD_STATE;
5826 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 if (!operation->can_output_key) {
5829 return PSA_ERROR_NOT_PERMITTED;
5830 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005831
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5833 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005834#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005836 /* Deriving a key in a secure element is not implemented yet. */
5837 status = PSA_ERROR_NOT_SUPPORTED;
5838 }
5839#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 if (status == PSA_SUCCESS) {
5841 status = psa_generate_derived_key_internal(slot,
5842 attributes->core.bits,
5843 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005844 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 if (status == PSA_SUCCESS) {
5846 status = psa_finish_key_creation(slot, driver, key);
5847 }
5848 if (status != PSA_SUCCESS) {
5849 psa_fail_key_creation(slot, driver);
5850 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005853}
5854
Gilles Peskineeab56e42018-07-12 17:12:33 +02005855
5856
5857/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005858/* Key derivation */
5859/****************************************************************/
5860
Steven Cooreman932ffb72021-02-15 12:14:32 +01005861#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005862static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005863{
5864#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5866 return 1;
5867 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005868#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005869#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5871 return 1;
5872 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005873#endif
5874#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5876 return 1;
5877 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005878#endif
5879#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5881 return 1;
5882 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005883#endif
5884#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5886 return 1;
5887 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005888#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005889#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5891 return 1;
5892 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005893#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005895}
5896
Gilles Peskine449bd832023-01-11 14:50:10 +01005897static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005898{
5899 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 psa_status_t status = psa_hash_setup(&operation, alg);
5901 psa_hash_abort(&operation);
5902 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005903}
5904
Gilles Peskine969c5d62019-01-16 15:53:06 +01005905static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005906 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005908{
Janos Follath5fe19732019-06-20 15:09:30 +01005909 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5910 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005912
Gilles Peskine969c5d62019-01-16 15:53:06 +01005913 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 if (!is_kdf_alg_supported(kdf_alg)) {
5915 return PSA_ERROR_NOT_SUPPORTED;
5916 }
John Durkop07cc04a2020-11-16 22:08:34 -08005917
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005918 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005919 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5921 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5922 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5923 if (hash_size == 0) {
5924 return PSA_ERROR_NOT_SUPPORTED;
5925 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005926
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005927 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5928 * we might fail later, which is somewhat unfriendly and potentially
5929 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 psa_status_t status = psa_hash_try_support(hash_alg);
5931 if (status != PSA_SUCCESS) {
5932 return status;
5933 }
5934 } else {
5935 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005936 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005937
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5939 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5940 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5941 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005942 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005943#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005944 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5946 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005947 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005949#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5950 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 operation->capacity = 255 * hash_size;
5952 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005953}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005954
Gilles Peskine449bd832023-01-11 14:50:10 +01005955static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005956{
5957#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 if (alg == PSA_ALG_ECDH) {
5959 return PSA_SUCCESS;
5960 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005961#endif
5962 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005964}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005965
5966static int psa_key_derivation_allows_free_form_secret_input(
5967 psa_algorithm_t kdf_alg)
5968{
5969#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5970 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5971 return 0;
5972 }
5973#endif
5974 (void) kdf_alg;
5975 return 1;
5976}
John Durkop07cc04a2020-11-16 22:08:34 -08005977#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5980 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005981{
5982 psa_status_t status;
5983
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 if (operation->alg != 0) {
5985 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005986 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005987
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5989 return PSA_ERROR_INVALID_ARGUMENT;
5990 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5991#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5992 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5993 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5994 status = psa_key_agreement_try_support(ka_alg);
5995 if (status != PSA_SUCCESS) {
5996 return status;
5997 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005998 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
5999 return PSA_ERROR_INVALID_ARGUMENT;
6000 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6002#else
6003 return PSA_ERROR_NOT_SUPPORTED;
6004#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6005 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6006#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6007 status = psa_key_derivation_setup_kdf(operation, alg);
6008#else
6009 return PSA_ERROR_NOT_SUPPORTED;
6010#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6011 } else {
6012 return PSA_ERROR_INVALID_ARGUMENT;
6013 }
6014
6015 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006016 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 }
6018 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006019}
6020
Przemek Stekiel69c46792022-06-10 12:59:51 +02006021#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006022static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6023 psa_algorithm_t kdf_alg,
6024 psa_key_derivation_step_t step,
6025 const uint8_t *data,
6026 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006027{
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006029 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006031 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006032#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6034 return PSA_ERROR_INVALID_ARGUMENT;
6035 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006036#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 if (hkdf->state != HKDF_STATE_INIT) {
6038 return PSA_ERROR_BAD_STATE;
6039 } else {
6040 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6041 hash_alg,
6042 data, data_length);
6043 if (status != PSA_SUCCESS) {
6044 return status;
6045 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006046 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006048 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006049 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006050#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006052 /* We shouldn't be in different state as HKDF_EXPAND only allows
6053 * two inputs: SECRET (this case) and INFO which does not modify
6054 * the state. It could happen only if the hkdf
6055 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 if (hkdf->state != HKDF_STATE_INIT) {
6057 return PSA_ERROR_BAD_STATE;
6058 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006059
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006060 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6062 return PSA_ERROR_INVALID_ARGUMENT;
6063 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006064
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 memcpy(hkdf->prk, data, data_length);
6066 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006067#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006068 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006069 /* HKDF: If no salt was provided, use an empty salt.
6070 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006072#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6074 return PSA_ERROR_BAD_STATE;
6075 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006076#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6078 hash_alg,
6079 NULL, 0);
6080 if (status != PSA_SUCCESS) {
6081 return status;
6082 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006083 hkdf->state = HKDF_STATE_STARTED;
6084 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 if (hkdf->state != HKDF_STATE_STARTED) {
6086 return PSA_ERROR_BAD_STATE;
6087 }
6088 status = psa_mac_update(&hkdf->hmac,
6089 data, data_length);
6090 if (status != PSA_SUCCESS) {
6091 return status;
6092 }
6093 status = psa_mac_sign_finish(&hkdf->hmac,
6094 hkdf->prk,
6095 sizeof(hkdf->prk),
6096 &data_length);
6097 if (status != PSA_SUCCESS) {
6098 return status;
6099 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006100 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006101
Gilles Peskine2b522db2019-04-12 15:11:49 +02006102 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006103 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006104#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006106 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006108 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006110#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006111 {
6112 /* Block 0 is empty, and the next block will be
6113 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006115 }
6116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006118 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006119#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6121 return PSA_ERROR_INVALID_ARGUMENT;
6122 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006123#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006124#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6126 hkdf->state == HKDF_STATE_INIT) {
6127 return PSA_ERROR_BAD_STATE;
6128 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006129#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 if (hkdf->state == HKDF_STATE_OUTPUT) {
6131 return PSA_ERROR_BAD_STATE;
6132 }
6133 if (hkdf->info_set) {
6134 return PSA_ERROR_BAD_STATE;
6135 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006136 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 if (data_length != 0) {
6138 hkdf->info = mbedtls_calloc(1, data_length);
6139 if (hkdf->info == NULL) {
6140 return PSA_ERROR_INSUFFICIENT_MEMORY;
6141 }
6142 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006143 }
6144 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006146 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006148 }
6149}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006150#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006151
John Durkop07cc04a2020-11-16 22:08:34 -08006152#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6153 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006154static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6155 const uint8_t *data,
6156 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006157{
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6159 return PSA_ERROR_BAD_STATE;
6160 }
Janos Follathf08e2652019-06-13 09:05:41 +01006161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 if (data_length != 0) {
6163 prf->seed = mbedtls_calloc(1, data_length);
6164 if (prf->seed == NULL) {
6165 return PSA_ERROR_INSUFFICIENT_MEMORY;
6166 }
Janos Follathf08e2652019-06-13 09:05:41 +01006167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006169 prf->seed_length = data_length;
6170 }
Janos Follathf08e2652019-06-13 09:05:41 +01006171
Gilles Peskine43f958b2020-12-13 14:55:14 +01006172 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006175}
6176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6178 const uint8_t *data,
6179 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006180{
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6182 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6183 return PSA_ERROR_BAD_STATE;
6184 }
Janos Follath81550542019-06-13 14:26:34 +01006185
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 if (data_length != 0) {
6187 prf->secret = mbedtls_calloc(1, data_length);
6188 if (prf->secret == NULL) {
6189 return PSA_ERROR_INSUFFICIENT_MEMORY;
6190 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006193 prf->secret_length = data_length;
6194 }
Janos Follath81550542019-06-13 14:26:34 +01006195
Gilles Peskine43f958b2020-12-13 14:55:14 +01006196 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006197
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006199}
6200
Gilles Peskine449bd832023-01-11 14:50:10 +01006201static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6202 const uint8_t *data,
6203 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006204{
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6206 return PSA_ERROR_BAD_STATE;
6207 }
Janos Follath63028dd2019-06-13 09:15:47 +01006208
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 if (data_length != 0) {
6210 prf->label = mbedtls_calloc(1, data_length);
6211 if (prf->label == NULL) {
6212 return PSA_ERROR_INSUFFICIENT_MEMORY;
6213 }
Janos Follath63028dd2019-06-13 09:15:47 +01006214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006216 prf->label_length = data_length;
6217 }
Janos Follath63028dd2019-06-13 09:15:47 +01006218
Gilles Peskine43f958b2020-12-13 14:55:14 +01006219 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006220
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006222}
6223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6225 psa_key_derivation_step_t step,
6226 const uint8_t *data,
6227 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006228{
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006230 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006232 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006234 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006236 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006238 }
6239}
John Durkop07cc04a2020-11-16 22:08:34 -08006240#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6241 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6242
6243#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6244static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6245 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006246 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006248{
6249 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6251 4 + data_length + prf->other_secret_length :
6252 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006253
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6255 return PSA_ERROR_INVALID_ARGUMENT;
6256 }
John Durkop07cc04a2020-11-16 22:08:34 -08006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 uint8_t *pms = mbedtls_calloc(1, pms_len);
6259 if (pms == NULL) {
6260 return PSA_ERROR_INSUFFICIENT_MEMORY;
6261 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006262 uint8_t *cur = pms;
6263
6264 /* pure-PSK:
6265 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006266 *
6267 * The premaster secret is formed as follows: if the PSK is N octets
6268 * long, concatenate a uint16 with the value N, N zero octets, a second
6269 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006270 *
6271 * mixed-PSK:
6272 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6273 * follows: concatenate a uint16 with the length of the other secret,
6274 * the other secret itself, uint16 with the length of PSK, and the
6275 * PSK itself.
6276 * For details please check:
6277 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6278 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6279 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006280 */
6281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6283 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6284 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6285 if (prf->other_secret_length != 0) {
6286 memcpy(cur, prf->other_secret, prf->other_secret_length);
6287 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006288 cur += prf->other_secret_length;
6289 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 } else {
6291 *cur++ = MBEDTLS_BYTE_1(data_length);
6292 *cur++ = MBEDTLS_BYTE_0(data_length);
6293 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006294 cur += data_length;
6295 }
6296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 *cur++ = MBEDTLS_BYTE_1(data_length);
6298 *cur++ = MBEDTLS_BYTE_0(data_length);
6299 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006300 cur += data_length;
6301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 mbedtls_platform_zeroize(pms, pms_len);
6305 mbedtls_free(pms);
6306 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006307}
Janos Follath6660f0e2019-06-17 08:44:03 +01006308
Przemek Stekiele47201b2022-04-19 13:53:28 +02006309static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6310 psa_tls12_prf_key_derivation_t *prf,
6311 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006313{
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6315 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006316 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006317
6318 if (data_length != 0) {
6319 prf->other_secret = mbedtls_calloc(1, data_length);
6320 if (prf->other_secret == NULL) {
6321 return PSA_ERROR_INSUFFICIENT_MEMORY;
6322 }
6323
6324 memcpy(prf->other_secret, data, data_length);
6325 prf->other_secret_length = data_length;
6326 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006327 prf->other_secret_length = 0;
6328 }
6329
6330 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006333}
6334
Janos Follath6660f0e2019-06-17 08:44:03 +01006335static psa_status_t psa_tls12_prf_psk_to_ms_input(
6336 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006337 psa_key_derivation_step_t step,
6338 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006340{
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006342 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 return psa_tls12_prf_psk_to_ms_set_key(prf,
6344 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006345 break;
6346 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6348 data,
6349 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006350 break;
6351 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006353 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006354
Przemek Stekiele47201b2022-04-19 13:53:28 +02006355 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006356}
John Durkop07cc04a2020-11-16 22:08:34 -08006357#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006358
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006359#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6360static psa_status_t psa_tls12_ecjpake_to_pms_input(
6361 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006362 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006363 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006365{
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6367 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6368 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006369 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006370
6371 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 if (data[0] != 0x04) {
6373 return PSA_ERROR_INVALID_ARGUMENT;
6374 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006375
6376 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006380}
6381#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006382/** Check whether the given key type is acceptable for the given
6383 * input step of a key derivation.
6384 *
6385 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6386 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6387 * Both secret and non-secret inputs can alternatively have the type
6388 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6389 * that the input was passed as a buffer rather than via a key object.
6390 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006391static int psa_key_derivation_check_input_type(
6392 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006394{
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006396 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 if (key_type == PSA_KEY_TYPE_DERIVE) {
6398 return PSA_SUCCESS;
6399 }
6400 if (key_type == PSA_KEY_TYPE_NONE) {
6401 return PSA_SUCCESS;
6402 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006403 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006404 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 if (key_type == PSA_KEY_TYPE_DERIVE) {
6406 return PSA_SUCCESS;
6407 }
6408 if (key_type == PSA_KEY_TYPE_NONE) {
6409 return PSA_SUCCESS;
6410 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006411 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006412 case PSA_KEY_DERIVATION_INPUT_LABEL:
6413 case PSA_KEY_DERIVATION_INPUT_SALT:
6414 case PSA_KEY_DERIVATION_INPUT_INFO:
6415 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6417 return PSA_SUCCESS;
6418 }
6419 if (key_type == PSA_KEY_TYPE_NONE) {
6420 return PSA_SUCCESS;
6421 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006422 break;
6423 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006425}
6426
Janos Follathb80a94e2019-06-12 15:54:46 +01006427static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006428 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006429 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006430 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006431 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006433{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006434 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 status = psa_key_derivation_check_input_type(step, key_type);
6438 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006439 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006441
Przemek Stekiel69c46792022-06-10 12:59:51 +02006442#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6444 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6445 step, data, data_length);
6446 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006447#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006448#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6450 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6451 step, data, data_length);
6452 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006453#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6454#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6456 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6457 step, data, data_length);
6458 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006459#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006460#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006462 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6464 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006465#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006466 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006467 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006468 (void) data;
6469 (void) data_length;
6470 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006472 }
6473
Gilles Peskine224b0d62019-09-23 18:13:17 +02006474exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 if (status != PSA_SUCCESS) {
6476 psa_key_derivation_abort(operation);
6477 }
6478 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006479}
6480
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306481static psa_status_t psa_key_derivation_input_integer_internal(
6482 psa_key_derivation_operation_t *operation,
6483 psa_key_derivation_step_t step,
6484 uint64_t value)
6485{
6486 psa_status_t status;
6487 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6488
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306489 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306490 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306491 (void) value;
6492 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306493 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306494 }
6495
6496 if (status != PSA_SUCCESS) {
6497 psa_key_derivation_abort(operation);
6498 }
6499 return status;
6500}
6501
Janos Follath51f4a0f2019-06-14 11:35:55 +01006502psa_status_t psa_key_derivation_input_bytes(
6503 psa_key_derivation_operation_t *operation,
6504 psa_key_derivation_step_t step,
6505 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006507{
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 return psa_key_derivation_input_internal(operation, step,
6509 PSA_KEY_TYPE_NONE,
6510 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006511}
6512
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306513psa_status_t psa_key_derivation_input_integer(
6514 psa_key_derivation_operation_t *operation,
6515 psa_key_derivation_step_t step,
6516 uint64_t value)
6517{
6518 return psa_key_derivation_input_integer_internal(operation, step, value);
6519}
6520
Janos Follath51f4a0f2019-06-14 11:35:55 +01006521psa_status_t psa_key_derivation_input_key(
6522 psa_key_derivation_operation_t *operation,
6523 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006525{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006527 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006528 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006529
Ronald Cron5c522922020-11-14 16:35:34 +01006530 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6532 if (status != PSA_SUCCESS) {
6533 psa_key_derivation_abort(operation);
6534 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006535 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006536
6537 /* Passing a key object as a SECRET input unlocks the permission
6538 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006540 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006542
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 status = psa_key_derivation_input_internal(operation,
6544 step, slot->attr.type,
6545 slot->key.data,
6546 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006547
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006551}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006552
6553
6554
6555/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006556/* Key agreement */
6557/****************************************************************/
6558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6560 const uint8_t *key_buffer,
6561 size_t key_buffer_size,
6562 psa_algorithm_t alg,
6563 const uint8_t *peer_key,
6564 size_t peer_key_length,
6565 uint8_t *shared_secret,
6566 size_t shared_secret_size,
6567 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006568{
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006570#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006571 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6573 key_buffer_size, alg,
6574 peer_key, peer_key_length,
6575 shared_secret,
6576 shared_secret_size,
6577 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006578#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006579 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006580 (void) attributes;
6581 (void) key_buffer;
6582 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006583 (void) peer_key;
6584 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006585 (void) shared_secret;
6586 (void) shared_secret_size;
6587 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006589 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006590}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006591
Aditya Deshpande17845b82022-10-13 17:21:01 +01006592/** Internal function for raw key agreement
6593 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006594 * to the driver's implementation if a driver is present.
6595 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006596 * (psa_key_agreement_raw_builtin).
6597 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006598static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6599 psa_key_slot_t *private_key,
6600 const uint8_t *peer_key,
6601 size_t peer_key_length,
6602 uint8_t *shared_secret,
6603 size_t shared_secret_size,
6604 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006605{
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6607 return PSA_ERROR_NOT_SUPPORTED;
6608 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006609
6610 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006612 };
6613
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 return psa_driver_wrapper_key_agreement(&attributes,
6615 private_key->key.data,
6616 private_key->key.bytes, alg,
6617 peer_key, peer_key_length,
6618 shared_secret,
6619 shared_secret_size,
6620 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006621}
6622
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006623/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006624 * to potentially free embedded data structures and wipe confidential data.
6625 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006626static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6627 psa_key_derivation_step_t step,
6628 psa_key_slot_t *private_key,
6629 const uint8_t *peer_key,
6630 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006631{
6632 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006633 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006634 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006636
6637 /* Step 1: run the secret agreement algorithm to generate the shared
6638 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 status = psa_key_agreement_raw_internal(ka_alg,
6640 private_key,
6641 peer_key, peer_key_length,
6642 shared_secret,
6643 sizeof(shared_secret),
6644 &shared_secret_length);
6645 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006646 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006648
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006649 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006650 * the shared secret. A shared secret is permitted wherever a key
6651 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 status = psa_key_derivation_input_internal(operation, step,
6653 PSA_KEY_TYPE_DERIVE,
6654 shared_secret,
6655 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006656exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6658 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006659}
6660
Gilles Peskine449bd832023-01-11 14:50:10 +01006661psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6662 psa_key_derivation_step_t step,
6663 mbedtls_svc_key_id_t private_key,
6664 const uint8_t *peer_key,
6665 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006666{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006667 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006668 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006669 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6672 return PSA_ERROR_INVALID_ARGUMENT;
6673 }
Ronald Cron5c522922020-11-14 16:35:34 +01006674 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6676 if (status != PSA_SUCCESS) {
6677 return status;
6678 }
6679 status = psa_key_agreement_internal(operation, step,
6680 slot,
6681 peer_key, peer_key_length);
6682 if (status != PSA_SUCCESS) {
6683 psa_key_derivation_abort(operation);
6684 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006685 /* If a private key has been added as SECRET, we allow the derived
6686 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006688 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006690 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006691
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006693
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006695}
6696
Gilles Peskine449bd832023-01-11 14:50:10 +01006697psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6698 mbedtls_svc_key_id_t private_key,
6699 const uint8_t *peer_key,
6700 size_t peer_key_length,
6701 uint8_t *output,
6702 size_t output_size,
6703 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006704{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006705 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006706 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006707 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006708
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006710 status = PSA_ERROR_INVALID_ARGUMENT;
6711 goto exit;
6712 }
Ronald Cron5c522922020-11-14 16:35:34 +01006713 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6715 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006716 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006718
Gilles Peskine3e561302022-04-14 00:17:15 +02006719 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6720 * for the output size. The PSA specification only guarantees that this
6721 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6722 * but it might be nice to allow smaller buffers if the output fits.
6723 * At the time of writing this comment, with only ECDH implemented,
6724 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6725 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6726 * be exact for it as well. */
6727 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6729 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006730 status = PSA_ERROR_BUFFER_TOO_SMALL;
6731 goto exit;
6732 }
6733
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 status = psa_key_agreement_raw_internal(alg, slot,
6735 peer_key, peer_key_length,
6736 output, output_size,
6737 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006738
6739exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006741 /* If an error happens and is not handled properly, the output
6742 * may be used as a key to protect sensitive data. Arrange for such
6743 * a key to be random, which is likely to result in decryption or
6744 * verification errors. This is better than filling the buffer with
6745 * some constant data such as zeros, which would result in the data
6746 * being protected with a reproducible, easily knowable key.
6747 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006749 *output_length = output_size;
6750 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006751
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006753
Gilles Peskine449bd832023-01-11 14:50:10 +01006754 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006755}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006756
6757
Gilles Peskine30524eb2020-11-13 17:02:26 +01006758
Gilles Peskine86a440b2018-11-12 18:39:40 +01006759/****************************************************************/
6760/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006761/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006762
Gilles Peskine30524eb2020-11-13 17:02:26 +01006763/** Initialize the PSA random generator.
6764 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006765static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006766{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006767#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006769#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6770
Gilles Peskine30524eb2020-11-13 17:02:26 +01006771 /* Set default configuration if
6772 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006774 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 }
6776 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006777 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006779
Gilles Peskine449bd832023-01-11 14:50:10 +01006780 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006781#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6782 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6783 /* The PSA entropy injection feature depends on using NV seed as an entropy
6784 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 mbedtls_entropy_add_source(&rng->entropy,
6786 mbedtls_nv_seed_poll, NULL,
6787 MBEDTLS_ENTROPY_BLOCK_SIZE,
6788 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006789#endif
6790
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006792#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006793}
6794
6795/** Deinitialize the PSA random generator.
6796 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006797static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006798{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006799#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006801#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6803 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006804#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006805}
6806
6807/** Seed the PSA random generator.
6808 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006809static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006810{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006811#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6812 /* Do nothing: the external RNG seeds itself. */
6813 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006815#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006816 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6818 drbg_seed, sizeof(drbg_seed) - 1);
6819 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006820#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006821}
6822
Gilles Peskine449bd832023-01-11 14:50:10 +01006823psa_status_t psa_generate_random(uint8_t *output,
6824 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006825{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006826 GUARD_MODULE_INITIALIZED;
6827
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006828#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6829
6830 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6832 output, output_size,
6833 &output_length);
6834 if (status != PSA_SUCCESS) {
6835 return status;
6836 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006837 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006838 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 if (output_length != output_size) {
6840 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6841 }
6842 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006843
6844#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6845
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006847 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6849 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6850 output_size);
6851 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6852 output, request_size);
6853 if (ret != 0) {
6854 return mbedtls_to_psa_error(ret);
6855 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006856 output_size -= request_size;
6857 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006858 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006860#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006861}
6862
Gilles Peskine8814fc42020-12-14 15:33:44 +01006863/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006864 *
6865 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6866 * `psa_generate_random(...)`. The state parameter is ignored since the
6867 * PSA API doesn't support passing an explicit state.
6868 *
6869 * In the non-external case, psa_generate_random() calls an
6870 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6871 * and semantics as mbedtls_psa_get_random(). As an optimization,
6872 * instead of doing this back-and-forth between the PSA API and the
6873 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6874 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006876#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6877int mbedtls_psa_get_random(void *p_rng,
6878 unsigned char *output,
6879 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006880{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006881 /* This function takes a pointer to the RNG state because that's what
6882 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6883 * its own state internally and doesn't let the caller access that state.
6884 * So we just ignore the state parameter, and in practice we'll pass
6885 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006886 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 psa_status_t status = psa_generate_random(output, output_size);
6888 if (status == PSA_SUCCESS) {
6889 return 0;
6890 } else {
6891 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6892 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006893}
6894#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6895
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006896#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006897#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006898
Gilles Peskine449bd832023-01-11 14:50:10 +01006899psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6900 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006901{
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 if (global_data.initialized) {
6903 return PSA_ERROR_NOT_PERMITTED;
6904 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006905
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6907 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6908 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6909 return PSA_ERROR_INVALID_ARGUMENT;
6910 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006911
Gilles Peskine449bd832023-01-11 14:50:10 +01006912 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006913}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006914#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006915
Ronald Cron3772afe2021-02-08 16:10:05 +01006916/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006917 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006918 * \param type The key type
6919 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006920 *
6921 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006922 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006923 * \retval #PSA_ERROR_INVALID_ARGUMENT
6924 * The size in bits of the key is not valid.
6925 * \retval #PSA_ERROR_NOT_SUPPORTED
6926 * The type and/or the size in bits of the key or the combination of
6927 * the two is not supported.
6928 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006929static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006931{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006932 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006933
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 if (key_type_is_raw_bytes(type)) {
6935 status = psa_validate_unstructured_key_bit_size(type, bits);
6936 if (status != PSA_SUCCESS) {
6937 return status;
6938 }
6939 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006940#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6942 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6943 return PSA_ERROR_NOT_SUPPORTED;
6944 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006945
Ronald Cron01b2aba2020-10-05 09:42:02 +02006946 /* Accept only byte-aligned keys, for the same reasons as
6947 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 if (bits % 8 != 0) {
6949 return PSA_ERROR_NOT_SUPPORTED;
6950 }
6951 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006952#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006953
Ronald Cron5c4d3862020-12-07 11:07:24 +01006954#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006956 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 return PSA_SUCCESS;
6958 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006959#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006960 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006962 }
6963
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006965}
6966
Ronald Cron55ed0592020-10-05 10:30:40 +02006967psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006968 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006970{
6971 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006972 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006973
Gilles Peskine449bd832023-01-11 14:50:10 +01006974 if ((attributes->domain_parameters == NULL) &&
6975 (attributes->domain_parameters_size != 0)) {
6976 return PSA_ERROR_INVALID_ARGUMENT;
6977 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006978
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 if (key_type_is_raw_bytes(type)) {
6980 status = psa_generate_random(key_buffer, key_buffer_size);
6981 if (status != PSA_SUCCESS) {
6982 return status;
6983 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006984
David Brown12ca5032021-02-11 11:02:00 -07006985#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 if (type == PSA_KEY_TYPE_DES) {
6987 psa_des_set_key_parity(key_buffer, key_buffer_size);
6988 }
David Brown8107e312021-02-12 08:08:46 -07006989#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006991
Jaeden Amero424fa932021-05-14 08:34:32 +01006992#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6993 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6995 return mbedtls_psa_rsa_generate_key(attributes,
6996 key_buffer,
6997 key_buffer_size,
6998 key_buffer_length);
6999 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01007000#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
7001 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007002
John Durkop9814fa22020-11-04 12:28:15 -08007003#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7005 return mbedtls_psa_ecp_generate_key(attributes,
7006 key_buffer,
7007 key_buffer_size,
7008 key_buffer_length);
7009 } else
John Durkop9814fa22020-11-04 12:28:15 -08007010#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02007011 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 (void) key_buffer_length;
7013 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007014 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007015
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007017}
Darryl Green0c6575a2018-11-07 16:05:30 +00007018
Gilles Peskine449bd832023-01-11 14:50:10 +01007019psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7020 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007021{
7022 psa_status_t status;
7023 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007024 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007025 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007026
Ronald Cron81709fc2020-11-14 12:10:32 +01007027 *key = MBEDTLS_SVC_KEY_ID_INIT;
7028
Gilles Peskine0f84d622019-09-12 19:03:13 +02007029 /* Reject any attempt to create a zero-length key so that we don't
7030 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 if (psa_get_key_bits(attributes) == 0) {
7032 return PSA_ERROR_INVALID_ARGUMENT;
7033 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007034
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007035 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7037 return PSA_ERROR_INVALID_ARGUMENT;
7038 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007039
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7041 &slot, &driver);
7042 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007043 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 }
Gilles Peskine11792082019-08-06 18:36:36 +02007045
Ronald Cron977c2472020-10-13 08:32:21 +02007046 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307047 * storage ( thus not in the case of generating a key in a secure element
7048 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7049 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 if (slot->key.data == NULL) {
7051 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7052 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007053 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 attributes->core.type, attributes->core.bits);
7055 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007056 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007058
7059 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 attributes->core.type,
7061 attributes->core.bits);
7062 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007063 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 attributes, &key_buffer_size);
7065 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007066 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 }
Ronald Cron977c2472020-10-13 08:32:21 +02007068 }
Ronald Cron977c2472020-10-13 08:32:21 +02007069
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7071 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007072 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 }
Ronald Cron977c2472020-10-13 08:32:21 +02007074 }
7075
Gilles Peskine449bd832023-01-11 14:50:10 +01007076 status = psa_driver_wrapper_generate_key(attributes,
7077 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007078
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 if (status != PSA_SUCCESS) {
7080 psa_remove_key_data_from_memory(slot);
7081 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007082
Gilles Peskine11792082019-08-06 18:36:36 +02007083exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 if (status == PSA_SUCCESS) {
7085 status = psa_finish_key_creation(slot, driver, key);
7086 }
7087 if (status != PSA_SUCCESS) {
7088 psa_fail_key_creation(slot, driver);
7089 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007090
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007092}
7093
Gilles Peskinee59236f2018-01-27 23:32:46 +01007094/****************************************************************/
7095/* Module setup */
7096/****************************************************************/
7097
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007098#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007099psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 void (* entropy_init)(mbedtls_entropy_context *ctx),
7101 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007102{
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7104 return PSA_ERROR_BAD_STATE;
7105 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007106 global_data.rng.entropy_init = entropy_init;
7107 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007109}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007110#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007111
Gilles Peskine449bd832023-01-11 14:50:10 +01007112void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007113{
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 psa_wipe_all_key_slots();
7115 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7116 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007117 }
7118 /* Wipe all remaining data, including configuration.
7119 * In particular, this sets all state indicator to the value
7120 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007122
7123 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007124 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007125}
7126
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007127#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7128/** Recover a transaction that was interrupted by a power failure.
7129 *
7130 * This function is called during initialization, before psa_crypto_init()
7131 * returns. If this function returns a failure status, the initialization
7132 * fails.
7133 */
7134static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007136{
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007138 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7139 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 /* TODO - fall through to the failure case until this
7141 * is implemented.
7142 * https://github.com/ARMmbed/mbed-crypto/issues/218
7143 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007144 default:
7145 /* We found an unsupported transaction in the storage.
7146 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007148 }
7149}
7150#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7151
Gilles Peskine449bd832023-01-11 14:50:10 +01007152psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007153{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007154 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007155
Gilles Peskinec6b69072018-11-20 21:42:52 +01007156 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 if (global_data.initialized != 0) {
7158 return PSA_SUCCESS;
7159 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007160
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007161 /* Init drivers */
7162 status = psa_driver_wrapper_init();
7163 if (status != PSA_SUCCESS) {
7164 goto exit;
7165 }
7166 global_data.drivers_initialized = 1;
7167
Gilles Peskine30524eb2020-11-13 17:02:26 +01007168 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007170 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 status = mbedtls_psa_random_seed(&global_data.rng);
7172 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007175 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007176
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 status = psa_initialize_key_slots();
7178 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007179 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007180 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007181
Gilles Peskine4b734222019-07-24 15:56:31 +02007182#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 status = psa_crypto_load_transaction();
7184 if (status == PSA_SUCCESS) {
7185 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7186 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007187 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 }
7189 status = psa_crypto_stop_transaction();
7190 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007191 /* There's no transaction to complete. It's all good. */
7192 status = PSA_SUCCESS;
7193 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007194#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007195
Gilles Peskinec6b69072018-11-20 21:42:52 +01007196 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007197 global_data.initialized = 1;
7198
7199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 if (status != PSA_SUCCESS) {
7201 mbedtls_psa_crypto_free();
7202 }
7203 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007204}
7205
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007206psa_status_t psa_crypto_driver_pake_get_password_len(
7207 const psa_crypto_driver_pake_inputs_t *inputs,
7208 size_t *password_len)
7209{
7210 if (inputs->password_len == 0) {
7211 return PSA_ERROR_BAD_STATE;
7212 }
7213
7214 *password_len = inputs->password_len;
7215
7216 return PSA_SUCCESS;
7217}
7218
7219psa_status_t psa_crypto_driver_pake_get_password(
7220 const psa_crypto_driver_pake_inputs_t *inputs,
7221 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7222{
7223 if (inputs->password_len == 0) {
7224 return PSA_ERROR_BAD_STATE;
7225 }
7226
7227 if (buffer_size < inputs->password_len) {
7228 return PSA_ERROR_BUFFER_TOO_SMALL;
7229 }
7230
7231 memcpy(buffer, inputs->password, inputs->password_len);
7232 *buffer_length = inputs->password_len;
7233
7234 return PSA_SUCCESS;
7235}
7236
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007237psa_status_t psa_crypto_driver_pake_get_user_len(
7238 const psa_crypto_driver_pake_inputs_t *inputs,
7239 size_t *user_len)
7240{
7241 if (inputs->user_len == 0) {
7242 return PSA_ERROR_BAD_STATE;
7243 }
7244
7245 *user_len = inputs->user_len;
7246
7247 return PSA_SUCCESS;
7248}
7249
7250psa_status_t psa_crypto_driver_pake_get_user(
7251 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007252 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007253{
7254 if (inputs->user_len == 0) {
7255 return PSA_ERROR_BAD_STATE;
7256 }
7257
Przemek Stekielc0e62502023-03-14 11:49:36 +01007258 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007259 return PSA_ERROR_BUFFER_TOO_SMALL;
7260 }
7261
Przemek Stekielc0e62502023-03-14 11:49:36 +01007262 memcpy(user_id, inputs->user, inputs->user_len);
7263 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007264
7265 return PSA_SUCCESS;
7266}
7267
7268psa_status_t psa_crypto_driver_pake_get_peer_len(
7269 const psa_crypto_driver_pake_inputs_t *inputs,
7270 size_t *peer_len)
7271{
7272 if (inputs->peer_len == 0) {
7273 return PSA_ERROR_BAD_STATE;
7274 }
7275
7276 *peer_len = inputs->peer_len;
7277
7278 return PSA_SUCCESS;
7279}
7280
7281psa_status_t psa_crypto_driver_pake_get_peer(
7282 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007283 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007284{
7285 if (inputs->peer_len == 0) {
7286 return PSA_ERROR_BAD_STATE;
7287 }
7288
Przemek Stekielc0e62502023-03-14 11:49:36 +01007289 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007290 return PSA_ERROR_BUFFER_TOO_SMALL;
7291 }
7292
Przemek Stekielc0e62502023-03-14 11:49:36 +01007293 memcpy(peer_id, inputs->peer, inputs->peer_len);
7294 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007295
7296 return PSA_SUCCESS;
7297}
7298
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007299psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7300 const psa_crypto_driver_pake_inputs_t *inputs,
7301 psa_pake_cipher_suite_t *cipher_suite)
7302{
7303 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7304 return PSA_ERROR_BAD_STATE;
7305 }
7306
7307 *cipher_suite = inputs->cipher_suite;
7308
7309 return PSA_SUCCESS;
7310}
7311
Neil Armstronga7d08c32022-06-01 18:21:20 +02007312psa_status_t psa_pake_setup(
7313 psa_pake_operation_t *operation,
7314 const psa_pake_cipher_suite_t *cipher_suite)
7315{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007316 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007317
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007318 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007319 status = PSA_ERROR_BAD_STATE;
7320 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007321 }
7322
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007323 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007324 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007325 status = PSA_ERROR_INVALID_ARGUMENT;
7326 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007327 }
7328
Przemek Stekiel51eac532022-12-07 11:04:51 +01007329 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7330
Przemek Stekiele12ed362022-12-21 12:54:46 +01007331 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007332 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7333 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007334 operation->data.inputs.cipher_suite = *cipher_suite;
7335
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007336#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007337 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007338 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007339 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007340
Przemek Stekiele12ed362022-12-21 12:54:46 +01007341 computation_stage->state = PSA_PAKE_STATE_SETUP;
7342 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7343 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7344 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007345 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007346#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007347 {
7348 status = PSA_ERROR_NOT_SUPPORTED;
7349 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007350 }
7351
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007352 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7353
Przemek Stekiel51eac532022-12-07 11:04:51 +01007354 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007355exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007356 psa_pake_abort(operation);
7357 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007358}
7359
7360psa_status_t psa_pake_set_password_key(
7361 psa_pake_operation_t *operation,
7362 mbedtls_svc_key_id_t password)
7363{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007365 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7366 psa_key_slot_t *slot = NULL;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007367
Przemek Stekiel51eac532022-12-07 11:04:51 +01007368 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007369 status = PSA_ERROR_BAD_STATE;
7370 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007371 }
7372
Przemek Stekiel6c764412022-11-22 14:05:12 +01007373 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7374 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007375 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007376 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007377 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007378 }
7379
Przemek Stekiel6c764412022-11-22 14:05:12 +01007380 psa_key_attributes_t attributes = {
7381 .core = slot->attr
7382 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007383
Przemek Stekiel51eac532022-12-07 11:04:51 +01007384 psa_key_type_t type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007385
Przemek Stekiel51eac532022-12-07 11:04:51 +01007386 if (type != PSA_KEY_TYPE_PASSWORD &&
7387 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7388 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007389 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007390 }
7391
Przemek Stekiel51eac532022-12-07 11:04:51 +01007392 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7393 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007394 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007395 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007396 }
7397
7398 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7399 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007400 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007401exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007402 if (status != PSA_SUCCESS) {
7403 psa_pake_abort(operation);
7404 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007405 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007406 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007407}
7408
7409psa_status_t psa_pake_set_user(
7410 psa_pake_operation_t *operation,
7411 const uint8_t *user_id,
7412 size_t user_id_len)
7413{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007414 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007415
7416 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007417 status = PSA_ERROR_BAD_STATE;
7418 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007419 }
7420
Przemek Stekiel51eac532022-12-07 11:04:51 +01007421 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007422 status = PSA_ERROR_INVALID_ARGUMENT;
7423 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007424 }
7425
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007426 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007427 status = PSA_ERROR_BAD_STATE;
7428 goto exit;
7429 }
7430
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007431 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7432 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007433 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7434 goto exit;
7435 }
7436
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007437 memcpy(operation->data.inputs.user, user_id, user_id_len);
7438 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007439
7440 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007441exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007442 psa_pake_abort(operation);
7443 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007444}
7445
7446psa_status_t psa_pake_set_peer(
7447 psa_pake_operation_t *operation,
7448 const uint8_t *peer_id,
7449 size_t peer_id_len)
7450{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007451 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007452
7453 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007454 status = PSA_ERROR_BAD_STATE;
7455 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007456 }
7457
Przemek Stekiel51eac532022-12-07 11:04:51 +01007458 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007459 status = PSA_ERROR_INVALID_ARGUMENT;
7460 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007461 }
7462
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007463 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007464 status = PSA_ERROR_BAD_STATE;
7465 goto exit;
7466 }
7467
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007468 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7469 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007470 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7471 goto exit;
7472 }
7473
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007474 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7475 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007476
7477 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007478exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007479 psa_pake_abort(operation);
7480 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007481}
7482
7483psa_status_t psa_pake_set_role(
7484 psa_pake_operation_t *operation,
7485 psa_pake_role_t role)
7486{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007487 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007488
Przemek Stekiel51eac532022-12-07 11:04:51 +01007489 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007490 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007491 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007492 }
7493
Przemek Stekiel09104b82023-03-06 14:11:51 +01007494 switch (operation->alg) {
7495#if defined(PSA_WANT_ALG_JPAKE)
7496 case PSA_ALG_JPAKE:
7497 if (role == PSA_PAKE_ROLE_NONE) {
7498 return PSA_SUCCESS;
7499 }
7500 status = PSA_ERROR_INVALID_ARGUMENT;
7501 break;
7502#endif
7503 default:
7504 (void) role;
7505 status = PSA_ERROR_NOT_SUPPORTED;
7506 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007507 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007508exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007509 psa_pake_abort(operation);
7510 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007511}
7512
Przemek Stekielb09c4872023-01-17 12:05:38 +01007513/* Auxiliary function to convert core computation stage(step, sequence, state) to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007514#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007515static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007516 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007517{
Przemek Stekieldde6a912023-01-26 08:46:37 +01007518 switch (stage->state) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007519 case PSA_PAKE_OUTPUT_X1_X2:
7520 case PSA_PAKE_INPUT_X1_X2:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007521 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007522 case PSA_PAKE_X1_STEP_KEY_SHARE:
7523 return PSA_JPAKE_X1_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007524 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7525 return PSA_JPAKE_X1_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007526 case PSA_PAKE_X1_STEP_ZK_PROOF:
7527 return PSA_JPAKE_X1_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007528 case PSA_PAKE_X2_STEP_KEY_SHARE:
7529 return PSA_JPAKE_X2_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007530 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7531 return PSA_JPAKE_X2_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007532 case PSA_PAKE_X2_STEP_ZK_PROOF:
7533 return PSA_JPAKE_X2_STEP_ZK_PROOF;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007534 default:
7535 return PSA_JPAKE_STEP_INVALID;
7536 }
7537 break;
7538 case PSA_PAKE_OUTPUT_X2S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007539 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007540 case PSA_PAKE_X1_STEP_KEY_SHARE:
7541 return PSA_JPAKE_X2S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007542 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7543 return PSA_JPAKE_X2S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007544 case PSA_PAKE_X1_STEP_ZK_PROOF:
7545 return PSA_JPAKE_X2S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007546 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007547 return PSA_JPAKE_STEP_INVALID;
7548 }
7549 break;
7550 case PSA_PAKE_INPUT_X4S:
Przemek Stekieldde6a912023-01-26 08:46:37 +01007551 switch (stage->sequence) {
Przemek Stekielb09c4872023-01-17 12:05:38 +01007552 case PSA_PAKE_X1_STEP_KEY_SHARE:
7553 return PSA_JPAKE_X4S_STEP_KEY_SHARE;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007554 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7555 return PSA_JPAKE_X4S_STEP_ZK_PUBLIC;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007556 case PSA_PAKE_X1_STEP_ZK_PROOF:
7557 return PSA_JPAKE_X4S_STEP_ZK_PROOF;
Przemek Stekiel57580f22023-03-01 12:21:26 +01007558 default:
Przemek Stekielb09c4872023-01-17 12:05:38 +01007559 return PSA_JPAKE_STEP_INVALID;
7560 }
7561 break;
7562 default:
7563 return PSA_JPAKE_STEP_INVALID;
7564 }
7565 return PSA_JPAKE_STEP_INVALID;
7566}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007567#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007568
Przemek Stekiel2797d372022-12-22 11:19:22 +01007569static psa_status_t psa_pake_complete_inputs(
7570 psa_pake_operation_t *operation)
7571{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007572 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007573 /* Create copy of the inputs on stack as inputs share memory
7574 with the driver context which will be setup by the driver. */
7575 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007576
Przemek Stekielfde11282023-03-13 16:06:09 +01007577 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007578 return PSA_ERROR_BAD_STATE;
7579 }
7580
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007581 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007582 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7583 return PSA_ERROR_BAD_STATE;
7584 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007585 }
7586
Przemek Stekiel18620a32023-01-17 16:34:52 +01007587 /* Clear driver context */
7588 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7589
7590 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007591
7592 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007593 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
7594 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007595
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007596 /* User and peer are translated to role. */
7597 mbedtls_free(inputs.user);
7598 mbedtls_free(inputs.peer);
7599
Przemek Stekiel2797d372022-12-22 11:19:22 +01007600 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007601#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007602 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007603 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007604 psa_jpake_computation_stage_t *computation_stage =
7605 &operation->computation_stage.jpake;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007606 computation_stage->state = PSA_PAKE_STATE_READY;
7607 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7608 computation_stage->input_step = PSA_PAKE_STEP_X1_X2;
7609 computation_stage->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007610 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007611#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007612 {
7613 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007614 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007615 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007616 return status;
7617}
7618
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007619#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007620static psa_status_t psa_jpake_output_prologue(
7621 psa_pake_operation_t *operation,
7622 psa_pake_step_t step)
7623{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007624 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7625 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7626 step != PSA_PAKE_STEP_ZK_PROOF) {
7627 return PSA_ERROR_INVALID_ARGUMENT;
7628 }
7629
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007630 psa_jpake_computation_stage_t *computation_stage =
7631 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007632
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007633 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7634 return PSA_ERROR_BAD_STATE;
7635 }
7636
7637 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7638 computation_stage->state != PSA_PAKE_OUTPUT_X1_X2 &&
7639 computation_stage->state != PSA_PAKE_OUTPUT_X2S) {
7640 return PSA_ERROR_BAD_STATE;
7641 }
7642
7643 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7644 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007645 return PSA_ERROR_BAD_STATE;
7646 }
7647
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007648 switch (computation_stage->output_step) {
7649 case PSA_PAKE_STEP_X1_X2:
7650 computation_stage->state = PSA_PAKE_OUTPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007651 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007652 case PSA_PAKE_STEP_X2S:
7653 computation_stage->state = PSA_PAKE_OUTPUT_X2S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007654 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007655 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007656 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007657 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007658
7659 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7660 }
7661
7662 /* Check if step matches current sequence */
7663 switch (computation_stage->sequence) {
7664 case PSA_PAKE_X1_STEP_KEY_SHARE:
7665 case PSA_PAKE_X2_STEP_KEY_SHARE:
7666 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7667 return PSA_ERROR_BAD_STATE;
7668 }
7669 break;
7670
7671 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7672 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7673 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7674 return PSA_ERROR_BAD_STATE;
7675 }
7676 break;
7677
7678 case PSA_PAKE_X1_STEP_ZK_PROOF:
7679 case PSA_PAKE_X2_STEP_ZK_PROOF:
7680 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7681 return PSA_ERROR_BAD_STATE;
7682 }
7683 break;
7684
7685 default:
7686 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007687 }
7688
7689 return PSA_SUCCESS;
7690}
7691
7692static psa_status_t psa_jpake_output_epilogue(
7693 psa_pake_operation_t *operation)
7694{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007695 psa_jpake_computation_stage_t *computation_stage =
7696 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007697
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007698 if ((computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007699 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007700 (computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007701 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007702 computation_stage->state = PSA_PAKE_STATE_READY;
7703 computation_stage->output_step++;
7704 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7705 } else {
7706 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007707 }
7708
7709 return PSA_SUCCESS;
7710}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007711#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007712
Neil Armstronga7d08c32022-06-01 18:21:20 +02007713psa_status_t psa_pake_output(
7714 psa_pake_operation_t *operation,
7715 psa_pake_step_t step,
7716 uint8_t *output,
7717 size_t output_size,
7718 size_t *output_length)
7719{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007720 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007721 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007722 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007723
7724 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007725 status = psa_pake_complete_inputs(operation);
7726 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007727 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007728 }
7729 }
7730
7731 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007732 status = PSA_ERROR_BAD_STATE;
7733 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007734 }
7735
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007736 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007737 status = PSA_ERROR_INVALID_ARGUMENT;
7738 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007739 }
7740
Przemek Stekiele12ed362022-12-21 12:54:46 +01007741 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007742#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007743 case PSA_ALG_JPAKE:
7744 status = psa_jpake_output_prologue(operation, step);
7745 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007746 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007747 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007748 driver_step = convert_jpake_computation_stage_to_driver_step(
7749 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007750 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007751#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007752 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007753 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007754 status = PSA_ERROR_NOT_SUPPORTED;
7755 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007756 }
7757
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007758 status = psa_driver_wrapper_pake_output(operation, driver_step,
7759 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007760
7761 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007762 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007763 }
7764
7765 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007766#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007767 case PSA_ALG_JPAKE:
7768 status = psa_jpake_output_epilogue(operation);
7769 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007770 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007771 }
7772 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007773#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007774 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007775 status = PSA_ERROR_NOT_SUPPORTED;
7776 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007777 }
7778
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007779 return PSA_SUCCESS;
7780exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007781 psa_pake_abort(operation);
7782 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007783}
7784
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007785#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007786static psa_status_t psa_jpake_input_prologue(
7787 psa_pake_operation_t *operation,
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007788 psa_pake_step_t step)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007789{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007790 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7791 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7792 step != PSA_PAKE_STEP_ZK_PROOF) {
7793 return PSA_ERROR_INVALID_ARGUMENT;
7794 }
7795
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007796 psa_jpake_computation_stage_t *computation_stage =
7797 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007798
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007799 if (computation_stage->state == PSA_PAKE_STATE_INVALID) {
7800 return PSA_ERROR_BAD_STATE;
7801 }
7802
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007803 if (computation_stage->state != PSA_PAKE_STATE_READY &&
7804 computation_stage->state != PSA_PAKE_INPUT_X1_X2 &&
7805 computation_stage->state != PSA_PAKE_INPUT_X4S) {
7806 return PSA_ERROR_BAD_STATE;
7807 }
7808
7809 if (computation_stage->state == PSA_PAKE_STATE_READY) {
7810 if (step != PSA_PAKE_STEP_KEY_SHARE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007811 return PSA_ERROR_BAD_STATE;
7812 }
7813
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007814 switch (computation_stage->input_step) {
7815 case PSA_PAKE_STEP_X1_X2:
7816 computation_stage->state = PSA_PAKE_INPUT_X1_X2;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007817 break;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007818 case PSA_PAKE_STEP_X2S:
7819 computation_stage->state = PSA_PAKE_INPUT_X4S;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007820 break;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007821 default:
Przemek Stekiele12ed362022-12-21 12:54:46 +01007822 return PSA_ERROR_BAD_STATE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007823 }
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007824
7825 computation_stage->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
7826 }
7827
7828 /* Check if step matches current sequence */
7829 switch (computation_stage->sequence) {
7830 case PSA_PAKE_X1_STEP_KEY_SHARE:
7831 case PSA_PAKE_X2_STEP_KEY_SHARE:
7832 if (step != PSA_PAKE_STEP_KEY_SHARE) {
7833 return PSA_ERROR_BAD_STATE;
7834 }
7835 break;
7836
7837 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
7838 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
7839 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
7840 return PSA_ERROR_BAD_STATE;
7841 }
7842 break;
7843
7844 case PSA_PAKE_X1_STEP_ZK_PROOF:
7845 case PSA_PAKE_X2_STEP_ZK_PROOF:
7846 if (step != PSA_PAKE_STEP_ZK_PROOF) {
7847 return PSA_ERROR_BAD_STATE;
7848 }
7849 break;
7850
7851 default:
7852 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007853 }
7854
7855 return PSA_SUCCESS;
7856}
7857
Przemek Stekiele12ed362022-12-21 12:54:46 +01007858static psa_status_t psa_jpake_input_epilogue(
7859 psa_pake_operation_t *operation)
7860{
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007861 psa_jpake_computation_stage_t *computation_stage =
7862 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007863
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007864 if ((computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007865 computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007866 (computation_stage->state == PSA_PAKE_INPUT_X4S &&
Przemek Stekiel083745e2023-02-23 17:28:23 +01007867 computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007868 computation_stage->state = PSA_PAKE_STATE_READY;
7869 computation_stage->input_step++;
7870 computation_stage->sequence = PSA_PAKE_SEQ_INVALID;
7871 } else {
7872 computation_stage->sequence++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007873 }
7874
7875 return PSA_SUCCESS;
7876}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007877#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007878
Neil Armstronga7d08c32022-06-01 18:21:20 +02007879psa_status_t psa_pake_input(
7880 psa_pake_operation_t *operation,
7881 psa_pake_step_t step,
7882 const uint8_t *input,
7883 size_t input_length)
7884{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007885 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007886 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007887 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
7888 operation->primitive,
7889 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007890
7891 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007892 status = psa_pake_complete_inputs(operation);
7893 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007894 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007895 }
7896 }
7897
7898 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007899 status = PSA_ERROR_BAD_STATE;
7900 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007901 }
7902
Przemek Stekiel256c75d2023-03-23 14:09:34 +01007903 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007904 status = PSA_ERROR_INVALID_ARGUMENT;
7905 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007906 }
7907
Przemek Stekiele12ed362022-12-21 12:54:46 +01007908 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007909#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007910 case PSA_ALG_JPAKE:
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007911 status = psa_jpake_input_prologue(operation, step);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007912 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007913 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007914 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007915 driver_step = convert_jpake_computation_stage_to_driver_step(
7916 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007917 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007918#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007919 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01007920 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007921 status = PSA_ERROR_NOT_SUPPORTED;
7922 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007923 }
7924
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007925 status = psa_driver_wrapper_pake_input(operation, driver_step,
7926 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01007927
7928 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007929 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007930 }
7931
7932 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007933#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007934 case PSA_ALG_JPAKE:
7935 status = psa_jpake_input_epilogue(operation);
7936 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007937 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007938 }
7939 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007940#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007941 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007942 status = PSA_ERROR_NOT_SUPPORTED;
7943 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007944 }
7945
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007946 return PSA_SUCCESS;
7947exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007948 psa_pake_abort(operation);
7949 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007950}
7951
7952psa_status_t psa_pake_get_implicit_key(
7953 psa_pake_operation_t *operation,
7954 psa_key_derivation_operation_t *output)
7955{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007956 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007957 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007958 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01007959 size_t shared_key_len = 0;
7960
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007961 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007962 status = PSA_ERROR_BAD_STATE;
7963 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007964 }
7965
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007966#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007967 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007968 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01007969 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007970 if (computation_stage->input_step != PSA_PAKE_STEP_DERIVE ||
7971 computation_stage->output_step != PSA_PAKE_STEP_DERIVE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007972 status = PSA_ERROR_BAD_STATE;
7973 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007974 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01007975 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007976#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007977 {
7978 status = PSA_ERROR_NOT_SUPPORTED;
7979 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007980 }
7981
Przemek Stekiel0c781802022-11-29 14:53:13 +01007982 status = psa_driver_wrapper_pake_get_implicit_key(operation,
7983 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01007984 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01007985 &shared_key_len);
7986
7987 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007988 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01007989 }
7990
7991 status = psa_key_derivation_input_bytes(output,
7992 PSA_KEY_DERIVATION_INPUT_SECRET,
7993 shared_key,
7994 shared_key_len);
7995
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007996 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007997exit:
7998 abort_status = psa_pake_abort(operation);
7999 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008000}
8001
8002psa_status_t psa_pake_abort(
8003 psa_pake_operation_t *operation)
8004{
Przemek Stekield69dca92023-01-31 19:59:20 +01008005 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008006
Przemek Stekield69dca92023-01-31 19:59:20 +01008007 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008008 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008009 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008010
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008011 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8012 if (operation->data.inputs.password != NULL) {
8013 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008014 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008015 mbedtls_free(operation->data.inputs.password);
8016 }
8017 if (operation->data.inputs.user != NULL) {
8018 mbedtls_free(operation->data.inputs.user);
8019 }
8020 if (operation->data.inputs.peer != NULL) {
8021 mbedtls_free(operation->data.inputs.peer);
8022 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008023 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008024 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008025
Przemek Stekield69dca92023-01-31 19:59:20 +01008026 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008027}
Neil Armstronga7d08c32022-06-01 18:21:20 +02008028
Gilles Peskinee59236f2018-01-27 23:32:46 +01008029#endif /* MBEDTLS_PSA_CRYPTO_C */