blob: 9cc2aa924f4110df5825c5f6e926b5acfccc8893 [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,
346 size_t output_buffer_size, size_t output_buffer_length) {
347 size_t offset = 0;
348
349 if (output_buffer_size == 0) {
350 /* If output_buffer_size is 0 then we have nothing to do. We must not
351 call memset because output_buffer may be NULL in this case */
352 return;
353 }
354
355 if (status == PSA_SUCCESS) {
356 offset = output_buffer_length;
357 }
358
359 memset(output_buffer + offset, '!', output_buffer_size - offset);
360}
361
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200362
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200363
364
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100365/****************************************************************/
366/* Key management */
367/****************************************************************/
368
John Durkop9814fa22020-11-04 12:28:15 -0800369#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800370 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100371 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
372 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
373 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100374mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
375 size_t bits,
376 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200377{
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100379 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100381#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100382 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100384#endif
385#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100386 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100388#endif
389#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100392#endif
393#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100396#endif
397#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100398 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100400 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (bits_is_sloppy) {
402 return MBEDTLS_ECP_DP_SECP521R1;
403 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100404 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100406 }
407 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100408
Paul Elliott8ff510a2020-06-02 17:19:28 +0100409 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100411#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100412 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100414#endif
415#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100416 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100418#endif
419#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100422#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100423 }
424 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100425
Paul Elliott8ff510a2020-06-02 17:19:28 +0100426 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100428#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (bits_is_sloppy) {
433 return MBEDTLS_ECP_DP_CURVE25519;
434 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100436#endif
437#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100438 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100440#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100441 }
442 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100443
Paul Elliott8ff510a2020-06-02 17:19:28 +0100444 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100446#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100449#endif
450#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#endif
454#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 }
459 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200460 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100461
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100462 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200464}
John Durkop9814fa22020-11-04 12:28:15 -0800465#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100466 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
467 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
468 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
469 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
472 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200473{
474 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200476 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200478 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200479 case PSA_KEY_TYPE_PASSWORD:
480 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200481 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100482#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200483 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (bits != 128 && bits != 192 && bits != 256) {
485 return PSA_ERROR_INVALID_ARGUMENT;
486 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 break;
488#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200489#if defined(PSA_WANT_KEY_TYPE_ARIA)
490 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (bits != 128 && bits != 192 && bits != 256) {
492 return PSA_ERROR_INVALID_ARGUMENT;
493 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200494 break;
495#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100496#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (bits != 128 && bits != 192 && bits != 256) {
499 return PSA_ERROR_INVALID_ARGUMENT;
500 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501 break;
502#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100503#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (bits != 64 && bits != 128 && bits != 192) {
506 return PSA_ERROR_INVALID_ARGUMENT;
507 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508 break;
509#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100510#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200511 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (bits != 256) {
513 return PSA_ERROR_INVALID_ARGUMENT;
514 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200515 break;
516#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200519 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (bits % 8 != 0) {
521 return PSA_ERROR_INVALID_ARGUMENT;
522 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200525}
526
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100527/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100528 *
Steven Cooremancd640932021-03-08 12:00:27 +0100529 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
530 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100531 *
532 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100533 * \param[in] key_type The key type of the key to be used with the
534 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100535 *
536 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100537 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100538 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100539 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100540 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100541MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100542 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100544{
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (PSA_ALG_IS_HMAC(algorithm)) {
546 if (key_type == PSA_KEY_TYPE_HMAC) {
547 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
552 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
553 * key. */
554 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
555 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
556 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
557 * the block length (larger than 1) for block ciphers. */
558 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
559 return PSA_SUCCESS;
560 }
561 }
562 }
563
564 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100565}
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
568 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200569{
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (slot->key.data != NULL) {
571 return PSA_ERROR_ALREADY_EXISTS;
572 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 slot->key.data = mbedtls_calloc(1, buffer_length);
575 if (slot->key.data == NULL) {
576 return PSA_ERROR_INSUFFICIENT_MEMORY;
577 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200578
Ronald Cronea0f8a62020-11-25 17:52:23 +0100579 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200581}
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
584 const uint8_t *data,
585 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200586{
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 psa_status_t status = psa_allocate_buffer_to_slot(slot,
588 data_length);
589 if (status != PSA_SUCCESS) {
590 return status;
591 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 memcpy(slot->key.data, data, data_length);
594 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200595}
596
Ronald Crona0fe59f2020-11-29 11:14:53 +0100597psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100598 const psa_key_attributes_t *attributes,
599 const uint8_t *data, size_t data_length,
600 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100602{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100603 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
604 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200606 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (data_length == 0) {
608 return PSA_ERROR_NOT_SUPPORTED;
609 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (key_type_is_raw_bytes(type)) {
612 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
615 *bits);
616 if (status != PSA_SUCCESS) {
617 return status;
618 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200619
Ronald Crondd04d422020-11-28 15:14:42 +0100620 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100622 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 return PSA_SUCCESS;
626 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800627#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
629 if (PSA_KEY_TYPE_IS_ECC(type)) {
630 return mbedtls_psa_ecp_import_key(attributes,
631 data, data_length,
632 key_buffer, key_buffer_size,
633 key_buffer_length,
634 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100635 }
John Durkop9814fa22020-11-04 12:28:15 -0800636#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
637 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700638#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
640 if (PSA_KEY_TYPE_IS_RSA(type)) {
641 return mbedtls_psa_rsa_import_key(attributes,
642 data, data_length,
643 key_buffer, key_buffer_size,
644 key_buffer_length,
645 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200646 }
John Durkop9814fa22020-11-04 12:28:15 -0800647#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
648 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100649 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100652}
653
Gilles Peskinef603c712019-01-19 13:40:11 +0100654/** Calculate the intersection of two algorithm usage policies.
655 *
656 * Return 0 (which allows no operation) on incompatibility.
657 */
658static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100659 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100660 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100662{
Gilles Peskine549ea862019-05-22 11:45:59 +0200663 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (alg1 == alg2) {
665 return alg1;
666 }
Steven Cooreman03488022021-02-18 12:07:20 +0100667 /* If the policies are from the same hash-and-sign family, check
668 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
670 PSA_ALG_IS_SIGN_HASH(alg2) &&
671 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
672 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
673 return alg2;
674 }
675 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
676 return alg1;
677 }
Steven Cooreman03488022021-02-18 12:07:20 +0100678 }
679 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100680 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100681 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
683 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
684 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
685 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
686 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100687 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100688
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100689 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
691 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
692 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
693 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100694 }
695 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
697 (alg1_len <= alg2_len)) {
698 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100699 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
701 (alg2_len <= alg1_len)) {
702 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100703 }
704 }
705 /* If the policies are from the same MAC family, check whether one
706 * of them is a minimum-MAC-length policy. Calculate the most
707 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
709 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
710 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100711 /* Validate the combination of key type and algorithm. Since the base
712 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
714 return 0;
715 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100716
Steven Cooreman31a876d2021-03-03 20:47:40 +0100717 /* Get the (exact or at-least) output lengths for both sides of the
718 * requested intersection. None of the currently supported algorithms
719 * have an output length dependent on the actual key size, so setting it
720 * to a bogus value of 0 is currently OK.
721 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100722 * Note that for at-least-this-length wildcard algorithms, the output
723 * length is set to the shortest allowed length, which allows us to
724 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
726 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100727 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100728
Steven Cooremana1d83222021-02-25 10:20:29 +0100729 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
731 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
732 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100733 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100734
735 /* If only one is an at-least-this-length policy, the intersection would
736 * be the other (fixed-length) policy as long as said fixed length is
737 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
739 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100740 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
742 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100743 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100744
Steven Cooremancd640932021-03-08 12:00:27 +0100745 /* If none of them are wildcards, check whether they define the same tag
746 * length. This is still possible here when one is default-length and
747 * the other specific-length. Ensure to always return the
748 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (alg1_len == alg2_len) {
750 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
751 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100752 }
753 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100755}
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757static int psa_key_algorithm_permits(psa_key_type_t key_type,
758 psa_algorithm_t policy_alg,
759 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200760{
Gilles Peskine549ea862019-05-22 11:45:59 +0200761 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (requested_alg == policy_alg) {
763 return 1;
764 }
Steven Cooreman03488022021-02-18 12:07:20 +0100765 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
766 * and requested_alg is the same hash-and-sign family with any hash,
767 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
769 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
770 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
771 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100772 }
773 /* If policy_alg is a wildcard AEAD algorithm of the same base as
774 * the requested algorithm, check the requested tag length to be
775 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (PSA_ALG_IS_AEAD(policy_alg) &&
777 PSA_ALG_IS_AEAD(requested_alg) &&
778 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
779 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
780 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
781 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
782 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100783 }
Steven Cooremancd640932021-03-08 12:00:27 +0100784 /* If policy_alg is a MAC algorithm of the same base as the requested
785 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (PSA_ALG_IS_MAC(policy_alg) &&
787 PSA_ALG_IS_MAC(requested_alg) &&
788 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
789 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100790 /* Validate the combination of key type and algorithm. Since the policy
791 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
793 return 0;
794 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100795
Steven Cooreman31a876d2021-03-03 20:47:40 +0100796 /* Get both the requested output length for the algorithm which is to be
797 * verified, and the default output length for the base algorithm.
798 * Note that none of the currently supported algorithms have an output
799 * length dependent on actual key size, so setting it to a bogus value
800 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100801 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100803 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 key_type, 0,
805 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100806
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100807 /* If the policy is default-length, only allow an algorithm with
808 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
810 return requested_output_length == default_output_length;
811 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100812
813 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100814 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
816 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
817 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100818 }
819
Steven Cooremancd640932021-03-08 12:00:27 +0100820 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
821 * check for the requested MAC length to be equal to or longer than the
822 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
824 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
825 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100826 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200827 }
Steven Cooremance48e852020-10-05 16:02:45 +0200828 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200829 * a key derivation with that key agreement should also be allowed. This
830 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
832 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
833 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
834 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200835 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100836 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200838}
839
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100840/** Test whether a policy permits an algorithm.
841 *
842 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100843 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100844 * \note This function requires providing the key type for which the policy is
845 * being validated, since some algorithm policy definitions (e.g. MAC)
846 * have different properties depending on what kind of cipher it is
847 * combined with.
848 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100849 * \retval PSA_SUCCESS When \p alg is a specific algorithm
850 * allowed by the \p policy.
851 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
852 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
853 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100854 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
856 psa_key_type_t key_type,
857 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100858{
Steven Cooremand788fab2021-02-25 11:29:17 +0100859 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (alg == 0) {
861 return PSA_ERROR_INVALID_ARGUMENT;
862 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100863
Steven Cooreman7e39f052021-02-23 14:18:32 +0100864 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (PSA_ALG_IS_WILDCARD(alg)) {
866 return PSA_ERROR_INVALID_ARGUMENT;
867 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
870 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
871 return PSA_SUCCESS;
872 } else {
873 return PSA_ERROR_NOT_PERMITTED;
874 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100875}
876
Gilles Peskinef603c712019-01-19 13:40:11 +0100877/** Restrict a key policy based on a constraint.
878 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100879 * \note This function requires providing the key type for which the policy is
880 * being restricted, since some algorithm policy definitions (e.g. MAC)
881 * have different properties depending on what kind of cipher it is
882 * combined with.
883 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100884 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100885 * \param[in,out] policy The policy to restrict.
886 * \param[in] constraint The policy constraint to apply.
887 *
888 * \retval #PSA_SUCCESS
889 * \c *policy contains the intersection of the original value of
890 * \c *policy and \c *constraint.
891 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100892 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100893 * \c *policy is unchanged.
894 */
895static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100896 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100897 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100899{
900 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 psa_key_policy_algorithm_intersection(key_type, policy->alg,
902 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200903 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
905 constraint->alg2);
906 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
907 return PSA_ERROR_INVALID_ARGUMENT;
908 }
909 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
910 return PSA_ERROR_INVALID_ARGUMENT;
911 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100912 policy->usage &= constraint->usage;
913 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200914 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100916}
917
Przemek Stekiel348410f2022-11-15 22:22:07 +0100918psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100919 mbedtls_svc_key_id_t key,
920 psa_key_slot_t **p_slot,
921 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100923{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200924 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
925 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 status = psa_get_and_lock_key_slot(key, p_slot);
928 if (status != PSA_SUCCESS) {
929 return status;
930 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200931 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100932
933 /* Enforce that usage policy for the key slot contains all the flags
934 * required by the usage parameter. There is one exception: public
935 * keys can always be exported, so we treat public key objects as
936 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100938 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100942 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200943 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100944 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100945
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800946 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (alg != 0) {
948 status = psa_key_policy_permits(&slot->attr.policy,
949 slot->attr.type,
950 alg);
951 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100952 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100954 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200957
958error:
959 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100963}
Darryl Green940d72c2018-07-13 13:18:51 +0100964
Ronald Cron5c522922020-11-14 16:35:34 +0100965/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200966 *
967 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200968 * available, as opposed to a key in a secure element and/or to be used
969 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200970 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200971 * This is a temporary function that may be used instead of
972 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
973 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200974 *
Ronald Cron5c522922020-11-14 16:35:34 +0100975 * On success, the returned key slot is locked. It is the responsibility of the
976 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200977 */
Ronald Cron5c522922020-11-14 16:35:34 +0100978static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
979 mbedtls_svc_key_id_t key,
980 psa_key_slot_t **p_slot,
981 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200983{
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
985 usage, alg);
986 if (status != PSA_SUCCESS) {
987 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200988 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
991 psa_unlock_key_slot(*p_slot);
992 *p_slot = NULL;
993 return PSA_ERROR_NOT_SUPPORTED;
994 }
995
996 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200997}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001000{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001001 /* Data pointer will always be either a valid pointer or NULL in an
1002 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if (slot->key.data != NULL) {
1004 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1005 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001008 slot->key.data = NULL;
1009 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001012}
1013
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001014/** Completely wipe a slot in memory, including its policy.
1015 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001017{
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 /*
1021 * As the return error code may not be handled in case of multiple errors,
1022 * do our best to report an unexpected lock counter. Assert with
1023 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1024 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1025 * function is called as part of the execution of a test suite, the
1026 * execution of the test suite is stopped in error if the assertion fails.
1027 */
1028 if (slot->lock_count != 1) {
1029 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001030 status = PSA_ERROR_CORRUPTION_DETECTED;
1031 }
1032
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001033 /* Multipart operations may still be using the key. This is safe
1034 * because all multipart operation objects are independent from
1035 * the key slot: if they need to access the key after the setup
1036 * phase, they have a copy of the key. Note that this means that
1037 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001038 /* At this point, key material and other type-specific content has
1039 * been wiped. Clear remaining metadata. We can call memset and not
1040 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 memset(slot, 0, sizeof(*slot));
1042 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001043}
1044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001046{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001047 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001048 psa_status_t status; /* status of the last operation */
1049 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001050#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1051 psa_se_drv_table_entry_t *driver;
1052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (mbedtls_svc_key_id_is_null(key)) {
1055 return PSA_SUCCESS;
1056 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001057
Ronald Cronf2911112020-10-29 17:51:10 +01001058 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001059 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001060 * key, this will load the key description from persistent memory if not
1061 * done yet. We cannot avoid this loading as without it we don't know if
1062 * the key is operated by an SE or not and this information is needed by
1063 * the current implementation.
1064 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 status = psa_get_and_lock_key_slot(key, &slot);
1066 if (status != PSA_SUCCESS) {
1067 return status;
1068 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001069
Ronald Cronf2911112020-10-29 17:51:10 +01001070 /*
1071 * If the key slot containing the key description is under access by the
1072 * library (apart from the present access), the key cannot be destroyed
1073 * yet. For the time being, just return in error. Eventually (to be
1074 * implemented), the key should be destroyed when all accesses have
1075 * stopped.
1076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if (slot->lock_count > 1) {
1078 psa_unlock_key_slot(slot);
1079 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001080 }
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001083 /* Refuse the destruction of a read-only key (which may or may not work
1084 * if we attempt it, depending on whether the key is merely read-only
1085 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001086 * Just do the best we can, which is to wipe the copy in memory
1087 * (done in this function's cleanup code). */
1088 overall_status = PSA_ERROR_NOT_PERMITTED;
1089 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001090 }
1091
Gilles Peskine354f7672019-07-12 23:46:38 +02001092#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1094 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001095 /* For a key in a secure element, we need to do three things:
1096 * remove the key file in internal storage, destroy the
1097 * key inside the secure element, and update the driver's
1098 * persistent data. Start a transaction that will encompass these
1099 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001101 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001103 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 status = psa_crypto_save_transaction();
1105 if (status != PSA_SUCCESS) {
1106 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001107 /* We should still try to destroy the key in the secure
1108 * element and the key metadata in storage. This is especially
1109 * important if the error is that the storage is full.
1110 * But how to do it exactly without risking an inconsistent
1111 * state after a reset?
1112 * https://github.com/ARMmbed/mbed-crypto/issues/215
1113 */
1114 overall_status = status;
1115 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001116 }
1117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 status = psa_destroy_se_key(driver,
1119 psa_key_slot_get_slot_number(slot));
1120 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001121 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001123 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001124#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1125
Darryl Greend49a4992018-06-18 17:27:26 +01001126#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1128 status = psa_destroy_persistent_key(slot->attr.id);
1129 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001130 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001132
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001133 /* TODO: other slots may have a copy of the same key. We should
1134 * invalidate them.
1135 * https://github.com/ARMmbed/mbed-crypto/issues/214
1136 */
Darryl Greend49a4992018-06-18 17:27:26 +01001137 }
1138#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001139
Gilles Peskinefc762652019-07-22 19:30:34 +02001140#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (driver != NULL) {
1142 status = psa_save_se_persistent_data(driver);
1143 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001144 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 }
1146 status = psa_crypto_stop_transaction();
1147 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001148 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001150 }
1151#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1152
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001153exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001155 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001157 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
1159 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001160}
1161
John Durkop07cc04a2020-11-16 22:08:34 -08001162#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001163 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001164static psa_status_t psa_get_rsa_public_exponent(
1165 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001167{
1168 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001170 uint8_t *buffer = NULL;
1171 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1175 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001176 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 }
1178 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001179 /* It's the default value, which is reported as an empty string,
1180 * so there's nothing to do. */
1181 goto exit;
1182 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 buflen = mbedtls_mpi_size(&mpi);
1185 buffer = mbedtls_calloc(1, buflen);
1186 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001187 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1188 goto exit;
1189 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1191 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001194 attributes->domain_parameters = buffer;
1195 attributes->domain_parameters_size = buflen;
1196
1197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 mbedtls_mpi_free(&mpi);
1199 if (ret != 0) {
1200 mbedtls_free(buffer);
1201 }
1202 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001203}
John Durkop07cc04a2020-11-16 22:08:34 -08001204#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001205 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001206
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001207/** Retrieve all the publicly-accessible attributes of a key.
1208 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1210 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001211{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001212 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001213 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001214 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1219 if (status != PSA_SUCCESS) {
1220 return status;
1221 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001222
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001223 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1225 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001226
1227#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1229 psa_set_key_slot_number(attributes,
1230 psa_key_slot_get_slot_number(slot));
1231 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001232#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001235#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001236 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001237 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001238 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001239 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001240 * is not yet implemented.
1241 * https://github.com/ARMmbed/mbed-crypto/issues/216
1242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001244 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001245
Ronald Cron90857082020-11-25 14:58:33 +01001246 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 slot->attr.type,
1248 slot->key.data,
1249 slot->key.bytes,
1250 &rsa);
1251 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001252 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 status = psa_get_rsa_public_exponent(rsa,
1256 attributes);
1257 mbedtls_rsa_free(rsa);
1258 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001259 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001260 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001261#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001262 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001263 default:
1264 /* Nothing else to do. */
1265 break;
1266 }
1267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (status != PSA_SUCCESS) {
1269 psa_reset_key_attributes(attributes);
1270 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001275}
1276
Gilles Peskinec8000c02019-08-02 20:15:51 +02001277#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1278psa_status_t psa_get_key_slot_number(
1279 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001281{
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001283 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 return PSA_SUCCESS;
1285 } else {
1286 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001287 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001288}
1289#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1292 size_t key_buffer_size,
1293 uint8_t *data,
1294 size_t data_size,
1295 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001296{
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (key_buffer_size > data_size) {
1298 return PSA_ERROR_BUFFER_TOO_SMALL;
1299 }
1300 memcpy(data, key_buffer, key_buffer_size);
1301 memset(data + key_buffer_size, 0,
1302 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001303 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001305}
1306
Ronald Cron7285cda2020-11-26 14:46:37 +01001307psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001308 const psa_key_attributes_t *attributes,
1309 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001311{
Ronald Crond18b5f82020-11-25 16:46:05 +01001312 psa_key_type_t type = attributes->core.type;
1313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (key_type_is_raw_bytes(type) ||
1315 PSA_KEY_TYPE_IS_RSA(type) ||
1316 PSA_KEY_TYPE_IS_ECC(type)) {
1317 return psa_export_key_buffer_internal(
1318 key_buffer, key_buffer_size,
1319 data, data_size, data_length);
1320 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001321 /* This shouldn't happen in the reference implementation, but
1322 it is valid for a special-purpose implementation to omit
1323 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001325 }
1326}
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1329 uint8_t *data,
1330 size_t data_size,
1331 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001332{
1333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1334 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1335 psa_key_slot_t *slot;
1336
Ronald Crone907e552021-01-18 13:22:38 +01001337 /* Reject a zero-length output buffer now, since this can never be a
1338 * valid key representation. This way we know that data must be a valid
1339 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if (data_size == 0) {
1341 return PSA_ERROR_BUFFER_TOO_SMALL;
1342 }
Ronald Crone907e552021-01-18 13:22:38 +01001343
Ronald Cron9486f9d2020-11-26 13:36:23 +01001344 /* Set the key to empty now, so that even when there are errors, we always
1345 * set data_length to a value between 0 and data_size. On error, setting
1346 * the key to empty is a good choice because an empty key representation is
1347 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001348 *data_length = 0;
1349
Ronald Cron9486f9d2020-11-26 13:36:23 +01001350 /* Export requires the EXPORT flag. There is an exception for public keys,
1351 * which don't require any flag, but
1352 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1355 PSA_KEY_USAGE_EXPORT, 0);
1356 if (status != PSA_SUCCESS) {
1357 return status;
1358 }
mohammad160306e79202018-03-28 13:17:44 +03001359
Ronald Crond18b5f82020-11-25 16:46:05 +01001360 psa_key_attributes_t attributes = {
1361 .core = slot->attr
1362 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 status = psa_driver_wrapper_export_key(&attributes,
1364 slot->key.data, slot->key.bytes,
1365 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001370}
1371
Ronald Cron7285cda2020-11-26 14:46:37 +01001372psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001373 const psa_key_attributes_t *attributes,
1374 const uint8_t *key_buffer,
1375 size_t key_buffer_size,
1376 uint8_t *data,
1377 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001379{
Ronald Crond18b5f82020-11-25 16:46:05 +01001380 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1383 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001384 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 return psa_export_key_buffer_internal(
1386 key_buffer, key_buffer_size,
1387 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001388 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001391#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1393 return mbedtls_psa_rsa_export_public_key(attributes,
1394 key_buffer,
1395 key_buffer_size,
1396 data,
1397 data_size,
1398 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001399#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001400 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001402#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1403 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001405#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1407 return mbedtls_psa_ecp_export_public_key(attributes,
1408 key_buffer,
1409 key_buffer_size,
1410 data,
1411 data_size,
1412 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001413#else
1414 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001416#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1417 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001418 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001420 /* This shouldn't happen in the reference implementation, but
1421 it is valid for a special-purpose implementation to omit
1422 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001424 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001425}
Ronald Crond18b5f82020-11-25 16:46:05 +01001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1428 uint8_t *data,
1429 size_t data_size,
1430 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001431{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001433 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001434 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001435
Ronald Crone907e552021-01-18 13:22:38 +01001436 /* Reject a zero-length output buffer now, since this can never be a
1437 * valid key representation. This way we know that data must be a valid
1438 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (data_size == 0) {
1440 return PSA_ERROR_BUFFER_TOO_SMALL;
1441 }
Ronald Crone907e552021-01-18 13:22:38 +01001442
Darryl Greendd8fb772018-11-07 16:00:44 +00001443 /* Set the key to empty now, so that even when there are errors, we always
1444 * set data_length to a value between 0 and data_size. On error, setting
1445 * the key to empty is a good choice because an empty key representation is
1446 * unlikely to be accepted anywhere. */
1447 *data_length = 0;
1448
1449 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1451 if (status != PSA_SUCCESS) {
1452 return status;
1453 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1456 status = PSA_ERROR_INVALID_ARGUMENT;
1457 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001458 }
1459
Ronald Crond18b5f82020-11-25 16:46:05 +01001460 psa_key_attributes_t attributes = {
1461 .core = slot->attr
1462 };
Ronald Cron67227982020-11-26 15:16:05 +01001463 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001464 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001466
1467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001471}
1472
Gilles Peskine91e8c332019-08-02 19:19:39 +02001473#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001474static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1475 "One or more key attribute flag is listed as both external-only and dual-use");
1476static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1477 "One or more key attribute flag is listed as both internal-only and dual-use");
1478static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1479 "One or more key attribute flag is listed as both internal-only and external-only");
Gilles Peskine91e8c332019-08-02 19:19:39 +02001480#endif
1481
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001482/** Validate that a key policy is internally well-formed.
1483 *
1484 * This function only rejects invalid policies. It does not validate the
1485 * consistency of the policy with respect to other attributes of the key
1486 * such as the key type.
1487 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001489{
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1491 PSA_KEY_USAGE_COPY |
1492 PSA_KEY_USAGE_ENCRYPT |
1493 PSA_KEY_USAGE_DECRYPT |
1494 PSA_KEY_USAGE_SIGN_MESSAGE |
1495 PSA_KEY_USAGE_VERIFY_MESSAGE |
1496 PSA_KEY_USAGE_SIGN_HASH |
1497 PSA_KEY_USAGE_VERIFY_HASH |
1498 PSA_KEY_USAGE_VERIFY_DERIVATION |
1499 PSA_KEY_USAGE_DERIVE)) != 0) {
1500 return PSA_ERROR_INVALID_ARGUMENT;
1501 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001504}
1505
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001506/** Validate the internal consistency of key attributes.
1507 *
1508 * This function only rejects invalid attribute values. If does not
1509 * validate the consistency of the attributes with any key data that may
1510 * be involved in the creation of the key.
1511 *
1512 * Call this function early in the key creation process.
1513 *
1514 * \param[in] attributes Key attributes for the new key.
1515 * \param[out] p_drv On any return, the driver for the key, if any.
1516 * NULL for a transparent key.
1517 *
1518 */
1519static psa_status_t psa_validate_key_attributes(
1520 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001522{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001523 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1525 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 status = psa_validate_key_location(lifetime, p_drv);
1528 if (status != PSA_SUCCESS) {
1529 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001530 }
1531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 status = psa_validate_key_persistence(lifetime);
1533 if (status != PSA_SUCCESS) {
1534 return status;
1535 }
1536
1537 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1538 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1539 return PSA_ERROR_INVALID_ARGUMENT;
1540 }
1541 } else {
1542 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1543 return PSA_ERROR_INVALID_ARGUMENT;
1544 }
1545 }
1546
1547 status = psa_validate_key_policy(&attributes->core.policy);
1548 if (status != PSA_SUCCESS) {
1549 return status;
1550 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001551
1552 /* Refuse to create overly large keys.
1553 * Note that this doesn't trigger on import if the attributes don't
1554 * explicitly specify a size (so psa_get_key_bits returns 0), so
1555 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1557 return PSA_ERROR_NOT_SUPPORTED;
1558 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001559
Gilles Peskine91e8c332019-08-02 19:19:39 +02001560 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1562 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1563 return PSA_ERROR_INVALID_ARGUMENT;
1564 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001567}
1568
Gilles Peskine4747d192019-04-17 15:05:45 +02001569/** Prepare a key slot to receive key material.
1570 *
1571 * This function allocates a key slot and sets its metadata.
1572 *
1573 * If this function fails, call psa_fail_key_creation().
1574 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001575 * This function is intended to be used as follows:
1576 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001577 * it with the specified attributes, and in case of a volatile key assign it
1578 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001579 * -# Populate the slot with the key material.
1580 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1581 * In case of failure at any step, stop the sequence and call
1582 * psa_fail_key_creation().
1583 *
Ronald Cron5c522922020-11-14 16:35:34 +01001584 * On success, the key slot is locked. It is the responsibility of the caller
1585 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001586 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001587 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001588 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001589 * \param[out] p_slot On success, a pointer to the prepared slot.
1590 * \param[out] p_drv On any return, the driver for the key, if any.
1591 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001592 *
1593 * \retval #PSA_SUCCESS
1594 * The key slot is ready to receive key material.
1595 * \return If this function fails, the key slot is an invalid state.
1596 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001597 */
1598static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001599 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001600 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001601 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001603{
1604 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001605 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001606 psa_key_slot_t *slot;
1607
Gilles Peskinedf179142019-07-15 22:02:14 +02001608 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001609 *p_drv = NULL;
1610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 status = psa_validate_key_attributes(attributes, p_drv);
1612 if (status != PSA_SUCCESS) {
1613 return status;
1614 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1617 if (status != PSA_SUCCESS) {
1618 return status;
1619 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001620 slot = *p_slot;
1621
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001622 /* We're storing the declared bit-size of the key. It's up to each
1623 * creation mechanism to verify that this information is correct.
1624 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001625 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001626 * is optional (import, copy). In case of a volatile key, assign it the
1627 * volatile key identifier associated to the slot returned to contain its
1628 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001629
1630 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001632#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1633 slot->attr.id = volatile_key_id;
1634#else
1635 slot->attr.id.key_id = volatile_key_id;
1636#endif
1637 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001638
Gilles Peskine91e8c332019-08-02 19:19:39 +02001639 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001640 * external-only flags, query `attributes`. Thanks to the check
1641 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001642 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001643 * may have set. */
1644 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001645
Gilles Peskinecbaff462019-07-12 23:46:04 +02001646#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001647 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001648 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001649 * create the key file in internal storage, create the
1650 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001651 * persistent data. This is done by starting a transaction that will
1652 * encompass these three actions.
1653 * For registering a volatile key, we just need to find an appropriate
1654 * slot number inside the SE. Since the key is designated volatile, creating
1655 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001656 /* The first thing to do is to find a slot number for the new key.
1657 * We save the slot number in persistent storage as part of the
1658 * transaction data. It will be needed to recover if the power
1659 * fails during the key creation process, to clean up on the secure
1660 * element side after restarting. Obtaining a slot number from the
1661 * secure element driver updates its persistent state, but we do not yet
1662 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001663 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001665 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1667 &slot_number);
1668 if (status != PSA_SUCCESS) {
1669 return status;
1670 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1673 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001674 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001675 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001676 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 status = psa_crypto_save_transaction();
1678 if (status != PSA_SUCCESS) {
1679 (void) psa_crypto_stop_transaction();
1680 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001681 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001682 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001683
1684 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001686 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001689 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001691 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001692#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001695}
Gilles Peskine4747d192019-04-17 15:05:45 +02001696
1697/** Finalize the creation of a key once its key material has been set.
1698 *
1699 * This entails writing the key to persistent storage.
1700 *
1701 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001702 * See the documentation of psa_start_key_creation() for the intended use
1703 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001704 *
Ronald Cron5c522922020-11-14 16:35:34 +01001705 * If the finalization succeeds, the function unlocks the key slot (it was
1706 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1707 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001708 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001709 * \param[in,out] slot Pointer to the slot with key material.
1710 * \param[in] driver The secure element driver for the key,
1711 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001712 * \param[out] key On success, identifier of the key. Note that the
1713 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001714 *
1715 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001716 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001717 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1718 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1719 * \retval #PSA_ERROR_ALREADY_EXISTS
1720 * \retval #PSA_ERROR_DATA_INVALID
1721 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001722 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001723 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001724 * \return If this function fails, the key slot is an invalid state.
1725 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001727static psa_status_t psa_finish_key_creation(
1728 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001729 psa_se_drv_table_entry_t *driver,
1730 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001731{
1732 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001733 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001734 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001735
1736#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001738#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001740 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001741 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001743
Gilles Peskineb46bef22019-07-30 21:32:04 +02001744#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 static_assert(sizeof(slot_number) ==
1746 sizeof(data.slot_number),
1747 "Slot number size does not match psa_se_key_data_storage_t");
Gilles Peskineb46bef22019-07-30 21:32:04 +02001748#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1750 status = psa_save_persistent_key(&slot->attr,
1751 (uint8_t *) &data,
1752 sizeof(data));
1753 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001754#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1755 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001756 /* Key material is saved in export representation in the slot, so
1757 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 status = psa_save_persistent_key(&slot->attr,
1759 slot->key.data,
1760 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001761 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001762 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001763#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1764
Gilles Peskinecbaff462019-07-12 23:46:04 +02001765#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001766 /* Finish the transaction for a key creation. This does not
1767 * happen when registering an existing key. Detect this case
1768 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001769 * creation of a persistent key in a secure element requires a transaction,
1770 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (driver != NULL &&
1772 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1773 status = psa_save_se_persistent_data(driver);
1774 if (status != PSA_SUCCESS) {
1775 psa_destroy_persistent_key(slot->attr.id);
1776 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001777 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001779 }
1780#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001783 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 status = psa_unlock_key_slot(slot);
1785 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001786 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001788 }
Ronald Cron50972942020-11-14 11:28:25 +01001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001791}
1792
1793/** Abort the creation of a key.
1794 *
1795 * You may call this function after calling psa_start_key_creation(),
1796 * or after psa_finish_key_creation() fails. In other circumstances, this
1797 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001798 * See the documentation of psa_start_key_creation() for the intended use
1799 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001800 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001801 * \param[in,out] slot Pointer to the slot with key material.
1802 * \param[in] driver The secure element driver for the key,
1803 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001804 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001805static void psa_fail_key_creation(psa_key_slot_t *slot,
1806 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001807{
Gilles Peskine011e4282019-06-26 18:34:38 +02001808 (void) driver;
1809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001811 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001813
1814#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001815 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001816 * element, and the failure happened later (when saving metadata
1817 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001818 * element.
1819 * https://github.com/ARMmbed/mbed-crypto/issues/217
1820 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001821
Gilles Peskined7729582019-08-05 15:55:54 +02001822 /* Abort the ongoing transaction if any (there may not be one if
1823 * the creation process failed before starting one, or if the
1824 * key creation is a registration of a key in a secure element).
1825 * Earlier functions must already have done what it takes to undo any
1826 * partial creation. All that's left is to update the transaction data
1827 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001829#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1830
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001832}
1833
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001834/** Validate optional attributes during key creation.
1835 *
1836 * Some key attributes are optional during key creation. If they are
1837 * specified in the attributes structure, check that they are consistent
1838 * with the data in the slot.
1839 *
1840 * This function should be called near the end of key creation, after
1841 * the slot in memory is fully populated but before saving persistent data.
1842 */
1843static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001844 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001846{
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (attributes->core.type != 0) {
1848 if (attributes->core.type != slot->attr.type) {
1849 return PSA_ERROR_INVALID_ARGUMENT;
1850 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851 }
1852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001854#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1856 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001857 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001858 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001859 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001860
Ronald Cron90857082020-11-25 14:58:33 +01001861 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 slot->attr.type,
1863 slot->key.data,
1864 slot->key.bytes,
1865 &rsa);
1866 if (status != PSA_SUCCESS) {
1867 return status;
1868 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 mbedtls_mpi_init(&actual);
1871 mbedtls_mpi_init(&required);
1872 ret = mbedtls_rsa_export(rsa,
1873 NULL, NULL, NULL, NULL, &actual);
1874 mbedtls_rsa_free(rsa);
1875 mbedtls_free(rsa);
1876 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001877 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 }
1879 ret = mbedtls_mpi_read_binary(&required,
1880 attributes->domain_parameters,
1881 attributes->domain_parameters_size);
1882 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001883 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 }
1885 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001886 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 }
1888rsa_exit:
1889 mbedtls_mpi_free(&actual);
1890 mbedtls_mpi_free(&required);
1891 if (ret != 0) {
1892 return mbedtls_to_psa_error(ret);
1893 }
1894 } else
John Durkop9814fa22020-11-04 12:28:15 -08001895#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1896 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001897 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899 }
1900 }
1901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 if (attributes->core.bits != 0) {
1903 if (attributes->core.bits != slot->attr.bits) {
1904 return PSA_ERROR_INVALID_ARGUMENT;
1905 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001906 }
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001909}
1910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1912 const uint8_t *data,
1913 size_t data_length,
1914 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001915{
1916 psa_status_t status;
1917 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001918 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001919 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301920 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001921
Ronald Cron81709fc2020-11-14 12:10:32 +01001922 *key = MBEDTLS_SVC_KEY_ID_INIT;
1923
Gilles Peskine0f84d622019-09-12 19:03:13 +02001924 /* Reject zero-length symmetric keys (including raw data key objects).
1925 * This also rejects any key which might be encoded as an empty string,
1926 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 if (data_length == 0) {
1928 return PSA_ERROR_INVALID_ARGUMENT;
1929 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001930
Archana449608b2021-09-08 15:36:05 +05301931 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 if (data_length > SIZE_MAX / 8) {
1933 return PSA_ERROR_NOT_SUPPORTED;
1934 }
Archana6ed4bda2021-08-04 10:47:15 +05301935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1937 &slot, &driver);
1938 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001939 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001941
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001942 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301943 * storage ( thus not in the case of importing a key in a secure element
1944 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1945 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (slot->key.data == NULL) {
1947 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301948 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 attributes, data, data_length, &storage_size);
1950 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 }
Archanad8a83dc2021-06-14 10:04:16 +05301953 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 status = psa_allocate_buffer_to_slot(slot, storage_size);
1955 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001956 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001958 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001959
1960 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 status = psa_driver_wrapper_import_key(attributes,
1962 data, data_length,
1963 slot->key.data,
1964 slot->key.bytes,
1965 &slot->key.bytes, &bits);
1966 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001967 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001971 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001973 status = PSA_ERROR_INVALID_ARGUMENT;
1974 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001975 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001976
Archana6ed4bda2021-08-04 10:47:15 +05301977 /* Enforce a size limit, and in particular ensure that the bit
1978 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301980 status = PSA_ERROR_NOT_SUPPORTED;
1981 goto exit;
1982 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_validate_optional_attributes(slot, attributes);
1984 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001985 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 if (status != PSA_SUCCESS) {
1991 psa_fail_key_creation(slot, driver);
1992 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001995}
1996
Gilles Peskined7729582019-08-05 15:55:54 +02001997#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1998psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002000{
2001 psa_status_t status;
2002 psa_key_slot_t *slot = NULL;
2003 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002005
2006 /* Leaving attributes unspecified is not currently supported.
2007 * It could make sense to query the key type and size from the
2008 * secure element, but not all secure elements support this
2009 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2011 return PSA_ERROR_NOT_SUPPORTED;
2012 }
2013 if (psa_get_key_bits(attributes) == 0) {
2014 return PSA_ERROR_NOT_SUPPORTED;
2015 }
Gilles Peskined7729582019-08-05 15:55:54 +02002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2018 &slot, &driver);
2019 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002020 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 }
Gilles Peskined7729582019-08-05 15:55:54 +02002022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002024
2025exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (status != PSA_SUCCESS) {
2027 psa_fail_key_creation(slot, driver);
2028 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002029
Gilles Peskined7729582019-08-05 15:55:54 +02002030 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 psa_close_key(key);
2032 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002033}
2034#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2037 const psa_key_attributes_t *specified_attributes,
2038 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002039{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002040 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002041 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002042 psa_key_slot_t *source_slot = NULL;
2043 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002044 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002045 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302046 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002047
Ronald Cron81709fc2020-11-14 12:10:32 +01002048 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2049
Archana8a180362021-07-05 02:18:48 +05302050 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2052 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002053 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 status = psa_validate_optional_attributes(source_slot,
2057 specified_attributes);
2058 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002059 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002061
Archana9d17bf42021-09-10 06:22:44 +05302062 /* The target key type and number of bits have been validated by
2063 * psa_validate_optional_attributes() to be either equal to zero or
2064 * equal to the ones of the source key. So it is safe to inherit
2065 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302066 * */
Archana9d17bf42021-09-10 06:22:44 +05302067 actual_attributes.core.bits = source_slot->attr.bits;
2068 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302069
2070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 status = psa_restrict_key_policy(source_slot->attr.type,
2072 &actual_attributes.core.policy,
2073 &source_slot->attr.policy);
2074 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002075 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2079 &target_slot, &driver);
2080 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002081 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 }
2083 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2084 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302085 /*
Archana9d17bf42021-09-10 06:22:44 +05302086 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302087 * the source key would need to be exported as plaintext and re-imported
2088 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302089 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302090 * appropriate API invocations from the application, if needed.
2091 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002092 status = PSA_ERROR_NOT_SUPPORTED;
2093 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002094 }
Archana8a180362021-07-05 02:18:48 +05302095 /*
2096 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302097 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302098 * - For opaque keys this translates to an invocation of the drivers'
2099 * copy_key entry point through the dispatch layer.
2100 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2102 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2103 &storage_size);
2104 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302105 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 }
Archana374fe5b2021-09-08 15:50:28 +05302107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2109 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 }
Archana374fe5b2021-09-08 15:50:28 +05302112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 status = psa_driver_wrapper_copy_key(&actual_attributes,
2114 source_slot->key.data,
2115 source_slot->key.bytes,
2116 target_slot->key.data,
2117 target_slot->key.bytes,
2118 &target_slot->key.bytes);
2119 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 }
2122 } else {
2123 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302124 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 source_slot->key.bytes);
2126 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 }
Archana8a180362021-07-05 02:18:48 +05302129 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (status != PSA_SUCCESS) {
2133 psa_fail_key_creation(target_slot, driver);
2134 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002139}
2140
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002141
2142
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002143/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002144/* Message digests */
2145/****************************************************************/
2146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002148{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002149 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (operation->id == 0) {
2151 return PSA_SUCCESS;
2152 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002155 operation->id = 0;
2156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002158}
2159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2161 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002162{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002163 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002164
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002165 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002167 status = PSA_ERROR_BAD_STATE;
2168 goto exit;
2169 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002172 status = PSA_ERROR_INVALID_ARGUMENT;
2173 goto exit;
2174 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002175
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002176 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2177 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002181
2182exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (status != PSA_SUCCESS) {
2184 psa_hash_abort(operation);
2185 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002186
2187 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002188}
2189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2191 const uint8_t *input,
2192 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002194 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002197 status = PSA_ERROR_BAD_STATE;
2198 goto exit;
2199 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002200
Steven Cooremanc8288352021-03-04 14:02:19 +01002201 /* Don't require hash implementations to behave correctly on a
2202 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (input_length == 0) {
2204 return PSA_SUCCESS;
2205 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002208
2209exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (status != PSA_SUCCESS) {
2211 psa_hash_abort(operation);
2212 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002215}
2216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2218 uint8_t *hash,
2219 size_t hash_size,
2220 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002222 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if (operation->id == 0) {
2224 return PSA_ERROR_BAD_STATE;
2225 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002226
Steven Cooreman1e582352021-02-18 17:24:37 +01002227 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 operation, hash, hash_size, hash_length);
2229 psa_hash_abort(operation);
2230 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002231}
2232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2234 const uint8_t *hash,
2235 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236{
Ronald Cron69a63422021-10-18 09:47:58 +02002237 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002239 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 operation,
2241 actual_hash, sizeof(actual_hash),
2242 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002245 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002249 status = PSA_ERROR_INVALID_SIGNATURE;
2250 goto exit;
2251 }
2252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002254 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002256
2257exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2259 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002260 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002264}
2265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266psa_status_t psa_hash_compute(psa_algorithm_t alg,
2267 const uint8_t *input, size_t input_length,
2268 uint8_t *hash, size_t hash_size,
2269 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002270{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002271 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 if (!PSA_ALG_IS_HASH(alg)) {
2273 return PSA_ERROR_INVALID_ARGUMENT;
2274 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2277 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002278}
2279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280psa_status_t psa_hash_compare(psa_algorithm_t alg,
2281 const uint8_t *input, size_t input_length,
2282 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002283{
Ronald Cron69a63422021-10-18 09:47:58 +02002284 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002285 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 if (!PSA_ALG_IS_HASH(alg)) {
2288 return PSA_ERROR_INVALID_ARGUMENT;
2289 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002290
Steven Cooreman1e582352021-02-18 17:24:37 +01002291 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 alg, input, input_length,
2293 actual_hash, sizeof(actual_hash),
2294 &actual_hash_length);
2295 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002296 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 }
2298 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002299 status = PSA_ERROR_INVALID_SIGNATURE;
2300 goto exit;
2301 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002303 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002305
2306exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2308 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002309}
2310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2312 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002313{
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (source_operation->id == 0 ||
2315 target_operation->id != 0) {
2316 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002317 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2320 target_operation);
2321 if (status != PSA_SUCCESS) {
2322 psa_hash_abort(target_operation);
2323 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002326}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327
2328
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002330/* MAC */
2331/****************************************************************/
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002334{
Steven Cooremane6804192021-03-19 18:28:56 +01002335 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (operation->id == 0) {
2337 return PSA_SUCCESS;
2338 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002341 operation->mac_size = 0;
2342 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002343 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002346}
2347
Ronald Cron2dff3b22021-06-17 16:33:22 +02002348static psa_status_t psa_mac_finalize_alg_and_key_validation(
2349 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002350 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002352{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 psa_key_type_t key_type = psa_get_key_type(attributes);
2355 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if (!PSA_ALG_IS_MAC(alg)) {
2358 return PSA_ERROR_INVALID_ARGUMENT;
2359 }
Steven Cooremane6804192021-03-19 18:28:56 +01002360
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002361 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 status = psa_mac_key_can_do(alg, key_type);
2363 if (status != PSA_SUCCESS) {
2364 return status;
2365 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002366
Steven Cooremand1a68f12021-05-10 11:13:41 +02002367 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002371 /* A very short MAC is too short for security since it can be
2372 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2373 * so we make this our minimum, even though 32 bits is still
2374 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002376 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2379 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002380 /* It's impossible to "truncate" to a larger length than the full length
2381 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002383 }
2384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002386 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2387 * that is disabled in the compile-time configuration. The result can
2388 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2389 * configuration into account. In this case, force a return of
2390 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2391 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2392 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2393 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2394 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002396 }
2397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002399}
2400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2402 mbedtls_svc_key_id_t key,
2403 psa_algorithm_t alg,
2404 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002405{
2406 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2407 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002408 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409
2410 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002412 status = PSA_ERROR_BAD_STATE;
2413 goto exit;
2414 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415
2416 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 key,
2418 &slot,
2419 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2420 alg);
2421 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002422 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002424
2425 psa_key_attributes_t attributes = {
2426 .core = slot->attr
2427 };
2428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2430 &operation->mac_size);
2431 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002434
2435 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002436 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (is_sign) {
2438 status = psa_driver_wrapper_mac_sign_setup(operation,
2439 &attributes,
2440 slot->key.data,
2441 slot->key.bytes,
2442 alg);
2443 } else {
2444 status = psa_driver_wrapper_mac_verify_setup(operation,
2445 &attributes,
2446 slot->key.data,
2447 slot->key.bytes,
2448 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002450
Gilles Peskinefbfac682018-07-08 20:51:54 +02002451exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 if (status != PSA_SUCCESS) {
2453 psa_mac_abort(operation);
2454 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459}
2460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2462 mbedtls_svc_key_id_t key,
2463 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002464{
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002466}
2467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2469 mbedtls_svc_key_id_t key,
2470 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002471{
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002473}
2474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2476 const uint8_t *input,
2477 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478{
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (operation->id == 0) {
2480 return PSA_ERROR_BAD_STATE;
2481 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002482
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002483 /* Don't require hash implementations to behave correctly on a
2484 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (input_length == 0) {
2486 return PSA_SUCCESS;
2487 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2490 input, input_length);
2491 if (status != PSA_SUCCESS) {
2492 psa_mac_abort(operation);
2493 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496}
2497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2499 uint8_t *mac,
2500 size_t mac_size,
2501 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002502{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002503 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2504 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002507 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002508 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002509 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002512 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002513 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002514 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002515
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002516 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2517 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002519 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002520 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002521 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002524 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002525 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002526 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 status = psa_driver_wrapper_mac_sign_finish(operation,
2529 mac, operation->mac_size,
2530 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002531
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002532exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002533 /* In case of success, set the potential excess room in the output buffer
2534 * to an invalid value, to avoid potentially leaking a longer MAC.
2535 * In case of error, set the output length and content to a safe default,
2536 * such that in case the caller misses an error check, the output would be
2537 * an unachievable MAC.
2538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002540 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002541 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002542 }
mohammad16036df908f2018-04-02 08:34:15 -07002543
Paul Elliottdc42ca82023-02-24 18:11:59 +00002544 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002549}
2550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2552 const uint8_t *mac,
2553 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002555 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2556 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002559 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002560 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002561 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002564 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002565 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002566 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002569 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002570 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002571 }
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 status = psa_driver_wrapper_mac_verify_finish(operation,
2574 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002575
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580}
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2583 psa_algorithm_t alg,
2584 const uint8_t *input,
2585 size_t input_length,
2586 uint8_t *mac,
2587 size_t mac_size,
2588 size_t *mac_length,
2589 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002590{
2591 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002592 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2593 psa_key_slot_t *slot;
2594 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595
Ronald Cron51131b52021-06-17 17:17:20 +02002596 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 key,
2598 &slot,
2599 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2600 alg);
2601 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002602 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002604
Ronald Cron51131b52021-06-17 17:17:20 +02002605 psa_key_attributes_t attributes = {
2606 .core = slot->attr
2607 };
2608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2610 &operation_mac_size);
2611 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002612 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002616 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002617 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002618 }
2619
2620 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 &attributes,
2622 slot->key.data, slot->key.bytes,
2623 alg,
2624 input, input_length,
2625 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002626
2627exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002628 /* In case of success, set the potential excess room in the output buffer
2629 * to an invalid value, to avoid potentially leaking a longer MAC.
2630 * In case of error, set the output length and content to a safe default,
2631 * such that in case the caller misses an error check, the output would be
2632 * an unachievable MAC.
2633 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002635 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002636 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002637 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002638
2639 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002644}
2645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002647 psa_algorithm_t alg,
2648 const uint8_t *input,
2649 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 uint8_t *mac,
2651 size_t mac_size,
2652 size_t *mac_length)
2653{
2654 return psa_mac_compute_internal(key, alg,
2655 input, input_length,
2656 mac, mac_size, mac_length, 1);
2657}
2658
2659psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2660 psa_algorithm_t alg,
2661 const uint8_t *input,
2662 size_t input_length,
2663 const uint8_t *mac,
2664 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002665{
2666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002667 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2668 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 status = psa_mac_compute_internal(key, alg,
2671 input, input_length,
2672 actual_mac, sizeof(actual_mac),
2673 &actual_mac_length, 0);
2674 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002675 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002679 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002680 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002681 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002683 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002684 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002685 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002686
2687exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002691}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002692
Gilles Peskinee59236f2018-01-27 23:32:46 +01002693/****************************************************************/
2694/* Asymmetric cryptography */
2695/****************************************************************/
2696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2698 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002699{
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 if (input_is_message) {
2701 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2702 return PSA_ERROR_INVALID_ARGUMENT;
2703 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2706 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2707 return PSA_ERROR_INVALID_ARGUMENT;
2708 }
2709 }
2710 } else {
2711 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2712 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002713 }
2714 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002717}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2720 int input_is_message,
2721 psa_algorithm_t alg,
2722 const uint8_t *input,
2723 size_t input_length,
2724 uint8_t *signature,
2725 size_t signature_size,
2726 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002727{
2728 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2729 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2730 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002731
2732 *signature_length = 0;
2733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 status = psa_sign_verify_check_alg(input_is_message, alg);
2735 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002736 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002738
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002739 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002740 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002741 * buffer can in principle be empty since it doesn't actually have
2742 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 if (signature_size == 0) {
2744 return PSA_ERROR_BUFFER_TOO_SMALL;
2745 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002746
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002747 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 key, &slot,
2749 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2750 PSA_KEY_USAGE_SIGN_HASH,
2751 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002754 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002758 status = PSA_ERROR_INVALID_ARGUMENT;
2759 goto exit;
2760 }
2761
2762 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002764 };
2765
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002767 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002768 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002769 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 signature, signature_size, signature_length);
2771 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002772
2773 status = psa_driver_wrapper_sign_hash(
2774 &attributes, slot->key.data, slot->key.bytes,
2775 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002777 }
2778
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002779
2780exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002781 psa_wipe_tag_output_buffer(signature, status, signature_size,
2782 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002787}
2788
Gilles Peskine449bd832023-01-11 14:50:10 +01002789static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2790 int input_is_message,
2791 psa_algorithm_t alg,
2792 const uint8_t *input,
2793 size_t input_length,
2794 const uint8_t *signature,
2795 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002796{
2797 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2798 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2799 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 status = psa_sign_verify_check_alg(input_is_message, alg);
2802 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002803 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002805
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002806 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 key, &slot,
2808 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2809 PSA_KEY_USAGE_VERIFY_HASH,
2810 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 if (status != PSA_SUCCESS) {
2813 return status;
2814 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002815
2816 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002818 };
2819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002821 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002822 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002823 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 signature, signature_length);
2825 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002826 status = psa_driver_wrapper_verify_hash(
2827 &attributes, slot->key.data, slot->key.bytes,
2828 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002830 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002831
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002835
2836}
2837
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002838psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002839 const psa_key_attributes_t *attributes,
2840 const uint8_t *key_buffer,
2841 size_t key_buffer_size,
2842 psa_algorithm_t alg,
2843 const uint8_t *input,
2844 size_t input_length,
2845 uint8_t *signature,
2846 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002848{
2849 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002852 size_t hash_length;
2853 uint8_t hash[PSA_HASH_MAX_SIZE];
2854
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002855 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 PSA_ALG_SIGN_GET_HASH(alg),
2857 input, input_length,
2858 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002861 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002863
2864 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 attributes, key_buffer, key_buffer_size,
2866 alg, hash, hash_length,
2867 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002868 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002871}
2872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2874 psa_algorithm_t alg,
2875 const uint8_t *input,
2876 size_t input_length,
2877 uint8_t *signature,
2878 size_t signature_size,
2879 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002880{
2881 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002882 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002884}
2885
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002886psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002887 const psa_key_attributes_t *attributes,
2888 const uint8_t *key_buffer,
2889 size_t key_buffer_size,
2890 psa_algorithm_t alg,
2891 const uint8_t *input,
2892 size_t input_length,
2893 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002895{
2896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002899 size_t hash_length;
2900 uint8_t hash[PSA_HASH_MAX_SIZE];
2901
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002902 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 PSA_ALG_SIGN_GET_HASH(alg),
2904 input, input_length,
2905 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002908 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002910
2911 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 attributes, key_buffer, key_buffer_size,
2913 alg, hash, hash_length,
2914 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002915 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002918}
2919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2921 psa_algorithm_t alg,
2922 const uint8_t *input,
2923 size_t input_length,
2924 const uint8_t *signature,
2925 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002926{
2927 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002928 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002930}
2931
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002932psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002933 const psa_key_attributes_t *attributes,
2934 const uint8_t *key_buffer, size_t key_buffer_size,
2935 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002937{
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2939 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2940 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002941#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2943 return mbedtls_psa_rsa_sign_hash(
2944 attributes,
2945 key_buffer, key_buffer_size,
2946 alg, hash, hash_length,
2947 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002948#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2949 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 } else {
2951 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002952 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2954 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002955#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2957 return mbedtls_psa_ecdsa_sign_hash(
2958 attributes,
2959 key_buffer, key_buffer_size,
2960 alg, hash, hash_length,
2961 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002962#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2963 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 } else {
2965 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002966 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002967 }
Ronald Cron4501c982021-03-19 20:09:32 +01002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 (void) key_buffer;
2970 (void) key_buffer_size;
2971 (void) hash;
2972 (void) hash_length;
2973 (void) signature;
2974 (void) signature_size;
2975 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002976
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002978}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2981 psa_algorithm_t alg,
2982 const uint8_t *hash,
2983 size_t hash_length,
2984 uint8_t *signature,
2985 size_t signature_size,
2986 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002987{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002988 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002989 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002991}
2992
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002993psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002994 const psa_key_attributes_t *attributes,
2995 const uint8_t *key_buffer, size_t key_buffer_size,
2996 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002998{
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3000 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3001 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003002#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3004 return mbedtls_psa_rsa_verify_hash(
3005 attributes,
3006 key_buffer, key_buffer_size,
3007 alg, hash, hash_length,
3008 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003009#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3010 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 } else {
3012 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003013 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3015 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003016#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3018 return mbedtls_psa_ecdsa_verify_hash(
3019 attributes,
3020 key_buffer, key_buffer_size,
3021 alg, hash, hash_length,
3022 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003023#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3024 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 } else {
3026 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003027 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003028 }
Ronald Cron4501c982021-03-19 20:09:32 +01003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 (void) key_buffer;
3031 (void) key_buffer_size;
3032 (void) hash;
3033 (void) hash_length;
3034 (void) signature;
3035 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003038}
3039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3041 psa_algorithm_t alg,
3042 const uint8_t *hash,
3043 size_t hash_length,
3044 const uint8_t *signature,
3045 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003046{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003047 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003048 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003050}
3051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3053 psa_algorithm_t alg,
3054 const uint8_t *input,
3055 size_t input_length,
3056 const uint8_t *salt,
3057 size_t salt_length,
3058 uint8_t *output,
3059 size_t output_size,
3060 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003061{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003062 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003063 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003064 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003065
Darryl Green5cc689a2018-07-24 15:34:10 +01003066 (void) input;
3067 (void) input_length;
3068 (void) salt;
3069 (void) output;
3070 (void) output_size;
3071
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003072 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3075 return PSA_ERROR_INVALID_ARGUMENT;
3076 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003077
Ronald Cron5c522922020-11-14 16:35:34 +01003078 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3080 if (status != PSA_SUCCESS) {
3081 return status;
3082 }
3083 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3084 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003085 status = PSA_ERROR_INVALID_ARGUMENT;
3086 goto exit;
3087 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003088
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003089 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003091 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003092
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003093 status = psa_driver_wrapper_asymmetric_encrypt(
3094 &attributes, slot->key.data, slot->key.bytes,
3095 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003097exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003099
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003101}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3104 psa_algorithm_t alg,
3105 const uint8_t *input,
3106 size_t input_length,
3107 const uint8_t *salt,
3108 size_t salt_length,
3109 uint8_t *output,
3110 size_t output_size,
3111 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003112{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003113 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003114 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003115 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003116
Darryl Green5cc689a2018-07-24 15:34:10 +01003117 (void) input;
3118 (void) input_length;
3119 (void) salt;
3120 (void) output;
3121 (void) output_size;
3122
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003123 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3126 return PSA_ERROR_INVALID_ARGUMENT;
3127 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003128
Ronald Cron5c522922020-11-14 16:35:34 +01003129 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3131 if (status != PSA_SUCCESS) {
3132 return status;
3133 }
3134 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003135 status = PSA_ERROR_INVALID_ARGUMENT;
3136 goto exit;
3137 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003138
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003139 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003141 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003143 status = psa_driver_wrapper_asymmetric_decrypt(
3144 &attributes, slot->key.data, slot->key.bytes,
3145 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003147
3148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003152}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003153
Paul Elliott9fe12f62022-11-30 19:16:02 +00003154/****************************************************************/
3155/* Asymmetric interruptible cryptography */
3156/****************************************************************/
3157
3158void psa_interruptible_set_max_ops(uint32_t max_ops)
3159{
3160 psa_driver_wrapper_interruptible_set_max_ops(max_ops);
3161}
3162
3163uint32_t psa_interruptible_get_max_ops(void)
3164{
3165 return psa_driver_wrapper_interruptible_get_max_ops();
3166}
3167
3168
3169uint32_t psa_sign_hash_get_num_ops(
3170 const psa_sign_hash_interruptible_operation_t *operation)
3171{
Paul Elliott296ede92022-12-15 17:00:30 +00003172 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003173}
3174
3175uint32_t psa_verify_hash_get_num_ops(
3176 const psa_verify_hash_interruptible_operation_t *operation)
3177{
Paul Elliott296ede92022-12-15 17:00:30 +00003178 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003179}
3180
Paul Elliott59ad9452022-12-18 15:09:02 +00003181static psa_status_t psa_sign_hash_abort_internal(
3182 psa_sign_hash_interruptible_operation_t *operation)
3183{
3184 if (operation->id == 0) {
3185 /* The object has (apparently) been initialized but it is not (yet)
3186 * in use. It's ok to call abort on such an object, and there's
3187 * nothing to do. */
3188 return PSA_SUCCESS;
3189 }
3190
3191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3192
3193 status = psa_driver_wrapper_sign_hash_abort(operation);
3194
3195 operation->id = 0;
3196
Paul Elliotte6145dc2023-02-07 12:51:21 +00003197 /* Do not clear either the error_occurred or num_ops elements here as they
3198 * only want to be cleared by the application calling abort, not by abort
3199 * being called at completion of an operation. */
3200
Paul Elliott59ad9452022-12-18 15:09:02 +00003201 return status;
3202}
3203
Paul Elliott9fe12f62022-11-30 19:16:02 +00003204psa_status_t psa_sign_hash_start(
3205 psa_sign_hash_interruptible_operation_t *operation,
3206 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3207 const uint8_t *hash, size_t hash_length)
3208{
3209 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3210 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3211 psa_key_slot_t *slot;
3212
Paul Elliottc9774412023-02-06 15:14:07 +00003213 /* Check that start has not been previously called, or operation has not
3214 * previously errored. */
3215 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003216 return PSA_ERROR_BAD_STATE;
3217 }
3218
Paul Elliott9fe12f62022-11-30 19:16:02 +00003219 status = psa_sign_verify_check_alg(0, alg);
3220 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003221 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003222 return status;
3223 }
3224
3225 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3226 PSA_KEY_USAGE_SIGN_HASH,
3227 alg);
3228
3229 if (status != PSA_SUCCESS) {
3230 goto exit;
3231 }
3232
3233 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3234 status = PSA_ERROR_INVALID_ARGUMENT;
3235 goto exit;
3236 }
3237
3238 psa_key_attributes_t attributes = {
3239 .core = slot->attr
3240 };
3241
Paul Elliott296ede92022-12-15 17:00:30 +00003242 /* Ensure ops count gets reset, in case of operation re-use. */
3243 operation->num_ops = 0;
3244
Paul Elliott9fe12f62022-11-30 19:16:02 +00003245 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3246 slot->key.data,
3247 slot->key.bytes, alg,
3248 hash, hash_length);
3249exit:
3250
3251 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003252 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003253 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003254 }
3255
3256 unlock_status = psa_unlock_key_slot(slot);
3257
Paul Elliottc9774412023-02-06 15:14:07 +00003258 if (unlock_status != PSA_SUCCESS) {
3259 operation->error_occurred = 1;
3260 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003261
Paul Elliottc9774412023-02-06 15:14:07 +00003262 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003263}
3264
3265
3266psa_status_t psa_sign_hash_complete(
3267 psa_sign_hash_interruptible_operation_t *operation,
3268 uint8_t *signature, size_t signature_size,
3269 size_t *signature_length)
3270{
3271 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3272
3273 *signature_length = 0;
3274
Paul Elliottc9774412023-02-06 15:14:07 +00003275 /* Check that start has been called first, and that operation has not
3276 * previously errored. */
3277 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003278 status = PSA_ERROR_BAD_STATE;
3279 goto exit;
3280 }
3281
Paul Elliott096abc42023-02-03 18:33:23 +00003282 /* Immediately reject a zero-length signature buffer. This guarantees that
3283 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003284 if (signature_size == 0) {
3285 status = PSA_ERROR_BUFFER_TOO_SMALL;
3286 goto exit;
3287 }
3288
3289 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3290 signature_size,
3291 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003292
Paul Elliott296ede92022-12-15 17:00:30 +00003293 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003294 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003295
Paul Elliottebe225c2023-02-10 14:32:53 +00003296exit:
3297
Paul Elliott59200a22023-02-21 15:07:40 +00003298 psa_wipe_tag_output_buffer(signature, status, signature_size,
3299 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003300
Paul Elliott53bb3122023-02-10 14:22:22 +00003301 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003302 if (status != PSA_SUCCESS) {
3303 operation->error_occurred = 1;
3304 }
3305
Paul Elliott59ad9452022-12-18 15:09:02 +00003306 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003307 }
3308
3309 return status;
3310}
3311
3312psa_status_t psa_sign_hash_abort(
3313 psa_sign_hash_interruptible_operation_t *operation)
3314{
Paul Elliott59ad9452022-12-18 15:09:02 +00003315 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3316
3317 status = psa_sign_hash_abort_internal(operation);
3318
3319 /* We clear the number of ops done here, so that it is not cleared when
3320 * the operation fails or succeeds, only on manual abort. */
3321 operation->num_ops = 0;
3322
Paul Elliottc9774412023-02-06 15:14:07 +00003323 /* Likewise, failure state. */
3324 operation->error_occurred = 0;
3325
Paul Elliott59ad9452022-12-18 15:09:02 +00003326 return status;
3327}
3328
3329static psa_status_t psa_verify_hash_abort_internal(
3330 psa_verify_hash_interruptible_operation_t *operation)
3331{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003332 if (operation->id == 0) {
3333 /* The object has (apparently) been initialized but it is not (yet)
3334 * in use. It's ok to call abort on such an object, and there's
3335 * nothing to do. */
3336 return PSA_SUCCESS;
3337 }
3338
Paul Elliott59ad9452022-12-18 15:09:02 +00003339 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3340
3341 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003342
3343 operation->id = 0;
3344
Paul Elliotte6145dc2023-02-07 12:51:21 +00003345 /* Do not clear either the error_occurred or num_ops elements here as they
3346 * only want to be cleared by the application calling abort, not by abort
3347 * being called at completion of an operation. */
3348
Paul Elliott59ad9452022-12-18 15:09:02 +00003349 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003350}
3351
3352psa_status_t psa_verify_hash_start(
3353 psa_verify_hash_interruptible_operation_t *operation,
3354 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3355 const uint8_t *hash, size_t hash_length,
3356 const uint8_t *signature, size_t signature_length)
3357{
3358 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3359 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3360 psa_key_slot_t *slot;
3361
Paul Elliottc9774412023-02-06 15:14:07 +00003362 /* Check that start has not been previously called, or operation has not
3363 * previously errored. */
3364 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003365 return PSA_ERROR_BAD_STATE;
3366 }
3367
3368 status = psa_sign_verify_check_alg(0, alg);
3369 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003370 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003371 return status;
3372 }
3373
3374 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3375 PSA_KEY_USAGE_VERIFY_HASH,
3376 alg);
3377
3378 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003379 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003380 return status;
3381 }
3382
3383 psa_key_attributes_t attributes = {
3384 .core = slot->attr
3385 };
3386
Paul Elliott296ede92022-12-15 17:00:30 +00003387 /* Ensure ops count gets reset, in case of operation re-use. */
3388 operation->num_ops = 0;
3389
Paul Elliott9fe12f62022-11-30 19:16:02 +00003390 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3391 slot->key.data,
3392 slot->key.bytes,
3393 alg, hash, hash_length,
3394 signature, signature_length);
3395
3396 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003397 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003398 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003399 }
3400
3401 unlock_status = psa_unlock_key_slot(slot);
3402
Paul Elliottc9774412023-02-06 15:14:07 +00003403 if (unlock_status != PSA_SUCCESS) {
3404 operation->error_occurred = 1;
3405 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003406
Paul Elliottc9774412023-02-06 15:14:07 +00003407 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003408}
3409
3410psa_status_t psa_verify_hash_complete(
3411 psa_verify_hash_interruptible_operation_t *operation)
3412{
3413 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3414
Paul Elliottc9774412023-02-06 15:14:07 +00003415 /* Check that start has been called first, and that operation has not
3416 * previously errored. */
3417 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003418 status = PSA_ERROR_BAD_STATE;
3419 goto exit;
3420 }
3421
3422 status = psa_driver_wrapper_verify_hash_complete(operation);
3423
Paul Elliott296ede92022-12-15 17:00:30 +00003424 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003425 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003426 operation);
3427
Paul Elliottebe225c2023-02-10 14:32:53 +00003428exit:
3429
Paul Elliott9fe12f62022-11-30 19:16:02 +00003430 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003431 if (status != PSA_SUCCESS) {
3432 operation->error_occurred = 1;
3433 }
3434
Paul Elliott59ad9452022-12-18 15:09:02 +00003435 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003436 }
3437
3438 return status;
3439}
3440
3441psa_status_t psa_verify_hash_abort(
3442 psa_verify_hash_interruptible_operation_t *operation)
3443{
Paul Elliott59ad9452022-12-18 15:09:02 +00003444 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003445
Paul Elliott59ad9452022-12-18 15:09:02 +00003446 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003447
Paul Elliott59ad9452022-12-18 15:09:02 +00003448 /* We clear the number of ops done here, so that it is not cleared when
3449 * the operation fails or succeeds, only on manual abort. */
3450 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003451
Paul Elliottc9774412023-02-06 15:14:07 +00003452 /* Likewise, failure state. */
3453 operation->error_occurred = 0;
3454
Paul Elliott59ad9452022-12-18 15:09:02 +00003455 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003456}
3457
Paul Elliott588f8ed2022-12-02 18:10:26 +00003458/****************************************************************/
3459/* Asymmetric interruptible cryptography internal */
3460/* implementations */
3461/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003462
Paul Elliott588f8ed2022-12-02 18:10:26 +00003463static uint32_t mbedtls_psa_interruptible_max_ops =
3464 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3465
3466void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3467{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003468 mbedtls_psa_interruptible_max_ops = max_ops;
3469
Paul Elliott588f8ed2022-12-02 18:10:26 +00003470#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3471 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3472 defined(MBEDTLS_ECP_RESTARTABLE)
3473
3474 /* Internal implementation uses zero to indicate infinite number max ops,
3475 * therefore avoid this value, and set to minimum possible. */
3476 if (max_ops == 0) {
3477 max_ops = 1;
3478 }
3479
Paul Elliott588f8ed2022-12-02 18:10:26 +00003480 mbedtls_ecp_set_max_ops(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
3486uint32_t mbedtls_psa_interruptible_get_max_ops(void)
3487{
3488 return mbedtls_psa_interruptible_max_ops;
3489}
3490
3491uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003492 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003493{
3494#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3495 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3496 defined(MBEDTLS_ECP_RESTARTABLE)
3497
Paul Elliott93d9ca82023-02-15 18:14:21 +00003498 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003499#else
3500 (void) operation;
3501 return 0;
3502#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3503 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3504 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3505}
3506
3507uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003508 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003509{
3510 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3511 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3512 defined(MBEDTLS_ECP_RESTARTABLE)
3513
Paul Elliott93d9ca82023-02-15 18:14:21 +00003514 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003515#else
3516 (void) operation;
3517 return 0;
3518#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3519 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3520 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3521}
3522
3523psa_status_t mbedtls_psa_sign_hash_start(
3524 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3525 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3526 size_t key_buffer_size, psa_algorithm_t alg,
3527 const uint8_t *hash, size_t hash_length)
3528{
3529 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003530 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003531
Paul Elliott068fe072023-01-16 13:59:15 +00003532 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3533 return PSA_ERROR_NOT_SUPPORTED;
3534 }
3535
3536 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003537 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003538 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003539
3540#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003541 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3542 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003543
Paul Elliotta1c94092023-02-15 16:38:04 +00003544 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3545
Paul Elliott93d9ca82023-02-15 18:14:21 +00003546 /* Ensure num_ops is zero'ed in case of context re-use. */
3547 operation->num_ops = 0;
3548
Paul Elliott068fe072023-01-16 13:59:15 +00003549 /* Ensure default is set even if
3550 * mbedtls_psa_interruptible_set_max_ops() has not been called. */
3551 mbedtls_psa_interruptible_set_max_ops(
3552 mbedtls_psa_interruptible_get_max_ops());
Paul Elliott588f8ed2022-12-02 18:10:26 +00003553
Paul Elliott068fe072023-01-16 13:59:15 +00003554 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3555 attributes->core.bits,
3556 key_buffer,
3557 key_buffer_size,
3558 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003559
Paul Elliott068fe072023-01-16 13:59:15 +00003560 if (status != PSA_SUCCESS) {
3561 return status;
3562 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003563
Paul Elliott1bc59df2023-02-05 13:41:57 +00003564 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003565 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003566
Paul Elliott068fe072023-01-16 13:59:15 +00003567 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3568 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3569 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003570
Paul Elliott0290a762023-02-09 14:30:24 +00003571 /* We only need to store the same length of hash as the private key size
3572 * here, it would be truncated by the internal implementation anyway. */
3573 required_hash_length = (hash_length < operation->coordinate_bytes ?
3574 hash_length : operation->coordinate_bytes);
3575
Paul Elliottba70ad42023-02-15 18:23:53 +00003576 if (required_hash_length > sizeof(operation->hash)) {
3577 /* Shouldn't happen, but better safe than sorry. */
3578 return PSA_ERROR_CORRUPTION_DETECTED;
3579 }
3580
Paul Elliott0290a762023-02-09 14:30:24 +00003581 memcpy(operation->hash, hash, required_hash_length);
3582 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003583
3584 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003585
3586#else
Paul Elliott068fe072023-01-16 13:59:15 +00003587 (void) operation;
3588 (void) key_buffer;
3589 (void) key_buffer_size;
3590 (void) alg;
3591 (void) hash;
3592 (void) hash_length;
3593 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003594 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003595
Paul Elliott068fe072023-01-16 13:59:15 +00003596 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003597#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3598 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3599 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003600}
3601
3602psa_status_t mbedtls_psa_sign_hash_complete(
3603 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3604 uint8_t *signature, size_t signature_size,
3605 size_t *signature_length)
3606{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003607#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3608 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3609 defined(MBEDTLS_ECP_RESTARTABLE)
3610
Paul Elliotta1c94092023-02-15 16:38:04 +00003611 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003612 mbedtls_mpi r;
3613 mbedtls_mpi s;
3614
Paul Elliott46845252023-02-03 14:59:11 +00003615 mbedtls_mpi_init(&r);
3616 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003617
Paul Elliotta1c94092023-02-15 16:38:04 +00003618 if (signature_size < 2 * operation->coordinate_bytes) {
3619 status = PSA_ERROR_BUFFER_TOO_SMALL;
3620 goto exit;
3621 }
3622
Paul Elliott588f8ed2022-12-02 18:10:26 +00003623 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003624
Paul Elliott588f8ed2022-12-02 18:10:26 +00003625#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3626 status = mbedtls_to_psa_error(
3627 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003628 &r,
3629 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003630 &operation->ctx->d,
3631 operation->hash,
3632 operation->hash_length,
3633 operation->md_alg,
3634 mbedtls_psa_get_random,
3635 MBEDTLS_PSA_RANDOM_STATE,
3636 &operation->restart_ctx));
3637#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003638 status = PSA_ERROR_NOT_SUPPORTED;
3639 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3641 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003642 status = mbedtls_to_psa_error(
3643 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003644 &r,
3645 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646 &operation->ctx->d,
3647 operation->hash,
3648 operation->hash_length,
3649 mbedtls_psa_get_random,
3650 MBEDTLS_PSA_RANDOM_STATE,
3651 mbedtls_psa_get_random,
3652 MBEDTLS_PSA_RANDOM_STATE,
3653 &operation->restart_ctx));
3654 }
3655
Paul Elliottf8e5b562023-02-19 18:43:45 +00003656 /* Hide the fact that the restart context only holds a delta of number of
3657 * ops done during the last operation, not an absolute value. */
3658 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3659
Paul Elliott724bd252023-02-08 12:35:08 +00003660 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003661 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003662 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003663 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003664 operation->coordinate_bytes)
3665 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003666
3667 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003668 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669 }
3670
3671 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003672 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003673 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003674 operation->coordinate_bytes,
3675 operation->coordinate_bytes)
3676 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003677
3678 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003679 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003680 }
3681
Paul Elliott1bc59df2023-02-05 13:41:57 +00003682 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003683
Paul Elliott724bd252023-02-08 12:35:08 +00003684 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003685 }
Paul Elliott724bd252023-02-08 12:35:08 +00003686
3687exit:
3688
3689 mbedtls_mpi_free(&r);
3690 mbedtls_mpi_free(&s);
3691 return status;
3692
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693 #else
3694
3695 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003696 (void) signature;
3697 (void) signature_size;
3698 (void) signature_length;
3699
3700 return PSA_ERROR_NOT_SUPPORTED;
3701
3702#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3703 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3704 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3705}
3706
3707psa_status_t mbedtls_psa_sign_hash_abort(
3708 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3709{
3710
3711#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3712 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3713 defined(MBEDTLS_ECP_RESTARTABLE)
3714
3715 if (operation->ctx) {
3716 mbedtls_ecdsa_free(operation->ctx);
3717 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003718 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719 }
3720
3721 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3722
Paul Elliott93d9ca82023-02-15 18:14:21 +00003723 operation->num_ops = 0;
3724
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725 return PSA_SUCCESS;
3726
3727#else
3728
3729 (void) operation;
3730
3731 return PSA_ERROR_NOT_SUPPORTED;
3732
3733#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3734 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3735 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3736}
3737
3738psa_status_t mbedtls_psa_verify_hash_start(
3739 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3740 const psa_key_attributes_t *attributes,
3741 const uint8_t *key_buffer, size_t key_buffer_size,
3742 psa_algorithm_t alg,
3743 const uint8_t *hash, size_t hash_length,
3744 const uint8_t *signature, size_t signature_length)
3745{
3746 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003747 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003748 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003749
Paul Elliott068fe072023-01-16 13:59:15 +00003750 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3751 return PSA_ERROR_NOT_SUPPORTED;
3752 }
3753
3754 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003755 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003756 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003757
3758#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003759 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3760 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003761
Paul Elliotta1c94092023-02-15 16:38:04 +00003762 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3763 mbedtls_mpi_init(&operation->r);
3764 mbedtls_mpi_init(&operation->s);
3765
Paul Elliott93d9ca82023-02-15 18:14:21 +00003766 /* Ensure num_ops is zero'ed in case of context re-use. */
3767 operation->num_ops = 0;
3768
Paul Elliott068fe072023-01-16 13:59:15 +00003769 /* Ensure default is set even if
3770 * mbedtls_psa_interruptible_set_max_ops() has not been called. */
3771 mbedtls_psa_interruptible_set_max_ops(
3772 mbedtls_psa_interruptible_get_max_ops());
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773
Paul Elliott068fe072023-01-16 13:59:15 +00003774 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3775 attributes->core.bits,
3776 key_buffer,
3777 key_buffer_size,
3778 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003779
Paul Elliott068fe072023-01-16 13:59:15 +00003780 if (status != PSA_SUCCESS) {
3781 return status;
3782 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783
Paul Elliottc569fc22023-02-10 13:02:54 +00003784 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003785
Paul Elliott1bc59df2023-02-05 13:41:57 +00003786 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003787 return PSA_ERROR_INVALID_SIGNATURE;
3788 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003789
Paul Elliott068fe072023-01-16 13:59:15 +00003790 status = mbedtls_to_psa_error(
3791 mbedtls_mpi_read_binary(&operation->r,
3792 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003793 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003794
Paul Elliott068fe072023-01-16 13:59:15 +00003795 if (status != PSA_SUCCESS) {
3796 return status;
3797 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798
Paul Elliott068fe072023-01-16 13:59:15 +00003799 status = mbedtls_to_psa_error(
3800 mbedtls_mpi_read_binary(&operation->s,
3801 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003802 coordinate_bytes,
3803 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003804
Paul Elliott068fe072023-01-16 13:59:15 +00003805 if (status != PSA_SUCCESS) {
3806 return status;
3807 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003808
Paul Elliott2c9843f2023-02-15 17:32:42 +00003809 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003810
Paul Elliott2c9843f2023-02-15 17:32:42 +00003811 if (status != PSA_SUCCESS) {
3812 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003813 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003814
Paul Elliott0290a762023-02-09 14:30:24 +00003815 /* We only need to store the same length of hash as the private key size
3816 * here, it would be truncated by the internal implementation anyway. */
3817 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3818 coordinate_bytes);
3819
Paul Elliottba70ad42023-02-15 18:23:53 +00003820 if (required_hash_length > sizeof(operation->hash)) {
3821 /* Shouldn't happen, but better safe than sorry. */
3822 return PSA_ERROR_CORRUPTION_DETECTED;
3823 }
3824
Paul Elliott0290a762023-02-09 14:30:24 +00003825 memcpy(operation->hash, hash, required_hash_length);
3826 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003827
3828 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003829#else
Paul Elliott068fe072023-01-16 13:59:15 +00003830 (void) operation;
3831 (void) key_buffer;
3832 (void) key_buffer_size;
3833 (void) alg;
3834 (void) hash;
3835 (void) hash_length;
3836 (void) signature;
3837 (void) signature_length;
3838 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003839 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003840 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841
Paul Elliott068fe072023-01-16 13:59:15 +00003842 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3844 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3845 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846}
3847
3848psa_status_t mbedtls_psa_verify_hash_complete(
3849 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3850{
3851
3852#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3853 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3854 defined(MBEDTLS_ECP_RESTARTABLE)
3855
Paul Elliottf8e5b562023-02-19 18:43:45 +00003856 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3857
3858 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003859 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3860 operation->hash,
3861 operation->hash_length,
3862 &operation->ctx->Q,
3863 &operation->r,
3864 &operation->s,
3865 &operation->restart_ctx));
3866
Paul Elliottf8e5b562023-02-19 18:43:45 +00003867 /* Hide the fact that the restart context only holds a delta of number of
3868 * ops done during the last operation, not an absolute value. */
3869 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3870
3871 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003872#else
3873 (void) operation;
3874
3875 return PSA_ERROR_NOT_SUPPORTED;
3876
3877#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3878 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3879 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3880}
3881
3882psa_status_t mbedtls_psa_verify_hash_abort(
3883 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3884{
3885
3886#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3887 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3888 defined(MBEDTLS_ECP_RESTARTABLE)
3889
3890 if (operation->ctx) {
3891 mbedtls_ecdsa_free(operation->ctx);
3892 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003893 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003894 }
3895
3896 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3897
Paul Elliott93d9ca82023-02-15 18:14:21 +00003898 operation->num_ops = 0;
3899
Paul Elliott588f8ed2022-12-02 18:10:26 +00003900 mbedtls_mpi_free(&operation->r);
3901 mbedtls_mpi_free(&operation->s);
3902
3903 return PSA_SUCCESS;
3904
3905#else
3906 (void) operation;
3907
3908 return PSA_ERROR_NOT_SUPPORTED;
3909
3910#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3911 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3912 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3913}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003914
mohammad1603503973b2018-03-12 15:59:30 +02003915/****************************************************************/
3916/* Symmetric cryptography */
3917/****************************************************************/
3918
Gilles Peskine449bd832023-01-11 14:50:10 +01003919static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3920 mbedtls_svc_key_id_t key,
3921 psa_algorithm_t alg,
3922 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003923{
3924 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3925 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003926 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3928 PSA_KEY_USAGE_ENCRYPT :
3929 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003930
3931 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003932 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003933 status = PSA_ERROR_BAD_STATE;
3934 goto exit;
3935 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003936
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003938 status = PSA_ERROR_INVALID_ARGUMENT;
3939 goto exit;
3940 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003941
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3943 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003944 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003946
Ronald Cron49fafa92021-03-10 08:34:23 +01003947 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003948 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003949 * so we only set it (in the driver wrapper) after resources have been
3950 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003951 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003953 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003955 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003956 }
3957 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003958
Ronald Crona4af55f2020-12-14 14:36:06 +01003959 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003961 };
3962
Ronald Cronab99ac22020-10-01 16:38:28 +02003963 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 if (cipher_operation == MBEDTLS_ENCRYPT) {
3965 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3966 &attributes,
3967 slot->key.data,
3968 slot->key.bytes,
3969 alg);
3970 } else {
3971 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3972 &attributes,
3973 slot->key.data,
3974 slot->key.bytes,
3975 alg);
3976 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003977
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003978exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 if (status != PSA_SUCCESS) {
3980 psa_cipher_abort(operation);
3981 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003982
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003984
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003986}
3987
Gilles Peskine449bd832023-01-11 14:50:10 +01003988psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3989 mbedtls_svc_key_id_t key,
3990 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003991{
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003993}
3994
Gilles Peskine449bd832023-01-11 14:50:10 +01003995psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3996 mbedtls_svc_key_id_t key,
3997 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003998{
Gilles Peskine449bd832023-01-11 14:50:10 +01003999 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004000}
4001
Gilles Peskine449bd832023-01-11 14:50:10 +01004002psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4003 uint8_t *iv,
4004 size_t iv_size,
4005 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004006{
Ronald Cron6d051732020-10-01 14:10:20 +02004007 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004008 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4009 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004010
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004012 status = PSA_ERROR_BAD_STATE;
4013 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004014 }
4015
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004017 status = PSA_ERROR_BAD_STATE;
4018 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004019 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004020
Ronald Cron2fb90522021-07-05 12:31:44 +02004021 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004023 status = PSA_ERROR_BUFFER_TOO_SMALL;
4024 goto exit;
4025 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004028 status = PSA_ERROR_GENERIC_ERROR;
4029 goto exit;
4030 }
4031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 status = psa_generate_random(local_iv, default_iv_length);
4033 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004034 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 }
Ronald Cron5618a392021-03-26 09:52:26 +01004036
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 status = psa_driver_wrapper_cipher_set_iv(operation,
4038 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004039
4040exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 if (status == PSA_SUCCESS) {
4042 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004043 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004044 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004046 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004048 }
Ronald Cron6d051732020-10-01 14:10:20 +02004049
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004051}
4052
Gilles Peskine449bd832023-01-11 14:50:10 +01004053psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4054 const uint8_t *iv,
4055 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004056{
Ronald Cron6d051732020-10-01 14:10:20 +02004057 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004058
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004060 status = PSA_ERROR_BAD_STATE;
4061 goto exit;
4062 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004063
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004065 status = PSA_ERROR_BAD_STATE;
4066 goto exit;
4067 }
Ronald Crona0d68172021-03-26 10:15:08 +01004068
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004070 status = PSA_ERROR_INVALID_ARGUMENT;
4071 goto exit;
4072 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004073
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 status = psa_driver_wrapper_cipher_set_iv(operation,
4075 iv,
4076 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004077
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004078exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004080 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 } else {
4082 psa_cipher_abort(operation);
4083 }
4084 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004085}
4086
Gilles Peskine449bd832023-01-11 14:50:10 +01004087psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4088 const uint8_t *input,
4089 size_t input_length,
4090 uint8_t *output,
4091 size_t output_size,
4092 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004093{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004095
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004097 status = PSA_ERROR_BAD_STATE;
4098 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004099 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004102 status = PSA_ERROR_BAD_STATE;
4103 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004104 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004105
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 status = psa_driver_wrapper_cipher_update(operation,
4107 input,
4108 input_length,
4109 output,
4110 output_size,
4111 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004112
4113exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 if (status != PSA_SUCCESS) {
4115 psa_cipher_abort(operation);
4116 }
Ronald Cron6d051732020-10-01 14:10:20 +02004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004119}
4120
Gilles Peskine449bd832023-01-11 14:50:10 +01004121psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4122 uint8_t *output,
4123 size_t output_size,
4124 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004125{
David Saadab4ecc272019-02-14 13:48:10 +02004126 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004127
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004129 status = PSA_ERROR_BAD_STATE;
4130 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004131 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004132
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004134 status = PSA_ERROR_BAD_STATE;
4135 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004136 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004137
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 status = psa_driver_wrapper_cipher_finish(operation,
4139 output,
4140 output_size,
4141 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004142
4143exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 if (status == PSA_SUCCESS) {
4145 return psa_cipher_abort(operation);
4146 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004147 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004151 }
mohammad1603503973b2018-03-12 15:59:30 +02004152}
4153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004155{
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004157 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004158 * in use. It's ok to call abort on such an object, and there's
4159 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004161 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004164
Ronald Cron49fafa92021-03-10 08:34:23 +01004165 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004166 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004167 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004170}
4171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4173 psa_algorithm_t alg,
4174 const uint8_t *input,
4175 size_t input_length,
4176 uint8_t *output,
4177 size_t output_size,
4178 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004179{
4180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004181 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004182 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004183 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4184 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004187 status = PSA_ERROR_INVALID_ARGUMENT;
4188 goto exit;
4189 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4192 PSA_KEY_USAGE_ENCRYPT,
4193 alg);
4194 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004195 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004197
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004198 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004200 };
4201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4203 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004204 status = PSA_ERROR_GENERIC_ERROR;
4205 goto exit;
4206 }
4207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 if (default_iv_length > 0) {
4209 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004210 status = PSA_ERROR_BUFFER_TOO_SMALL;
4211 goto exit;
4212 }
4213
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 status = psa_generate_random(local_iv, default_iv_length);
4215 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004216 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004218 }
4219
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004220 status = psa_driver_wrapper_cipher_encrypt(
4221 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004222 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 mbedtls_buffer_offset(output, default_iv_length),
4224 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004225
4226exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 unlock_status = psa_unlock_key_slot(slot);
4228 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004229 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004230 }
Ronald Cron23919522021-07-08 19:00:07 +02004231
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 if (status == PSA_SUCCESS) {
4233 if (default_iv_length > 0) {
4234 memcpy(output, local_iv, default_iv_length);
4235 }
4236 *output_length += default_iv_length;
4237 } else {
4238 *output_length = 0;
4239 }
4240
4241 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004242}
4243
Gilles Peskine449bd832023-01-11 14:50:10 +01004244psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4245 psa_algorithm_t alg,
4246 const uint8_t *input,
4247 size_t input_length,
4248 uint8_t *output,
4249 size_t output_size,
4250 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004251{
4252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004253 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004254 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004257 status = PSA_ERROR_INVALID_ARGUMENT;
4258 goto exit;
4259 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004260
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4262 PSA_KEY_USAGE_DECRYPT,
4263 alg);
4264 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004265 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004267
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004268 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004270 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4273 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004274 status = PSA_ERROR_INVALID_ARGUMENT;
4275 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004277 status = PSA_ERROR_INVALID_ARGUMENT;
4278 goto exit;
4279 }
4280
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004281 status = psa_driver_wrapper_cipher_decrypt(
4282 &attributes, slot->key.data, slot->key.bytes,
4283 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004285
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004286exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 unlock_status = psa_unlock_key_slot(slot);
4288 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004289 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004291
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004293 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 }
Ronald Cron23919522021-07-08 19:00:07 +02004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004297}
4298
4299
mohammad16035955c982018-04-26 00:53:03 +03004300/****************************************************************/
4301/* AEAD */
4302/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004303
Paul Elliottbaff51c2021-09-28 17:44:45 +01004304/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004305static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004306{
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004308}
4309
4310/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004311static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4312 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004313{
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004317#if defined(PSA_WANT_ALG_GCM)
4318 case PSA_ALG_GCM:
4319 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 * arbitrarily large nonces. Please note that we do not generally
4321 * recommend the usage of nonces of greater length than
4322 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4323 * size, which can then lead to collisions if you encrypt a very
4324 * large number of messages.*/
4325 if (nonce_length != 0) {
4326 return PSA_SUCCESS;
4327 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004328 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004329#endif /* PSA_WANT_ALG_GCM */
4330#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004331 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 if (nonce_length >= 7 && nonce_length <= 13) {
4333 return PSA_SUCCESS;
4334 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004335 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004336#endif /* PSA_WANT_ALG_CCM */
4337#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004338 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 if (nonce_length == 12) {
4340 return PSA_SUCCESS;
4341 } else if (nonce_length == 8) {
4342 return PSA_ERROR_NOT_SUPPORTED;
4343 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004344 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004345#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004346 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004347 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004349 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004352}
4353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004355{
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4357 return PSA_ERROR_INVALID_ARGUMENT;
4358 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004361}
4362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4364 psa_algorithm_t alg,
4365 const uint8_t *nonce,
4366 size_t nonce_length,
4367 const uint8_t *additional_data,
4368 size_t additional_data_length,
4369 const uint8_t *plaintext,
4370 size_t plaintext_length,
4371 uint8_t *ciphertext,
4372 size_t ciphertext_size,
4373 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004374{
Ronald Cron215633c2021-03-16 17:15:37 +01004375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4376 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004377
mohammad1603f08a5502018-06-03 15:05:47 +03004378 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004379
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 status = psa_aead_check_algorithm(alg);
4381 if (status != PSA_SUCCESS) {
4382 return status;
4383 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004384
Ronald Cron9a986162021-03-26 12:40:07 +01004385 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4387 if (status != PSA_SUCCESS) {
4388 return status;
4389 }
mohammad16035955c982018-04-26 00:53:03 +03004390
Ronald Cron215633c2021-03-16 17:15:37 +01004391 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004393 };
mohammad16035955c982018-04-26 00:53:03 +03004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 status = psa_aead_check_nonce_length(alg, nonce_length);
4396 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004397 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004399
Ronald Cronde822812021-03-17 16:08:20 +01004400 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004401 &attributes, slot->key.data, slot->key.bytes,
4402 alg,
4403 nonce, nonce_length,
4404 additional_data, additional_data_length,
4405 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004407
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4409 memset(ciphertext, 0, ciphertext_size);
4410 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004411
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004412exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004416}
4417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4419 psa_algorithm_t alg,
4420 const uint8_t *nonce,
4421 size_t nonce_length,
4422 const uint8_t *additional_data,
4423 size_t additional_data_length,
4424 const uint8_t *ciphertext,
4425 size_t ciphertext_length,
4426 uint8_t *plaintext,
4427 size_t plaintext_size,
4428 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004429{
Ronald Cron215633c2021-03-16 17:15:37 +01004430 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4431 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004432
Gilles Peskineee652a32018-06-01 19:23:52 +02004433 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004434
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 status = psa_aead_check_algorithm(alg);
4436 if (status != PSA_SUCCESS) {
4437 return status;
4438 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004439
Ronald Cron9a986162021-03-26 12:40:07 +01004440 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4442 if (status != PSA_SUCCESS) {
4443 return status;
4444 }
mohammad16035955c982018-04-26 00:53:03 +03004445
Ronald Cron215633c2021-03-16 17:15:37 +01004446 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004448 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 status = psa_aead_check_nonce_length(alg, nonce_length);
4451 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004452 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004454
Ronald Cronde822812021-03-17 16:08:20 +01004455 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004456 &attributes, slot->key.data, slot->key.bytes,
4457 alg,
4458 nonce, nonce_length,
4459 additional_data, additional_data_length,
4460 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 if (status != PSA_SUCCESS && plaintext_size != 0) {
4464 memset(plaintext, 0, plaintext_size);
4465 }
mohammad160360a64d02018-06-03 17:20:42 +03004466
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 return status;
mohammad16035955c982018-04-26 00:53:03 +03004471}
4472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4474{
4475 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004478#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004480 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
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_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004486
Przemek Stekiel86679c72022-10-06 17:06:56 +02004487#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004489 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || 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_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004495
Przemek Stekiel86679c72022-10-06 17:06:56 +02004496#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004498 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 if (tag_len != 16) {
4500 return PSA_ERROR_INVALID_ARGUMENT;
4501 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004502 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004503#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004504
4505 default:
4506 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004508 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004510}
4511
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004512/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004513static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4514 int is_encrypt,
4515 mbedtls_svc_key_id_t key,
4516 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004517{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004518 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4519 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4520 psa_key_slot_t *slot = NULL;
4521 psa_key_usage_t key_usage = 0;
4522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 status = psa_aead_check_algorithm(alg);
4524 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004525 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004527
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004529 status = PSA_ERROR_BAD_STATE;
4530 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004531 }
4532
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 if (operation->nonce_set || operation->lengths_set ||
4534 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004535 status = PSA_ERROR_BAD_STATE;
4536 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004537 }
4538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004540 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004542 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004544
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4546 alg);
4547 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004548 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004550
4551 psa_key_attributes_t attributes = {
4552 .core = slot->attr
4553 };
4554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004556 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 if (is_encrypt) {
4560 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4561 &attributes,
4562 slot->key.data,
4563 slot->key.bytes,
4564 alg);
4565 } else {
4566 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4567 &attributes,
4568 slot->key.data,
4569 slot->key.bytes,
4570 alg);
4571 }
4572 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004573 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004577
4578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004582 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004584 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 } else {
4586 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004587 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004590}
4591
Paul Elliott302ff6b2021-04-20 18:10:30 +01004592/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004593psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4594 mbedtls_svc_key_id_t key,
4595 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004596{
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004598}
4599
4600/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004601psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4602 mbedtls_svc_key_id_t key,
4603 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004604{
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004606}
4607
4608/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004609psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4610 uint8_t *nonce,
4611 size_t nonce_size,
4612 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004613{
4614 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004615 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004616 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004617
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004618 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004621 status = PSA_ERROR_BAD_STATE;
4622 goto exit;
4623 }
4624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004626 status = PSA_ERROR_BAD_STATE;
4627 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004628 }
4629
Paul Elliotte193ea82021-10-01 13:00:16 +01004630 /* For CCM, this size may not be correct according to the PSA
4631 * specification. The PSA Crypto 1.0.1 specification states:
4632 *
4633 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4634 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4635 *
4636 * However this restriction that L has to be the smallest integer is not
4637 * applied in practice, and it is not implementable here since the
4638 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4640 operation->alg);
4641 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004642 status = PSA_ERROR_BUFFER_TOO_SMALL;
4643 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004644 }
4645
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 status = psa_generate_random(local_nonce, required_nonce_size);
4647 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004648 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004652
Paul Elliott39dc6b82021-05-11 19:16:09 +01004653exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 if (status == PSA_SUCCESS) {
4655 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004656 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 } else {
4658 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004659 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004662}
4663
4664/* Set the nonce for a multipart authenticated encryption or decryption
4665 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004666psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4667 const uint8_t *nonce,
4668 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004669{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004670 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004673 status = PSA_ERROR_BAD_STATE;
4674 goto exit;
4675 }
4676
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004678 status = PSA_ERROR_BAD_STATE;
4679 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004680 }
4681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4683 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004684 status = PSA_ERROR_INVALID_ARGUMENT;
4685 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004686 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4689 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004690
Paul Elliott39dc6b82021-05-11 19:16:09 +01004691exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004693 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 } else {
4695 psa_aead_abort(operation);
4696 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004699}
4700
4701/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004702psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4703 size_t ad_length,
4704 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004705{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004709 status = PSA_ERROR_BAD_STATE;
4710 goto exit;
4711 }
4712
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 if (operation->lengths_set || operation->ad_started ||
4714 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004715 status = PSA_ERROR_BAD_STATE;
4716 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004717 }
4718
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004720#if defined(PSA_WANT_ALG_GCM)
4721 case PSA_ALG_GCM:
4722 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 * bits. Without the guard this code will generate warnings on 32bit
4724 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004725#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (((uint64_t) ad_length) >> 61 != 0 ||
4727 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004728 status = PSA_ERROR_INVALID_ARGUMENT;
4729 goto exit;
4730 }
Paul Elliott325d3742021-09-27 17:56:28 +01004731#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004732 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004733#endif /* PSA_WANT_ALG_GCM */
4734#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004735 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004737 status = PSA_ERROR_INVALID_ARGUMENT;
4738 goto exit;
4739 }
4740 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004741#endif /* PSA_WANT_ALG_CCM */
4742#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004743 case PSA_ALG_CHACHA20_POLY1305:
4744 /* No length restrictions for ChaChaPoly. */
4745 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004746#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004747 default:
4748 break;
4749 }
Paul Elliott325d3742021-09-27 17:56:28 +01004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4752 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004753
Paul Elliott39dc6b82021-05-11 19:16:09 +01004754exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004756 operation->ad_remaining = ad_length;
4757 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004758 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 } else {
4760 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004761 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004764}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004765
4766/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004767psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4768 const uint8_t *input,
4769 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004770{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004771 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4772
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004774 status = PSA_ERROR_BAD_STATE;
4775 goto exit;
4776 }
4777
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004779 status = PSA_ERROR_BAD_STATE;
4780 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004781 }
4782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 if (operation->lengths_set) {
4784 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004785 status = PSA_ERROR_INVALID_ARGUMENT;
4786 goto exit;
4787 }
4788
4789 operation->ad_remaining -= input_length;
4790 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004791#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004793 status = PSA_ERROR_BAD_STATE;
4794 goto exit;
4795 }
4796#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 status = psa_driver_wrapper_aead_update_ad(operation, input,
4799 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004800
Paul Elliott39dc6b82021-05-11 19:16:09 +01004801exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004803 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 } else {
4805 psa_aead_abort(operation);
4806 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004809}
4810
4811/* Encrypt or decrypt a message fragment in an active multipart AEAD
4812 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004813psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4814 const uint8_t *input,
4815 size_t input_length,
4816 uint8_t *output,
4817 size_t output_size,
4818 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004819{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004820 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004821
4822 *output_length = 0;
4823
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004825 status = PSA_ERROR_BAD_STATE;
4826 goto exit;
4827 }
4828
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004830 status = PSA_ERROR_BAD_STATE;
4831 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004832 }
4833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004835 /* Additional data length was supplied, but not all the additional
4836 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004838 status = PSA_ERROR_INVALID_ARGUMENT;
4839 goto exit;
4840 }
4841
4842 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004844 status = PSA_ERROR_INVALID_ARGUMENT;
4845 goto exit;
4846 }
4847
4848 operation->body_remaining -= input_length;
4849 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004850#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004852 status = PSA_ERROR_BAD_STATE;
4853 goto exit;
4854 }
4855#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4858 output, output_size,
4859 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004860
Paul Elliott39dc6b82021-05-11 19:16:09 +01004861exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004863 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 } else {
4865 psa_aead_abort(operation);
4866 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004869}
4870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004872{
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 if (operation->id == 0 || !operation->nonce_set) {
4874 return PSA_ERROR_BAD_STATE;
4875 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4878 operation->body_remaining != 0)) {
4879 return PSA_ERROR_INVALID_ARGUMENT;
4880 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004883}
4884
Paul Elliott302ff6b2021-04-20 18:10:30 +01004885/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004886psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4887 uint8_t *ciphertext,
4888 size_t ciphertext_size,
4889 size_t *ciphertext_length,
4890 uint8_t *tag,
4891 size_t tag_size,
4892 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004893{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004894 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4895
Paul Elliott302ff6b2021-04-20 18:10:30 +01004896 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004897 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004898
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 status = psa_aead_final_checks(operation);
4900 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004901 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004903
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004905 status = PSA_ERROR_BAD_STATE;
4906 goto exit;
4907 }
4908
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4910 ciphertext_size,
4911 ciphertext_length,
4912 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004913
Paul Elliott39dc6b82021-05-11 19:16:09 +01004914exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004915
4916
Paul Elliottf47b0952021-05-21 18:02:33 +01004917 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004918 * the zero tag size, make sure the tag is set to something implausible.
4919 * Even if the operation succeeds, make sure we clear the rest of the
4920 * buffer to prevent potential leakage of anything previously placed in
4921 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004922 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004927}
4928
4929/* Finish authenticating and decrypting a message in a multipart AEAD
4930 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004931psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4932 uint8_t *plaintext,
4933 size_t plaintext_size,
4934 size_t *plaintext_length,
4935 const uint8_t *tag,
4936 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004937{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004938 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4939
Paul Elliott302ff6b2021-04-20 18:10:30 +01004940 *plaintext_length = 0;
4941
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 status = psa_aead_final_checks(operation);
4943 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004944 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004946
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004948 status = PSA_ERROR_BAD_STATE;
4949 goto exit;
4950 }
4951
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4953 plaintext_size,
4954 plaintext_length,
4955 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004956
4957exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004961}
4962
4963/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004964psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004965{
4966 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004969 /* The object has (apparently) been initialized but it is not (yet)
4970 * in use. It's ok to call abort on such an object, and there's
4971 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004973 }
4974
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004976
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004978
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004980}
4981
Gilles Peskinee59236f2018-01-27 23:32:46 +01004982/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004983/* Generators */
4984/****************************************************************/
4985
Przemek Stekiel69c46792022-06-10 12:59:51 +02004986#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004987 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004988 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4989 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004990#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004991#endif /* At least one builtin KDF */
4992
Przemek Stekiel69c46792022-06-10 12:59:51 +02004993#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004994 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4995 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4996static psa_status_t psa_key_derivation_start_hmac(
4997 psa_mac_operation_t *operation,
4998 psa_algorithm_t hash_alg,
4999 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005001{
5002 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5005 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5006 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005007
Steven Cooreman72f736a2021-05-07 14:14:37 +02005008 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005010
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 status = psa_driver_wrapper_mac_sign_setup(operation,
5012 &attributes,
5013 hmac_key, hmac_key_length,
5014 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005015
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 psa_reset_key_attributes(&attributes);
5017 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005018}
5019#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005020
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005021#define HKDF_STATE_INIT 0 /* no input yet */
5022#define HKDF_STATE_STARTED 1 /* got salt */
5023#define HKDF_STATE_KEYED 2 /* got key */
5024#define HKDF_STATE_OUTPUT 3 /* output started */
5025
Gilles Peskinecbe66502019-05-16 16:59:18 +02005026static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005028{
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5030 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5031 } else {
5032 return operation->alg;
5033 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005034}
5035
Gilles Peskine449bd832023-01-11 14:50:10 +01005036psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005037{
5038 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5040 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005041 /* The object has (apparently) been initialized but it is not
5042 * in use. It's ok to call abort on such an object, and there's
5043 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005045#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5047 mbedtls_free(operation->ctx.hkdf.info);
5048 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5049 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005050#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005051#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5052 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5054 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5055 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5056 if (operation->ctx.tls12_prf.secret != NULL) {
5057 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5058 operation->ctx.tls12_prf.secret_length);
5059 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005060 }
5061
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 if (operation->ctx.tls12_prf.seed != NULL) {
5063 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5064 operation->ctx.tls12_prf.seed_length);
5065 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005066 }
5067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 if (operation->ctx.tls12_prf.label != NULL) {
5069 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5070 operation->ctx.tls12_prf.label_length);
5071 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005072 }
5073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (operation->ctx.tls12_prf.other_secret != NULL) {
5075 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5076 operation->ctx.tls12_prf.other_secret_length);
5077 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005078 }
5079
Steven Cooremana6df6042021-04-29 19:32:25 +02005080 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005081
5082 /* We leave the fields Ai and output_block to be erased safely by the
5083 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005085#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5086 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005087#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5089 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5090 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5091 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005092#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005093 {
5094 status = PSA_ERROR_BAD_STATE;
5095 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 mbedtls_platform_zeroize(operation, sizeof(*operation));
5097 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005098}
5099
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005100psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005102{
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005104 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005106 }
5107
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005110}
5111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5113 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005114{
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 if (operation->alg == 0) {
5116 return PSA_ERROR_BAD_STATE;
5117 }
5118 if (capacity > operation->capacity) {
5119 return PSA_ERROR_INVALID_ARGUMENT;
5120 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005123}
5124
Przemek Stekiel69c46792022-06-10 12:59:51 +02005125#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005126/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005127static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5128 psa_algorithm_t kdf_alg,
5129 uint8_t *output,
5130 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005131{
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5133 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005134 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005135 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005136#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005138#else
5139 const uint8_t last_block = 0xff;
5140#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 if (hkdf->state < HKDF_STATE_KEYED ||
5143 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005144#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005146#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 )) {
5148 return PSA_ERROR_BAD_STATE;
5149 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005150 hkdf->state = HKDF_STATE_OUTPUT;
5151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005153 /* Copy what remains of the current block */
5154 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005156 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 }
5158 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005159 output += n;
5160 output_length -= n;
5161 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005163 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005165 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005166 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005167 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005168 * object was corrupted or if this function is called directly
5169 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 if (hkdf->block_number == last_block) {
5171 return PSA_ERROR_BAD_STATE;
5172 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005173
5174 /* We need a new block */
5175 ++hkdf->block_number;
5176 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5179 hash_alg,
5180 hkdf->prk,
5181 hash_length);
5182 if (status != PSA_SUCCESS) {
5183 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005184 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005185
5186 if (hkdf->block_number != 1) {
5187 status = psa_mac_update(&hkdf->hmac,
5188 hkdf->output_block,
5189 hash_length);
5190 if (status != PSA_SUCCESS) {
5191 return status;
5192 }
5193 }
5194 status = psa_mac_update(&hkdf->hmac,
5195 hkdf->info,
5196 hkdf->info_length);
5197 if (status != PSA_SUCCESS) {
5198 return status;
5199 }
5200 status = psa_mac_update(&hkdf->hmac,
5201 &hkdf->block_number, 1);
5202 if (status != PSA_SUCCESS) {
5203 return status;
5204 }
5205 status = psa_mac_sign_finish(&hkdf->hmac,
5206 hkdf->output_block,
5207 sizeof(hkdf->output_block),
5208 &hmac_output_length);
5209 if (status != PSA_SUCCESS) {
5210 return status;
5211 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005212 }
5213
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005215}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005216#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005217
John Durkop07cc04a2020-11-16 22:08:34 -08005218#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5219 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005220static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5221 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005223{
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5225 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005226 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5227 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005228 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005229
Janos Follath7742fee2019-06-17 12:58:10 +01005230 /* We can't be wanting more output after block 0xff, otherwise
5231 * the capacity check in psa_key_derivation_output_bytes() would have
5232 * prevented this call. It could happen only if the operation
5233 * object was corrupted or if this function is called directly
5234 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 if (tls12_prf->block_number == 0xff) {
5236 return PSA_ERROR_CORRUPTION_DETECTED;
5237 }
Janos Follath7742fee2019-06-17 12:58:10 +01005238
5239 /* We need a new block */
5240 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005241 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005242
5243 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5244 *
5245 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5246 *
5247 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5248 * HMAC_hash(secret, A(2) + seed) +
5249 * HMAC_hash(secret, A(3) + seed) + ...
5250 *
5251 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005252 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005253 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005254 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005255 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005256 * is currently extracted as `output_block` and where i is
5257 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005258 */
5259
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 status = psa_key_derivation_start_hmac(&hmac,
5261 hash_alg,
5262 tls12_prf->secret,
5263 tls12_prf->secret_length);
5264 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005265 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005267
5268 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005270 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5271 * the variable seed and in this instance means it in the context of the
5272 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 status = psa_mac_update(&hmac,
5274 tls12_prf->label,
5275 tls12_prf->label_length);
5276 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005277 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 }
5279 status = psa_mac_update(&hmac,
5280 tls12_prf->seed,
5281 tls12_prf->seed_length);
5282 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005283 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 }
5285 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005286 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5288 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005289 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005291 }
5292
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 status = psa_mac_sign_finish(&hmac,
5294 tls12_prf->Ai, hash_length,
5295 &hmac_output_length);
5296 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005297 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 }
5299 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005300 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005302
5303 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 status = psa_key_derivation_start_hmac(&hmac,
5305 hash_alg,
5306 tls12_prf->secret,
5307 tls12_prf->secret_length);
5308 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005309 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 }
5311 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5312 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005313 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 }
5315 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5316 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005317 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 }
5319 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5320 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005321 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 }
5323 status = psa_mac_sign_finish(&hmac,
5324 tls12_prf->output_block, hash_length,
5325 &hmac_output_length);
5326 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005327 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005329
Janos Follath7742fee2019-06-17 12:58:10 +01005330
5331cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 cleanup_status = psa_mac_abort(&hmac);
5333 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005334 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005336
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005338}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005339
Janos Follath844eb0e2019-06-19 12:10:49 +01005340static psa_status_t psa_key_derivation_tls12_prf_read(
5341 psa_tls12_prf_key_derivation_t *tls12_prf,
5342 psa_algorithm_t alg,
5343 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005345{
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5347 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005348 psa_status_t status;
5349 uint8_t offset, length;
5350
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005352 case PSA_TLS12_PRF_STATE_LABEL_SET:
5353 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5354 break;
5355 case PSA_TLS12_PRF_STATE_OUTPUT:
5356 break;
5357 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005359 }
5360
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005362 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 if (tls12_prf->left_in_block == 0) {
5364 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5365 alg);
5366 if (status != PSA_SUCCESS) {
5367 return status;
5368 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005369
5370 continue;
5371 }
5372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005374 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005376 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005378
5379 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005381 output += length;
5382 output_length -= length;
5383 tls12_prf->left_in_block -= length;
5384 }
5385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005387}
John Durkop07cc04a2020-11-16 22:08:34 -08005388#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5389 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005390
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005391#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5392static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5393 psa_tls12_ecjpake_to_pms_t *ecjpake,
5394 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005396{
5397 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005398 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 if (output_length != 32) {
5401 return PSA_ERROR_INVALID_ARGUMENT;
5402 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5405 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5406 &output_size);
5407 if (status != PSA_SUCCESS) {
5408 return status;
5409 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005410
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 if (output_size != output_length) {
5412 return PSA_ERROR_GENERIC_ERROR;
5413 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005416}
5417#endif
5418
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005419psa_status_t psa_key_derivation_output_bytes(
5420 psa_key_derivation_operation_t *operation,
5421 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005423{
5424 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005426
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005428 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005430 }
5431
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005434 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005435 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005436 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005437 goto exit;
5438 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005440 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005441 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005442 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5443 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005444 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005445 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005447 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005448 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005449
Przemek Stekiel69c46792022-06-10 12:59:51 +02005450#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5452 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5453 output, output_length);
5454 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005455#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005456#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5457 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5459 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5460 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5461 kdf_alg, output,
5462 output_length);
5463 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005464#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5465 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005466#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005468 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5470 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005471#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5472
Gilles Peskineeab56e42018-07-12 17:12:33 +02005473 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005474 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005476 }
5477
5478exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005480 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005481 * This allows us to differentiate between exhausted operations and
5482 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5483 * operations. */
5484 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005486 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005488 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005490}
5491
David Brown7807bf72021-02-09 16:28:23 -07005492#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005493static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005494{
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 if (data_size >= 8) {
5496 mbedtls_des_key_set_parity(data);
5497 }
5498 if (data_size >= 16) {
5499 mbedtls_des_key_set_parity(data + 8);
5500 }
5501 if (data_size >= 24) {
5502 mbedtls_des_key_set_parity(data + 16);
5503 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005504}
David Brown7807bf72021-02-09 16:28:23 -07005505#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005506
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005507/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 * ECC keys on a Weierstrass elliptic curve require the generation
5509 * of a private key which is an integer
5510 * in the range [1, N - 1], where N is the boundary of the private key domain:
5511 * N is the prime p for Diffie-Hellman, or the order of the
5512 * curve’s base point for ECC.
5513 *
5514 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5515 * This function generates the private key using the following process:
5516 *
5517 * 1. Draw a byte string of length ceiling(m/8) bytes.
5518 * 2. If m is not a multiple of 8, set the most significant
5519 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5520 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5521 * 4. If k > N - 2, discard the result and return to step 1.
5522 * 5. Output k + 1 as the private key.
5523 *
5524 * This method allows compliance to NIST standards, specifically the methods titled
5525 * Key-Pair Generation by Testing Candidates in the following publications:
5526 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5527 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5528 * Diffie-Hellman keys.
5529 *
5530 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5531 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5532 *
5533 * Note: Function allocates memory for *data buffer, so given *data should be
5534 * always NULL.
5535 */
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005536#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5537 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5538 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5539 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5540 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005541static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005542 psa_key_slot_t *slot,
5543 size_t bits,
5544 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005545 uint8_t **data
5546 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005547{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005548 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005549 mbedtls_mpi k;
5550 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 mbedtls_mpi_init(&k);
5555 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005556
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005557 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005559 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005563 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005564 goto cleanup;
5565 }
5566
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005567 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005569
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005571
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005572 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005573 /* Let m be the bit size of N. */
5574 size_t m = ecp_group.nbits;
5575
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005577
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005578 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005580
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005581 /* Note: This function is always called with *data == NULL and it
5582 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 *data = mbedtls_calloc(1, m_bytes);
5584 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005585 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005586 goto cleanup;
5587 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005588
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005590 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005592 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005594
5595 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005597 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005598 * (8 * ceiling(m/8) - m) bits of the first byte in
5599 * the string to zero.
5600 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005602 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005603 }
5604
5605 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 * big-endian byte string.
5607 */
5608 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005609
5610 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 * Result of comparison is returned. When it indicates error
5612 * then this function is called again.
5613 */
5614 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005615 }
5616
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005617 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5619 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005620cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 if (ret != 0) {
5622 status = mbedtls_to_psa_error(ret);
5623 }
5624 if (status != PSA_SUCCESS) {
5625 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005626 *data = NULL;
5627 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 mbedtls_mpi_free(&k);
5629 mbedtls_mpi_free(&diff_N_2);
5630 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005631}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005632
5633/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5634 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5635 *
5636 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5637 * draw a 32-byte string and process it as specified in
5638 * Elliptic Curves for Security [RFC7748] §5.
5639 *
5640 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5641 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5642 *
5643 * Note: Function allocates memory for *data buffer, so given *data should be
5644 * always NULL.
5645 */
5646
5647static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5648 size_t bits,
5649 psa_key_derivation_operation_t *operation,
5650 uint8_t **data
5651 )
5652{
5653 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005654 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005655
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005657 case 255:
5658 output_length = 32;
5659 break;
5660 case 448:
5661 output_length = 56;
5662 break;
5663 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005665 break;
5666 }
5667
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005669
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 if (*data == NULL) {
5671 return PSA_ERROR_INSUFFICIENT_MEMORY;
5672 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005677 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005681 case 255:
5682 (*data)[0] &= 248;
5683 (*data)[31] &= 127;
5684 (*data)[31] |= 64;
5685 break;
5686 case 448:
5687 (*data)[0] &= 252;
5688 (*data)[55] |= 128;
5689 break;
5690 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005692 break;
5693 }
5694
5695 return status;
5696}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005697#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5698 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5699 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5700 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5701 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005702
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005703static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005704 psa_key_slot_t *slot,
5705 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005707{
5708 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305710 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005711 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5714 return PSA_ERROR_INVALID_ARGUMENT;
5715 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005716
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005717#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5718 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5719 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5720 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5721 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5723 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5724 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005725 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5727 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005728 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 }
5730 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005731 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5733 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005734 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005736 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005737 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005738#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5739 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5740 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5741 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5742 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 if (key_type_is_raw_bytes(slot->attr.type)) {
5744 if (bits % 8 != 0) {
5745 return PSA_ERROR_INVALID_ARGUMENT;
5746 }
5747 data = mbedtls_calloc(1, bytes);
5748 if (data == NULL) {
5749 return PSA_ERROR_INSUFFICIENT_MEMORY;
5750 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 status = psa_key_derivation_output_bytes(operation, data, bytes);
5753 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005754 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 }
David Brown12ca5032021-02-11 11:02:00 -07005756#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5758 psa_des_set_key_parity(data, bytes);
5759 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005760#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 } else {
5762 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005763 }
Ronald Crondd04d422020-11-28 15:14:42 +01005764
Ronald Cronbf33c932020-11-28 18:06:53 +01005765 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005766 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005768 };
5769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5771 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5772 &storage_size);
5773 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305774 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 }
Archanad8a83dc2021-06-14 10:04:16 +05305776 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 status = psa_allocate_buffer_to_slot(slot, storage_size);
5778 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305779 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 }
Archanad8a83dc2021-06-14 10:04:16 +05305781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 status = psa_driver_wrapper_import_key(&attributes,
5783 data, bytes,
5784 slot->key.data,
5785 slot->key.bytes,
5786 &slot->key.bytes, &bits);
5787 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005788 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005790
5791exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 mbedtls_free(data);
5793 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005794}
5795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5797 psa_key_derivation_operation_t *operation,
5798 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005799{
5800 psa_status_t status;
5801 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005802 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005803
Ronald Cron81709fc2020-11-14 12:10:32 +01005804 *key = MBEDTLS_SVC_KEY_ID_INIT;
5805
Gilles Peskine0f84d622019-09-12 19:03:13 +02005806 /* Reject any attempt to create a zero-length key so that we don't
5807 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 if (psa_get_key_bits(attributes) == 0) {
5809 return PSA_ERROR_INVALID_ARGUMENT;
5810 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005811
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 if (operation->alg == PSA_ALG_NONE) {
5813 return PSA_ERROR_BAD_STATE;
5814 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 if (!operation->can_output_key) {
5817 return PSA_ERROR_NOT_PERMITTED;
5818 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5821 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005822#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005824 /* Deriving a key in a secure element is not implemented yet. */
5825 status = PSA_ERROR_NOT_SUPPORTED;
5826 }
5827#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 if (status == PSA_SUCCESS) {
5829 status = psa_generate_derived_key_internal(slot,
5830 attributes->core.bits,
5831 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005832 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 if (status == PSA_SUCCESS) {
5834 status = psa_finish_key_creation(slot, driver, key);
5835 }
5836 if (status != PSA_SUCCESS) {
5837 psa_fail_key_creation(slot, driver);
5838 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005839
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005841}
5842
Gilles Peskineeab56e42018-07-12 17:12:33 +02005843
5844
5845/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005846/* Key derivation */
5847/****************************************************************/
5848
Steven Cooreman932ffb72021-02-15 12:14:32 +01005849#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005850static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005851{
5852#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5854 return 1;
5855 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005856#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005857#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5859 return 1;
5860 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005861#endif
5862#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5864 return 1;
5865 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005866#endif
5867#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5869 return 1;
5870 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005871#endif
5872#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5874 return 1;
5875 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005876#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005877#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5879 return 1;
5880 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005881#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005883}
5884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005886{
5887 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 psa_status_t status = psa_hash_setup(&operation, alg);
5889 psa_hash_abort(&operation);
5890 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005891}
5892
Gilles Peskine969c5d62019-01-16 15:53:06 +01005893static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005894 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005896{
Janos Follath5fe19732019-06-20 15:09:30 +01005897 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5898 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005900
Gilles Peskine969c5d62019-01-16 15:53:06 +01005901 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 if (!is_kdf_alg_supported(kdf_alg)) {
5903 return PSA_ERROR_NOT_SUPPORTED;
5904 }
John Durkop07cc04a2020-11-16 22:08:34 -08005905
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005906 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005907 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5909 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5910 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5911 if (hash_size == 0) {
5912 return PSA_ERROR_NOT_SUPPORTED;
5913 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005914
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005915 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5916 * we might fail later, which is somewhat unfriendly and potentially
5917 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 psa_status_t status = psa_hash_try_support(hash_alg);
5919 if (status != PSA_SUCCESS) {
5920 return status;
5921 }
5922 } else {
5923 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005924 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5927 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5928 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5929 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005930 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005931#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005932 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5934 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005935 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005937#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5938 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 operation->capacity = 255 * hash_size;
5940 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005941}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005944{
5945#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 if (alg == PSA_ALG_ECDH) {
5947 return PSA_SUCCESS;
5948 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005949#endif
5950 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005952}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005953
5954static int psa_key_derivation_allows_free_form_secret_input(
5955 psa_algorithm_t kdf_alg)
5956{
5957#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5958 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5959 return 0;
5960 }
5961#endif
5962 (void) kdf_alg;
5963 return 1;
5964}
John Durkop07cc04a2020-11-16 22:08:34 -08005965#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5968 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005969{
5970 psa_status_t status;
5971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 if (operation->alg != 0) {
5973 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005974 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5977 return PSA_ERROR_INVALID_ARGUMENT;
5978 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5979#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5980 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5981 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5982 status = psa_key_agreement_try_support(ka_alg);
5983 if (status != PSA_SUCCESS) {
5984 return status;
5985 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005986 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
5987 return PSA_ERROR_INVALID_ARGUMENT;
5988 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
5990#else
5991 return PSA_ERROR_NOT_SUPPORTED;
5992#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5993 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
5994#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5995 status = psa_key_derivation_setup_kdf(operation, alg);
5996#else
5997 return PSA_ERROR_NOT_SUPPORTED;
5998#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5999 } else {
6000 return PSA_ERROR_INVALID_ARGUMENT;
6001 }
6002
6003 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006004 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 }
6006 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006007}
6008
Przemek Stekiel69c46792022-06-10 12:59:51 +02006009#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006010static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6011 psa_algorithm_t kdf_alg,
6012 psa_key_derivation_step_t step,
6013 const uint8_t *data,
6014 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006015{
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006017 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006019 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006020#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6022 return PSA_ERROR_INVALID_ARGUMENT;
6023 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006024#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 if (hkdf->state != HKDF_STATE_INIT) {
6026 return PSA_ERROR_BAD_STATE;
6027 } else {
6028 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6029 hash_alg,
6030 data, data_length);
6031 if (status != PSA_SUCCESS) {
6032 return status;
6033 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006034 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006036 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006037 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006038#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006040 /* We shouldn't be in different state as HKDF_EXPAND only allows
6041 * two inputs: SECRET (this case) and INFO which does not modify
6042 * the state. It could happen only if the hkdf
6043 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 if (hkdf->state != HKDF_STATE_INIT) {
6045 return PSA_ERROR_BAD_STATE;
6046 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006047
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006048 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6050 return PSA_ERROR_INVALID_ARGUMENT;
6051 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 memcpy(hkdf->prk, data, data_length);
6054 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006055#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006056 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006057 /* HKDF: If no salt was provided, use an empty salt.
6058 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006060#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6062 return PSA_ERROR_BAD_STATE;
6063 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006064#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6066 hash_alg,
6067 NULL, 0);
6068 if (status != PSA_SUCCESS) {
6069 return status;
6070 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006071 hkdf->state = HKDF_STATE_STARTED;
6072 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 if (hkdf->state != HKDF_STATE_STARTED) {
6074 return PSA_ERROR_BAD_STATE;
6075 }
6076 status = psa_mac_update(&hkdf->hmac,
6077 data, data_length);
6078 if (status != PSA_SUCCESS) {
6079 return status;
6080 }
6081 status = psa_mac_sign_finish(&hkdf->hmac,
6082 hkdf->prk,
6083 sizeof(hkdf->prk),
6084 &data_length);
6085 if (status != PSA_SUCCESS) {
6086 return status;
6087 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006088 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006089
Gilles Peskine2b522db2019-04-12 15:11:49 +02006090 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006091 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006092#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006094 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006096 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006098#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006099 {
6100 /* Block 0 is empty, and the next block will be
6101 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006103 }
6104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006106 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006107#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6109 return PSA_ERROR_INVALID_ARGUMENT;
6110 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006111#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006112#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6114 hkdf->state == HKDF_STATE_INIT) {
6115 return PSA_ERROR_BAD_STATE;
6116 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006117#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 if (hkdf->state == HKDF_STATE_OUTPUT) {
6119 return PSA_ERROR_BAD_STATE;
6120 }
6121 if (hkdf->info_set) {
6122 return PSA_ERROR_BAD_STATE;
6123 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006124 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 if (data_length != 0) {
6126 hkdf->info = mbedtls_calloc(1, data_length);
6127 if (hkdf->info == NULL) {
6128 return PSA_ERROR_INSUFFICIENT_MEMORY;
6129 }
6130 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006131 }
6132 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006134 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006136 }
6137}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006138#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006139
John Durkop07cc04a2020-11-16 22:08:34 -08006140#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6141 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006142static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6143 const uint8_t *data,
6144 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006145{
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6147 return PSA_ERROR_BAD_STATE;
6148 }
Janos Follathf08e2652019-06-13 09:05:41 +01006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if (data_length != 0) {
6151 prf->seed = mbedtls_calloc(1, data_length);
6152 if (prf->seed == NULL) {
6153 return PSA_ERROR_INSUFFICIENT_MEMORY;
6154 }
Janos Follathf08e2652019-06-13 09:05:41 +01006155
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006157 prf->seed_length = data_length;
6158 }
Janos Follathf08e2652019-06-13 09:05:41 +01006159
Gilles Peskine43f958b2020-12-13 14:55:14 +01006160 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006163}
6164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6166 const uint8_t *data,
6167 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006168{
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6170 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6171 return PSA_ERROR_BAD_STATE;
6172 }
Janos Follath81550542019-06-13 14:26:34 +01006173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 if (data_length != 0) {
6175 prf->secret = mbedtls_calloc(1, data_length);
6176 if (prf->secret == NULL) {
6177 return PSA_ERROR_INSUFFICIENT_MEMORY;
6178 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006181 prf->secret_length = data_length;
6182 }
Janos Follath81550542019-06-13 14:26:34 +01006183
Gilles Peskine43f958b2020-12-13 14:55:14 +01006184 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006185
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006187}
6188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6190 const uint8_t *data,
6191 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006192{
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6194 return PSA_ERROR_BAD_STATE;
6195 }
Janos Follath63028dd2019-06-13 09:15:47 +01006196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 if (data_length != 0) {
6198 prf->label = mbedtls_calloc(1, data_length);
6199 if (prf->label == NULL) {
6200 return PSA_ERROR_INSUFFICIENT_MEMORY;
6201 }
Janos Follath63028dd2019-06-13 09:15:47 +01006202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006204 prf->label_length = data_length;
6205 }
Janos Follath63028dd2019-06-13 09:15:47 +01006206
Gilles Peskine43f958b2020-12-13 14:55:14 +01006207 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006208
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006210}
6211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6213 psa_key_derivation_step_t step,
6214 const uint8_t *data,
6215 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006216{
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006218 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006220 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006222 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006224 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006226 }
6227}
John Durkop07cc04a2020-11-16 22:08:34 -08006228#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6229 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6230
6231#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6232static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6233 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006234 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006236{
6237 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6239 4 + data_length + prf->other_secret_length :
6240 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6243 return PSA_ERROR_INVALID_ARGUMENT;
6244 }
John Durkop07cc04a2020-11-16 22:08:34 -08006245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 uint8_t *pms = mbedtls_calloc(1, pms_len);
6247 if (pms == NULL) {
6248 return PSA_ERROR_INSUFFICIENT_MEMORY;
6249 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006250 uint8_t *cur = pms;
6251
6252 /* pure-PSK:
6253 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006254 *
6255 * The premaster secret is formed as follows: if the PSK is N octets
6256 * long, concatenate a uint16 with the value N, N zero octets, a second
6257 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006258 *
6259 * mixed-PSK:
6260 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6261 * follows: concatenate a uint16 with the length of the other secret,
6262 * the other secret itself, uint16 with the length of PSK, and the
6263 * PSK itself.
6264 * For details please check:
6265 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6266 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6267 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006268 */
6269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6271 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6272 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6273 if (prf->other_secret_length != 0) {
6274 memcpy(cur, prf->other_secret, prf->other_secret_length);
6275 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006276 cur += prf->other_secret_length;
6277 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 } else {
6279 *cur++ = MBEDTLS_BYTE_1(data_length);
6280 *cur++ = MBEDTLS_BYTE_0(data_length);
6281 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006282 cur += data_length;
6283 }
6284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 *cur++ = MBEDTLS_BYTE_1(data_length);
6286 *cur++ = MBEDTLS_BYTE_0(data_length);
6287 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006288 cur += data_length;
6289
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 mbedtls_platform_zeroize(pms, pms_len);
6293 mbedtls_free(pms);
6294 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006295}
Janos Follath6660f0e2019-06-17 08:44:03 +01006296
Przemek Stekiele47201b2022-04-19 13:53:28 +02006297static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6298 psa_tls12_prf_key_derivation_t *prf,
6299 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006301{
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6303 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006304 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006305
6306 if (data_length != 0) {
6307 prf->other_secret = mbedtls_calloc(1, data_length);
6308 if (prf->other_secret == NULL) {
6309 return PSA_ERROR_INSUFFICIENT_MEMORY;
6310 }
6311
6312 memcpy(prf->other_secret, data, data_length);
6313 prf->other_secret_length = data_length;
6314 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006315 prf->other_secret_length = 0;
6316 }
6317
6318 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006321}
6322
Janos Follath6660f0e2019-06-17 08:44:03 +01006323static psa_status_t psa_tls12_prf_psk_to_ms_input(
6324 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006325 psa_key_derivation_step_t step,
6326 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006328{
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006330 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 return psa_tls12_prf_psk_to_ms_set_key(prf,
6332 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006333 break;
6334 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6336 data,
6337 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006338 break;
6339 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006341 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006342
Przemek Stekiele47201b2022-04-19 13:53:28 +02006343 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006344}
John Durkop07cc04a2020-11-16 22:08:34 -08006345#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006346
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006347#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6348static psa_status_t psa_tls12_ecjpake_to_pms_input(
6349 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006350 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006351 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006353{
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6355 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6356 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006357 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006358
6359 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 if (data[0] != 0x04) {
6361 return PSA_ERROR_INVALID_ARGUMENT;
6362 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006363
6364 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006368}
6369#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006370/** Check whether the given key type is acceptable for the given
6371 * input step of a key derivation.
6372 *
6373 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6374 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6375 * Both secret and non-secret inputs can alternatively have the type
6376 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6377 * that the input was passed as a buffer rather than via a key object.
6378 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006379static int psa_key_derivation_check_input_type(
6380 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006382{
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006384 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 if (key_type == PSA_KEY_TYPE_DERIVE) {
6386 return PSA_SUCCESS;
6387 }
6388 if (key_type == PSA_KEY_TYPE_NONE) {
6389 return PSA_SUCCESS;
6390 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006391 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006392 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 if (key_type == PSA_KEY_TYPE_DERIVE) {
6394 return PSA_SUCCESS;
6395 }
6396 if (key_type == PSA_KEY_TYPE_NONE) {
6397 return PSA_SUCCESS;
6398 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006399 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006400 case PSA_KEY_DERIVATION_INPUT_LABEL:
6401 case PSA_KEY_DERIVATION_INPUT_SALT:
6402 case PSA_KEY_DERIVATION_INPUT_INFO:
6403 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6405 return PSA_SUCCESS;
6406 }
6407 if (key_type == PSA_KEY_TYPE_NONE) {
6408 return PSA_SUCCESS;
6409 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006410 break;
6411 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006413}
6414
Janos Follathb80a94e2019-06-12 15:54:46 +01006415static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006416 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006417 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006418 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006419 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006421{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006422 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 status = psa_key_derivation_check_input_type(step, key_type);
6426 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006427 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006429
Przemek Stekiel69c46792022-06-10 12:59:51 +02006430#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6432 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6433 step, data, data_length);
6434 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006435#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006436#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6438 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6439 step, data, data_length);
6440 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006441#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6442#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6444 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6445 step, data, data_length);
6446 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006447#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006448#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006450 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6452 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006453#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006454 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006455 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006456 (void) data;
6457 (void) data_length;
6458 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006460 }
6461
Gilles Peskine224b0d62019-09-23 18:13:17 +02006462exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 if (status != PSA_SUCCESS) {
6464 psa_key_derivation_abort(operation);
6465 }
6466 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006467}
6468
Janos Follath51f4a0f2019-06-14 11:35:55 +01006469psa_status_t psa_key_derivation_input_bytes(
6470 psa_key_derivation_operation_t *operation,
6471 psa_key_derivation_step_t step,
6472 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006474{
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 return psa_key_derivation_input_internal(operation, step,
6476 PSA_KEY_TYPE_NONE,
6477 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006478}
6479
Janos Follath51f4a0f2019-06-14 11:35:55 +01006480psa_status_t psa_key_derivation_input_key(
6481 psa_key_derivation_operation_t *operation,
6482 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006484{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006485 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006486 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006487 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006488
Ronald Cron5c522922020-11-14 16:35:34 +01006489 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6491 if (status != PSA_SUCCESS) {
6492 psa_key_derivation_abort(operation);
6493 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006494 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006495
6496 /* Passing a key object as a SECRET input unlocks the permission
6497 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006499 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006501
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 status = psa_key_derivation_input_internal(operation,
6503 step, slot->attr.type,
6504 slot->key.data,
6505 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006506
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006510}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006511
6512
6513
6514/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006515/* Key agreement */
6516/****************************************************************/
6517
Gilles Peskine449bd832023-01-11 14:50:10 +01006518psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6519 const uint8_t *key_buffer,
6520 size_t key_buffer_size,
6521 psa_algorithm_t alg,
6522 const uint8_t *peer_key,
6523 size_t peer_key_length,
6524 uint8_t *shared_secret,
6525 size_t shared_secret_size,
6526 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006527{
Gilles Peskine449bd832023-01-11 14:50:10 +01006528 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006529#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006530 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6532 key_buffer_size, alg,
6533 peer_key, peer_key_length,
6534 shared_secret,
6535 shared_secret_size,
6536 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006537#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006538 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006539 (void) attributes;
6540 (void) key_buffer;
6541 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006542 (void) peer_key;
6543 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006544 (void) shared_secret;
6545 (void) shared_secret_size;
6546 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006548 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006549}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006550
Aditya Deshpande17845b82022-10-13 17:21:01 +01006551/** Internal function for raw key agreement
6552 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006553 * to the driver's implementation if a driver is present.
6554 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006555 * (psa_key_agreement_raw_builtin).
6556 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006557static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6558 psa_key_slot_t *private_key,
6559 const uint8_t *peer_key,
6560 size_t peer_key_length,
6561 uint8_t *shared_secret,
6562 size_t shared_secret_size,
6563 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006564{
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6566 return PSA_ERROR_NOT_SUPPORTED;
6567 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006568
6569 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006571 };
6572
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 return psa_driver_wrapper_key_agreement(&attributes,
6574 private_key->key.data,
6575 private_key->key.bytes, alg,
6576 peer_key, peer_key_length,
6577 shared_secret,
6578 shared_secret_size,
6579 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006580}
6581
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006582/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006583 * to potentially free embedded data structures and wipe confidential data.
6584 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006585static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6586 psa_key_derivation_step_t step,
6587 psa_key_slot_t *private_key,
6588 const uint8_t *peer_key,
6589 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006590{
6591 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006592 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006593 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006594 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006595
6596 /* Step 1: run the secret agreement algorithm to generate the shared
6597 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 status = psa_key_agreement_raw_internal(ka_alg,
6599 private_key,
6600 peer_key, peer_key_length,
6601 shared_secret,
6602 sizeof(shared_secret),
6603 &shared_secret_length);
6604 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006605 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006607
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006608 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006609 * the shared secret. A shared secret is permitted wherever a key
6610 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 status = psa_key_derivation_input_internal(operation, step,
6612 PSA_KEY_TYPE_DERIVE,
6613 shared_secret,
6614 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006615exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6617 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006618}
6619
Gilles Peskine449bd832023-01-11 14:50:10 +01006620psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6621 psa_key_derivation_step_t step,
6622 mbedtls_svc_key_id_t private_key,
6623 const uint8_t *peer_key,
6624 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006625{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006627 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006628 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006629
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6631 return PSA_ERROR_INVALID_ARGUMENT;
6632 }
Ronald Cron5c522922020-11-14 16:35:34 +01006633 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6635 if (status != PSA_SUCCESS) {
6636 return status;
6637 }
6638 status = psa_key_agreement_internal(operation, step,
6639 slot,
6640 peer_key, peer_key_length);
6641 if (status != PSA_SUCCESS) {
6642 psa_key_derivation_abort(operation);
6643 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006644 /* If a private key has been added as SECRET, we allow the derived
6645 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006647 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006649 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006650
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006652
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006654}
6655
Gilles Peskine449bd832023-01-11 14:50:10 +01006656psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6657 mbedtls_svc_key_id_t private_key,
6658 const uint8_t *peer_key,
6659 size_t peer_key_length,
6660 uint8_t *output,
6661 size_t output_size,
6662 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006663{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006665 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006666 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006669 status = PSA_ERROR_INVALID_ARGUMENT;
6670 goto exit;
6671 }
Ronald Cron5c522922020-11-14 16:35:34 +01006672 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6674 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006675 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006677
Gilles Peskine3e561302022-04-14 00:17:15 +02006678 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6679 * for the output size. The PSA specification only guarantees that this
6680 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6681 * but it might be nice to allow smaller buffers if the output fits.
6682 * At the time of writing this comment, with only ECDH implemented,
6683 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6684 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6685 * be exact for it as well. */
6686 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6688 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006689 status = PSA_ERROR_BUFFER_TOO_SMALL;
6690 goto exit;
6691 }
6692
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 status = psa_key_agreement_raw_internal(alg, slot,
6694 peer_key, peer_key_length,
6695 output, output_size,
6696 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006697
6698exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006700 /* If an error happens and is not handled properly, the output
6701 * may be used as a key to protect sensitive data. Arrange for such
6702 * a key to be random, which is likely to result in decryption or
6703 * verification errors. This is better than filling the buffer with
6704 * some constant data such as zeros, which would result in the data
6705 * being protected with a reproducible, easily knowable key.
6706 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006708 *output_length = output_size;
6709 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006710
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006712
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006714}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006715
6716
Gilles Peskine30524eb2020-11-13 17:02:26 +01006717
Gilles Peskine86a440b2018-11-12 18:39:40 +01006718/****************************************************************/
6719/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006720/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006721
Gilles Peskine30524eb2020-11-13 17:02:26 +01006722/** Initialize the PSA random generator.
6723 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006724static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006725{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006726#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006727 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006728#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6729
Gilles Peskine30524eb2020-11-13 17:02:26 +01006730 /* Set default configuration if
6731 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006733 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 }
6735 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006736 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006738
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006740#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6741 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6742 /* The PSA entropy injection feature depends on using NV seed as an entropy
6743 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 mbedtls_entropy_add_source(&rng->entropy,
6745 mbedtls_nv_seed_poll, NULL,
6746 MBEDTLS_ENTROPY_BLOCK_SIZE,
6747 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006748#endif
6749
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006751#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006752}
6753
6754/** Deinitialize the PSA random generator.
6755 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006756static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006757{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006758#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006759 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006760#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6762 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006763#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006764}
6765
6766/** Seed the PSA random generator.
6767 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006768static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006769{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006770#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6771 /* Do nothing: the external RNG seeds itself. */
6772 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006774#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006775 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6777 drbg_seed, sizeof(drbg_seed) - 1);
6778 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006779#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006780}
6781
Gilles Peskine449bd832023-01-11 14:50:10 +01006782psa_status_t psa_generate_random(uint8_t *output,
6783 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006784{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006785 GUARD_MODULE_INITIALIZED;
6786
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006787#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6788
6789 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6791 output, output_size,
6792 &output_length);
6793 if (status != PSA_SUCCESS) {
6794 return status;
6795 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006796 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006797 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 if (output_length != output_size) {
6799 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6800 }
6801 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006802
6803#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6804
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006806 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6808 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6809 output_size);
6810 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6811 output, request_size);
6812 if (ret != 0) {
6813 return mbedtls_to_psa_error(ret);
6814 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006815 output_size -= request_size;
6816 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006817 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006819#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006820}
6821
Gilles Peskine8814fc42020-12-14 15:33:44 +01006822/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006823 *
6824 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6825 * `psa_generate_random(...)`. The state parameter is ignored since the
6826 * PSA API doesn't support passing an explicit state.
6827 *
6828 * In the non-external case, psa_generate_random() calls an
6829 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6830 * and semantics as mbedtls_psa_get_random(). As an optimization,
6831 * instead of doing this back-and-forth between the PSA API and the
6832 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6833 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006834 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006835#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6836int mbedtls_psa_get_random(void *p_rng,
6837 unsigned char *output,
6838 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006839{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006840 /* This function takes a pointer to the RNG state because that's what
6841 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6842 * its own state internally and doesn't let the caller access that state.
6843 * So we just ignore the state parameter, and in practice we'll pass
6844 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006845 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 psa_status_t status = psa_generate_random(output, output_size);
6847 if (status == PSA_SUCCESS) {
6848 return 0;
6849 } else {
6850 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6851 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006852}
6853#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6854
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006855#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006856#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006857
Gilles Peskine449bd832023-01-11 14:50:10 +01006858psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6859 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006860{
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 if (global_data.initialized) {
6862 return PSA_ERROR_NOT_PERMITTED;
6863 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006864
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6866 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6867 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6868 return PSA_ERROR_INVALID_ARGUMENT;
6869 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006870
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006872}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006873#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006874
Ronald Cron3772afe2021-02-08 16:10:05 +01006875/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006876 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006877 * \param type The key type
6878 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006879 *
6880 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006881 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006882 * \retval #PSA_ERROR_INVALID_ARGUMENT
6883 * The size in bits of the key is not valid.
6884 * \retval #PSA_ERROR_NOT_SUPPORTED
6885 * The type and/or the size in bits of the key or the combination of
6886 * the two is not supported.
6887 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006888static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006890{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006891 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006892
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 if (key_type_is_raw_bytes(type)) {
6894 status = psa_validate_unstructured_key_bit_size(type, bits);
6895 if (status != PSA_SUCCESS) {
6896 return status;
6897 }
6898 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006899#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6901 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6902 return PSA_ERROR_NOT_SUPPORTED;
6903 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006904
Ronald Cron01b2aba2020-10-05 09:42:02 +02006905 /* Accept only byte-aligned keys, for the same reasons as
6906 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 if (bits % 8 != 0) {
6908 return PSA_ERROR_NOT_SUPPORTED;
6909 }
6910 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006911#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006912
Ronald Cron5c4d3862020-12-07 11:07:24 +01006913#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006915 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006916 return PSA_SUCCESS;
6917 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006918#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006919 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006921 }
6922
Gilles Peskine449bd832023-01-11 14:50:10 +01006923 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006924}
6925
Ronald Cron55ed0592020-10-05 10:30:40 +02006926psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006927 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006929{
6930 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006931 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006932
Gilles Peskine449bd832023-01-11 14:50:10 +01006933 if ((attributes->domain_parameters == NULL) &&
6934 (attributes->domain_parameters_size != 0)) {
6935 return PSA_ERROR_INVALID_ARGUMENT;
6936 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006937
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 if (key_type_is_raw_bytes(type)) {
6939 status = psa_generate_random(key_buffer, key_buffer_size);
6940 if (status != PSA_SUCCESS) {
6941 return status;
6942 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006943
David Brown12ca5032021-02-11 11:02:00 -07006944#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 if (type == PSA_KEY_TYPE_DES) {
6946 psa_des_set_key_parity(key_buffer, key_buffer_size);
6947 }
David Brown8107e312021-02-12 08:08:46 -07006948#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006950
Jaeden Amero424fa932021-05-14 08:34:32 +01006951#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6952 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6954 return mbedtls_psa_rsa_generate_key(attributes,
6955 key_buffer,
6956 key_buffer_size,
6957 key_buffer_length);
6958 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006959#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6960 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006961
John Durkop9814fa22020-11-04 12:28:15 -08006962#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6964 return mbedtls_psa_ecp_generate_key(attributes,
6965 key_buffer,
6966 key_buffer_size,
6967 key_buffer_length);
6968 } else
John Durkop9814fa22020-11-04 12:28:15 -08006969#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006970 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 (void) key_buffer_length;
6972 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006973 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006974
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006976}
Darryl Green0c6575a2018-11-07 16:05:30 +00006977
Gilles Peskine449bd832023-01-11 14:50:10 +01006978psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6979 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006980{
6981 psa_status_t status;
6982 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006983 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006984 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006985
Ronald Cron81709fc2020-11-14 12:10:32 +01006986 *key = MBEDTLS_SVC_KEY_ID_INIT;
6987
Gilles Peskine0f84d622019-09-12 19:03:13 +02006988 /* Reject any attempt to create a zero-length key so that we don't
6989 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 if (psa_get_key_bits(attributes) == 0) {
6991 return PSA_ERROR_INVALID_ARGUMENT;
6992 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006993
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006994 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006995 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
6996 return PSA_ERROR_INVALID_ARGUMENT;
6997 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006998
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7000 &slot, &driver);
7001 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007002 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 }
Gilles Peskine11792082019-08-06 18:36:36 +02007004
Ronald Cron977c2472020-10-13 08:32:21 +02007005 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307006 * storage ( thus not in the case of generating a key in a secure element
7007 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7008 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 if (slot->key.data == NULL) {
7010 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7011 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007012 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 attributes->core.type, attributes->core.bits);
7014 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007015 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007017
7018 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 attributes->core.type,
7020 attributes->core.bits);
7021 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007022 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 attributes, &key_buffer_size);
7024 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007025 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 }
Ronald Cron977c2472020-10-13 08:32:21 +02007027 }
Ronald Cron977c2472020-10-13 08:32:21 +02007028
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7030 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007031 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007032 }
Ronald Cron977c2472020-10-13 08:32:21 +02007033 }
7034
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 status = psa_driver_wrapper_generate_key(attributes,
7036 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007037
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 if (status != PSA_SUCCESS) {
7039 psa_remove_key_data_from_memory(slot);
7040 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007041
Gilles Peskine11792082019-08-06 18:36:36 +02007042exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 if (status == PSA_SUCCESS) {
7044 status = psa_finish_key_creation(slot, driver, key);
7045 }
7046 if (status != PSA_SUCCESS) {
7047 psa_fail_key_creation(slot, driver);
7048 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007049
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007051}
7052
Gilles Peskinee59236f2018-01-27 23:32:46 +01007053/****************************************************************/
7054/* Module setup */
7055/****************************************************************/
7056
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007057#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007058psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 void (* entropy_init)(mbedtls_entropy_context *ctx),
7060 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007061{
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7063 return PSA_ERROR_BAD_STATE;
7064 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007065 global_data.rng.entropy_init = entropy_init;
7066 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007068}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007069#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007070
Gilles Peskine449bd832023-01-11 14:50:10 +01007071void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007072{
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 psa_wipe_all_key_slots();
7074 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7075 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007076 }
7077 /* Wipe all remaining data, including configuration.
7078 * In particular, this sets all state indicator to the value
7079 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007081
7082 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007084}
7085
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007086#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7087/** Recover a transaction that was interrupted by a power failure.
7088 *
7089 * This function is called during initialization, before psa_crypto_init()
7090 * returns. If this function returns a failure status, the initialization
7091 * fails.
7092 */
7093static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007095{
Gilles Peskine449bd832023-01-11 14:50:10 +01007096 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007097 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7098 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 /* TODO - fall through to the failure case until this
7100 * is implemented.
7101 * https://github.com/ARMmbed/mbed-crypto/issues/218
7102 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007103 default:
7104 /* We found an unsupported transaction in the storage.
7105 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007107 }
7108}
7109#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7110
Gilles Peskine449bd832023-01-11 14:50:10 +01007111psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007112{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007113 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007114
Gilles Peskinec6b69072018-11-20 21:42:52 +01007115 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 if (global_data.initialized != 0) {
7117 return PSA_SUCCESS;
7118 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007119
Gilles Peskine30524eb2020-11-13 17:02:26 +01007120 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007122 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 status = mbedtls_psa_random_seed(&global_data.rng);
7124 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007127 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007128
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 status = psa_initialize_key_slots();
7130 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007131 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007133
Ronald Cron9ba76912021-04-10 16:57:30 +02007134 /* Init drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 status = psa_driver_wrapper_init();
7136 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02007137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 }
Gilles Peskined9348f22019-10-01 15:22:29 +02007139
Gilles Peskine4b734222019-07-24 15:56:31 +02007140#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 status = psa_crypto_load_transaction();
7142 if (status == PSA_SUCCESS) {
7143 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7144 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 }
7147 status = psa_crypto_stop_transaction();
7148 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007149 /* There's no transaction to complete. It's all good. */
7150 status = PSA_SUCCESS;
7151 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007152#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007153
Gilles Peskinec6b69072018-11-20 21:42:52 +01007154 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007155 global_data.initialized = 1;
7156
7157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 if (status != PSA_SUCCESS) {
7159 mbedtls_psa_crypto_free();
7160 }
7161 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007162}
7163
7164#endif /* MBEDTLS_PSA_CRYPTO_C */