blob: 96547e04dc7bffebf87a9b921be1250d35f32cec [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
Gilles Peskinee59236f2018-01-27 23:32:46 +010029#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010030#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031
Ronald Crond6d28882020-12-14 14:56:02 +010032#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020035#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010037#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010038#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010039#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010040#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020042#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010044#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010045/* Include internal declarations that are useful for implementing persistently
46 * stored keys. */
47#include "psa_crypto_storage.h"
48
Gilles Peskineb2b64d32020-12-14 16:43:58 +010049#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010050
Gilles Peskineb46bef22019-07-30 21:32:04 +020051#include <assert.h>
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;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100111 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112} psa_global_data_t;
113
114static psa_global_data_t global_data;
115
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100116#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
117mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
118 &global_data.rng.drbg;
119#endif
120
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (global_data.initialized == 0) \
123 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100127 /* Mbed TLS error codes can combine a high-level error code and a
128 * low-level error code. The low-level error usually reflects the
129 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 int low_level_ret = -(-ret & 0x007f);
131 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100134
135 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
136 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200138 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
139 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
140 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
141 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
142 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200144 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200146 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200148
Jaeden Amero93e21112019-02-20 13:57:28 +0000149#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000150 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000151#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100152 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100154
155 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100159
Gilles Peskine26869f22019-05-06 15:25:00 +0200160 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200162
163 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200165 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200167
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100170 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
184 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100185 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
186 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100187 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
190 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100194#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100195
196 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100198
Gilles Peskinee59236f2018-01-27 23:32:46 +0100199 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
200 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
201 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203
204 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200206 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210
Gilles Peskine82e57d12020-11-13 21:31:17 +0100211#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100213 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
214 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100215 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100217 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100220 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100222#endif
223
Gilles Peskinea5905292018-02-07 20:59:33 +0100224 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100226 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100228 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
Gilles Peskinef76aa772018-10-29 19:24:33 +0100233 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100235 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100237 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100239 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100241 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100243 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100245 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100247 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100249
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100250 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100252 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
253 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100255 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100257 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
258 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100260 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
263 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
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_INVALID_ALG:
268 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
269 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200273 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100275
Gilles Peskineff2d2002019-05-06 15:26:23 +0200276 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200278 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200280
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100283 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100285 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100289 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
290 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200298
itayzafrir5c753392018-05-08 11:18:38 +0300299 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300300 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300302 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300304 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300306 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
307 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300309 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100311 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100313
Paul Elliott588f8ed2022-12-02 18:10:26 +0000314 case MBEDTLS_ERR_ECP_IN_PROGRESS:
315 return PSA_OPERATION_INCOMPLETE;
316
Janos Follatha13b9052019-11-22 12:48:59 +0000317 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300319
Gilles Peskinee59236f2018-01-27 23:32:46 +0100320 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100322 }
323}
324
Paul Elliottdc42ca82023-02-24 18:11:59 +0000325/**
326 * \brief For output buffers which contain "tags"
327 * (outputs that may be checked for validity like
328 * hashes, MACs and signatures), fill the unused
329 * part of the output buffer (the whole buffer on
330 * error, the trailing part on success) with
331 * something that isn't a valid tag (barring an
332 * attack on the tag and deliberately-crafted
333 * input), in case the caller doesn't check the
334 * return status properly.
335 *
336 * \param output_buffer Pointer to buffer to wipe. May not be NULL
337 * unless \p output_buffer_size is zero.
338 * \param status Status of function called to generate
339 * output_buffer originally
340 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
341 * could be NULL.
342 * \param output_buffer_length Length of data written to output_buffer, must be
343 * less than \p output_buffer_size
344 */
345static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000346 size_t output_buffer_size, size_t output_buffer_length)
347{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000348 size_t offset = 0;
349
350 if (output_buffer_size == 0) {
351 /* If output_buffer_size is 0 then we have nothing to do. We must not
352 call memset because output_buffer may be NULL in this case */
353 return;
354 }
355
356 if (status == PSA_SUCCESS) {
357 offset = output_buffer_length;
358 }
359
360 memset(output_buffer + offset, '!', output_buffer_size - offset);
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200364
365
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366/****************************************************************/
367/* Key management */
368/****************************************************************/
369
John Durkop9814fa22020-11-04 12:28:15 -0800370#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800371 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100372 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
373 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
374 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100375mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
376 size_t bits,
377 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200378{
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100380 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100382#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100383 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100385#endif
386#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100387 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100389#endif
390#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100391 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100393#endif
394#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100397#endif
398#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100399 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100401 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (bits_is_sloppy) {
403 return MBEDTLS_ECP_DP_SECP521R1;
404 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100405 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100406#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100407 }
408 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100409
Paul Elliott8ff510a2020-06-02 17:19:28 +0100410 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100412#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100413 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100415#endif
416#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100419#endif
420#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100421 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100423#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 }
425 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100426
Paul Elliott8ff510a2020-06-02 17:19:28 +0100427 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100429#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100430 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100432 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (bits_is_sloppy) {
434 return MBEDTLS_ECP_DP_CURVE25519;
435 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100436 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100437#endif
438#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100439 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100441#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100442 }
443 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100444
Paul Elliott8ff510a2020-06-02 17:19:28 +0100445 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100447#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100448 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#endif
451#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100452 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100454#endif
455#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100456 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100458#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100459 }
460 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200461 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100462
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100463 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200465}
John Durkop9814fa22020-11-04 12:28:15 -0800466#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100467 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
468 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
469 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
470 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
473 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474{
475 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200478 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200479 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200480 case PSA_KEY_TYPE_PASSWORD:
481 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200482 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100483#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (bits != 128 && bits != 192 && bits != 256) {
486 return PSA_ERROR_INVALID_ARGUMENT;
487 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200488 break;
489#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200490#if defined(PSA_WANT_KEY_TYPE_ARIA)
491 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (bits != 128 && bits != 192 && bits != 256) {
493 return PSA_ERROR_INVALID_ARGUMENT;
494 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200495 break;
496#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100497#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200498 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (bits != 128 && bits != 192 && bits != 256) {
500 return PSA_ERROR_INVALID_ARGUMENT;
501 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200502 break;
503#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100504#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (bits != 64 && bits != 128 && bits != 192) {
507 return PSA_ERROR_INVALID_ARGUMENT;
508 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509 break;
510#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100511#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200512 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (bits != 256) {
514 return PSA_ERROR_INVALID_ARGUMENT;
515 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200516 break;
517#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200518 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200520 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (bits % 8 != 0) {
522 return PSA_ERROR_INVALID_ARGUMENT;
523 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526}
527
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100528/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100529 *
Steven Cooremancd640932021-03-08 12:00:27 +0100530 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
531 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100532 *
533 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100534 * \param[in] key_type The key type of the key to be used with the
535 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100536 *
537 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100538 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100539 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100540 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100541 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100542MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100543 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100545{
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (PSA_ALG_IS_HMAC(algorithm)) {
547 if (key_type == PSA_KEY_TYPE_HMAC) {
548 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100549 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100550 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
553 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
554 * key. */
555 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
556 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
557 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
558 * the block length (larger than 1) for block ciphers. */
559 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
560 return PSA_SUCCESS;
561 }
562 }
563 }
564
565 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100566}
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
569 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200570{
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (slot->key.data != NULL) {
572 return PSA_ERROR_ALREADY_EXISTS;
573 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 slot->key.data = mbedtls_calloc(1, buffer_length);
576 if (slot->key.data == NULL) {
577 return PSA_ERROR_INSUFFICIENT_MEMORY;
578 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200579
Ronald Cronea0f8a62020-11-25 17:52:23 +0100580 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200582}
583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
585 const uint8_t *data,
586 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200587{
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 psa_status_t status = psa_allocate_buffer_to_slot(slot,
589 data_length);
590 if (status != PSA_SUCCESS) {
591 return status;
592 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 memcpy(slot->key.data, data, data_length);
595 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200596}
597
Ronald Crona0fe59f2020-11-29 11:14:53 +0100598psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100599 const psa_key_attributes_t *attributes,
600 const uint8_t *data, size_t data_length,
601 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
605 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200607 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (data_length == 0) {
609 return PSA_ERROR_NOT_SUPPORTED;
610 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (key_type_is_raw_bytes(type)) {
613 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
616 *bits);
617 if (status != PSA_SUCCESS) {
618 return status;
619 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200620
Ronald Crondd04d422020-11-28 15:14:42 +0100621 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100623 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return PSA_SUCCESS;
627 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800628#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
630 if (PSA_KEY_TYPE_IS_ECC(type)) {
631 return mbedtls_psa_ecp_import_key(attributes,
632 data, data_length,
633 key_buffer, key_buffer_size,
634 key_buffer_length,
635 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100636 }
John Durkop9814fa22020-11-04 12:28:15 -0800637#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
638 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700639#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
641 if (PSA_KEY_TYPE_IS_RSA(type)) {
642 return mbedtls_psa_rsa_import_key(attributes,
643 data, data_length,
644 key_buffer, key_buffer_size,
645 key_buffer_length,
646 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200647 }
John Durkop9814fa22020-11-04 12:28:15 -0800648#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
649 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100650 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100653}
654
Gilles Peskinef603c712019-01-19 13:40:11 +0100655/** Calculate the intersection of two algorithm usage policies.
656 *
657 * Return 0 (which allows no operation) on incompatibility.
658 */
659static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100660 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100661 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100663{
Gilles Peskine549ea862019-05-22 11:45:59 +0200664 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (alg1 == alg2) {
666 return alg1;
667 }
Steven Cooreman03488022021-02-18 12:07:20 +0100668 /* If the policies are from the same hash-and-sign family, check
669 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
671 PSA_ALG_IS_SIGN_HASH(alg2) &&
672 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
673 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
674 return alg2;
675 }
676 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
677 return alg1;
678 }
Steven Cooreman03488022021-02-18 12:07:20 +0100679 }
680 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100681 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100682 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
684 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
685 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
686 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
687 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100688 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100689
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100690 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
692 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
693 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
694 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100695 }
696 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
698 (alg1_len <= alg2_len)) {
699 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100700 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
702 (alg2_len <= alg1_len)) {
703 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100704 }
705 }
706 /* If the policies are from the same MAC family, check whether one
707 * of them is a minimum-MAC-length policy. Calculate the most
708 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
710 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
711 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100712 /* Validate the combination of key type and algorithm. Since the base
713 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
715 return 0;
716 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100717
Steven Cooreman31a876d2021-03-03 20:47:40 +0100718 /* Get the (exact or at-least) output lengths for both sides of the
719 * requested intersection. None of the currently supported algorithms
720 * have an output length dependent on the actual key size, so setting it
721 * to a bogus value of 0 is currently OK.
722 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100723 * Note that for at-least-this-length wildcard algorithms, the output
724 * length is set to the shortest allowed length, which allows us to
725 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
727 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100728 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100729
Steven Cooremana1d83222021-02-25 10:20:29 +0100730 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
732 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
733 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100734 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100735
736 /* If only one is an at-least-this-length policy, the intersection would
737 * be the other (fixed-length) policy as long as said fixed length is
738 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
740 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100741 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
743 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100744 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100745
Steven Cooremancd640932021-03-08 12:00:27 +0100746 /* If none of them are wildcards, check whether they define the same tag
747 * length. This is still possible here when one is default-length and
748 * the other specific-length. Ensure to always return the
749 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (alg1_len == alg2_len) {
751 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
752 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100753 }
754 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100756}
757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758static int psa_key_algorithm_permits(psa_key_type_t key_type,
759 psa_algorithm_t policy_alg,
760 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200761{
Gilles Peskine549ea862019-05-22 11:45:59 +0200762 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 if (requested_alg == policy_alg) {
764 return 1;
765 }
Steven Cooreman03488022021-02-18 12:07:20 +0100766 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
767 * and requested_alg is the same hash-and-sign family with any hash,
768 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
770 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
771 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
772 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100773 }
774 /* If policy_alg is a wildcard AEAD algorithm of the same base as
775 * the requested algorithm, check the requested tag length to be
776 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (PSA_ALG_IS_AEAD(policy_alg) &&
778 PSA_ALG_IS_AEAD(requested_alg) &&
779 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
780 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
781 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
782 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
783 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100784 }
Steven Cooremancd640932021-03-08 12:00:27 +0100785 /* If policy_alg is a MAC algorithm of the same base as the requested
786 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (PSA_ALG_IS_MAC(policy_alg) &&
788 PSA_ALG_IS_MAC(requested_alg) &&
789 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
790 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100791 /* Validate the combination of key type and algorithm. Since the policy
792 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
794 return 0;
795 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100796
Steven Cooreman31a876d2021-03-03 20:47:40 +0100797 /* Get both the requested output length for the algorithm which is to be
798 * verified, and the default output length for the base algorithm.
799 * Note that none of the currently supported algorithms have an output
800 * length dependent on actual key size, so setting it to a bogus value
801 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100802 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100804 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 key_type, 0,
806 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100807
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100808 /* If the policy is default-length, only allow an algorithm with
809 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
811 return requested_output_length == default_output_length;
812 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100813
814 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100815 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
817 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
818 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100819 }
820
Steven Cooremancd640932021-03-08 12:00:27 +0100821 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
822 * check for the requested MAC length to be equal to or longer than the
823 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
825 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
826 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100827 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200828 }
Steven Cooremance48e852020-10-05 16:02:45 +0200829 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200830 * a key derivation with that key agreement should also be allowed. This
831 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
833 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
834 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
835 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200836 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100837 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200839}
840
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841/** Test whether a policy permits an algorithm.
842 *
843 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100844 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100845 * \note This function requires providing the key type for which the policy is
846 * being validated, since some algorithm policy definitions (e.g. MAC)
847 * have different properties depending on what kind of cipher it is
848 * combined with.
849 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100850 * \retval PSA_SUCCESS When \p alg is a specific algorithm
851 * allowed by the \p policy.
852 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
853 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
854 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100855 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
857 psa_key_type_t key_type,
858 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100859{
Steven Cooremand788fab2021-02-25 11:29:17 +0100860 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (alg == 0) {
862 return PSA_ERROR_INVALID_ARGUMENT;
863 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100864
Steven Cooreman7e39f052021-02-23 14:18:32 +0100865 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (PSA_ALG_IS_WILDCARD(alg)) {
867 return PSA_ERROR_INVALID_ARGUMENT;
868 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
871 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
872 return PSA_SUCCESS;
873 } else {
874 return PSA_ERROR_NOT_PERMITTED;
875 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100876}
877
Gilles Peskinef603c712019-01-19 13:40:11 +0100878/** Restrict a key policy based on a constraint.
879 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100880 * \note This function requires providing the key type for which the policy is
881 * being restricted, since some algorithm policy definitions (e.g. MAC)
882 * have different properties depending on what kind of cipher it is
883 * combined with.
884 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100885 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100886 * \param[in,out] policy The policy to restrict.
887 * \param[in] constraint The policy constraint to apply.
888 *
889 * \retval #PSA_SUCCESS
890 * \c *policy contains the intersection of the original value of
891 * \c *policy and \c *constraint.
892 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100893 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100894 * \c *policy is unchanged.
895 */
896static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100897 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100898 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100900{
901 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 psa_key_policy_algorithm_intersection(key_type, policy->alg,
903 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200904 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
906 constraint->alg2);
907 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
908 return PSA_ERROR_INVALID_ARGUMENT;
909 }
910 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
911 return PSA_ERROR_INVALID_ARGUMENT;
912 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100913 policy->usage &= constraint->usage;
914 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200915 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100917}
918
Przemek Stekiel348410f2022-11-15 22:22:07 +0100919psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100920 mbedtls_svc_key_id_t key,
921 psa_key_slot_t **p_slot,
922 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100924{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200925 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
926 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 status = psa_get_and_lock_key_slot(key, p_slot);
929 if (status != PSA_SUCCESS) {
930 return status;
931 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200932 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100933
934 /* Enforce that usage policy for the key slot contains all the flags
935 * required by the usage parameter. There is one exception: public
936 * keys can always be exported, so we treat public key objects as
937 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100939 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100943 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200944 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100945 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100946
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800947 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (alg != 0) {
949 status = psa_key_policy_permits(&slot->attr.policy,
950 slot->attr.type,
951 alg);
952 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100953 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100955 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200958
959error:
960 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100964}
Darryl Green940d72c2018-07-13 13:18:51 +0100965
Ronald Cron5c522922020-11-14 16:35:34 +0100966/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200967 *
968 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200969 * available, as opposed to a key in a secure element and/or to be used
970 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200971 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200972 * This is a temporary function that may be used instead of
973 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
974 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200975 *
Ronald Cron5c522922020-11-14 16:35:34 +0100976 * On success, the returned key slot is locked. It is the responsibility of the
977 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200978 */
Ronald Cron5c522922020-11-14 16:35:34 +0100979static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
980 mbedtls_svc_key_id_t key,
981 psa_key_slot_t **p_slot,
982 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200984{
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
986 usage, alg);
987 if (status != PSA_SUCCESS) {
988 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200989 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
992 psa_unlock_key_slot(*p_slot);
993 *p_slot = NULL;
994 return PSA_ERROR_NOT_SUPPORTED;
995 }
996
997 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200998}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001001{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001002 /* Data pointer will always be either a valid pointer or NULL in an
1003 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (slot->key.data != NULL) {
1005 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1006 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001009 slot->key.data = NULL;
1010 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001013}
1014
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001015/** Completely wipe a slot in memory, including its policy.
1016 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001017psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001018{
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 /*
1022 * As the return error code may not be handled in case of multiple errors,
1023 * do our best to report an unexpected lock counter. Assert with
1024 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1025 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1026 * function is called as part of the execution of a test suite, the
1027 * execution of the test suite is stopped in error if the assertion fails.
1028 */
1029 if (slot->lock_count != 1) {
1030 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001031 status = PSA_ERROR_CORRUPTION_DETECTED;
1032 }
1033
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001034 /* Multipart operations may still be using the key. This is safe
1035 * because all multipart operation objects are independent from
1036 * the key slot: if they need to access the key after the setup
1037 * phase, they have a copy of the key. Note that this means that
1038 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001039 /* At this point, key material and other type-specific content has
1040 * been wiped. Clear remaining metadata. We can call memset and not
1041 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 memset(slot, 0, sizeof(*slot));
1043 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001044}
1045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001047{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001048 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001049 psa_status_t status; /* status of the last operation */
1050 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001051#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1052 psa_se_drv_table_entry_t *driver;
1053#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (mbedtls_svc_key_id_is_null(key)) {
1056 return PSA_SUCCESS;
1057 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001058
Ronald Cronf2911112020-10-29 17:51:10 +01001059 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001060 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001061 * key, this will load the key description from persistent memory if not
1062 * done yet. We cannot avoid this loading as without it we don't know if
1063 * the key is operated by an SE or not and this information is needed by
1064 * the current implementation.
1065 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 status = psa_get_and_lock_key_slot(key, &slot);
1067 if (status != PSA_SUCCESS) {
1068 return status;
1069 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001070
Ronald Cronf2911112020-10-29 17:51:10 +01001071 /*
1072 * If the key slot containing the key description is under access by the
1073 * library (apart from the present access), the key cannot be destroyed
1074 * yet. For the time being, just return in error. Eventually (to be
1075 * implemented), the key should be destroyed when all accesses have
1076 * stopped.
1077 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if (slot->lock_count > 1) {
1079 psa_unlock_key_slot(slot);
1080 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001081 }
1082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001084 /* Refuse the destruction of a read-only key (which may or may not work
1085 * if we attempt it, depending on whether the key is merely read-only
1086 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001087 * Just do the best we can, which is to wipe the copy in memory
1088 * (done in this function's cleanup code). */
1089 overall_status = PSA_ERROR_NOT_PERMITTED;
1090 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001091 }
1092
Gilles Peskine354f7672019-07-12 23:46:38 +02001093#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1095 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001096 /* For a key in a secure element, we need to do three things:
1097 * remove the key file in internal storage, destroy the
1098 * key inside the secure element, and update the driver's
1099 * persistent data. Start a transaction that will encompass these
1100 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001102 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001104 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 status = psa_crypto_save_transaction();
1106 if (status != PSA_SUCCESS) {
1107 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001108 /* We should still try to destroy the key in the secure
1109 * element and the key metadata in storage. This is especially
1110 * important if the error is that the storage is full.
1111 * But how to do it exactly without risking an inconsistent
1112 * state after a reset?
1113 * https://github.com/ARMmbed/mbed-crypto/issues/215
1114 */
1115 overall_status = status;
1116 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001117 }
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 status = psa_destroy_se_key(driver,
1120 psa_key_slot_get_slot_number(slot));
1121 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001122 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001124 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001125#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1126
Darryl Greend49a4992018-06-18 17:27:26 +01001127#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1129 status = psa_destroy_persistent_key(slot->attr.id);
1130 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001131 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001133
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001134 /* TODO: other slots may have a copy of the same key. We should
1135 * invalidate them.
1136 * https://github.com/ARMmbed/mbed-crypto/issues/214
1137 */
Darryl Greend49a4992018-06-18 17:27:26 +01001138 }
1139#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001140
Gilles Peskinefc762652019-07-22 19:30:34 +02001141#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (driver != NULL) {
1143 status = psa_save_se_persistent_data(driver);
1144 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001145 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 }
1147 status = psa_crypto_stop_transaction();
1148 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001149 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001151 }
1152#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1153
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001154exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001156 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001158 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 }
1160 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001161}
1162
John Durkop07cc04a2020-11-16 22:08:34 -08001163#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001164 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001165static psa_status_t psa_get_rsa_public_exponent(
1166 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001168{
1169 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001171 uint8_t *buffer = NULL;
1172 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1176 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001177 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 }
1179 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001180 /* It's the default value, which is reported as an empty string,
1181 * so there's nothing to do. */
1182 goto exit;
1183 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 buflen = mbedtls_mpi_size(&mpi);
1186 buffer = mbedtls_calloc(1, buflen);
1187 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001188 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1189 goto exit;
1190 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1192 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001193 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001195 attributes->domain_parameters = buffer;
1196 attributes->domain_parameters_size = buflen;
1197
1198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 mbedtls_mpi_free(&mpi);
1200 if (ret != 0) {
1201 mbedtls_free(buffer);
1202 }
1203 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001204}
John Durkop07cc04a2020-11-16 22:08:34 -08001205#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001206 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001207
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001208/** Retrieve all the publicly-accessible attributes of a key.
1209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1211 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001212{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001214 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001215 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1220 if (status != PSA_SUCCESS) {
1221 return status;
1222 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001223
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001224 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1226 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001227
1228#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1230 psa_set_key_slot_number(attributes,
1231 psa_key_slot_get_slot_number(slot));
1232 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001233#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001236#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001237 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001238 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001239 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001240 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001241 * is not yet implemented.
1242 * https://github.com/ARMmbed/mbed-crypto/issues/216
1243 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001245 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001246
Ronald Cron90857082020-11-25 14:58:33 +01001247 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 slot->attr.type,
1249 slot->key.data,
1250 slot->key.bytes,
1251 &rsa);
1252 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001253 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 status = psa_get_rsa_public_exponent(rsa,
1257 attributes);
1258 mbedtls_rsa_free(rsa);
1259 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001260 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001261 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001262#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001263 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001264 default:
1265 /* Nothing else to do. */
1266 break;
1267 }
1268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (status != PSA_SUCCESS) {
1270 psa_reset_key_attributes(attributes);
1271 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001276}
1277
Gilles Peskinec8000c02019-08-02 20:15:51 +02001278#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1279psa_status_t psa_get_key_slot_number(
1280 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001282{
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001284 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 return PSA_SUCCESS;
1286 } else {
1287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001288 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001289}
1290#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1293 size_t key_buffer_size,
1294 uint8_t *data,
1295 size_t data_size,
1296 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001297{
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (key_buffer_size > data_size) {
1299 return PSA_ERROR_BUFFER_TOO_SMALL;
1300 }
1301 memcpy(data, key_buffer, key_buffer_size);
1302 memset(data + key_buffer_size, 0,
1303 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001304 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001306}
1307
Ronald Cron7285cda2020-11-26 14:46:37 +01001308psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001309 const psa_key_attributes_t *attributes,
1310 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312{
Ronald Crond18b5f82020-11-25 16:46:05 +01001313 psa_key_type_t type = attributes->core.type;
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (key_type_is_raw_bytes(type) ||
1316 PSA_KEY_TYPE_IS_RSA(type) ||
1317 PSA_KEY_TYPE_IS_ECC(type)) {
1318 return psa_export_key_buffer_internal(
1319 key_buffer, key_buffer_size,
1320 data, data_size, data_length);
1321 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001322 /* This shouldn't happen in the reference implementation, but
1323 it is valid for a special-purpose implementation to omit
1324 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001326 }
1327}
1328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1330 uint8_t *data,
1331 size_t data_size,
1332 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001333{
1334 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1335 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1336 psa_key_slot_t *slot;
1337
Ronald Crone907e552021-01-18 13:22:38 +01001338 /* Reject a zero-length output buffer now, since this can never be a
1339 * valid key representation. This way we know that data must be a valid
1340 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 if (data_size == 0) {
1342 return PSA_ERROR_BUFFER_TOO_SMALL;
1343 }
Ronald Crone907e552021-01-18 13:22:38 +01001344
Ronald Cron9486f9d2020-11-26 13:36:23 +01001345 /* Set the key to empty now, so that even when there are errors, we always
1346 * set data_length to a value between 0 and data_size. On error, setting
1347 * the key to empty is a good choice because an empty key representation is
1348 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001349 *data_length = 0;
1350
Ronald Cron9486f9d2020-11-26 13:36:23 +01001351 /* Export requires the EXPORT flag. There is an exception for public keys,
1352 * which don't require any flag, but
1353 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1354 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1356 PSA_KEY_USAGE_EXPORT, 0);
1357 if (status != PSA_SUCCESS) {
1358 return status;
1359 }
mohammad160306e79202018-03-28 13:17:44 +03001360
Ronald Crond18b5f82020-11-25 16:46:05 +01001361 psa_key_attributes_t attributes = {
1362 .core = slot->attr
1363 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 status = psa_driver_wrapper_export_key(&attributes,
1365 slot->key.data, slot->key.bytes,
1366 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001371}
1372
Ronald Cron7285cda2020-11-26 14:46:37 +01001373psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001374 const psa_key_attributes_t *attributes,
1375 const uint8_t *key_buffer,
1376 size_t key_buffer_size,
1377 uint8_t *data,
1378 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001380{
Ronald Crond18b5f82020-11-25 16:46:05 +01001381 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1384 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001385 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 return psa_export_key_buffer_internal(
1387 key_buffer, key_buffer_size,
1388 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001389 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001392#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1394 return mbedtls_psa_rsa_export_public_key(attributes,
1395 key_buffer,
1396 key_buffer_size,
1397 data,
1398 data_size,
1399 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001400#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001401 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001403#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1404 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001406#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1408 return mbedtls_psa_ecp_export_public_key(attributes,
1409 key_buffer,
1410 key_buffer_size,
1411 data,
1412 data_size,
1413 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001414#else
1415 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001417#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1418 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001419 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001421 /* This shouldn't happen in the reference implementation, but
1422 it is valid for a special-purpose implementation to omit
1423 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001425 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001426}
Ronald Crond18b5f82020-11-25 16:46:05 +01001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1429 uint8_t *data,
1430 size_t data_size,
1431 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001432{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001433 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001434 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001435 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001436
Ronald Crone907e552021-01-18 13:22:38 +01001437 /* Reject a zero-length output buffer now, since this can never be a
1438 * valid key representation. This way we know that data must be a valid
1439 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (data_size == 0) {
1441 return PSA_ERROR_BUFFER_TOO_SMALL;
1442 }
Ronald Crone907e552021-01-18 13:22:38 +01001443
Darryl Greendd8fb772018-11-07 16:00:44 +00001444 /* Set the key to empty now, so that even when there are errors, we always
1445 * set data_length to a value between 0 and data_size. On error, setting
1446 * the key to empty is a good choice because an empty key representation is
1447 * unlikely to be accepted anywhere. */
1448 *data_length = 0;
1449
1450 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1452 if (status != PSA_SUCCESS) {
1453 return status;
1454 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1457 status = PSA_ERROR_INVALID_ARGUMENT;
1458 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001459 }
1460
Ronald Crond18b5f82020-11-25 16:46:05 +01001461 psa_key_attributes_t attributes = {
1462 .core = slot->attr
1463 };
Ronald Cron67227982020-11-26 15:16:05 +01001464 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001465 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001467
1468exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001472}
1473
Gilles Peskine91e8c332019-08-02 19:19:39 +02001474#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001475static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1476 "One or more key attribute flag is listed as both external-only and dual-use");
1477static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1478 "One or more key attribute flag is listed as both internal-only and dual-use");
1479static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1480 "One or more key attribute flag is listed as both internal-only and external-only");
Gilles Peskine91e8c332019-08-02 19:19:39 +02001481#endif
1482
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001483/** Validate that a key policy is internally well-formed.
1484 *
1485 * This function only rejects invalid policies. It does not validate the
1486 * consistency of the policy with respect to other attributes of the key
1487 * such as the key type.
1488 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001489static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001490{
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1492 PSA_KEY_USAGE_COPY |
1493 PSA_KEY_USAGE_ENCRYPT |
1494 PSA_KEY_USAGE_DECRYPT |
1495 PSA_KEY_USAGE_SIGN_MESSAGE |
1496 PSA_KEY_USAGE_VERIFY_MESSAGE |
1497 PSA_KEY_USAGE_SIGN_HASH |
1498 PSA_KEY_USAGE_VERIFY_HASH |
1499 PSA_KEY_USAGE_VERIFY_DERIVATION |
1500 PSA_KEY_USAGE_DERIVE)) != 0) {
1501 return PSA_ERROR_INVALID_ARGUMENT;
1502 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001505}
1506
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001507/** Validate the internal consistency of key attributes.
1508 *
1509 * This function only rejects invalid attribute values. If does not
1510 * validate the consistency of the attributes with any key data that may
1511 * be involved in the creation of the key.
1512 *
1513 * Call this function early in the key creation process.
1514 *
1515 * \param[in] attributes Key attributes for the new key.
1516 * \param[out] p_drv On any return, the driver for the key, if any.
1517 * NULL for a transparent key.
1518 *
1519 */
1520static psa_status_t psa_validate_key_attributes(
1521 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001523{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001524 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1526 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 status = psa_validate_key_location(lifetime, p_drv);
1529 if (status != PSA_SUCCESS) {
1530 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001531 }
1532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 status = psa_validate_key_persistence(lifetime);
1534 if (status != PSA_SUCCESS) {
1535 return status;
1536 }
1537
1538 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1539 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1540 return PSA_ERROR_INVALID_ARGUMENT;
1541 }
1542 } else {
1543 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1544 return PSA_ERROR_INVALID_ARGUMENT;
1545 }
1546 }
1547
1548 status = psa_validate_key_policy(&attributes->core.policy);
1549 if (status != PSA_SUCCESS) {
1550 return status;
1551 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001552
1553 /* Refuse to create overly large keys.
1554 * Note that this doesn't trigger on import if the attributes don't
1555 * explicitly specify a size (so psa_get_key_bits returns 0), so
1556 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1558 return PSA_ERROR_NOT_SUPPORTED;
1559 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001560
Gilles Peskine91e8c332019-08-02 19:19:39 +02001561 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1563 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1564 return PSA_ERROR_INVALID_ARGUMENT;
1565 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001568}
1569
Gilles Peskine4747d192019-04-17 15:05:45 +02001570/** Prepare a key slot to receive key material.
1571 *
1572 * This function allocates a key slot and sets its metadata.
1573 *
1574 * If this function fails, call psa_fail_key_creation().
1575 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001576 * This function is intended to be used as follows:
1577 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001578 * it with the specified attributes, and in case of a volatile key assign it
1579 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001580 * -# Populate the slot with the key material.
1581 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1582 * In case of failure at any step, stop the sequence and call
1583 * psa_fail_key_creation().
1584 *
Ronald Cron5c522922020-11-14 16:35:34 +01001585 * On success, the key slot is locked. It is the responsibility of the caller
1586 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001587 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001588 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001589 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001590 * \param[out] p_slot On success, a pointer to the prepared slot.
1591 * \param[out] p_drv On any return, the driver for the key, if any.
1592 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001593 *
1594 * \retval #PSA_SUCCESS
1595 * The key slot is ready to receive key material.
1596 * \return If this function fails, the key slot is an invalid state.
1597 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001598 */
1599static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001600 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001601 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001602 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001604{
1605 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001606 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001607 psa_key_slot_t *slot;
1608
Gilles Peskinedf179142019-07-15 22:02:14 +02001609 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001610 *p_drv = NULL;
1611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 status = psa_validate_key_attributes(attributes, p_drv);
1613 if (status != PSA_SUCCESS) {
1614 return status;
1615 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1618 if (status != PSA_SUCCESS) {
1619 return status;
1620 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001621 slot = *p_slot;
1622
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001623 /* We're storing the declared bit-size of the key. It's up to each
1624 * creation mechanism to verify that this information is correct.
1625 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001626 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001627 * is optional (import, copy). In case of a volatile key, assign it the
1628 * volatile key identifier associated to the slot returned to contain its
1629 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001630
1631 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001633#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1634 slot->attr.id = volatile_key_id;
1635#else
1636 slot->attr.id.key_id = volatile_key_id;
1637#endif
1638 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001639
Gilles Peskine91e8c332019-08-02 19:19:39 +02001640 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001641 * external-only flags, query `attributes`. Thanks to the check
1642 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001643 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001644 * may have set. */
1645 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001646
Gilles Peskinecbaff462019-07-12 23:46:04 +02001647#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001648 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001649 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001650 * create the key file in internal storage, create the
1651 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001652 * persistent data. This is done by starting a transaction that will
1653 * encompass these three actions.
1654 * For registering a volatile key, we just need to find an appropriate
1655 * slot number inside the SE. Since the key is designated volatile, creating
1656 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001657 /* The first thing to do is to find a slot number for the new key.
1658 * We save the slot number in persistent storage as part of the
1659 * transaction data. It will be needed to recover if the power
1660 * fails during the key creation process, to clean up on the secure
1661 * element side after restarting. Obtaining a slot number from the
1662 * secure element driver updates its persistent state, but we do not yet
1663 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001664 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001666 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1668 &slot_number);
1669 if (status != PSA_SUCCESS) {
1670 return status;
1671 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1674 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001675 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001676 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001677 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 status = psa_crypto_save_transaction();
1679 if (status != PSA_SUCCESS) {
1680 (void) psa_crypto_stop_transaction();
1681 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001682 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001683 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001684
1685 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001687 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001690 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001692 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001693#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001696}
Gilles Peskine4747d192019-04-17 15:05:45 +02001697
1698/** Finalize the creation of a key once its key material has been set.
1699 *
1700 * This entails writing the key to persistent storage.
1701 *
1702 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001703 * See the documentation of psa_start_key_creation() for the intended use
1704 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001705 *
Ronald Cron5c522922020-11-14 16:35:34 +01001706 * If the finalization succeeds, the function unlocks the key slot (it was
1707 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1708 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001709 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001710 * \param[in,out] slot Pointer to the slot with key material.
1711 * \param[in] driver The secure element driver for the key,
1712 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001713 * \param[out] key On success, identifier of the key. Note that the
1714 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001715 *
1716 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001717 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001718 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1719 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1720 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1721 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1722 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1723 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001724 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001725 * \return If this function fails, the key slot is an invalid state.
1726 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001727 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001728static psa_status_t psa_finish_key_creation(
1729 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001730 psa_se_drv_table_entry_t *driver,
1731 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001732{
1733 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001734 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001735 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001736
1737#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001739#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001741 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001742 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001744
Gilles Peskineb46bef22019-07-30 21:32:04 +02001745#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 static_assert(sizeof(slot_number) ==
1747 sizeof(data.slot_number),
1748 "Slot number size does not match psa_se_key_data_storage_t");
Gilles Peskineb46bef22019-07-30 21:32:04 +02001749#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1751 status = psa_save_persistent_key(&slot->attr,
1752 (uint8_t *) &data,
1753 sizeof(data));
1754 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001755#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1756 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001757 /* Key material is saved in export representation in the slot, so
1758 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 status = psa_save_persistent_key(&slot->attr,
1760 slot->key.data,
1761 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001762 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001763 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001764#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1765
Gilles Peskinecbaff462019-07-12 23:46:04 +02001766#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001767 /* Finish the transaction for a key creation. This does not
1768 * happen when registering an existing key. Detect this case
1769 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001770 * creation of a persistent key in a secure element requires a transaction,
1771 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if (driver != NULL &&
1773 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1774 status = psa_save_se_persistent_data(driver);
1775 if (status != PSA_SUCCESS) {
1776 psa_destroy_persistent_key(slot->attr.id);
1777 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001778 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001780 }
1781#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001784 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 status = psa_unlock_key_slot(slot);
1786 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001787 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001789 }
Ronald Cron50972942020-11-14 11:28:25 +01001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001792}
1793
1794/** Abort the creation of a key.
1795 *
1796 * You may call this function after calling psa_start_key_creation(),
1797 * or after psa_finish_key_creation() fails. In other circumstances, this
1798 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001799 * See the documentation of psa_start_key_creation() for the intended use
1800 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001801 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001802 * \param[in,out] slot Pointer to the slot with key material.
1803 * \param[in] driver The secure element driver for the key,
1804 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001805 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001806static void psa_fail_key_creation(psa_key_slot_t *slot,
1807 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001808{
Gilles Peskine011e4282019-06-26 18:34:38 +02001809 (void) driver;
1810
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001812 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001814
1815#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001816 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001817 * element, and the failure happened later (when saving metadata
1818 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001819 * element.
1820 * https://github.com/ARMmbed/mbed-crypto/issues/217
1821 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001822
Gilles Peskined7729582019-08-05 15:55:54 +02001823 /* Abort the ongoing transaction if any (there may not be one if
1824 * the creation process failed before starting one, or if the
1825 * key creation is a registration of a key in a secure element).
1826 * Earlier functions must already have done what it takes to undo any
1827 * partial creation. All that's left is to update the transaction data
1828 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001830#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001833}
1834
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001835/** Validate optional attributes during key creation.
1836 *
1837 * Some key attributes are optional during key creation. If they are
1838 * specified in the attributes structure, check that they are consistent
1839 * with the data in the slot.
1840 *
1841 * This function should be called near the end of key creation, after
1842 * the slot in memory is fully populated but before saving persistent data.
1843 */
1844static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001845 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001847{
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 if (attributes->core.type != 0) {
1849 if (attributes->core.type != slot->attr.type) {
1850 return PSA_ERROR_INVALID_ARGUMENT;
1851 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001852 }
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001855#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1857 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001858 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001859 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001860 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001861
Ronald Cron90857082020-11-25 14:58:33 +01001862 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 slot->attr.type,
1864 slot->key.data,
1865 slot->key.bytes,
1866 &rsa);
1867 if (status != PSA_SUCCESS) {
1868 return status;
1869 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 mbedtls_mpi_init(&actual);
1872 mbedtls_mpi_init(&required);
1873 ret = mbedtls_rsa_export(rsa,
1874 NULL, NULL, NULL, NULL, &actual);
1875 mbedtls_rsa_free(rsa);
1876 mbedtls_free(rsa);
1877 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001878 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 }
1880 ret = mbedtls_mpi_read_binary(&required,
1881 attributes->domain_parameters,
1882 attributes->domain_parameters_size);
1883 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001884 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 }
1886 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001887 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 }
1889rsa_exit:
1890 mbedtls_mpi_free(&actual);
1891 mbedtls_mpi_free(&required);
1892 if (ret != 0) {
1893 return mbedtls_to_psa_error(ret);
1894 }
1895 } else
John Durkop9814fa22020-11-04 12:28:15 -08001896#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1897 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900 }
1901 }
1902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 if (attributes->core.bits != 0) {
1904 if (attributes->core.bits != slot->attr.bits) {
1905 return PSA_ERROR_INVALID_ARGUMENT;
1906 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001907 }
1908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001910}
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1913 const uint8_t *data,
1914 size_t data_length,
1915 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001916{
1917 psa_status_t status;
1918 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001919 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001920 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301921 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001922
Ronald Cron81709fc2020-11-14 12:10:32 +01001923 *key = MBEDTLS_SVC_KEY_ID_INIT;
1924
Gilles Peskine0f84d622019-09-12 19:03:13 +02001925 /* Reject zero-length symmetric keys (including raw data key objects).
1926 * This also rejects any key which might be encoded as an empty string,
1927 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 if (data_length == 0) {
1929 return PSA_ERROR_INVALID_ARGUMENT;
1930 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001931
Archana449608b2021-09-08 15:36:05 +05301932 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if (data_length > SIZE_MAX / 8) {
1934 return PSA_ERROR_NOT_SUPPORTED;
1935 }
Archana6ed4bda2021-08-04 10:47:15 +05301936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1938 &slot, &driver);
1939 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001942
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001943 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301944 * storage ( thus not in the case of importing a key in a secure element
1945 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1946 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (slot->key.data == NULL) {
1948 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301949 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 attributes, data, data_length, &storage_size);
1951 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301952 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 }
Archanad8a83dc2021-06-14 10:04:16 +05301954 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 status = psa_allocate_buffer_to_slot(slot, storage_size);
1956 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001957 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001959 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001960
1961 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 status = psa_driver_wrapper_import_key(attributes,
1963 data, data_length,
1964 slot->key.data,
1965 slot->key.bytes,
1966 &slot->key.bytes, &bits);
1967 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001968 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001972 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001974 status = PSA_ERROR_INVALID_ARGUMENT;
1975 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001976 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001977
Archana6ed4bda2021-08-04 10:47:15 +05301978 /* Enforce a size limit, and in particular ensure that the bit
1979 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301981 status = PSA_ERROR_NOT_SUPPORTED;
1982 goto exit;
1983 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 status = psa_validate_optional_attributes(slot, attributes);
1985 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001986 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 if (status != PSA_SUCCESS) {
1992 psa_fail_key_creation(slot, driver);
1993 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001996}
1997
Gilles Peskined7729582019-08-05 15:55:54 +02001998#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1999psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002001{
2002 psa_status_t status;
2003 psa_key_slot_t *slot = NULL;
2004 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002005 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002006
2007 /* Leaving attributes unspecified is not currently supported.
2008 * It could make sense to query the key type and size from the
2009 * secure element, but not all secure elements support this
2010 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2012 return PSA_ERROR_NOT_SUPPORTED;
2013 }
2014 if (psa_get_key_bits(attributes) == 0) {
2015 return PSA_ERROR_NOT_SUPPORTED;
2016 }
Gilles Peskined7729582019-08-05 15:55:54 +02002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2019 &slot, &driver);
2020 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002021 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 }
Gilles Peskined7729582019-08-05 15:55:54 +02002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002025
2026exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (status != PSA_SUCCESS) {
2028 psa_fail_key_creation(slot, driver);
2029 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002030
Gilles Peskined7729582019-08-05 15:55:54 +02002031 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 psa_close_key(key);
2033 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002034}
2035#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2038 const psa_key_attributes_t *specified_attributes,
2039 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002040{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002041 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002042 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002043 psa_key_slot_t *source_slot = NULL;
2044 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002045 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002046 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302047 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002048
Ronald Cron81709fc2020-11-14 12:10:32 +01002049 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2050
Archana8a180362021-07-05 02:18:48 +05302051 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2053 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002054 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 status = psa_validate_optional_attributes(source_slot,
2058 specified_attributes);
2059 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002060 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002062
Archana9d17bf42021-09-10 06:22:44 +05302063 /* The target key type and number of bits have been validated by
2064 * psa_validate_optional_attributes() to be either equal to zero or
2065 * equal to the ones of the source key. So it is safe to inherit
2066 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302067 * */
Archana9d17bf42021-09-10 06:22:44 +05302068 actual_attributes.core.bits = source_slot->attr.bits;
2069 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302070
2071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 status = psa_restrict_key_policy(source_slot->attr.type,
2073 &actual_attributes.core.policy,
2074 &source_slot->attr.policy);
2075 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2080 &target_slot, &driver);
2081 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002082 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 }
2084 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2085 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302086 /*
Archana9d17bf42021-09-10 06:22:44 +05302087 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302088 * the source key would need to be exported as plaintext and re-imported
2089 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302090 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302091 * appropriate API invocations from the application, if needed.
2092 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002093 status = PSA_ERROR_NOT_SUPPORTED;
2094 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002095 }
Archana8a180362021-07-05 02:18:48 +05302096 /*
2097 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302098 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302099 * - For opaque keys this translates to an invocation of the drivers'
2100 * copy_key entry point through the dispatch layer.
2101 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2103 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2104 &storage_size);
2105 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 }
Archana374fe5b2021-09-08 15:50:28 +05302108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2110 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 }
Archana374fe5b2021-09-08 15:50:28 +05302113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 status = psa_driver_wrapper_copy_key(&actual_attributes,
2115 source_slot->key.data,
2116 source_slot->key.bytes,
2117 target_slot->key.data,
2118 target_slot->key.bytes,
2119 &target_slot->key.bytes);
2120 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 }
2123 } else {
2124 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302125 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 source_slot->key.bytes);
2127 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 }
Archana8a180362021-07-05 02:18:48 +05302130 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002132exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (status != PSA_SUCCESS) {
2134 psa_fail_key_creation(target_slot, driver);
2135 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002140}
2141
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002142
2143
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002144/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002145/* Message digests */
2146/****************************************************************/
2147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002149{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002150 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (operation->id == 0) {
2152 return PSA_SUCCESS;
2153 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002156 operation->id = 0;
2157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002159}
2160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2162 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002165
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002166 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002168 status = PSA_ERROR_BAD_STATE;
2169 goto exit;
2170 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002173 status = PSA_ERROR_INVALID_ARGUMENT;
2174 goto exit;
2175 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002176
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002177 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2178 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002182
2183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (status != PSA_SUCCESS) {
2185 psa_hash_abort(operation);
2186 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002187
2188 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189}
2190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2192 const uint8_t *input,
2193 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002195 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002198 status = PSA_ERROR_BAD_STATE;
2199 goto exit;
2200 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002201
Steven Cooremanc8288352021-03-04 14:02:19 +01002202 /* Don't require hash implementations to behave correctly on a
2203 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (input_length == 0) {
2205 return PSA_SUCCESS;
2206 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002209
2210exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (status != PSA_SUCCESS) {
2212 psa_hash_abort(operation);
2213 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216}
2217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2219 uint8_t *hash,
2220 size_t hash_size,
2221 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002222{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002223 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (operation->id == 0) {
2225 return PSA_ERROR_BAD_STATE;
2226 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002227
Steven Cooreman1e582352021-02-18 17:24:37 +01002228 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 operation, hash, hash_size, hash_length);
2230 psa_hash_abort(operation);
2231 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002232}
2233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2235 const uint8_t *hash,
2236 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002237{
Ronald Cron69a63422021-10-18 09:47:58 +02002238 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002240 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 operation,
2242 actual_hash, sizeof(actual_hash),
2243 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002246 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002250 status = PSA_ERROR_INVALID_SIGNATURE;
2251 goto exit;
2252 }
2253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002255 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002257
2258exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2260 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002261 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265}
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267psa_status_t psa_hash_compute(psa_algorithm_t alg,
2268 const uint8_t *input, size_t input_length,
2269 uint8_t *hash, size_t hash_size,
2270 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002271{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002272 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (!PSA_ALG_IS_HASH(alg)) {
2274 return PSA_ERROR_INVALID_ARGUMENT;
2275 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2278 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002279}
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281psa_status_t psa_hash_compare(psa_algorithm_t alg,
2282 const uint8_t *input, size_t input_length,
2283 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002284{
Ronald Cron69a63422021-10-18 09:47:58 +02002285 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002286 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (!PSA_ALG_IS_HASH(alg)) {
2289 return PSA_ERROR_INVALID_ARGUMENT;
2290 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002291
Steven Cooreman1e582352021-02-18 17:24:37 +01002292 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 alg, input, input_length,
2294 actual_hash, sizeof(actual_hash),
2295 &actual_hash_length);
2296 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002297 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 }
2299 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002300 status = PSA_ERROR_INVALID_SIGNATURE;
2301 goto exit;
2302 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002304 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002306
2307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2309 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002310}
2311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2313 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002314{
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (source_operation->id == 0 ||
2316 target_operation->id != 0) {
2317 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002318 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2321 target_operation);
2322 if (status != PSA_SUCCESS) {
2323 psa_hash_abort(target_operation);
2324 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002327}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328
2329
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002331/* MAC */
2332/****************************************************************/
2333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002335{
Steven Cooremane6804192021-03-19 18:28:56 +01002336 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (operation->id == 0) {
2338 return PSA_SUCCESS;
2339 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002342 operation->mac_size = 0;
2343 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002344 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347}
2348
Ronald Cron2dff3b22021-06-17 16:33:22 +02002349static psa_status_t psa_mac_finalize_alg_and_key_validation(
2350 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002351 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002353{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 psa_key_type_t key_type = psa_get_key_type(attributes);
2356 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (!PSA_ALG_IS_MAC(alg)) {
2359 return PSA_ERROR_INVALID_ARGUMENT;
2360 }
Steven Cooremane6804192021-03-19 18:28:56 +01002361
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002362 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 status = psa_mac_key_can_do(alg, key_type);
2364 if (status != PSA_SUCCESS) {
2365 return status;
2366 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002367
Steven Cooremand1a68f12021-05-10 11:13:41 +02002368 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002372 /* A very short MAC is too short for security since it can be
2373 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2374 * so we make this our minimum, even though 32 bits is still
2375 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002377 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2380 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002381 /* It's impossible to "truncate" to a larger length than the full length
2382 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002384 }
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002387 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2388 * that is disabled in the compile-time configuration. The result can
2389 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2390 * configuration into account. In this case, force a return of
2391 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2392 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2393 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2394 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2395 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002397 }
2398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002400}
2401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2403 mbedtls_svc_key_id_t key,
2404 psa_algorithm_t alg,
2405 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002406{
2407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2408 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002409 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002410
2411 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002413 status = PSA_ERROR_BAD_STATE;
2414 goto exit;
2415 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002416
2417 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 key,
2419 &slot,
2420 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2421 alg);
2422 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002423 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002425
2426 psa_key_attributes_t attributes = {
2427 .core = slot->attr
2428 };
2429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2431 &operation->mac_size);
2432 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435
2436 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002437 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if (is_sign) {
2439 status = psa_driver_wrapper_mac_sign_setup(operation,
2440 &attributes,
2441 slot->key.data,
2442 slot->key.bytes,
2443 alg);
2444 } else {
2445 status = psa_driver_wrapper_mac_verify_setup(operation,
2446 &attributes,
2447 slot->key.data,
2448 slot->key.bytes,
2449 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002451
Gilles Peskinefbfac682018-07-08 20:51:54 +02002452exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 if (status != PSA_SUCCESS) {
2454 psa_mac_abort(operation);
2455 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002460}
2461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2463 mbedtls_svc_key_id_t key,
2464 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002465{
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002467}
2468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2470 mbedtls_svc_key_id_t key,
2471 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002472{
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002474}
2475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2477 const uint8_t *input,
2478 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002479{
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 if (operation->id == 0) {
2481 return PSA_ERROR_BAD_STATE;
2482 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002484 /* Don't require hash implementations to behave correctly on a
2485 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (input_length == 0) {
2487 return PSA_SUCCESS;
2488 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2491 input, input_length);
2492 if (status != PSA_SUCCESS) {
2493 psa_mac_abort(operation);
2494 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497}
2498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2500 uint8_t *mac,
2501 size_t mac_size,
2502 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002503{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002504 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2505 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002508 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002509 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002510 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002513 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002514 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002515 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002516
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002517 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2518 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 if (operation->mac_size == 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 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002525 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002526 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002527 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 status = psa_driver_wrapper_mac_sign_finish(operation,
2530 mac, operation->mac_size,
2531 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002532
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002533exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002534 /* In case of success, set the potential excess room in the output buffer
2535 * to an invalid value, to avoid potentially leaking a longer MAC.
2536 * In case of error, set the output length and content to a safe default,
2537 * such that in case the caller misses an error check, the output would be
2538 * an unachievable MAC.
2539 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002541 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002542 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002543 }
mohammad16036df908f2018-04-02 08:34:15 -07002544
Paul Elliottdc42ca82023-02-24 18:11:59 +00002545 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002550}
2551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2553 const uint8_t *mac,
2554 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002556 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2557 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002560 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002561 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002562 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002565 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002566 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002567 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002570 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002571 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002572 }
2573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 status = psa_driver_wrapper_mac_verify_finish(operation,
2575 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002576
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002577exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581}
2582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2584 psa_algorithm_t alg,
2585 const uint8_t *input,
2586 size_t input_length,
2587 uint8_t *mac,
2588 size_t mac_size,
2589 size_t *mac_length,
2590 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002591{
2592 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002593 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2594 psa_key_slot_t *slot;
2595 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002596
Ronald Cron51131b52021-06-17 17:17:20 +02002597 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 key,
2599 &slot,
2600 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2601 alg);
2602 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002605
Ronald Cron51131b52021-06-17 17:17:20 +02002606 psa_key_attributes_t attributes = {
2607 .core = slot->attr
2608 };
2609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2611 &operation_mac_size);
2612 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002613 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002617 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002618 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002619 }
2620
2621 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 &attributes,
2623 slot->key.data, slot->key.bytes,
2624 alg,
2625 input, input_length,
2626 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002627
2628exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002629 /* In case of success, set the potential excess room in the output buffer
2630 * to an invalid value, to avoid potentially leaking a longer MAC.
2631 * In case of error, set the output length and content to a safe default,
2632 * such that in case the caller misses an error check, the output would be
2633 * an unachievable MAC.
2634 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002636 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002637 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002638 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002639
2640 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002645}
2646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002648 psa_algorithm_t alg,
2649 const uint8_t *input,
2650 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 uint8_t *mac,
2652 size_t mac_size,
2653 size_t *mac_length)
2654{
2655 return psa_mac_compute_internal(key, alg,
2656 input, input_length,
2657 mac, mac_size, mac_length, 1);
2658}
2659
2660psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2661 psa_algorithm_t alg,
2662 const uint8_t *input,
2663 size_t input_length,
2664 const uint8_t *mac,
2665 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002666{
2667 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002668 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2669 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 status = psa_mac_compute_internal(key, alg,
2672 input, input_length,
2673 actual_mac, sizeof(actual_mac),
2674 &actual_mac_length, 0);
2675 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002676 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002680 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002681 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002682 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002684 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002685 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002686 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002687
2688exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002692}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002693
Gilles Peskinee59236f2018-01-27 23:32:46 +01002694/****************************************************************/
2695/* Asymmetric cryptography */
2696/****************************************************************/
2697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2699 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002700{
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 if (input_is_message) {
2702 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2703 return PSA_ERROR_INVALID_ARGUMENT;
2704 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2707 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2708 return PSA_ERROR_INVALID_ARGUMENT;
2709 }
2710 }
2711 } else {
2712 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2713 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002714 }
2715 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002718}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2721 int input_is_message,
2722 psa_algorithm_t alg,
2723 const uint8_t *input,
2724 size_t input_length,
2725 uint8_t *signature,
2726 size_t signature_size,
2727 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002728{
2729 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2730 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2731 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002732
2733 *signature_length = 0;
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 status = psa_sign_verify_check_alg(input_is_message, alg);
2736 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002737 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002739
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002740 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002741 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002742 * buffer can in principle be empty since it doesn't actually have
2743 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 if (signature_size == 0) {
2745 return PSA_ERROR_BUFFER_TOO_SMALL;
2746 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002747
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002748 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 key, &slot,
2750 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2751 PSA_KEY_USAGE_SIGN_HASH,
2752 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002755 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002759 status = PSA_ERROR_INVALID_ARGUMENT;
2760 goto exit;
2761 }
2762
2763 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002765 };
2766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002768 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002769 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002770 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 signature, signature_size, signature_length);
2772 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002773
2774 status = psa_driver_wrapper_sign_hash(
2775 &attributes, slot->key.data, slot->key.bytes,
2776 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002778 }
2779
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002780
2781exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002782 psa_wipe_tag_output_buffer(signature, status, signature_size,
2783 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002788}
2789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2791 int input_is_message,
2792 psa_algorithm_t alg,
2793 const uint8_t *input,
2794 size_t input_length,
2795 const uint8_t *signature,
2796 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002797{
2798 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2799 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2800 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 status = psa_sign_verify_check_alg(input_is_message, alg);
2803 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002804 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002806
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002807 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 key, &slot,
2809 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2810 PSA_KEY_USAGE_VERIFY_HASH,
2811 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 if (status != PSA_SUCCESS) {
2814 return status;
2815 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002816
2817 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002819 };
2820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002822 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002823 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002824 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 signature, signature_length);
2826 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002827 status = psa_driver_wrapper_verify_hash(
2828 &attributes, slot->key.data, slot->key.bytes,
2829 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002831 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002834
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002836
2837}
2838
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002839psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002840 const psa_key_attributes_t *attributes,
2841 const uint8_t *key_buffer,
2842 size_t key_buffer_size,
2843 psa_algorithm_t alg,
2844 const uint8_t *input,
2845 size_t input_length,
2846 uint8_t *signature,
2847 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002849{
2850 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002853 size_t hash_length;
2854 uint8_t hash[PSA_HASH_MAX_SIZE];
2855
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002856 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 PSA_ALG_SIGN_GET_HASH(alg),
2858 input, input_length,
2859 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002860
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002862 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002864
2865 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 attributes, key_buffer, key_buffer_size,
2867 alg, hash, hash_length,
2868 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002869 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002872}
2873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2875 psa_algorithm_t alg,
2876 const uint8_t *input,
2877 size_t input_length,
2878 uint8_t *signature,
2879 size_t signature_size,
2880 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002881{
2882 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002883 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002885}
2886
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002887psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002888 const psa_key_attributes_t *attributes,
2889 const uint8_t *key_buffer,
2890 size_t key_buffer_size,
2891 psa_algorithm_t alg,
2892 const uint8_t *input,
2893 size_t input_length,
2894 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002896{
2897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002898
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002900 size_t hash_length;
2901 uint8_t hash[PSA_HASH_MAX_SIZE];
2902
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002903 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 PSA_ALG_SIGN_GET_HASH(alg),
2905 input, input_length,
2906 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002907
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002909 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002911
2912 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 attributes, key_buffer, key_buffer_size,
2914 alg, hash, hash_length,
2915 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002916 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002919}
2920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2922 psa_algorithm_t alg,
2923 const uint8_t *input,
2924 size_t input_length,
2925 const uint8_t *signature,
2926 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002927{
2928 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002929 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002931}
2932
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002933psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002934 const psa_key_attributes_t *attributes,
2935 const uint8_t *key_buffer, size_t key_buffer_size,
2936 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002938{
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2940 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2941 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002942#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2944 return mbedtls_psa_rsa_sign_hash(
2945 attributes,
2946 key_buffer, key_buffer_size,
2947 alg, hash, hash_length,
2948 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002949#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2950 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 } else {
2952 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002953 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2955 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002956#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2958 return mbedtls_psa_ecdsa_sign_hash(
2959 attributes,
2960 key_buffer, key_buffer_size,
2961 alg, hash, hash_length,
2962 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002963#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2964 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 } else {
2966 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002967 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002968 }
Ronald Cron4501c982021-03-19 20:09:32 +01002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 (void) key_buffer;
2971 (void) key_buffer_size;
2972 (void) hash;
2973 (void) hash_length;
2974 (void) signature;
2975 (void) signature_size;
2976 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002977
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002979}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002980
Gilles Peskine449bd832023-01-11 14:50:10 +01002981psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2982 psa_algorithm_t alg,
2983 const uint8_t *hash,
2984 size_t hash_length,
2985 uint8_t *signature,
2986 size_t signature_size,
2987 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002988{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002989 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002990 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002992}
2993
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002994psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002995 const psa_key_attributes_t *attributes,
2996 const uint8_t *key_buffer, size_t key_buffer_size,
2997 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002999{
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3001 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3002 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003003#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3005 return mbedtls_psa_rsa_verify_hash(
3006 attributes,
3007 key_buffer, key_buffer_size,
3008 alg, hash, hash_length,
3009 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003010#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3011 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 } else {
3013 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003014 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3016 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003017#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3019 return mbedtls_psa_ecdsa_verify_hash(
3020 attributes,
3021 key_buffer, key_buffer_size,
3022 alg, hash, hash_length,
3023 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003024#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3025 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 } else {
3027 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003028 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003029 }
Ronald Cron4501c982021-03-19 20:09:32 +01003030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 (void) key_buffer;
3032 (void) key_buffer_size;
3033 (void) hash;
3034 (void) hash_length;
3035 (void) signature;
3036 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003039}
3040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3042 psa_algorithm_t alg,
3043 const uint8_t *hash,
3044 size_t hash_length,
3045 const uint8_t *signature,
3046 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003047{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003048 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003049 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003051}
3052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3054 psa_algorithm_t alg,
3055 const uint8_t *input,
3056 size_t input_length,
3057 const uint8_t *salt,
3058 size_t salt_length,
3059 uint8_t *output,
3060 size_t output_size,
3061 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003062{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003063 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003064 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003065 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003066
Darryl Green5cc689a2018-07-24 15:34:10 +01003067 (void) input;
3068 (void) input_length;
3069 (void) salt;
3070 (void) output;
3071 (void) output_size;
3072
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003073 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3076 return PSA_ERROR_INVALID_ARGUMENT;
3077 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003078
Ronald Cron5c522922020-11-14 16:35:34 +01003079 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3081 if (status != PSA_SUCCESS) {
3082 return status;
3083 }
3084 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3085 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003086 status = PSA_ERROR_INVALID_ARGUMENT;
3087 goto exit;
3088 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003089
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003090 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003092 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003093
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003094 status = psa_driver_wrapper_asymmetric_encrypt(
3095 &attributes, slot->key.data, slot->key.bytes,
3096 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003098exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003102}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003103
Gilles Peskine449bd832023-01-11 14:50:10 +01003104psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3105 psa_algorithm_t alg,
3106 const uint8_t *input,
3107 size_t input_length,
3108 const uint8_t *salt,
3109 size_t salt_length,
3110 uint8_t *output,
3111 size_t output_size,
3112 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003113{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003114 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003115 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003116 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003117
Darryl Green5cc689a2018-07-24 15:34:10 +01003118 (void) input;
3119 (void) input_length;
3120 (void) salt;
3121 (void) output;
3122 (void) output_size;
3123
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003124 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3127 return PSA_ERROR_INVALID_ARGUMENT;
3128 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003129
Ronald Cron5c522922020-11-14 16:35:34 +01003130 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3132 if (status != PSA_SUCCESS) {
3133 return status;
3134 }
3135 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003136 status = PSA_ERROR_INVALID_ARGUMENT;
3137 goto exit;
3138 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003139
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003140 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003142 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003143
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003144 status = psa_driver_wrapper_asymmetric_decrypt(
3145 &attributes, slot->key.data, slot->key.bytes,
3146 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003148
3149exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003153}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003154
Paul Elliott9fe12f62022-11-30 19:16:02 +00003155/****************************************************************/
3156/* Asymmetric interruptible cryptography */
3157/****************************************************************/
3158
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003159static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3160
Paul Elliott9fe12f62022-11-30 19:16:02 +00003161void psa_interruptible_set_max_ops(uint32_t max_ops)
3162{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003163 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003164}
3165
3166uint32_t psa_interruptible_get_max_ops(void)
3167{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003168 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003169}
3170
Paul Elliott9fe12f62022-11-30 19:16:02 +00003171uint32_t psa_sign_hash_get_num_ops(
3172 const psa_sign_hash_interruptible_operation_t *operation)
3173{
Paul Elliott296ede92022-12-15 17:00:30 +00003174 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003175}
3176
3177uint32_t psa_verify_hash_get_num_ops(
3178 const psa_verify_hash_interruptible_operation_t *operation)
3179{
Paul Elliott296ede92022-12-15 17:00:30 +00003180 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003181}
3182
Paul Elliott59ad9452022-12-18 15:09:02 +00003183static psa_status_t psa_sign_hash_abort_internal(
3184 psa_sign_hash_interruptible_operation_t *operation)
3185{
3186 if (operation->id == 0) {
3187 /* The object has (apparently) been initialized but it is not (yet)
3188 * in use. It's ok to call abort on such an object, and there's
3189 * nothing to do. */
3190 return PSA_SUCCESS;
3191 }
3192
3193 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3194
3195 status = psa_driver_wrapper_sign_hash_abort(operation);
3196
3197 operation->id = 0;
3198
Paul Elliotte6145dc2023-02-07 12:51:21 +00003199 /* Do not clear either the error_occurred or num_ops elements here as they
3200 * only want to be cleared by the application calling abort, not by abort
3201 * being called at completion of an operation. */
3202
Paul Elliott59ad9452022-12-18 15:09:02 +00003203 return status;
3204}
3205
Paul Elliott9fe12f62022-11-30 19:16:02 +00003206psa_status_t psa_sign_hash_start(
3207 psa_sign_hash_interruptible_operation_t *operation,
3208 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3209 const uint8_t *hash, size_t hash_length)
3210{
3211 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3212 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3213 psa_key_slot_t *slot;
3214
Paul Elliottc9774412023-02-06 15:14:07 +00003215 /* Check that start has not been previously called, or operation has not
3216 * previously errored. */
3217 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003218 return PSA_ERROR_BAD_STATE;
3219 }
3220
Paul Elliott9fe12f62022-11-30 19:16:02 +00003221 status = psa_sign_verify_check_alg(0, alg);
3222 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003223 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003224 return status;
3225 }
3226
3227 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3228 PSA_KEY_USAGE_SIGN_HASH,
3229 alg);
3230
3231 if (status != PSA_SUCCESS) {
3232 goto exit;
3233 }
3234
3235 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3236 status = PSA_ERROR_INVALID_ARGUMENT;
3237 goto exit;
3238 }
3239
3240 psa_key_attributes_t attributes = {
3241 .core = slot->attr
3242 };
3243
Paul Elliott296ede92022-12-15 17:00:30 +00003244 /* Ensure ops count gets reset, in case of operation re-use. */
3245 operation->num_ops = 0;
3246
Paul Elliott9fe12f62022-11-30 19:16:02 +00003247 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3248 slot->key.data,
3249 slot->key.bytes, alg,
3250 hash, hash_length);
3251exit:
3252
3253 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003254 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003255 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003256 }
3257
3258 unlock_status = psa_unlock_key_slot(slot);
3259
Paul Elliottc9774412023-02-06 15:14:07 +00003260 if (unlock_status != PSA_SUCCESS) {
3261 operation->error_occurred = 1;
3262 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003263
Paul Elliottc9774412023-02-06 15:14:07 +00003264 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003265}
3266
3267
3268psa_status_t psa_sign_hash_complete(
3269 psa_sign_hash_interruptible_operation_t *operation,
3270 uint8_t *signature, size_t signature_size,
3271 size_t *signature_length)
3272{
3273 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3274
3275 *signature_length = 0;
3276
Paul Elliottc9774412023-02-06 15:14:07 +00003277 /* Check that start has been called first, and that operation has not
3278 * previously errored. */
3279 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003280 status = PSA_ERROR_BAD_STATE;
3281 goto exit;
3282 }
3283
Paul Elliott096abc42023-02-03 18:33:23 +00003284 /* Immediately reject a zero-length signature buffer. This guarantees that
3285 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003286 if (signature_size == 0) {
3287 status = PSA_ERROR_BUFFER_TOO_SMALL;
3288 goto exit;
3289 }
3290
3291 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3292 signature_size,
3293 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003294
Paul Elliott296ede92022-12-15 17:00:30 +00003295 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003296 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003297
Paul Elliottebe225c2023-02-10 14:32:53 +00003298exit:
3299
Paul Elliott59200a22023-02-21 15:07:40 +00003300 psa_wipe_tag_output_buffer(signature, status, signature_size,
3301 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003302
Paul Elliott53bb3122023-02-10 14:22:22 +00003303 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003304 if (status != PSA_SUCCESS) {
3305 operation->error_occurred = 1;
3306 }
3307
Paul Elliott59ad9452022-12-18 15:09:02 +00003308 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003309 }
3310
3311 return status;
3312}
3313
3314psa_status_t psa_sign_hash_abort(
3315 psa_sign_hash_interruptible_operation_t *operation)
3316{
Paul Elliott59ad9452022-12-18 15:09:02 +00003317 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3318
3319 status = psa_sign_hash_abort_internal(operation);
3320
3321 /* We clear the number of ops done here, so that it is not cleared when
3322 * the operation fails or succeeds, only on manual abort. */
3323 operation->num_ops = 0;
3324
Paul Elliottc9774412023-02-06 15:14:07 +00003325 /* Likewise, failure state. */
3326 operation->error_occurred = 0;
3327
Paul Elliott59ad9452022-12-18 15:09:02 +00003328 return status;
3329}
3330
3331static psa_status_t psa_verify_hash_abort_internal(
3332 psa_verify_hash_interruptible_operation_t *operation)
3333{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003334 if (operation->id == 0) {
3335 /* The object has (apparently) been initialized but it is not (yet)
3336 * in use. It's ok to call abort on such an object, and there's
3337 * nothing to do. */
3338 return PSA_SUCCESS;
3339 }
3340
Paul Elliott59ad9452022-12-18 15:09:02 +00003341 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3342
3343 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003344
3345 operation->id = 0;
3346
Paul Elliotte6145dc2023-02-07 12:51:21 +00003347 /* Do not clear either the error_occurred or num_ops elements here as they
3348 * only want to be cleared by the application calling abort, not by abort
3349 * being called at completion of an operation. */
3350
Paul Elliott59ad9452022-12-18 15:09:02 +00003351 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003352}
3353
3354psa_status_t psa_verify_hash_start(
3355 psa_verify_hash_interruptible_operation_t *operation,
3356 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3357 const uint8_t *hash, size_t hash_length,
3358 const uint8_t *signature, size_t signature_length)
3359{
3360 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3361 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3362 psa_key_slot_t *slot;
3363
Paul Elliottc9774412023-02-06 15:14:07 +00003364 /* Check that start has not been previously called, or operation has not
3365 * previously errored. */
3366 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003367 return PSA_ERROR_BAD_STATE;
3368 }
3369
3370 status = psa_sign_verify_check_alg(0, alg);
3371 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003372 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003373 return status;
3374 }
3375
3376 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3377 PSA_KEY_USAGE_VERIFY_HASH,
3378 alg);
3379
3380 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003381 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003382 return status;
3383 }
3384
3385 psa_key_attributes_t attributes = {
3386 .core = slot->attr
3387 };
3388
Paul Elliott296ede92022-12-15 17:00:30 +00003389 /* Ensure ops count gets reset, in case of operation re-use. */
3390 operation->num_ops = 0;
3391
Paul Elliott9fe12f62022-11-30 19:16:02 +00003392 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3393 slot->key.data,
3394 slot->key.bytes,
3395 alg, hash, hash_length,
3396 signature, signature_length);
3397
3398 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003399 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003400 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003401 }
3402
3403 unlock_status = psa_unlock_key_slot(slot);
3404
Paul Elliottc9774412023-02-06 15:14:07 +00003405 if (unlock_status != PSA_SUCCESS) {
3406 operation->error_occurred = 1;
3407 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003408
Paul Elliottc9774412023-02-06 15:14:07 +00003409 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003410}
3411
3412psa_status_t psa_verify_hash_complete(
3413 psa_verify_hash_interruptible_operation_t *operation)
3414{
3415 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3416
Paul Elliottc9774412023-02-06 15:14:07 +00003417 /* Check that start has been called first, and that operation has not
3418 * previously errored. */
3419 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420 status = PSA_ERROR_BAD_STATE;
3421 goto exit;
3422 }
3423
3424 status = psa_driver_wrapper_verify_hash_complete(operation);
3425
Paul Elliott296ede92022-12-15 17:00:30 +00003426 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003427 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003428 operation);
3429
Paul Elliottebe225c2023-02-10 14:32:53 +00003430exit:
3431
Paul Elliott9fe12f62022-11-30 19:16:02 +00003432 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003433 if (status != PSA_SUCCESS) {
3434 operation->error_occurred = 1;
3435 }
3436
Paul Elliott59ad9452022-12-18 15:09:02 +00003437 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003438 }
3439
3440 return status;
3441}
3442
3443psa_status_t psa_verify_hash_abort(
3444 psa_verify_hash_interruptible_operation_t *operation)
3445{
Paul Elliott59ad9452022-12-18 15:09:02 +00003446 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003447
Paul Elliott59ad9452022-12-18 15:09:02 +00003448 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003449
Paul Elliott59ad9452022-12-18 15:09:02 +00003450 /* We clear the number of ops done here, so that it is not cleared when
3451 * the operation fails or succeeds, only on manual abort. */
3452 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003453
Paul Elliottc9774412023-02-06 15:14:07 +00003454 /* Likewise, failure state. */
3455 operation->error_occurred = 0;
3456
Paul Elliott59ad9452022-12-18 15:09:02 +00003457 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003458}
3459
Paul Elliott588f8ed2022-12-02 18:10:26 +00003460/****************************************************************/
3461/* Asymmetric interruptible cryptography internal */
3462/* implementations */
3463/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003464
Paul Elliott588f8ed2022-12-02 18:10:26 +00003465void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3466{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003467
Paul Elliott588f8ed2022-12-02 18:10:26 +00003468#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3469 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3470 defined(MBEDTLS_ECP_RESTARTABLE)
3471
3472 /* Internal implementation uses zero to indicate infinite number max ops,
3473 * therefore avoid this value, and set to minimum possible. */
3474 if (max_ops == 0) {
3475 max_ops = 1;
3476 }
3477
Paul Elliott588f8ed2022-12-02 18:10:26 +00003478 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003479#else
3480 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003481#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3482 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3483 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3484}
3485
Paul Elliott588f8ed2022-12-02 18:10:26 +00003486uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003487 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003488{
3489#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3490 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3491 defined(MBEDTLS_ECP_RESTARTABLE)
3492
Paul Elliott93d9ca82023-02-15 18:14:21 +00003493 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003494#else
3495 (void) operation;
3496 return 0;
3497#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3498 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3499 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3500}
3501
3502uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003503 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003504{
3505 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3506 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3507 defined(MBEDTLS_ECP_RESTARTABLE)
3508
Paul Elliott93d9ca82023-02-15 18:14:21 +00003509 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003510#else
3511 (void) operation;
3512 return 0;
3513#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3514 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3515 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3516}
3517
3518psa_status_t mbedtls_psa_sign_hash_start(
3519 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3520 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3521 size_t key_buffer_size, psa_algorithm_t alg,
3522 const uint8_t *hash, size_t hash_length)
3523{
3524 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003525 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003526
Paul Elliott068fe072023-01-16 13:59:15 +00003527 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3528 return PSA_ERROR_NOT_SUPPORTED;
3529 }
3530
3531 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003532 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003533 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003534
3535#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003536 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3537 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003538
Paul Elliotta1c94092023-02-15 16:38:04 +00003539 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3540
Paul Elliott93d9ca82023-02-15 18:14:21 +00003541 /* Ensure num_ops is zero'ed in case of context re-use. */
3542 operation->num_ops = 0;
3543
Paul Elliott068fe072023-01-16 13:59:15 +00003544 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3545 attributes->core.bits,
3546 key_buffer,
3547 key_buffer_size,
3548 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003549
Paul Elliott068fe072023-01-16 13:59:15 +00003550 if (status != PSA_SUCCESS) {
3551 return status;
3552 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003553
Paul Elliott1bc59df2023-02-05 13:41:57 +00003554 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003555 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003556
Paul Elliott068fe072023-01-16 13:59:15 +00003557 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3558 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3559 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003560
Paul Elliott0290a762023-02-09 14:30:24 +00003561 /* We only need to store the same length of hash as the private key size
3562 * here, it would be truncated by the internal implementation anyway. */
3563 required_hash_length = (hash_length < operation->coordinate_bytes ?
3564 hash_length : operation->coordinate_bytes);
3565
Paul Elliottba70ad42023-02-15 18:23:53 +00003566 if (required_hash_length > sizeof(operation->hash)) {
3567 /* Shouldn't happen, but better safe than sorry. */
3568 return PSA_ERROR_CORRUPTION_DETECTED;
3569 }
3570
Paul Elliott0290a762023-02-09 14:30:24 +00003571 memcpy(operation->hash, hash, required_hash_length);
3572 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003573
3574 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003575
3576#else
Paul Elliott068fe072023-01-16 13:59:15 +00003577 (void) operation;
3578 (void) key_buffer;
3579 (void) key_buffer_size;
3580 (void) alg;
3581 (void) hash;
3582 (void) hash_length;
3583 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003584 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003585
Paul Elliott068fe072023-01-16 13:59:15 +00003586 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003587#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3588 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3589 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003590}
3591
3592psa_status_t mbedtls_psa_sign_hash_complete(
3593 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3594 uint8_t *signature, size_t signature_size,
3595 size_t *signature_length)
3596{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003597#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3598 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3599 defined(MBEDTLS_ECP_RESTARTABLE)
3600
Paul Elliotta1c94092023-02-15 16:38:04 +00003601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003602 mbedtls_mpi r;
3603 mbedtls_mpi s;
3604
Paul Elliott46845252023-02-03 14:59:11 +00003605 mbedtls_mpi_init(&r);
3606 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003607
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003608 /* Ensure max_ops is set to the current value (or default). */
3609 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3610
Paul Elliotta1c94092023-02-15 16:38:04 +00003611 if (signature_size < 2 * operation->coordinate_bytes) {
3612 status = PSA_ERROR_BUFFER_TOO_SMALL;
3613 goto exit;
3614 }
3615
Paul Elliott588f8ed2022-12-02 18:10:26 +00003616 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003617
Paul Elliott588f8ed2022-12-02 18:10:26 +00003618#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3619 status = mbedtls_to_psa_error(
3620 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003621 &r,
3622 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003623 &operation->ctx->d,
3624 operation->hash,
3625 operation->hash_length,
3626 operation->md_alg,
3627 mbedtls_psa_get_random,
3628 MBEDTLS_PSA_RANDOM_STATE,
3629 &operation->restart_ctx));
3630#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003631 status = PSA_ERROR_NOT_SUPPORTED;
3632 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003633#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3634 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003635 status = mbedtls_to_psa_error(
3636 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003637 &r,
3638 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003639 &operation->ctx->d,
3640 operation->hash,
3641 operation->hash_length,
3642 mbedtls_psa_get_random,
3643 MBEDTLS_PSA_RANDOM_STATE,
3644 mbedtls_psa_get_random,
3645 MBEDTLS_PSA_RANDOM_STATE,
3646 &operation->restart_ctx));
3647 }
3648
Paul Elliottf8e5b562023-02-19 18:43:45 +00003649 /* Hide the fact that the restart context only holds a delta of number of
3650 * ops done during the last operation, not an absolute value. */
3651 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3652
Paul Elliott724bd252023-02-08 12:35:08 +00003653 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003654 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003655 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003656 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003657 operation->coordinate_bytes)
3658 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003659
3660 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003661 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003662 }
3663
3664 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003665 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003666 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003667 operation->coordinate_bytes,
3668 operation->coordinate_bytes)
3669 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003670
3671 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003672 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003673 }
3674
Paul Elliott1bc59df2023-02-05 13:41:57 +00003675 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003676
Paul Elliott724bd252023-02-08 12:35:08 +00003677 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678 }
Paul Elliott724bd252023-02-08 12:35:08 +00003679
3680exit:
3681
3682 mbedtls_mpi_free(&r);
3683 mbedtls_mpi_free(&s);
3684 return status;
3685
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686 #else
3687
3688 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689 (void) signature;
3690 (void) signature_size;
3691 (void) signature_length;
3692
3693 return PSA_ERROR_NOT_SUPPORTED;
3694
3695#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3696 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3697 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3698}
3699
3700psa_status_t mbedtls_psa_sign_hash_abort(
3701 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3702{
3703
3704#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3705 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3706 defined(MBEDTLS_ECP_RESTARTABLE)
3707
3708 if (operation->ctx) {
3709 mbedtls_ecdsa_free(operation->ctx);
3710 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003711 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712 }
3713
3714 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3715
Paul Elliott93d9ca82023-02-15 18:14:21 +00003716 operation->num_ops = 0;
3717
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718 return PSA_SUCCESS;
3719
3720#else
3721
3722 (void) operation;
3723
3724 return PSA_ERROR_NOT_SUPPORTED;
3725
3726#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3727 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3728 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3729}
3730
3731psa_status_t mbedtls_psa_verify_hash_start(
3732 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3733 const psa_key_attributes_t *attributes,
3734 const uint8_t *key_buffer, size_t key_buffer_size,
3735 psa_algorithm_t alg,
3736 const uint8_t *hash, size_t hash_length,
3737 const uint8_t *signature, size_t signature_length)
3738{
3739 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003740 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003741 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003742
Paul Elliott068fe072023-01-16 13:59:15 +00003743 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3744 return PSA_ERROR_NOT_SUPPORTED;
3745 }
3746
3747 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003748 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003749 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003750
3751#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003752 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3753 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003754
Paul Elliotta1c94092023-02-15 16:38:04 +00003755 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3756 mbedtls_mpi_init(&operation->r);
3757 mbedtls_mpi_init(&operation->s);
3758
Paul Elliott93d9ca82023-02-15 18:14:21 +00003759 /* Ensure num_ops is zero'ed in case of context re-use. */
3760 operation->num_ops = 0;
3761
Paul Elliott068fe072023-01-16 13:59:15 +00003762 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3763 attributes->core.bits,
3764 key_buffer,
3765 key_buffer_size,
3766 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767
Paul Elliott068fe072023-01-16 13:59:15 +00003768 if (status != PSA_SUCCESS) {
3769 return status;
3770 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771
Paul Elliottc569fc22023-02-10 13:02:54 +00003772 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773
Paul Elliott1bc59df2023-02-05 13:41:57 +00003774 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003775 return PSA_ERROR_INVALID_SIGNATURE;
3776 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003777
Paul Elliott068fe072023-01-16 13:59:15 +00003778 status = mbedtls_to_psa_error(
3779 mbedtls_mpi_read_binary(&operation->r,
3780 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003781 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003782
Paul Elliott068fe072023-01-16 13:59:15 +00003783 if (status != PSA_SUCCESS) {
3784 return status;
3785 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003786
Paul Elliott068fe072023-01-16 13:59:15 +00003787 status = mbedtls_to_psa_error(
3788 mbedtls_mpi_read_binary(&operation->s,
3789 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003790 coordinate_bytes,
3791 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003792
Paul Elliott068fe072023-01-16 13:59:15 +00003793 if (status != PSA_SUCCESS) {
3794 return status;
3795 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003796
Paul Elliott2c9843f2023-02-15 17:32:42 +00003797 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798
Paul Elliott2c9843f2023-02-15 17:32:42 +00003799 if (status != PSA_SUCCESS) {
3800 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003801 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802
Paul Elliott0290a762023-02-09 14:30:24 +00003803 /* We only need to store the same length of hash as the private key size
3804 * here, it would be truncated by the internal implementation anyway. */
3805 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3806 coordinate_bytes);
3807
Paul Elliottba70ad42023-02-15 18:23:53 +00003808 if (required_hash_length > sizeof(operation->hash)) {
3809 /* Shouldn't happen, but better safe than sorry. */
3810 return PSA_ERROR_CORRUPTION_DETECTED;
3811 }
3812
Paul Elliott0290a762023-02-09 14:30:24 +00003813 memcpy(operation->hash, hash, required_hash_length);
3814 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003815
3816 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003817#else
Paul Elliott068fe072023-01-16 13:59:15 +00003818 (void) operation;
3819 (void) key_buffer;
3820 (void) key_buffer_size;
3821 (void) alg;
3822 (void) hash;
3823 (void) hash_length;
3824 (void) signature;
3825 (void) signature_length;
3826 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003827 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003828 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003829
Paul Elliott068fe072023-01-16 13:59:15 +00003830 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3832 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3833 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834}
3835
3836psa_status_t mbedtls_psa_verify_hash_complete(
3837 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3838{
3839
3840#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3841 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3842 defined(MBEDTLS_ECP_RESTARTABLE)
3843
Paul Elliottf8e5b562023-02-19 18:43:45 +00003844 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3845
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003846 /* Ensure max_ops is set to the current value (or default). */
3847 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3848
Paul Elliottf8e5b562023-02-19 18:43:45 +00003849 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3851 operation->hash,
3852 operation->hash_length,
3853 &operation->ctx->Q,
3854 &operation->r,
3855 &operation->s,
3856 &operation->restart_ctx));
3857
Paul Elliottf8e5b562023-02-19 18:43:45 +00003858 /* Hide the fact that the restart context only holds a delta of number of
3859 * ops done during the last operation, not an absolute value. */
3860 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3861
3862 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003863#else
3864 (void) operation;
3865
3866 return PSA_ERROR_NOT_SUPPORTED;
3867
3868#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3869 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3870 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3871}
3872
3873psa_status_t mbedtls_psa_verify_hash_abort(
3874 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3875{
3876
3877#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3878 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3879 defined(MBEDTLS_ECP_RESTARTABLE)
3880
3881 if (operation->ctx) {
3882 mbedtls_ecdsa_free(operation->ctx);
3883 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003884 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885 }
3886
3887 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3888
Paul Elliott93d9ca82023-02-15 18:14:21 +00003889 operation->num_ops = 0;
3890
Paul Elliott588f8ed2022-12-02 18:10:26 +00003891 mbedtls_mpi_free(&operation->r);
3892 mbedtls_mpi_free(&operation->s);
3893
3894 return PSA_SUCCESS;
3895
3896#else
3897 (void) operation;
3898
3899 return PSA_ERROR_NOT_SUPPORTED;
3900
3901#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3902 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3903 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3904}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003905
mohammad1603503973b2018-03-12 15:59:30 +02003906/****************************************************************/
3907/* Symmetric cryptography */
3908/****************************************************************/
3909
Gilles Peskine449bd832023-01-11 14:50:10 +01003910static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3911 mbedtls_svc_key_id_t key,
3912 psa_algorithm_t alg,
3913 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003914{
3915 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3916 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003917 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003918 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3919 PSA_KEY_USAGE_ENCRYPT :
3920 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003921
3922 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003923 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003924 status = PSA_ERROR_BAD_STATE;
3925 goto exit;
3926 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003927
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003929 status = PSA_ERROR_INVALID_ARGUMENT;
3930 goto exit;
3931 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003932
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3934 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003935 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003936 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003937
Ronald Cron49fafa92021-03-10 08:34:23 +01003938 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003939 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003940 * so we only set it (in the driver wrapper) after resources have been
3941 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003942 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003943 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003944 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003946 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 }
3948 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003949
Ronald Crona4af55f2020-12-14 14:36:06 +01003950 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003952 };
3953
Ronald Cronab99ac22020-10-01 16:38:28 +02003954 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003955 if (cipher_operation == MBEDTLS_ENCRYPT) {
3956 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3957 &attributes,
3958 slot->key.data,
3959 slot->key.bytes,
3960 alg);
3961 } else {
3962 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3963 &attributes,
3964 slot->key.data,
3965 slot->key.bytes,
3966 alg);
3967 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003968
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003969exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003970 if (status != PSA_SUCCESS) {
3971 psa_cipher_abort(operation);
3972 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003973
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003975
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003977}
3978
Gilles Peskine449bd832023-01-11 14:50:10 +01003979psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3980 mbedtls_svc_key_id_t key,
3981 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003982{
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003984}
3985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3987 mbedtls_svc_key_id_t key,
3988 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003989{
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003991}
3992
Gilles Peskine449bd832023-01-11 14:50:10 +01003993psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3994 uint8_t *iv,
3995 size_t iv_size,
3996 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003997{
Ronald Cron6d051732020-10-01 14:10:20 +02003998 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003999 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004000 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004001
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004003 status = PSA_ERROR_BAD_STATE;
4004 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004005 }
4006
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004008 status = PSA_ERROR_BAD_STATE;
4009 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004010 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004011
Ronald Cron2fb90522021-07-05 12:31:44 +02004012 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004013 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004014 status = PSA_ERROR_BUFFER_TOO_SMALL;
4015 goto exit;
4016 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004017
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004019 status = PSA_ERROR_GENERIC_ERROR;
4020 goto exit;
4021 }
4022
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 status = psa_generate_random(local_iv, default_iv_length);
4024 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004025 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 }
Ronald Cron5618a392021-03-26 09:52:26 +01004027
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 status = psa_driver_wrapper_cipher_set_iv(operation,
4029 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004030
4031exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 if (status == PSA_SUCCESS) {
4033 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004034 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004035 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004037 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004039 }
Ronald Cron6d051732020-10-01 14:10:20 +02004040
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004042}
4043
Gilles Peskine449bd832023-01-11 14:50:10 +01004044psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4045 const uint8_t *iv,
4046 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004047{
Ronald Cron6d051732020-10-01 14:10:20 +02004048 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004049
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004051 status = PSA_ERROR_BAD_STATE;
4052 goto exit;
4053 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004054
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004056 status = PSA_ERROR_BAD_STATE;
4057 goto exit;
4058 }
Ronald Crona0d68172021-03-26 10:15:08 +01004059
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004061 status = PSA_ERROR_INVALID_ARGUMENT;
4062 goto exit;
4063 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004064
Gilles Peskine449bd832023-01-11 14:50:10 +01004065 status = psa_driver_wrapper_cipher_set_iv(operation,
4066 iv,
4067 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004068
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004069exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004071 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 } else {
4073 psa_cipher_abort(operation);
4074 }
4075 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004076}
4077
Gilles Peskine449bd832023-01-11 14:50:10 +01004078psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4079 const uint8_t *input,
4080 size_t input_length,
4081 uint8_t *output,
4082 size_t output_size,
4083 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004084{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004085 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004086
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004088 status = PSA_ERROR_BAD_STATE;
4089 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004090 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004093 status = PSA_ERROR_BAD_STATE;
4094 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004095 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004096
Gilles Peskine449bd832023-01-11 14:50:10 +01004097 status = psa_driver_wrapper_cipher_update(operation,
4098 input,
4099 input_length,
4100 output,
4101 output_size,
4102 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004103
4104exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 if (status != PSA_SUCCESS) {
4106 psa_cipher_abort(operation);
4107 }
Ronald Cron6d051732020-10-01 14:10:20 +02004108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004110}
4111
Gilles Peskine449bd832023-01-11 14:50:10 +01004112psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4113 uint8_t *output,
4114 size_t output_size,
4115 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004116{
David Saadab4ecc272019-02-14 13:48:10 +02004117 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004118
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004120 status = PSA_ERROR_BAD_STATE;
4121 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004122 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004123
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004125 status = PSA_ERROR_BAD_STATE;
4126 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004127 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004128
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 status = psa_driver_wrapper_cipher_finish(operation,
4130 output,
4131 output_size,
4132 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004133
4134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 if (status == PSA_SUCCESS) {
4136 return psa_cipher_abort(operation);
4137 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004138 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004142 }
mohammad1603503973b2018-03-12 15:59:30 +02004143}
4144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004146{
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004148 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004149 * in use. It's ok to call abort on such an object, and there's
4150 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004152 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004155
Ronald Cron49fafa92021-03-10 08:34:23 +01004156 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004157 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004158 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004161}
4162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4164 psa_algorithm_t alg,
4165 const uint8_t *input,
4166 size_t input_length,
4167 uint8_t *output,
4168 size_t output_size,
4169 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004170{
4171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004172 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004173 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004174 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4175 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004178 status = PSA_ERROR_INVALID_ARGUMENT;
4179 goto exit;
4180 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4183 PSA_KEY_USAGE_ENCRYPT,
4184 alg);
4185 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004186 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004188
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004189 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004191 };
4192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4194 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004195 status = PSA_ERROR_GENERIC_ERROR;
4196 goto exit;
4197 }
4198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 if (default_iv_length > 0) {
4200 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004201 status = PSA_ERROR_BUFFER_TOO_SMALL;
4202 goto exit;
4203 }
4204
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 status = psa_generate_random(local_iv, default_iv_length);
4206 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004207 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004209 }
4210
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004211 status = psa_driver_wrapper_cipher_encrypt(
4212 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004213 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 mbedtls_buffer_offset(output, default_iv_length),
4215 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004216
4217exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 unlock_status = psa_unlock_key_slot(slot);
4219 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004220 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004221 }
Ronald Cron23919522021-07-08 19:00:07 +02004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 if (status == PSA_SUCCESS) {
4224 if (default_iv_length > 0) {
4225 memcpy(output, local_iv, default_iv_length);
4226 }
4227 *output_length += default_iv_length;
4228 } else {
4229 *output_length = 0;
4230 }
4231
4232 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004233}
4234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4236 psa_algorithm_t alg,
4237 const uint8_t *input,
4238 size_t input_length,
4239 uint8_t *output,
4240 size_t output_size,
4241 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004242{
4243 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004244 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004245 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004246
Gilles Peskine449bd832023-01-11 14:50:10 +01004247 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004248 status = PSA_ERROR_INVALID_ARGUMENT;
4249 goto exit;
4250 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4253 PSA_KEY_USAGE_DECRYPT,
4254 alg);
4255 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004256 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004258
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004259 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004261 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4264 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004265 status = PSA_ERROR_INVALID_ARGUMENT;
4266 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004268 status = PSA_ERROR_INVALID_ARGUMENT;
4269 goto exit;
4270 }
4271
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004272 status = psa_driver_wrapper_cipher_decrypt(
4273 &attributes, slot->key.data, slot->key.bytes,
4274 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004276
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 unlock_status = psa_unlock_key_slot(slot);
4279 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004280 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004282
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004284 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 }
Ronald Cron23919522021-07-08 19:00:07 +02004286
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004288}
4289
4290
mohammad16035955c982018-04-26 00:53:03 +03004291/****************************************************************/
4292/* AEAD */
4293/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004294
Paul Elliottbaff51c2021-09-28 17:44:45 +01004295/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004296static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004297{
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004299}
4300
4301/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004302static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4303 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004304{
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004306
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004308#if defined(PSA_WANT_ALG_GCM)
4309 case PSA_ALG_GCM:
4310 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 * arbitrarily large nonces. Please note that we do not generally
4312 * recommend the usage of nonces of greater length than
4313 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4314 * size, which can then lead to collisions if you encrypt a very
4315 * large number of messages.*/
4316 if (nonce_length != 0) {
4317 return PSA_SUCCESS;
4318 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004319 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004320#endif /* PSA_WANT_ALG_GCM */
4321#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004322 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 if (nonce_length >= 7 && nonce_length <= 13) {
4324 return PSA_SUCCESS;
4325 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004326 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004327#endif /* PSA_WANT_ALG_CCM */
4328#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004329 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 if (nonce_length == 12) {
4331 return PSA_SUCCESS;
4332 } else if (nonce_length == 8) {
4333 return PSA_ERROR_NOT_SUPPORTED;
4334 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004335 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004336#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004337 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004338 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004340 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004341
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004343}
4344
Gilles Peskine449bd832023-01-11 14:50:10 +01004345static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004346{
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4348 return PSA_ERROR_INVALID_ARGUMENT;
4349 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004352}
4353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4355 psa_algorithm_t alg,
4356 const uint8_t *nonce,
4357 size_t nonce_length,
4358 const uint8_t *additional_data,
4359 size_t additional_data_length,
4360 const uint8_t *plaintext,
4361 size_t plaintext_length,
4362 uint8_t *ciphertext,
4363 size_t ciphertext_size,
4364 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004365{
Ronald Cron215633c2021-03-16 17:15:37 +01004366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4367 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004368
mohammad1603f08a5502018-06-03 15:05:47 +03004369 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 status = psa_aead_check_algorithm(alg);
4372 if (status != PSA_SUCCESS) {
4373 return status;
4374 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004375
Ronald Cron9a986162021-03-26 12:40:07 +01004376 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4378 if (status != PSA_SUCCESS) {
4379 return status;
4380 }
mohammad16035955c982018-04-26 00:53:03 +03004381
Ronald Cron215633c2021-03-16 17:15:37 +01004382 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004384 };
mohammad16035955c982018-04-26 00:53:03 +03004385
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 status = psa_aead_check_nonce_length(alg, nonce_length);
4387 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004388 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004390
Ronald Cronde822812021-03-17 16:08:20 +01004391 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004392 &attributes, slot->key.data, slot->key.bytes,
4393 alg,
4394 nonce, nonce_length,
4395 additional_data, additional_data_length,
4396 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004398
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4400 memset(ciphertext, 0, ciphertext_size);
4401 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004402
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004403exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004407}
4408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4410 psa_algorithm_t alg,
4411 const uint8_t *nonce,
4412 size_t nonce_length,
4413 const uint8_t *additional_data,
4414 size_t additional_data_length,
4415 const uint8_t *ciphertext,
4416 size_t ciphertext_length,
4417 uint8_t *plaintext,
4418 size_t plaintext_size,
4419 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004420{
Ronald Cron215633c2021-03-16 17:15:37 +01004421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4422 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004423
Gilles Peskineee652a32018-06-01 19:23:52 +02004424 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004425
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 status = psa_aead_check_algorithm(alg);
4427 if (status != PSA_SUCCESS) {
4428 return status;
4429 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004430
Ronald Cron9a986162021-03-26 12:40:07 +01004431 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4433 if (status != PSA_SUCCESS) {
4434 return status;
4435 }
mohammad16035955c982018-04-26 00:53:03 +03004436
Ronald Cron215633c2021-03-16 17:15:37 +01004437 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004439 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 status = psa_aead_check_nonce_length(alg, nonce_length);
4442 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004443 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004445
Ronald Cronde822812021-03-17 16:08:20 +01004446 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004447 &attributes, slot->key.data, slot->key.bytes,
4448 alg,
4449 nonce, nonce_length,
4450 additional_data, additional_data_length,
4451 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 if (status != PSA_SUCCESS && plaintext_size != 0) {
4455 memset(plaintext, 0, plaintext_size);
4456 }
mohammad160360a64d02018-06-03 17:20:42 +03004457
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004458exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 return status;
mohammad16035955c982018-04-26 00:53:03 +03004462}
4463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4465{
4466 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004469#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004471 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4473 return PSA_ERROR_INVALID_ARGUMENT;
4474 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004475 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004476#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004477
Przemek Stekiel86679c72022-10-06 17:06:56 +02004478#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004480 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4482 return PSA_ERROR_INVALID_ARGUMENT;
4483 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004484 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004485#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004486
Przemek Stekiel86679c72022-10-06 17:06:56 +02004487#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004489 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (tag_len != 16) {
4491 return PSA_ERROR_INVALID_ARGUMENT;
4492 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004493 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004494#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004495
4496 default:
4497 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004499 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004501}
4502
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004503/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004504static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4505 int is_encrypt,
4506 mbedtls_svc_key_id_t key,
4507 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004508{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004509 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4510 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4511 psa_key_slot_t *slot = NULL;
4512 psa_key_usage_t key_usage = 0;
4513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 status = psa_aead_check_algorithm(alg);
4515 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004516 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004520 status = PSA_ERROR_BAD_STATE;
4521 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004522 }
4523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 if (operation->nonce_set || operation->lengths_set ||
4525 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004526 status = PSA_ERROR_BAD_STATE;
4527 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004528 }
4529
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004531 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004533 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004535
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4537 alg);
4538 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004539 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004541
4542 psa_key_attributes_t attributes = {
4543 .core = slot->attr
4544 };
4545
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004547 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004549
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 if (is_encrypt) {
4551 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4552 &attributes,
4553 slot->key.data,
4554 slot->key.bytes,
4555 alg);
4556 } else {
4557 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4558 &attributes,
4559 slot->key.data,
4560 slot->key.bytes,
4561 alg);
4562 }
4563 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004564 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004568
4569exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004573 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004575 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 } else {
4577 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004578 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004581}
4582
Paul Elliott302ff6b2021-04-20 18:10:30 +01004583/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004584psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4585 mbedtls_svc_key_id_t key,
4586 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004587{
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004589}
4590
4591/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004592psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4593 mbedtls_svc_key_id_t key,
4594 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004595{
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004597}
4598
4599/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004600psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4601 uint8_t *nonce,
4602 size_t nonce_size,
4603 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004604{
4605 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004606 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004607 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004608
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004609 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004612 status = PSA_ERROR_BAD_STATE;
4613 goto exit;
4614 }
4615
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004617 status = PSA_ERROR_BAD_STATE;
4618 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004619 }
4620
Paul Elliotte193ea82021-10-01 13:00:16 +01004621 /* For CCM, this size may not be correct according to the PSA
4622 * specification. The PSA Crypto 1.0.1 specification states:
4623 *
4624 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4625 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4626 *
4627 * However this restriction that L has to be the smallest integer is not
4628 * applied in practice, and it is not implementable here since the
4629 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4631 operation->alg);
4632 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004633 status = PSA_ERROR_BUFFER_TOO_SMALL;
4634 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004635 }
4636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 status = psa_generate_random(local_nonce, required_nonce_size);
4638 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004639 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004643
Paul Elliott39dc6b82021-05-11 19:16:09 +01004644exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 if (status == PSA_SUCCESS) {
4646 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004647 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 } else {
4649 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004650 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004653}
4654
4655/* Set the nonce for a multipart authenticated encryption or decryption
4656 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004657psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4658 const uint8_t *nonce,
4659 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004660{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004661 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4662
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004664 status = PSA_ERROR_BAD_STATE;
4665 goto exit;
4666 }
4667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004669 status = PSA_ERROR_BAD_STATE;
4670 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004671 }
4672
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4674 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004675 status = PSA_ERROR_INVALID_ARGUMENT;
4676 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004677 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4680 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004681
Paul Elliott39dc6b82021-05-11 19:16:09 +01004682exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004684 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 } else {
4686 psa_aead_abort(operation);
4687 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004690}
4691
4692/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004693psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4694 size_t ad_length,
4695 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004696{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004697 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004700 status = PSA_ERROR_BAD_STATE;
4701 goto exit;
4702 }
4703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 if (operation->lengths_set || operation->ad_started ||
4705 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004706 status = PSA_ERROR_BAD_STATE;
4707 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004708 }
4709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004711#if defined(PSA_WANT_ALG_GCM)
4712 case PSA_ALG_GCM:
4713 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 * bits. Without the guard this code will generate warnings on 32bit
4715 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004716#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if (((uint64_t) ad_length) >> 61 != 0 ||
4718 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004719 status = PSA_ERROR_INVALID_ARGUMENT;
4720 goto exit;
4721 }
Paul Elliott325d3742021-09-27 17:56:28 +01004722#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004723 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004724#endif /* PSA_WANT_ALG_GCM */
4725#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004726 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004728 status = PSA_ERROR_INVALID_ARGUMENT;
4729 goto exit;
4730 }
4731 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004732#endif /* PSA_WANT_ALG_CCM */
4733#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004734 case PSA_ALG_CHACHA20_POLY1305:
4735 /* No length restrictions for ChaChaPoly. */
4736 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004737#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004738 default:
4739 break;
4740 }
Paul Elliott325d3742021-09-27 17:56:28 +01004741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4743 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004744
Paul Elliott39dc6b82021-05-11 19:16:09 +01004745exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004747 operation->ad_remaining = ad_length;
4748 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004749 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 } else {
4751 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004752 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004755}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004756
4757/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004758psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4759 const uint8_t *input,
4760 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004761{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004762 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004765 status = PSA_ERROR_BAD_STATE;
4766 goto exit;
4767 }
4768
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004770 status = PSA_ERROR_BAD_STATE;
4771 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004772 }
4773
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 if (operation->lengths_set) {
4775 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004776 status = PSA_ERROR_INVALID_ARGUMENT;
4777 goto exit;
4778 }
4779
4780 operation->ad_remaining -= input_length;
4781 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004782#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004784 status = PSA_ERROR_BAD_STATE;
4785 goto exit;
4786 }
4787#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004788
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 status = psa_driver_wrapper_aead_update_ad(operation, input,
4790 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004791
Paul Elliott39dc6b82021-05-11 19:16:09 +01004792exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004794 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 } else {
4796 psa_aead_abort(operation);
4797 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004798
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004800}
4801
4802/* Encrypt or decrypt a message fragment in an active multipart AEAD
4803 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004804psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4805 const uint8_t *input,
4806 size_t input_length,
4807 uint8_t *output,
4808 size_t output_size,
4809 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004810{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004811 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004812
4813 *output_length = 0;
4814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004816 status = PSA_ERROR_BAD_STATE;
4817 goto exit;
4818 }
4819
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004821 status = PSA_ERROR_BAD_STATE;
4822 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823 }
4824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004826 /* Additional data length was supplied, but not all the additional
4827 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004829 status = PSA_ERROR_INVALID_ARGUMENT;
4830 goto exit;
4831 }
4832
4833 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004835 status = PSA_ERROR_INVALID_ARGUMENT;
4836 goto exit;
4837 }
4838
4839 operation->body_remaining -= input_length;
4840 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004841#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004843 status = PSA_ERROR_BAD_STATE;
4844 goto exit;
4845 }
4846#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4849 output, output_size,
4850 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004851
Paul Elliott39dc6b82021-05-11 19:16:09 +01004852exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004854 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 } else {
4856 psa_aead_abort(operation);
4857 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004858
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004860}
4861
Gilles Peskine449bd832023-01-11 14:50:10 +01004862static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004863{
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (operation->id == 0 || !operation->nonce_set) {
4865 return PSA_ERROR_BAD_STATE;
4866 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4869 operation->body_remaining != 0)) {
4870 return PSA_ERROR_INVALID_ARGUMENT;
4871 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004874}
4875
Paul Elliott302ff6b2021-04-20 18:10:30 +01004876/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004877psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4878 uint8_t *ciphertext,
4879 size_t ciphertext_size,
4880 size_t *ciphertext_length,
4881 uint8_t *tag,
4882 size_t tag_size,
4883 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004884{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004885 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4886
Paul Elliott302ff6b2021-04-20 18:10:30 +01004887 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004888 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004889
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 status = psa_aead_final_checks(operation);
4891 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004892 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004894
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004896 status = PSA_ERROR_BAD_STATE;
4897 goto exit;
4898 }
4899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4901 ciphertext_size,
4902 ciphertext_length,
4903 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004904
Paul Elliott39dc6b82021-05-11 19:16:09 +01004905exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004906
4907
Paul Elliottf47b0952021-05-21 18:02:33 +01004908 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004909 * the zero tag size, make sure the tag is set to something implausible.
4910 * Even if the operation succeeds, make sure we clear the rest of the
4911 * buffer to prevent potential leakage of anything previously placed in
4912 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004913 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004918}
4919
4920/* Finish authenticating and decrypting a message in a multipart AEAD
4921 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004922psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4923 uint8_t *plaintext,
4924 size_t plaintext_size,
4925 size_t *plaintext_length,
4926 const uint8_t *tag,
4927 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004928{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004929 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4930
Paul Elliott302ff6b2021-04-20 18:10:30 +01004931 *plaintext_length = 0;
4932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 status = psa_aead_final_checks(operation);
4934 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004935 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004939 status = PSA_ERROR_BAD_STATE;
4940 goto exit;
4941 }
4942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4944 plaintext_size,
4945 plaintext_length,
4946 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004947
4948exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004952}
4953
4954/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004955psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004956{
4957 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004960 /* The object has (apparently) been initialized but it is not (yet)
4961 * in use. It's ok to call abort on such an object, and there's
4962 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004964 }
4965
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004971}
4972
Gilles Peskinee59236f2018-01-27 23:32:46 +01004973/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004974/* Generators */
4975/****************************************************************/
4976
Przemek Stekiel69c46792022-06-10 12:59:51 +02004977#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004978 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004979 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4980 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004981#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004982#endif /* At least one builtin KDF */
4983
Przemek Stekiel69c46792022-06-10 12:59:51 +02004984#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004985 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4986 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4987static psa_status_t psa_key_derivation_start_hmac(
4988 psa_mac_operation_t *operation,
4989 psa_algorithm_t hash_alg,
4990 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004992{
4993 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4996 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4997 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004998
Steven Cooreman72f736a2021-05-07 14:14:37 +02004999 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005001
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 status = psa_driver_wrapper_mac_sign_setup(operation,
5003 &attributes,
5004 hmac_key, hmac_key_length,
5005 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005006
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 psa_reset_key_attributes(&attributes);
5008 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005009}
5010#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005011
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005012#define HKDF_STATE_INIT 0 /* no input yet */
5013#define HKDF_STATE_STARTED 1 /* got salt */
5014#define HKDF_STATE_KEYED 2 /* got key */
5015#define HKDF_STATE_OUTPUT 3 /* output started */
5016
Gilles Peskinecbe66502019-05-16 16:59:18 +02005017static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005019{
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5021 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5022 } else {
5023 return operation->alg;
5024 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005025}
5026
Gilles Peskine449bd832023-01-11 14:50:10 +01005027psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005028{
5029 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5031 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005032 /* The object has (apparently) been initialized but it is not
5033 * in use. It's ok to call abort on such an object, and there's
5034 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005036#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5038 mbedtls_free(operation->ctx.hkdf.info);
5039 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5040 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005041#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005042#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5043 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5045 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5046 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5047 if (operation->ctx.tls12_prf.secret != NULL) {
5048 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5049 operation->ctx.tls12_prf.secret_length);
5050 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005051 }
5052
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 if (operation->ctx.tls12_prf.seed != NULL) {
5054 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5055 operation->ctx.tls12_prf.seed_length);
5056 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005057 }
5058
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 if (operation->ctx.tls12_prf.label != NULL) {
5060 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5061 operation->ctx.tls12_prf.label_length);
5062 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005063 }
5064
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 if (operation->ctx.tls12_prf.other_secret != NULL) {
5066 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5067 operation->ctx.tls12_prf.other_secret_length);
5068 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005069 }
5070
Steven Cooremana6df6042021-04-29 19:32:25 +02005071 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005072
5073 /* We leave the fields Ai and output_block to be erased safely by the
5074 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005076#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5077 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005078#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5080 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5081 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5082 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005083#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005084 {
5085 status = PSA_ERROR_BAD_STATE;
5086 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 mbedtls_platform_zeroize(operation, sizeof(*operation));
5088 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005089}
5090
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005091psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005093{
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005095 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005097 }
5098
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005101}
5102
Gilles Peskine449bd832023-01-11 14:50:10 +01005103psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5104 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005105{
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 if (operation->alg == 0) {
5107 return PSA_ERROR_BAD_STATE;
5108 }
5109 if (capacity > operation->capacity) {
5110 return PSA_ERROR_INVALID_ARGUMENT;
5111 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005114}
5115
Przemek Stekiel69c46792022-06-10 12:59:51 +02005116#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005117/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005118static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5119 psa_algorithm_t kdf_alg,
5120 uint8_t *output,
5121 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005122{
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5124 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005125 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005126 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005127#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005129#else
5130 const uint8_t last_block = 0xff;
5131#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005132
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 if (hkdf->state < HKDF_STATE_KEYED ||
5134 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005135#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005137#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 )) {
5139 return PSA_ERROR_BAD_STATE;
5140 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005141 hkdf->state = HKDF_STATE_OUTPUT;
5142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005144 /* Copy what remains of the current block */
5145 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005147 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 }
5149 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005150 output += n;
5151 output_length -= n;
5152 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005154 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005156 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005157 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005158 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005159 * object was corrupted or if this function is called directly
5160 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 if (hkdf->block_number == last_block) {
5162 return PSA_ERROR_BAD_STATE;
5163 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005164
5165 /* We need a new block */
5166 ++hkdf->block_number;
5167 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5170 hash_alg,
5171 hkdf->prk,
5172 hash_length);
5173 if (status != PSA_SUCCESS) {
5174 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005175 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005176
5177 if (hkdf->block_number != 1) {
5178 status = psa_mac_update(&hkdf->hmac,
5179 hkdf->output_block,
5180 hash_length);
5181 if (status != PSA_SUCCESS) {
5182 return status;
5183 }
5184 }
5185 status = psa_mac_update(&hkdf->hmac,
5186 hkdf->info,
5187 hkdf->info_length);
5188 if (status != PSA_SUCCESS) {
5189 return status;
5190 }
5191 status = psa_mac_update(&hkdf->hmac,
5192 &hkdf->block_number, 1);
5193 if (status != PSA_SUCCESS) {
5194 return status;
5195 }
5196 status = psa_mac_sign_finish(&hkdf->hmac,
5197 hkdf->output_block,
5198 sizeof(hkdf->output_block),
5199 &hmac_output_length);
5200 if (status != PSA_SUCCESS) {
5201 return status;
5202 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005203 }
5204
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005206}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005207#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005208
John Durkop07cc04a2020-11-16 22:08:34 -08005209#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5210 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005211static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5212 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005214{
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5216 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005217 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5218 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005219 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005220
Janos Follath7742fee2019-06-17 12:58:10 +01005221 /* We can't be wanting more output after block 0xff, otherwise
5222 * the capacity check in psa_key_derivation_output_bytes() would have
5223 * prevented this call. It could happen only if the operation
5224 * object was corrupted or if this function is called directly
5225 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 if (tls12_prf->block_number == 0xff) {
5227 return PSA_ERROR_CORRUPTION_DETECTED;
5228 }
Janos Follath7742fee2019-06-17 12:58:10 +01005229
5230 /* We need a new block */
5231 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005232 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005233
5234 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5235 *
5236 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5237 *
5238 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5239 * HMAC_hash(secret, A(2) + seed) +
5240 * HMAC_hash(secret, A(3) + seed) + ...
5241 *
5242 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005243 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005244 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005245 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005246 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005247 * is currently extracted as `output_block` and where i is
5248 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005249 */
5250
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 status = psa_key_derivation_start_hmac(&hmac,
5252 hash_alg,
5253 tls12_prf->secret,
5254 tls12_prf->secret_length);
5255 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005256 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005258
5259 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005261 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5262 * the variable seed and in this instance means it in the context of the
5263 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 status = psa_mac_update(&hmac,
5265 tls12_prf->label,
5266 tls12_prf->label_length);
5267 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005268 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 }
5270 status = psa_mac_update(&hmac,
5271 tls12_prf->seed,
5272 tls12_prf->seed_length);
5273 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005274 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 }
5276 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005277 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5279 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005280 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005282 }
5283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 status = psa_mac_sign_finish(&hmac,
5285 tls12_prf->Ai, hash_length,
5286 &hmac_output_length);
5287 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005288 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 }
5290 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005291 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005293
5294 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 status = psa_key_derivation_start_hmac(&hmac,
5296 hash_alg,
5297 tls12_prf->secret,
5298 tls12_prf->secret_length);
5299 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005300 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 }
5302 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5303 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005304 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 }
5306 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5307 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005308 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 }
5310 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5311 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005312 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 }
5314 status = psa_mac_sign_finish(&hmac,
5315 tls12_prf->output_block, hash_length,
5316 &hmac_output_length);
5317 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005318 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005320
Janos Follath7742fee2019-06-17 12:58:10 +01005321
5322cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 cleanup_status = psa_mac_abort(&hmac);
5324 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005325 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005329}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005330
Janos Follath844eb0e2019-06-19 12:10:49 +01005331static psa_status_t psa_key_derivation_tls12_prf_read(
5332 psa_tls12_prf_key_derivation_t *tls12_prf,
5333 psa_algorithm_t alg,
5334 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005336{
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5338 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005339 psa_status_t status;
5340 uint8_t offset, length;
5341
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005343 case PSA_TLS12_PRF_STATE_LABEL_SET:
5344 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5345 break;
5346 case PSA_TLS12_PRF_STATE_OUTPUT:
5347 break;
5348 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005350 }
5351
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005353 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 if (tls12_prf->left_in_block == 0) {
5355 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5356 alg);
5357 if (status != PSA_SUCCESS) {
5358 return status;
5359 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005360
5361 continue;
5362 }
5363
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005365 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005367 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005369
5370 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005372 output += length;
5373 output_length -= length;
5374 tls12_prf->left_in_block -= length;
5375 }
5376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005378}
John Durkop07cc04a2020-11-16 22:08:34 -08005379#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5380 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005381
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005382#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5383static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5384 psa_tls12_ecjpake_to_pms_t *ecjpake,
5385 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005387{
5388 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005389 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005390
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 if (output_length != 32) {
5392 return PSA_ERROR_INVALID_ARGUMENT;
5393 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5396 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5397 &output_size);
5398 if (status != PSA_SUCCESS) {
5399 return status;
5400 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 if (output_size != output_length) {
5403 return PSA_ERROR_GENERIC_ERROR;
5404 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005405
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005407}
5408#endif
5409
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005410psa_status_t psa_key_derivation_output_bytes(
5411 psa_key_derivation_operation_t *operation,
5412 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005414{
5415 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005417
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005419 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005421 }
5422
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005424 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005425 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005426 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005427 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005428 goto exit;
5429 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005431 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005432 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005433 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5434 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005435 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005436 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005438 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005440
Przemek Stekiel69c46792022-06-10 12:59:51 +02005441#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5443 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5444 output, output_length);
5445 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005446#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005447#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5448 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5450 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5451 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5452 kdf_alg, output,
5453 output_length);
5454 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005455#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5456 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005457#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005459 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5461 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005462#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5463
Gilles Peskineeab56e42018-07-12 17:12:33 +02005464 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005465 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005467 }
5468
5469exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005471 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005472 * This allows us to differentiate between exhausted operations and
5473 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5474 * operations. */
5475 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005477 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005479 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005481}
5482
David Brown7807bf72021-02-09 16:28:23 -07005483#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005484static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005485{
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 if (data_size >= 8) {
5487 mbedtls_des_key_set_parity(data);
5488 }
5489 if (data_size >= 16) {
5490 mbedtls_des_key_set_parity(data + 8);
5491 }
5492 if (data_size >= 24) {
5493 mbedtls_des_key_set_parity(data + 16);
5494 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005495}
David Brown7807bf72021-02-09 16:28:23 -07005496#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005497
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005498/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 * ECC keys on a Weierstrass elliptic curve require the generation
5500 * of a private key which is an integer
5501 * in the range [1, N - 1], where N is the boundary of the private key domain:
5502 * N is the prime p for Diffie-Hellman, or the order of the
5503 * curve’s base point for ECC.
5504 *
5505 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5506 * This function generates the private key using the following process:
5507 *
5508 * 1. Draw a byte string of length ceiling(m/8) bytes.
5509 * 2. If m is not a multiple of 8, set the most significant
5510 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5511 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5512 * 4. If k > N - 2, discard the result and return to step 1.
5513 * 5. Output k + 1 as the private key.
5514 *
5515 * This method allows compliance to NIST standards, specifically the methods titled
5516 * Key-Pair Generation by Testing Candidates in the following publications:
5517 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5518 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5519 * Diffie-Hellman keys.
5520 *
5521 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5522 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5523 *
5524 * Note: Function allocates memory for *data buffer, so given *data should be
5525 * always NULL.
5526 */
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005527#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5528 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5529 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5530 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5531 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005532static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005533 psa_key_slot_t *slot,
5534 size_t bits,
5535 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005536 uint8_t **data
5537 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005538{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005539 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005540 mbedtls_mpi k;
5541 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005543 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 mbedtls_mpi_init(&k);
5546 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005547
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005548 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005550 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005552
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005554 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005555 goto cleanup;
5556 }
5557
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005558 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005560
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005562
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005563 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005564 /* Let m be the bit size of N. */
5565 size_t m = ecp_group.nbits;
5566
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005568
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005569 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005571
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005572 /* Note: This function is always called with *data == NULL and it
5573 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 *data = mbedtls_calloc(1, m_bytes);
5575 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005576 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005577 goto cleanup;
5578 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005581 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005583 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005585
5586 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005588 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005589 * (8 * ceiling(m/8) - m) bits of the first byte in
5590 * the string to zero.
5591 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005593 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005594 }
5595
5596 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 * big-endian byte string.
5598 */
5599 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005600
5601 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 * Result of comparison is returned. When it indicates error
5603 * then this function is called again.
5604 */
5605 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005606 }
5607
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005608 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5610 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005611cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 if (ret != 0) {
5613 status = mbedtls_to_psa_error(ret);
5614 }
5615 if (status != PSA_SUCCESS) {
5616 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005617 *data = NULL;
5618 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 mbedtls_mpi_free(&k);
5620 mbedtls_mpi_free(&diff_N_2);
5621 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005622}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005623
5624/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5625 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5626 *
5627 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5628 * draw a 32-byte string and process it as specified in
5629 * Elliptic Curves for Security [RFC7748] §5.
5630 *
5631 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5632 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5633 *
5634 * Note: Function allocates memory for *data buffer, so given *data should be
5635 * always NULL.
5636 */
5637
5638static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5639 size_t bits,
5640 psa_key_derivation_operation_t *operation,
5641 uint8_t **data
5642 )
5643{
5644 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005645 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005648 case 255:
5649 output_length = 32;
5650 break;
5651 case 448:
5652 output_length = 56;
5653 break;
5654 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005656 break;
5657 }
5658
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005660
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 if (*data == NULL) {
5662 return PSA_ERROR_INSUFFICIENT_MEMORY;
5663 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005664
Gilles Peskine449bd832023-01-11 14:50:10 +01005665 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005666
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005668 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005672 case 255:
5673 (*data)[0] &= 248;
5674 (*data)[31] &= 127;
5675 (*data)[31] |= 64;
5676 break;
5677 case 448:
5678 (*data)[0] &= 252;
5679 (*data)[55] |= 128;
5680 break;
5681 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005683 break;
5684 }
5685
5686 return status;
5687}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005688#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5689 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5690 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5691 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5692 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005693
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005694static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005695 psa_key_slot_t *slot,
5696 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005698{
5699 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305701 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5705 return PSA_ERROR_INVALID_ARGUMENT;
5706 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005707
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005708#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5709 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5710 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5711 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5712 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5714 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5715 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005716 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5718 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005719 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 }
5721 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005722 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5724 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005725 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005727 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005728 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005729#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5730 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5731 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 (key_type_is_raw_bytes(slot->attr.type)) {
5735 if (bits % 8 != 0) {
5736 return PSA_ERROR_INVALID_ARGUMENT;
5737 }
5738 data = mbedtls_calloc(1, bytes);
5739 if (data == NULL) {
5740 return PSA_ERROR_INSUFFICIENT_MEMORY;
5741 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 status = psa_key_derivation_output_bytes(operation, data, bytes);
5744 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005745 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 }
David Brown12ca5032021-02-11 11:02:00 -07005747#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5749 psa_des_set_key_parity(data, bytes);
5750 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005751#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 } else {
5753 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005754 }
Ronald Crondd04d422020-11-28 15:14:42 +01005755
Ronald Cronbf33c932020-11-28 18:06:53 +01005756 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005757 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005759 };
5760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5762 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5763 &storage_size);
5764 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305765 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 }
Archanad8a83dc2021-06-14 10:04:16 +05305767 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 status = psa_allocate_buffer_to_slot(slot, storage_size);
5769 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305770 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 }
Archanad8a83dc2021-06-14 10:04:16 +05305772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 status = psa_driver_wrapper_import_key(&attributes,
5774 data, bytes,
5775 slot->key.data,
5776 slot->key.bytes,
5777 &slot->key.bytes, &bits);
5778 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005779 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005781
5782exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 mbedtls_free(data);
5784 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005785}
5786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5788 psa_key_derivation_operation_t *operation,
5789 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005790{
5791 psa_status_t status;
5792 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005793 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005794
Ronald Cron81709fc2020-11-14 12:10:32 +01005795 *key = MBEDTLS_SVC_KEY_ID_INIT;
5796
Gilles Peskine0f84d622019-09-12 19:03:13 +02005797 /* Reject any attempt to create a zero-length key so that we don't
5798 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 if (psa_get_key_bits(attributes) == 0) {
5800 return PSA_ERROR_INVALID_ARGUMENT;
5801 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 if (operation->alg == PSA_ALG_NONE) {
5804 return PSA_ERROR_BAD_STATE;
5805 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 if (!operation->can_output_key) {
5808 return PSA_ERROR_NOT_PERMITTED;
5809 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005810
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5812 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005813#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005815 /* Deriving a key in a secure element is not implemented yet. */
5816 status = PSA_ERROR_NOT_SUPPORTED;
5817 }
5818#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 if (status == PSA_SUCCESS) {
5820 status = psa_generate_derived_key_internal(slot,
5821 attributes->core.bits,
5822 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005823 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 if (status == PSA_SUCCESS) {
5825 status = psa_finish_key_creation(slot, driver, key);
5826 }
5827 if (status != PSA_SUCCESS) {
5828 psa_fail_key_creation(slot, driver);
5829 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005830
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005832}
5833
Gilles Peskineeab56e42018-07-12 17:12:33 +02005834
5835
5836/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005837/* Key derivation */
5838/****************************************************************/
5839
Steven Cooreman932ffb72021-02-15 12:14:32 +01005840#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005841static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005842{
5843#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5845 return 1;
5846 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005847#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005848#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5850 return 1;
5851 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005852#endif
5853#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5855 return 1;
5856 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005857#endif
5858#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5860 return 1;
5861 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005862#endif
5863#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5865 return 1;
5866 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005867#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005868#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5870 return 1;
5871 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005872#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005874}
5875
Gilles Peskine449bd832023-01-11 14:50:10 +01005876static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005877{
5878 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 psa_status_t status = psa_hash_setup(&operation, alg);
5880 psa_hash_abort(&operation);
5881 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005882}
5883
Gilles Peskine969c5d62019-01-16 15:53:06 +01005884static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005885 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005887{
Janos Follath5fe19732019-06-20 15:09:30 +01005888 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5889 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005891
Gilles Peskine969c5d62019-01-16 15:53:06 +01005892 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 if (!is_kdf_alg_supported(kdf_alg)) {
5894 return PSA_ERROR_NOT_SUPPORTED;
5895 }
John Durkop07cc04a2020-11-16 22:08:34 -08005896
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005897 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005898 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5900 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5901 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5902 if (hash_size == 0) {
5903 return PSA_ERROR_NOT_SUPPORTED;
5904 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005905
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005906 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5907 * we might fail later, which is somewhat unfriendly and potentially
5908 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 psa_status_t status = psa_hash_try_support(hash_alg);
5910 if (status != PSA_SUCCESS) {
5911 return status;
5912 }
5913 } else {
5914 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005915 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005916
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5918 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5919 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5920 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005921 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005922#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005923 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5925 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005926 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005928#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5929 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 operation->capacity = 255 * hash_size;
5931 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005932}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005935{
5936#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 if (alg == PSA_ALG_ECDH) {
5938 return PSA_SUCCESS;
5939 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005940#endif
5941 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005943}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005944
5945static int psa_key_derivation_allows_free_form_secret_input(
5946 psa_algorithm_t kdf_alg)
5947{
5948#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5949 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5950 return 0;
5951 }
5952#endif
5953 (void) kdf_alg;
5954 return 1;
5955}
John Durkop07cc04a2020-11-16 22:08:34 -08005956#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5959 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005960{
5961 psa_status_t status;
5962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 if (operation->alg != 0) {
5964 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005965 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5968 return PSA_ERROR_INVALID_ARGUMENT;
5969 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5970#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5971 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5972 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5973 status = psa_key_agreement_try_support(ka_alg);
5974 if (status != PSA_SUCCESS) {
5975 return status;
5976 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005977 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
5978 return PSA_ERROR_INVALID_ARGUMENT;
5979 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
5981#else
5982 return PSA_ERROR_NOT_SUPPORTED;
5983#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5984 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
5985#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5986 status = psa_key_derivation_setup_kdf(operation, alg);
5987#else
5988 return PSA_ERROR_NOT_SUPPORTED;
5989#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5990 } else {
5991 return PSA_ERROR_INVALID_ARGUMENT;
5992 }
5993
5994 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005995 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 }
5997 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005998}
5999
Przemek Stekiel69c46792022-06-10 12:59:51 +02006000#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006001static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6002 psa_algorithm_t kdf_alg,
6003 psa_key_derivation_step_t step,
6004 const uint8_t *data,
6005 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006006{
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006008 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006010 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006011#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6013 return PSA_ERROR_INVALID_ARGUMENT;
6014 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006015#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 if (hkdf->state != HKDF_STATE_INIT) {
6017 return PSA_ERROR_BAD_STATE;
6018 } else {
6019 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6020 hash_alg,
6021 data, data_length);
6022 if (status != PSA_SUCCESS) {
6023 return status;
6024 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006025 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006027 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006028 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006029#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006031 /* We shouldn't be in different state as HKDF_EXPAND only allows
6032 * two inputs: SECRET (this case) and INFO which does not modify
6033 * the state. It could happen only if the hkdf
6034 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 if (hkdf->state != HKDF_STATE_INIT) {
6036 return PSA_ERROR_BAD_STATE;
6037 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006038
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006039 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6041 return PSA_ERROR_INVALID_ARGUMENT;
6042 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 memcpy(hkdf->prk, data, data_length);
6045 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006046#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006047 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006048 /* HKDF: If no salt was provided, use an empty salt.
6049 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006051#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6053 return PSA_ERROR_BAD_STATE;
6054 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006055#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6057 hash_alg,
6058 NULL, 0);
6059 if (status != PSA_SUCCESS) {
6060 return status;
6061 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006062 hkdf->state = HKDF_STATE_STARTED;
6063 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 if (hkdf->state != HKDF_STATE_STARTED) {
6065 return PSA_ERROR_BAD_STATE;
6066 }
6067 status = psa_mac_update(&hkdf->hmac,
6068 data, data_length);
6069 if (status != PSA_SUCCESS) {
6070 return status;
6071 }
6072 status = psa_mac_sign_finish(&hkdf->hmac,
6073 hkdf->prk,
6074 sizeof(hkdf->prk),
6075 &data_length);
6076 if (status != PSA_SUCCESS) {
6077 return status;
6078 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006079 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006080
Gilles Peskine2b522db2019-04-12 15:11:49 +02006081 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006082 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006083#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006085 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006087 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006089#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006090 {
6091 /* Block 0 is empty, and the next block will be
6092 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006094 }
6095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006097 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006098#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6100 return PSA_ERROR_INVALID_ARGUMENT;
6101 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006102#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006103#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6105 hkdf->state == HKDF_STATE_INIT) {
6106 return PSA_ERROR_BAD_STATE;
6107 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006108#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 if (hkdf->state == HKDF_STATE_OUTPUT) {
6110 return PSA_ERROR_BAD_STATE;
6111 }
6112 if (hkdf->info_set) {
6113 return PSA_ERROR_BAD_STATE;
6114 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006115 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 if (data_length != 0) {
6117 hkdf->info = mbedtls_calloc(1, data_length);
6118 if (hkdf->info == NULL) {
6119 return PSA_ERROR_INSUFFICIENT_MEMORY;
6120 }
6121 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006122 }
6123 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006125 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006127 }
6128}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006129#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006130
John Durkop07cc04a2020-11-16 22:08:34 -08006131#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6132 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006133static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6134 const uint8_t *data,
6135 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006136{
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6138 return PSA_ERROR_BAD_STATE;
6139 }
Janos Follathf08e2652019-06-13 09:05:41 +01006140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 if (data_length != 0) {
6142 prf->seed = mbedtls_calloc(1, data_length);
6143 if (prf->seed == NULL) {
6144 return PSA_ERROR_INSUFFICIENT_MEMORY;
6145 }
Janos Follathf08e2652019-06-13 09:05:41 +01006146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006148 prf->seed_length = data_length;
6149 }
Janos Follathf08e2652019-06-13 09:05:41 +01006150
Gilles Peskine43f958b2020-12-13 14:55:14 +01006151 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006154}
6155
Gilles Peskine449bd832023-01-11 14:50:10 +01006156static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6157 const uint8_t *data,
6158 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006159{
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6161 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6162 return PSA_ERROR_BAD_STATE;
6163 }
Janos Follath81550542019-06-13 14:26:34 +01006164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 if (data_length != 0) {
6166 prf->secret = mbedtls_calloc(1, data_length);
6167 if (prf->secret == NULL) {
6168 return PSA_ERROR_INSUFFICIENT_MEMORY;
6169 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006170
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006172 prf->secret_length = data_length;
6173 }
Janos Follath81550542019-06-13 14:26:34 +01006174
Gilles Peskine43f958b2020-12-13 14:55:14 +01006175 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006178}
6179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6181 const uint8_t *data,
6182 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006183{
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6185 return PSA_ERROR_BAD_STATE;
6186 }
Janos Follath63028dd2019-06-13 09:15:47 +01006187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 if (data_length != 0) {
6189 prf->label = mbedtls_calloc(1, data_length);
6190 if (prf->label == NULL) {
6191 return PSA_ERROR_INSUFFICIENT_MEMORY;
6192 }
Janos Follath63028dd2019-06-13 09:15:47 +01006193
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006195 prf->label_length = data_length;
6196 }
Janos Follath63028dd2019-06-13 09:15:47 +01006197
Gilles Peskine43f958b2020-12-13 14:55:14 +01006198 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006201}
6202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6204 psa_key_derivation_step_t step,
6205 const uint8_t *data,
6206 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006207{
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006209 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006211 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006213 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006215 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006217 }
6218}
John Durkop07cc04a2020-11-16 22:08:34 -08006219#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6220 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6221
6222#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6223static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6224 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006225 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006227{
6228 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6230 4 + data_length + prf->other_secret_length :
6231 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006232
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6234 return PSA_ERROR_INVALID_ARGUMENT;
6235 }
John Durkop07cc04a2020-11-16 22:08:34 -08006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 uint8_t *pms = mbedtls_calloc(1, pms_len);
6238 if (pms == NULL) {
6239 return PSA_ERROR_INSUFFICIENT_MEMORY;
6240 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006241 uint8_t *cur = pms;
6242
6243 /* pure-PSK:
6244 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006245 *
6246 * The premaster secret is formed as follows: if the PSK is N octets
6247 * long, concatenate a uint16 with the value N, N zero octets, a second
6248 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006249 *
6250 * mixed-PSK:
6251 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6252 * follows: concatenate a uint16 with the length of the other secret,
6253 * the other secret itself, uint16 with the length of PSK, and the
6254 * PSK itself.
6255 * For details please check:
6256 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6257 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6258 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006259 */
6260
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6262 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6263 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6264 if (prf->other_secret_length != 0) {
6265 memcpy(cur, prf->other_secret, prf->other_secret_length);
6266 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006267 cur += prf->other_secret_length;
6268 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 } else {
6270 *cur++ = MBEDTLS_BYTE_1(data_length);
6271 *cur++ = MBEDTLS_BYTE_0(data_length);
6272 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006273 cur += data_length;
6274 }
6275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 *cur++ = MBEDTLS_BYTE_1(data_length);
6277 *cur++ = MBEDTLS_BYTE_0(data_length);
6278 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006279 cur += data_length;
6280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006282
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 mbedtls_platform_zeroize(pms, pms_len);
6284 mbedtls_free(pms);
6285 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006286}
Janos Follath6660f0e2019-06-17 08:44:03 +01006287
Przemek Stekiele47201b2022-04-19 13:53:28 +02006288static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6289 psa_tls12_prf_key_derivation_t *prf,
6290 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006292{
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6294 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006295 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006296
6297 if (data_length != 0) {
6298 prf->other_secret = mbedtls_calloc(1, data_length);
6299 if (prf->other_secret == NULL) {
6300 return PSA_ERROR_INSUFFICIENT_MEMORY;
6301 }
6302
6303 memcpy(prf->other_secret, data, data_length);
6304 prf->other_secret_length = data_length;
6305 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006306 prf->other_secret_length = 0;
6307 }
6308
6309 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006312}
6313
Janos Follath6660f0e2019-06-17 08:44:03 +01006314static psa_status_t psa_tls12_prf_psk_to_ms_input(
6315 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006316 psa_key_derivation_step_t step,
6317 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006319{
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006321 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 return psa_tls12_prf_psk_to_ms_set_key(prf,
6323 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006324 break;
6325 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6327 data,
6328 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006329 break;
6330 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006332 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006333
Przemek Stekiele47201b2022-04-19 13:53:28 +02006334 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006335}
John Durkop07cc04a2020-11-16 22:08:34 -08006336#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006337
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006338#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6339static psa_status_t psa_tls12_ecjpake_to_pms_input(
6340 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006341 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006342 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006344{
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6346 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6347 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006348 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006349
6350 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 if (data[0] != 0x04) {
6352 return PSA_ERROR_INVALID_ARGUMENT;
6353 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006354
6355 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006357
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006359}
6360#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006361/** Check whether the given key type is acceptable for the given
6362 * input step of a key derivation.
6363 *
6364 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6365 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6366 * Both secret and non-secret inputs can alternatively have the type
6367 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6368 * that the input was passed as a buffer rather than via a key object.
6369 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006370static int psa_key_derivation_check_input_type(
6371 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006373{
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006375 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 if (key_type == PSA_KEY_TYPE_DERIVE) {
6377 return PSA_SUCCESS;
6378 }
6379 if (key_type == PSA_KEY_TYPE_NONE) {
6380 return PSA_SUCCESS;
6381 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006382 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006383 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 if (key_type == PSA_KEY_TYPE_DERIVE) {
6385 return PSA_SUCCESS;
6386 }
6387 if (key_type == PSA_KEY_TYPE_NONE) {
6388 return PSA_SUCCESS;
6389 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006390 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006391 case PSA_KEY_DERIVATION_INPUT_LABEL:
6392 case PSA_KEY_DERIVATION_INPUT_SALT:
6393 case PSA_KEY_DERIVATION_INPUT_INFO:
6394 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6396 return PSA_SUCCESS;
6397 }
6398 if (key_type == PSA_KEY_TYPE_NONE) {
6399 return PSA_SUCCESS;
6400 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006401 break;
6402 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006404}
6405
Janos Follathb80a94e2019-06-12 15:54:46 +01006406static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006407 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006408 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006409 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006410 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006412{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006413 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 status = psa_key_derivation_check_input_type(step, key_type);
6417 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006418 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006420
Przemek Stekiel69c46792022-06-10 12:59:51 +02006421#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6423 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6424 step, data, data_length);
6425 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006426#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006427#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6429 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6430 step, data, data_length);
6431 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006432#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6433#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6435 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6436 step, data, data_length);
6437 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006438#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006439#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006441 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6443 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006444#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006445 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006446 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006447 (void) data;
6448 (void) data_length;
6449 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006451 }
6452
Gilles Peskine224b0d62019-09-23 18:13:17 +02006453exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 if (status != PSA_SUCCESS) {
6455 psa_key_derivation_abort(operation);
6456 }
6457 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006458}
6459
Janos Follath51f4a0f2019-06-14 11:35:55 +01006460psa_status_t psa_key_derivation_input_bytes(
6461 psa_key_derivation_operation_t *operation,
6462 psa_key_derivation_step_t step,
6463 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006465{
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 return psa_key_derivation_input_internal(operation, step,
6467 PSA_KEY_TYPE_NONE,
6468 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006469}
6470
Janos Follath51f4a0f2019-06-14 11:35:55 +01006471psa_status_t psa_key_derivation_input_key(
6472 psa_key_derivation_operation_t *operation,
6473 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006475{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006476 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006477 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006478 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006479
Ronald Cron5c522922020-11-14 16:35:34 +01006480 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6482 if (status != PSA_SUCCESS) {
6483 psa_key_derivation_abort(operation);
6484 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006485 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006486
6487 /* Passing a key object as a SECRET input unlocks the permission
6488 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006490 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006492
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 status = psa_key_derivation_input_internal(operation,
6494 step, slot->attr.type,
6495 slot->key.data,
6496 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006497
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006499
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006501}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006502
6503
6504
6505/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006506/* Key agreement */
6507/****************************************************************/
6508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6510 const uint8_t *key_buffer,
6511 size_t key_buffer_size,
6512 psa_algorithm_t alg,
6513 const uint8_t *peer_key,
6514 size_t peer_key_length,
6515 uint8_t *shared_secret,
6516 size_t shared_secret_size,
6517 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006518{
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006520#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006521 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6523 key_buffer_size, alg,
6524 peer_key, peer_key_length,
6525 shared_secret,
6526 shared_secret_size,
6527 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006528#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006529 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006530 (void) attributes;
6531 (void) key_buffer;
6532 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006533 (void) peer_key;
6534 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006535 (void) shared_secret;
6536 (void) shared_secret_size;
6537 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006539 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006540}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006541
Aditya Deshpande17845b82022-10-13 17:21:01 +01006542/** Internal function for raw key agreement
6543 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006544 * to the driver's implementation if a driver is present.
6545 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006546 * (psa_key_agreement_raw_builtin).
6547 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006548static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6549 psa_key_slot_t *private_key,
6550 const uint8_t *peer_key,
6551 size_t peer_key_length,
6552 uint8_t *shared_secret,
6553 size_t shared_secret_size,
6554 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006555{
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6557 return PSA_ERROR_NOT_SUPPORTED;
6558 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006559
6560 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006562 };
6563
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 return psa_driver_wrapper_key_agreement(&attributes,
6565 private_key->key.data,
6566 private_key->key.bytes, alg,
6567 peer_key, peer_key_length,
6568 shared_secret,
6569 shared_secret_size,
6570 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006571}
6572
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006573/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006574 * to potentially free embedded data structures and wipe confidential data.
6575 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006576static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6577 psa_key_derivation_step_t step,
6578 psa_key_slot_t *private_key,
6579 const uint8_t *peer_key,
6580 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006581{
6582 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006583 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006584 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006586
6587 /* Step 1: run the secret agreement algorithm to generate the shared
6588 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 status = psa_key_agreement_raw_internal(ka_alg,
6590 private_key,
6591 peer_key, peer_key_length,
6592 shared_secret,
6593 sizeof(shared_secret),
6594 &shared_secret_length);
6595 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006596 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006598
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006599 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006600 * the shared secret. A shared secret is permitted wherever a key
6601 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 status = psa_key_derivation_input_internal(operation, step,
6603 PSA_KEY_TYPE_DERIVE,
6604 shared_secret,
6605 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006606exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6608 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006609}
6610
Gilles Peskine449bd832023-01-11 14:50:10 +01006611psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6612 psa_key_derivation_step_t step,
6613 mbedtls_svc_key_id_t private_key,
6614 const uint8_t *peer_key,
6615 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006616{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006618 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006619 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006620
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6622 return PSA_ERROR_INVALID_ARGUMENT;
6623 }
Ronald Cron5c522922020-11-14 16:35:34 +01006624 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6626 if (status != PSA_SUCCESS) {
6627 return status;
6628 }
6629 status = psa_key_agreement_internal(operation, step,
6630 slot,
6631 peer_key, peer_key_length);
6632 if (status != PSA_SUCCESS) {
6633 psa_key_derivation_abort(operation);
6634 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006635 /* If a private key has been added as SECRET, we allow the derived
6636 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006637 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006638 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006640 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006643
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006645}
6646
Gilles Peskine449bd832023-01-11 14:50:10 +01006647psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6648 mbedtls_svc_key_id_t private_key,
6649 const uint8_t *peer_key,
6650 size_t peer_key_length,
6651 uint8_t *output,
6652 size_t output_size,
6653 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006654{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006655 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006656 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006657 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006658
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006660 status = PSA_ERROR_INVALID_ARGUMENT;
6661 goto exit;
6662 }
Ronald Cron5c522922020-11-14 16:35:34 +01006663 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6665 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006668
Gilles Peskine3e561302022-04-14 00:17:15 +02006669 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6670 * for the output size. The PSA specification only guarantees that this
6671 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6672 * but it might be nice to allow smaller buffers if the output fits.
6673 * At the time of writing this comment, with only ECDH implemented,
6674 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6675 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6676 * be exact for it as well. */
6677 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6679 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006680 status = PSA_ERROR_BUFFER_TOO_SMALL;
6681 goto exit;
6682 }
6683
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 status = psa_key_agreement_raw_internal(alg, slot,
6685 peer_key, peer_key_length,
6686 output, output_size,
6687 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006688
6689exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006691 /* If an error happens and is not handled properly, the output
6692 * may be used as a key to protect sensitive data. Arrange for such
6693 * a key to be random, which is likely to result in decryption or
6694 * verification errors. This is better than filling the buffer with
6695 * some constant data such as zeros, which would result in the data
6696 * being protected with a reproducible, easily knowable key.
6697 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006699 *output_length = output_size;
6700 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006701
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006703
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006705}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006706
6707
Gilles Peskine30524eb2020-11-13 17:02:26 +01006708
Gilles Peskine86a440b2018-11-12 18:39:40 +01006709/****************************************************************/
6710/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006711/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006712
Gilles Peskine30524eb2020-11-13 17:02:26 +01006713/** Initialize the PSA random generator.
6714 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006715static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006716{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006717#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006719#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6720
Gilles Peskine30524eb2020-11-13 17:02:26 +01006721 /* Set default configuration if
6722 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006724 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 }
6726 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006727 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006729
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006731#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6732 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6733 /* The PSA entropy injection feature depends on using NV seed as an entropy
6734 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 mbedtls_entropy_add_source(&rng->entropy,
6736 mbedtls_nv_seed_poll, NULL,
6737 MBEDTLS_ENTROPY_BLOCK_SIZE,
6738 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006739#endif
6740
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006742#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006743}
6744
6745/** Deinitialize the PSA random generator.
6746 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006747static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006748{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006749#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006751#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6753 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006754#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006755}
6756
6757/** Seed the PSA random generator.
6758 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006759static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006760{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006761#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6762 /* Do nothing: the external RNG seeds itself. */
6763 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006765#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006766 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6768 drbg_seed, sizeof(drbg_seed) - 1);
6769 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006770#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006771}
6772
Gilles Peskine449bd832023-01-11 14:50:10 +01006773psa_status_t psa_generate_random(uint8_t *output,
6774 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006775{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006776 GUARD_MODULE_INITIALIZED;
6777
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006778#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6779
6780 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6782 output, output_size,
6783 &output_length);
6784 if (status != PSA_SUCCESS) {
6785 return status;
6786 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006787 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006788 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006789 if (output_length != output_size) {
6790 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6791 }
6792 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006793
6794#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6795
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006797 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6799 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6800 output_size);
6801 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6802 output, request_size);
6803 if (ret != 0) {
6804 return mbedtls_to_psa_error(ret);
6805 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006806 output_size -= request_size;
6807 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006808 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006809 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006810#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006811}
6812
Gilles Peskine8814fc42020-12-14 15:33:44 +01006813/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006814 *
6815 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6816 * `psa_generate_random(...)`. The state parameter is ignored since the
6817 * PSA API doesn't support passing an explicit state.
6818 *
6819 * In the non-external case, psa_generate_random() calls an
6820 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6821 * and semantics as mbedtls_psa_get_random(). As an optimization,
6822 * instead of doing this back-and-forth between the PSA API and the
6823 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6824 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006825 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006826#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6827int mbedtls_psa_get_random(void *p_rng,
6828 unsigned char *output,
6829 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006830{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006831 /* This function takes a pointer to the RNG state because that's what
6832 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6833 * its own state internally and doesn't let the caller access that state.
6834 * So we just ignore the state parameter, and in practice we'll pass
6835 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006836 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 psa_status_t status = psa_generate_random(output, output_size);
6838 if (status == PSA_SUCCESS) {
6839 return 0;
6840 } else {
6841 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6842 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006843}
6844#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6845
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006846#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006847#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006848
Gilles Peskine449bd832023-01-11 14:50:10 +01006849psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6850 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006851{
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 if (global_data.initialized) {
6853 return PSA_ERROR_NOT_PERMITTED;
6854 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006855
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6857 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6858 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6859 return PSA_ERROR_INVALID_ARGUMENT;
6860 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006861
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006863}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006864#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006865
Ronald Cron3772afe2021-02-08 16:10:05 +01006866/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006867 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006868 * \param type The key type
6869 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006870 *
6871 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006872 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006873 * \retval #PSA_ERROR_INVALID_ARGUMENT
6874 * The size in bits of the key is not valid.
6875 * \retval #PSA_ERROR_NOT_SUPPORTED
6876 * The type and/or the size in bits of the key or the combination of
6877 * the two is not supported.
6878 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006879static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006881{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006882 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006883
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 if (key_type_is_raw_bytes(type)) {
6885 status = psa_validate_unstructured_key_bit_size(type, bits);
6886 if (status != PSA_SUCCESS) {
6887 return status;
6888 }
6889 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006890#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6892 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6893 return PSA_ERROR_NOT_SUPPORTED;
6894 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006895
Ronald Cron01b2aba2020-10-05 09:42:02 +02006896 /* Accept only byte-aligned keys, for the same reasons as
6897 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 if (bits % 8 != 0) {
6899 return PSA_ERROR_NOT_SUPPORTED;
6900 }
6901 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006902#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006903
Ronald Cron5c4d3862020-12-07 11:07:24 +01006904#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006906 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 return PSA_SUCCESS;
6908 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006909#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006910 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006912 }
6913
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006915}
6916
Ronald Cron55ed0592020-10-05 10:30:40 +02006917psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006918 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006919 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006920{
6921 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006922 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006923
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 if ((attributes->domain_parameters == NULL) &&
6925 (attributes->domain_parameters_size != 0)) {
6926 return PSA_ERROR_INVALID_ARGUMENT;
6927 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006928
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 if (key_type_is_raw_bytes(type)) {
6930 status = psa_generate_random(key_buffer, key_buffer_size);
6931 if (status != PSA_SUCCESS) {
6932 return status;
6933 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006934
David Brown12ca5032021-02-11 11:02:00 -07006935#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 if (type == PSA_KEY_TYPE_DES) {
6937 psa_des_set_key_parity(key_buffer, key_buffer_size);
6938 }
David Brown8107e312021-02-12 08:08:46 -07006939#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006941
Jaeden Amero424fa932021-05-14 08:34:32 +01006942#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6943 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6945 return mbedtls_psa_rsa_generate_key(attributes,
6946 key_buffer,
6947 key_buffer_size,
6948 key_buffer_length);
6949 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006950#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6951 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006952
John Durkop9814fa22020-11-04 12:28:15 -08006953#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6955 return mbedtls_psa_ecp_generate_key(attributes,
6956 key_buffer,
6957 key_buffer_size,
6958 key_buffer_length);
6959 } else
John Durkop9814fa22020-11-04 12:28:15 -08006960#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006961 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 (void) key_buffer_length;
6963 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006964 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006967}
Darryl Green0c6575a2018-11-07 16:05:30 +00006968
Gilles Peskine449bd832023-01-11 14:50:10 +01006969psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6970 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006971{
6972 psa_status_t status;
6973 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006974 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006975 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006976
Ronald Cron81709fc2020-11-14 12:10:32 +01006977 *key = MBEDTLS_SVC_KEY_ID_INIT;
6978
Gilles Peskine0f84d622019-09-12 19:03:13 +02006979 /* Reject any attempt to create a zero-length key so that we don't
6980 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006981 if (psa_get_key_bits(attributes) == 0) {
6982 return PSA_ERROR_INVALID_ARGUMENT;
6983 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006984
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006985 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
6987 return PSA_ERROR_INVALID_ARGUMENT;
6988 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006989
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
6991 &slot, &driver);
6992 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02006993 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 }
Gilles Peskine11792082019-08-06 18:36:36 +02006995
Ronald Cron977c2472020-10-13 08:32:21 +02006996 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05306997 * storage ( thus not in the case of generating a key in a secure element
6998 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
6999 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 if (slot->key.data == NULL) {
7001 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7002 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007003 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 attributes->core.type, attributes->core.bits);
7005 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007006 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007008
7009 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 attributes->core.type,
7011 attributes->core.bits);
7012 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007013 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 attributes, &key_buffer_size);
7015 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007016 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 }
Ronald Cron977c2472020-10-13 08:32:21 +02007018 }
Ronald Cron977c2472020-10-13 08:32:21 +02007019
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7021 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007022 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 }
Ronald Cron977c2472020-10-13 08:32:21 +02007024 }
7025
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 status = psa_driver_wrapper_generate_key(attributes,
7027 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007028
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 if (status != PSA_SUCCESS) {
7030 psa_remove_key_data_from_memory(slot);
7031 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007032
Gilles Peskine11792082019-08-06 18:36:36 +02007033exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 if (status == PSA_SUCCESS) {
7035 status = psa_finish_key_creation(slot, driver, key);
7036 }
7037 if (status != PSA_SUCCESS) {
7038 psa_fail_key_creation(slot, driver);
7039 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007040
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007042}
7043
Gilles Peskinee59236f2018-01-27 23:32:46 +01007044/****************************************************************/
7045/* Module setup */
7046/****************************************************************/
7047
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007048#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007049psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 void (* entropy_init)(mbedtls_entropy_context *ctx),
7051 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007052{
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7054 return PSA_ERROR_BAD_STATE;
7055 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007056 global_data.rng.entropy_init = entropy_init;
7057 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007059}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007060#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007061
Gilles Peskine449bd832023-01-11 14:50:10 +01007062void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007063{
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 psa_wipe_all_key_slots();
7065 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7066 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007067 }
7068 /* Wipe all remaining data, including configuration.
7069 * In particular, this sets all state indicator to the value
7070 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007072
7073 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007075}
7076
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007077#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7078/** Recover a transaction that was interrupted by a power failure.
7079 *
7080 * This function is called during initialization, before psa_crypto_init()
7081 * returns. If this function returns a failure status, the initialization
7082 * fails.
7083 */
7084static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007086{
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007088 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7089 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 /* TODO - fall through to the failure case until this
7091 * is implemented.
7092 * https://github.com/ARMmbed/mbed-crypto/issues/218
7093 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007094 default:
7095 /* We found an unsupported transaction in the storage.
7096 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007098 }
7099}
7100#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7101
Gilles Peskine449bd832023-01-11 14:50:10 +01007102psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007103{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007104 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007105
Gilles Peskinec6b69072018-11-20 21:42:52 +01007106 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 if (global_data.initialized != 0) {
7108 return PSA_SUCCESS;
7109 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007110
Gilles Peskine30524eb2020-11-13 17:02:26 +01007111 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007112 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007113 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 status = mbedtls_psa_random_seed(&global_data.rng);
7115 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007118 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007119
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 status = psa_initialize_key_slots();
7121 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007122 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007124
Ronald Cron9ba76912021-04-10 16:57:30 +02007125 /* Init drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 status = psa_driver_wrapper_init();
7127 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02007128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 }
Gilles Peskined9348f22019-10-01 15:22:29 +02007130
Gilles Peskine4b734222019-07-24 15:56:31 +02007131#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 status = psa_crypto_load_transaction();
7133 if (status == PSA_SUCCESS) {
7134 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7135 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007136 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 }
7138 status = psa_crypto_stop_transaction();
7139 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007140 /* There's no transaction to complete. It's all good. */
7141 status = PSA_SUCCESS;
7142 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007143#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007144
Gilles Peskinec6b69072018-11-20 21:42:52 +01007145 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007146 global_data.initialized = 1;
7147
7148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 if (status != PSA_SUCCESS) {
7150 mbedtls_psa_crypto_free();
7151 }
7152 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007153}
7154
7155#endif /* MBEDTLS_PSA_CRYPTO_C */