blob: 1a2ef59205d910fef6bb6442ad21e981b3e440bf [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"
Gilles Peskinea5905292018-02-07 20:59:33 +010084
Gilles Peskine449bd832023-01-11 14:50:10 +010085#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020086
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020090#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020091#endif
92
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010093/****************************************************************/
94/* Global data, support functions and library management */
95/****************************************************************/
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100}
101
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100102/* Values for psa_global_data_t::rng_state */
103#define RNG_NOT_INITIALIZED 0
104#define RNG_INITIALIZED 1
105#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109 unsigned rng_state : 2;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100110 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100111} psa_global_data_t;
112
113static psa_global_data_t global_data;
114
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100115#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
116mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
117 &global_data.rng.drbg;
118#endif
119
itayzafrir0adf0fc2018-09-06 16:24:41 +0300120#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (global_data.initialized == 0) \
122 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100125{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100126 /* Mbed TLS error codes can combine a high-level error code and a
127 * low-level error code. The low-level error usually reflects the
128 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 int low_level_ret = -(-ret & 0x007f);
130 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100133
134 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
135 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200137 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
138 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
139 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
140 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
141 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200143 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200145 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200147
Jaeden Amero93e21112019-02-20 13:57:28 +0000148#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000149 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000150#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100151 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100153
154 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100156 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100158
Gilles Peskine26869f22019-05-06 15:25:00 +0200159 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200161
162 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200164 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200166
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100179 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
183 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100184 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
185 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
189 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100193#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100194
195 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197
Gilles Peskinee59236f2018-01-27 23:32:46 +0100198 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
199 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
200 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202
203 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200205 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100209
Gilles Peskine82e57d12020-11-13 21:31:17 +0100210#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100212 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
213 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100214 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100216 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100219 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100221#endif
222
Gilles Peskinea5905292018-02-07 20:59:33 +0100223 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
Gilles Peskinef76aa772018-10-29 19:24:33 +0100232 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100234 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100236 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100238 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100240 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100242 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100244 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100246 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100248
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100249 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100251 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
252 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100254 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100256 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
257 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100259 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
262 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100264 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100266 case MBEDTLS_ERR_PK_INVALID_ALG:
267 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
268 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200272 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100274
Gilles Peskineff2d2002019-05-06 15:26:23 +0200275 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200277 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200279
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100282 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
289 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100291 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200297
itayzafrir5c753392018-05-08 11:18:38 +0300298 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300299 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300301 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300303 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300305 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
306 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300308 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100310 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100312
Janos Follatha13b9052019-11-22 12:48:59 +0000313 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300315
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100318 }
319}
320
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200321
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200322
323
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324/****************************************************************/
325/* Key management */
326/****************************************************************/
327
John Durkop9814fa22020-11-04 12:28:15 -0800328#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800329 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100330 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
331 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
332 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100333mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
334 size_t bits,
335 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200336{
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100338 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100340#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100341 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100343#endif
344#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100345 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100347#endif
348#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100349 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100351#endif
352#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100353 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100355#endif
356#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100357 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100359 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (bits_is_sloppy) {
361 return MBEDTLS_ECP_DP_SECP521R1;
362 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100363 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100364#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100365 }
366 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100367
Paul Elliott8ff510a2020-06-02 17:19:28 +0100368 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100370#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100371 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100373#endif
374#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100375 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100377#endif
378#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100379 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100381#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100382 }
383 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100384
Paul Elliott8ff510a2020-06-02 17:19:28 +0100385 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100387#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100388 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (bits_is_sloppy) {
392 return MBEDTLS_ECP_DP_CURVE25519;
393 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100395#endif
396#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100397 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100399#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100400 }
401 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100402
Paul Elliott8ff510a2020-06-02 17:19:28 +0100403 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100406 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100408#endif
409#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100410 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100412#endif
413#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100414 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100416#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100417 }
418 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200419 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100421 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200423}
John Durkop9814fa22020-11-04 12:28:15 -0800424#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100425 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
426 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
427 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
428 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
431 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200432{
433 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200435 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200437 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200438 case PSA_KEY_TYPE_PASSWORD:
439 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200440 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100441#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200442 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (bits != 128 && bits != 192 && bits != 256) {
444 return PSA_ERROR_INVALID_ARGUMENT;
445 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446 break;
447#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200448#if defined(PSA_WANT_KEY_TYPE_ARIA)
449 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (bits != 128 && bits != 192 && bits != 256) {
451 return PSA_ERROR_INVALID_ARGUMENT;
452 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200453 break;
454#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100455#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200456 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (bits != 128 && bits != 192 && bits != 256) {
458 return PSA_ERROR_INVALID_ARGUMENT;
459 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460 break;
461#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100462#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200463 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (bits != 64 && bits != 128 && bits != 192) {
465 return PSA_ERROR_INVALID_ARGUMENT;
466 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200467 break;
468#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100469#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200470 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (bits != 256) {
472 return PSA_ERROR_INVALID_ARGUMENT;
473 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200474 break;
475#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200476 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200478 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (bits % 8 != 0) {
480 return PSA_ERROR_INVALID_ARGUMENT;
481 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484}
485
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100486/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100487 *
Steven Cooremancd640932021-03-08 12:00:27 +0100488 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
489 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100490 *
491 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100492 * \param[in] key_type The key type of the key to be used with the
493 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100494 *
495 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100496 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100497 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100498 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100499 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100500MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100501 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100503{
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (PSA_ALG_IS_HMAC(algorithm)) {
505 if (key_type == PSA_KEY_TYPE_HMAC) {
506 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100507 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100508 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
511 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
512 * key. */
513 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
514 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
515 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
516 * the block length (larger than 1) for block ciphers. */
517 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
518 return PSA_SUCCESS;
519 }
520 }
521 }
522
523 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100524}
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
527 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200528{
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (slot->key.data != NULL) {
530 return PSA_ERROR_ALREADY_EXISTS;
531 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 slot->key.data = mbedtls_calloc(1, buffer_length);
534 if (slot->key.data == NULL) {
535 return PSA_ERROR_INSUFFICIENT_MEMORY;
536 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200537
Ronald Cronea0f8a62020-11-25 17:52:23 +0100538 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200540}
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
543 const uint8_t *data,
544 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200545{
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 psa_status_t status = psa_allocate_buffer_to_slot(slot,
547 data_length);
548 if (status != PSA_SUCCESS) {
549 return status;
550 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 memcpy(slot->key.data, data, data_length);
553 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200554}
555
Ronald Crona0fe59f2020-11-29 11:14:53 +0100556psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100557 const psa_key_attributes_t *attributes,
558 const uint8_t *data, size_t data_length,
559 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100561{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
563 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200565 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (data_length == 0) {
567 return PSA_ERROR_NOT_SUPPORTED;
568 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (key_type_is_raw_bytes(type)) {
571 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
574 *bits);
575 if (status != PSA_SUCCESS) {
576 return status;
577 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200578
Ronald Crondd04d422020-11-28 15:14:42 +0100579 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100581 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return PSA_SUCCESS;
585 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800586#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
588 if (PSA_KEY_TYPE_IS_ECC(type)) {
589 return mbedtls_psa_ecp_import_key(attributes,
590 data, data_length,
591 key_buffer, key_buffer_size,
592 key_buffer_length,
593 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100594 }
John Durkop9814fa22020-11-04 12:28:15 -0800595#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
596 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700597#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
599 if (PSA_KEY_TYPE_IS_RSA(type)) {
600 return mbedtls_psa_rsa_import_key(attributes,
601 data, data_length,
602 key_buffer, key_buffer_size,
603 key_buffer_length,
604 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200605 }
John Durkop9814fa22020-11-04 12:28:15 -0800606#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
607 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100608 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100611}
612
Gilles Peskinef603c712019-01-19 13:40:11 +0100613/** Calculate the intersection of two algorithm usage policies.
614 *
615 * Return 0 (which allows no operation) on incompatibility.
616 */
617static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100618 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100619 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100621{
Gilles Peskine549ea862019-05-22 11:45:59 +0200622 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if (alg1 == alg2) {
624 return alg1;
625 }
Steven Cooreman03488022021-02-18 12:07:20 +0100626 /* If the policies are from the same hash-and-sign family, check
627 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
629 PSA_ALG_IS_SIGN_HASH(alg2) &&
630 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
631 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
632 return alg2;
633 }
634 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
635 return alg1;
636 }
Steven Cooreman03488022021-02-18 12:07:20 +0100637 }
638 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100639 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100640 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
642 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
643 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
644 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
645 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100646 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100647
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100648 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
650 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
651 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
652 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100653 }
654 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
656 (alg1_len <= alg2_len)) {
657 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100658 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
660 (alg2_len <= alg1_len)) {
661 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100662 }
663 }
664 /* If the policies are from the same MAC family, check whether one
665 * of them is a minimum-MAC-length policy. Calculate the most
666 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
668 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
669 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100670 /* Validate the combination of key type and algorithm. Since the base
671 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
673 return 0;
674 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100675
Steven Cooreman31a876d2021-03-03 20:47:40 +0100676 /* Get the (exact or at-least) output lengths for both sides of the
677 * requested intersection. None of the currently supported algorithms
678 * have an output length dependent on the actual key size, so setting it
679 * to a bogus value of 0 is currently OK.
680 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100681 * Note that for at-least-this-length wildcard algorithms, the output
682 * length is set to the shortest allowed length, which allows us to
683 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
685 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100686 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100687
Steven Cooremana1d83222021-02-25 10:20:29 +0100688 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
690 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
691 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100692 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100693
694 /* If only one is an at-least-this-length policy, the intersection would
695 * be the other (fixed-length) policy as long as said fixed length is
696 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
698 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100699 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
701 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100702 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100703
Steven Cooremancd640932021-03-08 12:00:27 +0100704 /* If none of them are wildcards, check whether they define the same tag
705 * length. This is still possible here when one is default-length and
706 * the other specific-length. Ensure to always return the
707 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (alg1_len == alg2_len) {
709 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
710 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100711 }
712 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100714}
715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716static int psa_key_algorithm_permits(psa_key_type_t key_type,
717 psa_algorithm_t policy_alg,
718 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200719{
Gilles Peskine549ea862019-05-22 11:45:59 +0200720 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (requested_alg == policy_alg) {
722 return 1;
723 }
Steven Cooreman03488022021-02-18 12:07:20 +0100724 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
725 * and requested_alg is the same hash-and-sign family with any hash,
726 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
728 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
729 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
730 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100731 }
732 /* If policy_alg is a wildcard AEAD algorithm of the same base as
733 * the requested algorithm, check the requested tag length to be
734 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (PSA_ALG_IS_AEAD(policy_alg) &&
736 PSA_ALG_IS_AEAD(requested_alg) &&
737 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
738 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
739 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
740 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
741 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100742 }
Steven Cooremancd640932021-03-08 12:00:27 +0100743 /* If policy_alg is a MAC algorithm of the same base as the requested
744 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (PSA_ALG_IS_MAC(policy_alg) &&
746 PSA_ALG_IS_MAC(requested_alg) &&
747 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
748 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100749 /* Validate the combination of key type and algorithm. Since the policy
750 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
752 return 0;
753 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100754
Steven Cooreman31a876d2021-03-03 20:47:40 +0100755 /* Get both the requested output length for the algorithm which is to be
756 * verified, and the default output length for the base algorithm.
757 * Note that none of the currently supported algorithms have an output
758 * length dependent on actual key size, so setting it to a bogus value
759 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100760 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100762 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 key_type, 0,
764 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100765
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100766 /* If the policy is default-length, only allow an algorithm with
767 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
769 return requested_output_length == default_output_length;
770 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100771
772 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100773 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
775 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
776 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100777 }
778
Steven Cooremancd640932021-03-08 12:00:27 +0100779 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
780 * check for the requested MAC length to be equal to or longer than the
781 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
783 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
784 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100785 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200786 }
Steven Cooremance48e852020-10-05 16:02:45 +0200787 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200788 * a key derivation with that key agreement should also be allowed. This
789 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
791 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
792 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
793 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200794 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100795 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200797}
798
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100799/** Test whether a policy permits an algorithm.
800 *
801 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100802 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100803 * \note This function requires providing the key type for which the policy is
804 * being validated, since some algorithm policy definitions (e.g. MAC)
805 * have different properties depending on what kind of cipher it is
806 * combined with.
807 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100808 * \retval PSA_SUCCESS When \p alg is a specific algorithm
809 * allowed by the \p policy.
810 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
811 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
812 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100813 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
815 psa_key_type_t key_type,
816 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100817{
Steven Cooremand788fab2021-02-25 11:29:17 +0100818 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (alg == 0) {
820 return PSA_ERROR_INVALID_ARGUMENT;
821 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100822
Steven Cooreman7e39f052021-02-23 14:18:32 +0100823 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if (PSA_ALG_IS_WILDCARD(alg)) {
825 return PSA_ERROR_INVALID_ARGUMENT;
826 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
829 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
830 return PSA_SUCCESS;
831 } else {
832 return PSA_ERROR_NOT_PERMITTED;
833 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100834}
835
Gilles Peskinef603c712019-01-19 13:40:11 +0100836/** Restrict a key policy based on a constraint.
837 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100838 * \note This function requires providing the key type for which the policy is
839 * being restricted, since some algorithm policy definitions (e.g. MAC)
840 * have different properties depending on what kind of cipher it is
841 * combined with.
842 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100843 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100844 * \param[in,out] policy The policy to restrict.
845 * \param[in] constraint The policy constraint to apply.
846 *
847 * \retval #PSA_SUCCESS
848 * \c *policy contains the intersection of the original value of
849 * \c *policy and \c *constraint.
850 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100851 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100852 * \c *policy is unchanged.
853 */
854static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100855 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100856 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100858{
859 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 psa_key_policy_algorithm_intersection(key_type, policy->alg,
861 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200862 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
864 constraint->alg2);
865 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
866 return PSA_ERROR_INVALID_ARGUMENT;
867 }
868 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
869 return PSA_ERROR_INVALID_ARGUMENT;
870 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100871 policy->usage &= constraint->usage;
872 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200873 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100875}
876
Przemek Stekiel348410f2022-11-15 22:22:07 +0100877psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100878 mbedtls_svc_key_id_t key,
879 psa_key_slot_t **p_slot,
880 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100882{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200883 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
884 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 status = psa_get_and_lock_key_slot(key, p_slot);
887 if (status != PSA_SUCCESS) {
888 return status;
889 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200890 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100891
892 /* Enforce that usage policy for the key slot contains all the flags
893 * required by the usage parameter. There is one exception: public
894 * keys can always be exported, so we treat public key objects as
895 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100897 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100901 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200902 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100903 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100904
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800905 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (alg != 0) {
907 status = psa_key_policy_permits(&slot->attr.policy,
908 slot->attr.type,
909 alg);
910 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100911 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100913 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200916
917error:
918 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100922}
Darryl Green940d72c2018-07-13 13:18:51 +0100923
Ronald Cron5c522922020-11-14 16:35:34 +0100924/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200925 *
926 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200927 * available, as opposed to a key in a secure element and/or to be used
928 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200929 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200930 * This is a temporary function that may be used instead of
931 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
932 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200933 *
Ronald Cron5c522922020-11-14 16:35:34 +0100934 * On success, the returned key slot is locked. It is the responsibility of the
935 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200936 */
Ronald Cron5c522922020-11-14 16:35:34 +0100937static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
938 mbedtls_svc_key_id_t key,
939 psa_key_slot_t **p_slot,
940 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200942{
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
944 usage, alg);
945 if (status != PSA_SUCCESS) {
946 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200947 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
950 psa_unlock_key_slot(*p_slot);
951 *p_slot = NULL;
952 return PSA_ERROR_NOT_SUPPORTED;
953 }
954
955 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200956}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +0000959{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100960 /* Data pointer will always be either a valid pointer or NULL in an
961 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (slot->key.data != NULL) {
963 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
964 }
Ronald Cronea0f8a62020-11-25 17:52:23 +0100965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +0100967 slot->key.data = NULL;
968 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +0000971}
972
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100973/** Completely wipe a slot in memory, including its policy.
974 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100976{
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +0100978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 /*
980 * As the return error code may not be handled in case of multiple errors,
981 * do our best to report an unexpected lock counter. Assert with
982 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
983 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
984 * function is called as part of the execution of a test suite, the
985 * execution of the test suite is stopped in error if the assertion fails.
986 */
987 if (slot->lock_count != 1) {
988 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +0100989 status = PSA_ERROR_CORRUPTION_DETECTED;
990 }
991
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200992 /* Multipart operations may still be using the key. This is safe
993 * because all multipart operation objects are independent from
994 * the key slot: if they need to access the key after the setup
995 * phase, they have a copy of the key. Note that this means that
996 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100997 /* At this point, key material and other type-specific content has
998 * been wiped. Clear remaining metadata. We can call memset and not
999 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 memset(slot, 0, sizeof(*slot));
1001 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001002}
1003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001006 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001007 psa_status_t status; /* status of the last operation */
1008 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001009#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1010 psa_se_drv_table_entry_t *driver;
1011#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if (mbedtls_svc_key_id_is_null(key)) {
1014 return PSA_SUCCESS;
1015 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001016
Ronald Cronf2911112020-10-29 17:51:10 +01001017 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001018 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001019 * key, this will load the key description from persistent memory if not
1020 * done yet. We cannot avoid this loading as without it we don't know if
1021 * the key is operated by an SE or not and this information is needed by
1022 * the current implementation.
1023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 status = psa_get_and_lock_key_slot(key, &slot);
1025 if (status != PSA_SUCCESS) {
1026 return status;
1027 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001028
Ronald Cronf2911112020-10-29 17:51:10 +01001029 /*
1030 * If the key slot containing the key description is under access by the
1031 * library (apart from the present access), the key cannot be destroyed
1032 * yet. For the time being, just return in error. Eventually (to be
1033 * implemented), the key should be destroyed when all accesses have
1034 * stopped.
1035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if (slot->lock_count > 1) {
1037 psa_unlock_key_slot(slot);
1038 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001039 }
1040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001042 /* Refuse the destruction of a read-only key (which may or may not work
1043 * if we attempt it, depending on whether the key is merely read-only
1044 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001045 * Just do the best we can, which is to wipe the copy in memory
1046 * (done in this function's cleanup code). */
1047 overall_status = PSA_ERROR_NOT_PERMITTED;
1048 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001049 }
1050
Gilles Peskine354f7672019-07-12 23:46:38 +02001051#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1053 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001054 /* For a key in a secure element, we need to do three things:
1055 * remove the key file in internal storage, destroy the
1056 * key inside the secure element, and update the driver's
1057 * persistent data. Start a transaction that will encompass these
1058 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001060 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001062 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 status = psa_crypto_save_transaction();
1064 if (status != PSA_SUCCESS) {
1065 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001066 /* We should still try to destroy the key in the secure
1067 * element and the key metadata in storage. This is especially
1068 * important if the error is that the storage is full.
1069 * But how to do it exactly without risking an inconsistent
1070 * state after a reset?
1071 * https://github.com/ARMmbed/mbed-crypto/issues/215
1072 */
1073 overall_status = status;
1074 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001075 }
1076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 status = psa_destroy_se_key(driver,
1078 psa_key_slot_get_slot_number(slot));
1079 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001080 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001082 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001083#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1084
Darryl Greend49a4992018-06-18 17:27:26 +01001085#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1087 status = psa_destroy_persistent_key(slot->attr.id);
1088 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001089 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001091
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001092 /* TODO: other slots may have a copy of the same key. We should
1093 * invalidate them.
1094 * https://github.com/ARMmbed/mbed-crypto/issues/214
1095 */
Darryl Greend49a4992018-06-18 17:27:26 +01001096 }
1097#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001098
Gilles Peskinefc762652019-07-22 19:30:34 +02001099#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if (driver != NULL) {
1101 status = psa_save_se_persistent_data(driver);
1102 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001103 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 }
1105 status = psa_crypto_stop_transaction();
1106 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001107 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001109 }
1110#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1111
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001112exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001114 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001116 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 }
1118 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001119}
1120
John Durkop07cc04a2020-11-16 22:08:34 -08001121#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001122 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001123static psa_status_t psa_get_rsa_public_exponent(
1124 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001126{
1127 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001129 uint8_t *buffer = NULL;
1130 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1134 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 }
1137 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001138 /* It's the default value, which is reported as an empty string,
1139 * so there's nothing to do. */
1140 goto exit;
1141 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 buflen = mbedtls_mpi_size(&mpi);
1144 buffer = mbedtls_calloc(1, buflen);
1145 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001146 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1147 goto exit;
1148 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1150 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001153 attributes->domain_parameters = buffer;
1154 attributes->domain_parameters_size = buflen;
1155
1156exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 mbedtls_mpi_free(&mpi);
1158 if (ret != 0) {
1159 mbedtls_free(buffer);
1160 }
1161 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001162}
John Durkop07cc04a2020-11-16 22:08:34 -08001163#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001164 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001165
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001166/** Retrieve all the publicly-accessible attributes of a key.
1167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1169 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001170{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001172 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001173 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1178 if (status != PSA_SUCCESS) {
1179 return status;
1180 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001181
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001182 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1184 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001185
1186#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1188 psa_set_key_slot_number(attributes,
1189 psa_key_slot_get_slot_number(slot));
1190 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001191#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001194#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001195 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001196 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001197 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001198 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001199 * is not yet implemented.
1200 * https://github.com/ARMmbed/mbed-crypto/issues/216
1201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001203 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001204
Ronald Cron90857082020-11-25 14:58:33 +01001205 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 slot->attr.type,
1207 slot->key.data,
1208 slot->key.bytes,
1209 &rsa);
1210 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001211 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 status = psa_get_rsa_public_exponent(rsa,
1215 attributes);
1216 mbedtls_rsa_free(rsa);
1217 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001218 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001219 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001220#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001221 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001222 default:
1223 /* Nothing else to do. */
1224 break;
1225 }
1226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (status != PSA_SUCCESS) {
1228 psa_reset_key_attributes(attributes);
1229 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001234}
1235
Gilles Peskinec8000c02019-08-02 20:15:51 +02001236#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1237psa_status_t psa_get_key_slot_number(
1238 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001240{
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001242 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 return PSA_SUCCESS;
1244 } else {
1245 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001246 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001247}
1248#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1251 size_t key_buffer_size,
1252 uint8_t *data,
1253 size_t data_size,
1254 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001255{
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (key_buffer_size > data_size) {
1257 return PSA_ERROR_BUFFER_TOO_SMALL;
1258 }
1259 memcpy(data, key_buffer, key_buffer_size);
1260 memset(data + key_buffer_size, 0,
1261 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001262 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001264}
1265
Ronald Cron7285cda2020-11-26 14:46:37 +01001266psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001267 const psa_key_attributes_t *attributes,
1268 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001270{
Ronald Crond18b5f82020-11-25 16:46:05 +01001271 psa_key_type_t type = attributes->core.type;
1272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (key_type_is_raw_bytes(type) ||
1274 PSA_KEY_TYPE_IS_RSA(type) ||
1275 PSA_KEY_TYPE_IS_ECC(type)) {
1276 return psa_export_key_buffer_internal(
1277 key_buffer, key_buffer_size,
1278 data, data_size, data_length);
1279 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001280 /* This shouldn't happen in the reference implementation, but
1281 it is valid for a special-purpose implementation to omit
1282 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001284 }
1285}
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1288 uint8_t *data,
1289 size_t data_size,
1290 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001291{
1292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1293 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1294 psa_key_slot_t *slot;
1295
Ronald Crone907e552021-01-18 13:22:38 +01001296 /* Reject a zero-length output buffer now, since this can never be a
1297 * valid key representation. This way we know that data must be a valid
1298 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (data_size == 0) {
1300 return PSA_ERROR_BUFFER_TOO_SMALL;
1301 }
Ronald Crone907e552021-01-18 13:22:38 +01001302
Ronald Cron9486f9d2020-11-26 13:36:23 +01001303 /* Set the key to empty now, so that even when there are errors, we always
1304 * set data_length to a value between 0 and data_size. On error, setting
1305 * the key to empty is a good choice because an empty key representation is
1306 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001307 *data_length = 0;
1308
Ronald Cron9486f9d2020-11-26 13:36:23 +01001309 /* Export requires the EXPORT flag. There is an exception for public keys,
1310 * which don't require any flag, but
1311 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1314 PSA_KEY_USAGE_EXPORT, 0);
1315 if (status != PSA_SUCCESS) {
1316 return status;
1317 }
mohammad160306e79202018-03-28 13:17:44 +03001318
Ronald Crond18b5f82020-11-25 16:46:05 +01001319 psa_key_attributes_t attributes = {
1320 .core = slot->attr
1321 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 status = psa_driver_wrapper_export_key(&attributes,
1323 slot->key.data, slot->key.bytes,
1324 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001329}
1330
Ronald Cron7285cda2020-11-26 14:46:37 +01001331psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001332 const psa_key_attributes_t *attributes,
1333 const uint8_t *key_buffer,
1334 size_t key_buffer_size,
1335 uint8_t *data,
1336 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001338{
Ronald Crond18b5f82020-11-25 16:46:05 +01001339 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1342 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001343 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 return psa_export_key_buffer_internal(
1345 key_buffer, key_buffer_size,
1346 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001347 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001350#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1352 return mbedtls_psa_rsa_export_public_key(attributes,
1353 key_buffer,
1354 key_buffer_size,
1355 data,
1356 data_size,
1357 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001358#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001359 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001361#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1362 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001364#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1366 return mbedtls_psa_ecp_export_public_key(attributes,
1367 key_buffer,
1368 key_buffer_size,
1369 data,
1370 data_size,
1371 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001372#else
1373 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001375#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1376 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001377 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001379 /* This shouldn't happen in the reference implementation, but
1380 it is valid for a special-purpose implementation to omit
1381 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001383 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001384}
Ronald Crond18b5f82020-11-25 16:46:05 +01001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1387 uint8_t *data,
1388 size_t data_size,
1389 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001390{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001392 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001393 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001394
Ronald Crone907e552021-01-18 13:22:38 +01001395 /* Reject a zero-length output buffer now, since this can never be a
1396 * valid key representation. This way we know that data must be a valid
1397 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if (data_size == 0) {
1399 return PSA_ERROR_BUFFER_TOO_SMALL;
1400 }
Ronald Crone907e552021-01-18 13:22:38 +01001401
Darryl Greendd8fb772018-11-07 16:00:44 +00001402 /* Set the key to empty now, so that even when there are errors, we always
1403 * set data_length to a value between 0 and data_size. On error, setting
1404 * the key to empty is a good choice because an empty key representation is
1405 * unlikely to be accepted anywhere. */
1406 *data_length = 0;
1407
1408 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1410 if (status != PSA_SUCCESS) {
1411 return status;
1412 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1415 status = PSA_ERROR_INVALID_ARGUMENT;
1416 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001417 }
1418
Ronald Crond18b5f82020-11-25 16:46:05 +01001419 psa_key_attributes_t attributes = {
1420 .core = slot->attr
1421 };
Ronald Cron67227982020-11-26 15:16:05 +01001422 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001423 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001425
1426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001430}
1431
Gilles Peskine91e8c332019-08-02 19:19:39 +02001432#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001433static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1434 "One or more key attribute flag is listed as both external-only and dual-use");
1435static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1436 "One or more key attribute flag is listed as both internal-only and dual-use");
1437static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1438 "One or more key attribute flag is listed as both internal-only and external-only");
Gilles Peskine91e8c332019-08-02 19:19:39 +02001439#endif
1440
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001441/** Validate that a key policy is internally well-formed.
1442 *
1443 * This function only rejects invalid policies. It does not validate the
1444 * consistency of the policy with respect to other attributes of the key
1445 * such as the key type.
1446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001448{
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1450 PSA_KEY_USAGE_COPY |
1451 PSA_KEY_USAGE_ENCRYPT |
1452 PSA_KEY_USAGE_DECRYPT |
1453 PSA_KEY_USAGE_SIGN_MESSAGE |
1454 PSA_KEY_USAGE_VERIFY_MESSAGE |
1455 PSA_KEY_USAGE_SIGN_HASH |
1456 PSA_KEY_USAGE_VERIFY_HASH |
1457 PSA_KEY_USAGE_VERIFY_DERIVATION |
1458 PSA_KEY_USAGE_DERIVE)) != 0) {
1459 return PSA_ERROR_INVALID_ARGUMENT;
1460 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001463}
1464
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001465/** Validate the internal consistency of key attributes.
1466 *
1467 * This function only rejects invalid attribute values. If does not
1468 * validate the consistency of the attributes with any key data that may
1469 * be involved in the creation of the key.
1470 *
1471 * Call this function early in the key creation process.
1472 *
1473 * \param[in] attributes Key attributes for the new key.
1474 * \param[out] p_drv On any return, the driver for the key, if any.
1475 * NULL for a transparent key.
1476 *
1477 */
1478static psa_status_t psa_validate_key_attributes(
1479 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001481{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001482 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1484 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 status = psa_validate_key_location(lifetime, p_drv);
1487 if (status != PSA_SUCCESS) {
1488 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001489 }
1490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 status = psa_validate_key_persistence(lifetime);
1492 if (status != PSA_SUCCESS) {
1493 return status;
1494 }
1495
1496 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1497 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1498 return PSA_ERROR_INVALID_ARGUMENT;
1499 }
1500 } else {
1501 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1502 return PSA_ERROR_INVALID_ARGUMENT;
1503 }
1504 }
1505
1506 status = psa_validate_key_policy(&attributes->core.policy);
1507 if (status != PSA_SUCCESS) {
1508 return status;
1509 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001510
1511 /* Refuse to create overly large keys.
1512 * Note that this doesn't trigger on import if the attributes don't
1513 * explicitly specify a size (so psa_get_key_bits returns 0), so
1514 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1516 return PSA_ERROR_NOT_SUPPORTED;
1517 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001518
Gilles Peskine91e8c332019-08-02 19:19:39 +02001519 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1521 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1522 return PSA_ERROR_INVALID_ARGUMENT;
1523 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001526}
1527
Gilles Peskine4747d192019-04-17 15:05:45 +02001528/** Prepare a key slot to receive key material.
1529 *
1530 * This function allocates a key slot and sets its metadata.
1531 *
1532 * If this function fails, call psa_fail_key_creation().
1533 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001534 * This function is intended to be used as follows:
1535 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001536 * it with the specified attributes, and in case of a volatile key assign it
1537 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001538 * -# Populate the slot with the key material.
1539 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1540 * In case of failure at any step, stop the sequence and call
1541 * psa_fail_key_creation().
1542 *
Ronald Cron5c522922020-11-14 16:35:34 +01001543 * On success, the key slot is locked. It is the responsibility of the caller
1544 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001545 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001546 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001548 * \param[out] p_slot On success, a pointer to the prepared slot.
1549 * \param[out] p_drv On any return, the driver for the key, if any.
1550 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001551 *
1552 * \retval #PSA_SUCCESS
1553 * The key slot is ready to receive key material.
1554 * \return If this function fails, the key slot is an invalid state.
1555 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001556 */
1557static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001558 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001559 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001560 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001562{
1563 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001564 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001565 psa_key_slot_t *slot;
1566
Gilles Peskinedf179142019-07-15 22:02:14 +02001567 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001568 *p_drv = NULL;
1569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 status = psa_validate_key_attributes(attributes, p_drv);
1571 if (status != PSA_SUCCESS) {
1572 return status;
1573 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1576 if (status != PSA_SUCCESS) {
1577 return status;
1578 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001579 slot = *p_slot;
1580
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001581 /* We're storing the declared bit-size of the key. It's up to each
1582 * creation mechanism to verify that this information is correct.
1583 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001584 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001585 * is optional (import, copy). In case of a volatile key, assign it the
1586 * volatile key identifier associated to the slot returned to contain its
1587 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001588
1589 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001591#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1592 slot->attr.id = volatile_key_id;
1593#else
1594 slot->attr.id.key_id = volatile_key_id;
1595#endif
1596 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001597
Gilles Peskine91e8c332019-08-02 19:19:39 +02001598 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001599 * external-only flags, query `attributes`. Thanks to the check
1600 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001601 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001602 * may have set. */
1603 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001604
Gilles Peskinecbaff462019-07-12 23:46:04 +02001605#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001606 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001607 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001608 * create the key file in internal storage, create the
1609 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001610 * persistent data. This is done by starting a transaction that will
1611 * encompass these three actions.
1612 * For registering a volatile key, we just need to find an appropriate
1613 * slot number inside the SE. Since the key is designated volatile, creating
1614 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001615 /* The first thing to do is to find a slot number for the new key.
1616 * We save the slot number in persistent storage as part of the
1617 * transaction data. It will be needed to recover if the power
1618 * fails during the key creation process, to clean up on the secure
1619 * element side after restarting. Obtaining a slot number from the
1620 * secure element driver updates its persistent state, but we do not yet
1621 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001622 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001624 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1626 &slot_number);
1627 if (status != PSA_SUCCESS) {
1628 return status;
1629 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1632 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001633 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001634 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001635 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 status = psa_crypto_save_transaction();
1637 if (status != PSA_SUCCESS) {
1638 (void) psa_crypto_stop_transaction();
1639 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001640 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001641 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001642
1643 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001645 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001648 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001650 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001651#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001654}
Gilles Peskine4747d192019-04-17 15:05:45 +02001655
1656/** Finalize the creation of a key once its key material has been set.
1657 *
1658 * This entails writing the key to persistent storage.
1659 *
1660 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001661 * See the documentation of psa_start_key_creation() for the intended use
1662 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001663 *
Ronald Cron5c522922020-11-14 16:35:34 +01001664 * If the finalization succeeds, the function unlocks the key slot (it was
1665 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1666 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001667 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001668 * \param[in,out] slot Pointer to the slot with key material.
1669 * \param[in] driver The secure element driver for the key,
1670 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001671 * \param[out] key On success, identifier of the key. Note that the
1672 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001673 *
1674 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001675 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001676 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1677 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1678 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1679 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1680 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1681 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001682 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001683 * \return If this function fails, the key slot is an invalid state.
1684 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001685 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001686static psa_status_t psa_finish_key_creation(
1687 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001688 psa_se_drv_table_entry_t *driver,
1689 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001690{
1691 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001692 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001693 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001694
1695#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001699 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001700 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001702
Gilles Peskineb46bef22019-07-30 21:32:04 +02001703#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 static_assert(sizeof(slot_number) ==
1705 sizeof(data.slot_number),
1706 "Slot number size does not match psa_se_key_data_storage_t");
Gilles Peskineb46bef22019-07-30 21:32:04 +02001707#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1709 status = psa_save_persistent_key(&slot->attr,
1710 (uint8_t *) &data,
1711 sizeof(data));
1712 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001713#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1714 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001715 /* Key material is saved in export representation in the slot, so
1716 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 status = psa_save_persistent_key(&slot->attr,
1718 slot->key.data,
1719 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001720 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001721 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001722#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1723
Gilles Peskinecbaff462019-07-12 23:46:04 +02001724#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001725 /* Finish the transaction for a key creation. This does not
1726 * happen when registering an existing key. Detect this case
1727 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001728 * creation of a persistent key in a secure element requires a transaction,
1729 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 if (driver != NULL &&
1731 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1732 status = psa_save_se_persistent_data(driver);
1733 if (status != PSA_SUCCESS) {
1734 psa_destroy_persistent_key(slot->attr.id);
1735 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001736 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001738 }
1739#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001742 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 status = psa_unlock_key_slot(slot);
1744 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001745 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001747 }
Ronald Cron50972942020-11-14 11:28:25 +01001748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001750}
1751
1752/** Abort the creation of a key.
1753 *
1754 * You may call this function after calling psa_start_key_creation(),
1755 * or after psa_finish_key_creation() fails. In other circumstances, this
1756 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001757 * See the documentation of psa_start_key_creation() for the intended use
1758 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001760 * \param[in,out] slot Pointer to the slot with key material.
1761 * \param[in] driver The secure element driver for the key,
1762 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001763 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001764static void psa_fail_key_creation(psa_key_slot_t *slot,
1765 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001766{
Gilles Peskine011e4282019-06-26 18:34:38 +02001767 (void) driver;
1768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001772
1773#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001774 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001775 * element, and the failure happened later (when saving metadata
1776 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001777 * element.
1778 * https://github.com/ARMmbed/mbed-crypto/issues/217
1779 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001780
Gilles Peskined7729582019-08-05 15:55:54 +02001781 /* Abort the ongoing transaction if any (there may not be one if
1782 * the creation process failed before starting one, or if the
1783 * key creation is a registration of a key in a secure element).
1784 * Earlier functions must already have done what it takes to undo any
1785 * partial creation. All that's left is to update the transaction data
1786 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001788#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001791}
1792
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001793/** Validate optional attributes during key creation.
1794 *
1795 * Some key attributes are optional during key creation. If they are
1796 * specified in the attributes structure, check that they are consistent
1797 * with the data in the slot.
1798 *
1799 * This function should be called near the end of key creation, after
1800 * the slot in memory is fully populated but before saving persistent data.
1801 */
1802static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001803 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001805{
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (attributes->core.type != 0) {
1807 if (attributes->core.type != slot->attr.type) {
1808 return PSA_ERROR_INVALID_ARGUMENT;
1809 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001810 }
1811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001813#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1815 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001816 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001817 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001819
Ronald Cron90857082020-11-25 14:58:33 +01001820 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 slot->attr.type,
1822 slot->key.data,
1823 slot->key.bytes,
1824 &rsa);
1825 if (status != PSA_SUCCESS) {
1826 return status;
1827 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 mbedtls_mpi_init(&actual);
1830 mbedtls_mpi_init(&required);
1831 ret = mbedtls_rsa_export(rsa,
1832 NULL, NULL, NULL, NULL, &actual);
1833 mbedtls_rsa_free(rsa);
1834 mbedtls_free(rsa);
1835 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001836 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 }
1838 ret = mbedtls_mpi_read_binary(&required,
1839 attributes->domain_parameters,
1840 attributes->domain_parameters_size);
1841 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001842 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 }
1844 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001845 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 }
1847rsa_exit:
1848 mbedtls_mpi_free(&actual);
1849 mbedtls_mpi_free(&required);
1850 if (ret != 0) {
1851 return mbedtls_to_psa_error(ret);
1852 }
1853 } else
John Durkop9814fa22020-11-04 12:28:15 -08001854#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1855 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001856 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001858 }
1859 }
1860
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (attributes->core.bits != 0) {
1862 if (attributes->core.bits != slot->attr.bits) {
1863 return PSA_ERROR_INVALID_ARGUMENT;
1864 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001865 }
1866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868}
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1871 const uint8_t *data,
1872 size_t data_length,
1873 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001874{
1875 psa_status_t status;
1876 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001877 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001878 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301879 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001880
Ronald Cron81709fc2020-11-14 12:10:32 +01001881 *key = MBEDTLS_SVC_KEY_ID_INIT;
1882
Gilles Peskine0f84d622019-09-12 19:03:13 +02001883 /* Reject zero-length symmetric keys (including raw data key objects).
1884 * This also rejects any key which might be encoded as an empty string,
1885 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 if (data_length == 0) {
1887 return PSA_ERROR_INVALID_ARGUMENT;
1888 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001889
Archana449608b2021-09-08 15:36:05 +05301890 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 if (data_length > SIZE_MAX / 8) {
1892 return PSA_ERROR_NOT_SUPPORTED;
1893 }
Archana6ed4bda2021-08-04 10:47:15 +05301894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1896 &slot, &driver);
1897 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001901 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301902 * storage ( thus not in the case of importing a key in a secure element
1903 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1904 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 if (slot->key.data == NULL) {
1906 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301907 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 attributes, data, data_length, &storage_size);
1909 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301910 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 }
Archanad8a83dc2021-06-14 10:04:16 +05301912 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 status = psa_allocate_buffer_to_slot(slot, storage_size);
1914 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001915 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001917 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001918
1919 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 status = psa_driver_wrapper_import_key(attributes,
1921 data, data_length,
1922 slot->key.data,
1923 slot->key.bytes,
1924 &slot->key.bytes, &bits);
1925 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001926 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001930 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001932 status = PSA_ERROR_INVALID_ARGUMENT;
1933 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001934 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001935
Archana6ed4bda2021-08-04 10:47:15 +05301936 /* Enforce a size limit, and in particular ensure that the bit
1937 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301939 status = PSA_ERROR_NOT_SUPPORTED;
1940 goto exit;
1941 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 status = psa_validate_optional_attributes(slot, attributes);
1943 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001944 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001948exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (status != PSA_SUCCESS) {
1950 psa_fail_key_creation(slot, driver);
1951 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001954}
1955
Gilles Peskined7729582019-08-05 15:55:54 +02001956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1957psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02001959{
1960 psa_status_t status;
1961 psa_key_slot_t *slot = NULL;
1962 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001963 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001964
1965 /* Leaving attributes unspecified is not currently supported.
1966 * It could make sense to query the key type and size from the
1967 * secure element, but not all secure elements support this
1968 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
1970 return PSA_ERROR_NOT_SUPPORTED;
1971 }
1972 if (psa_get_key_bits(attributes) == 0) {
1973 return PSA_ERROR_NOT_SUPPORTED;
1974 }
Gilles Peskined7729582019-08-05 15:55:54 +02001975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
1977 &slot, &driver);
1978 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02001979 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 }
Gilles Peskined7729582019-08-05 15:55:54 +02001981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02001983
1984exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (status != PSA_SUCCESS) {
1986 psa_fail_key_creation(slot, driver);
1987 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001988
Gilles Peskined7729582019-08-05 15:55:54 +02001989 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 psa_close_key(key);
1991 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02001992}
1993#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
1996 const psa_key_attributes_t *specified_attributes,
1997 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01001998{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001999 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002000 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002001 psa_key_slot_t *source_slot = NULL;
2002 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002003 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002004 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302005 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002006
Ronald Cron81709fc2020-11-14 12:10:32 +01002007 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2008
Archana8a180362021-07-05 02:18:48 +05302009 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2011 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002012 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 status = psa_validate_optional_attributes(source_slot,
2016 specified_attributes);
2017 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020
Archana9d17bf42021-09-10 06:22:44 +05302021 /* The target key type and number of bits have been validated by
2022 * psa_validate_optional_attributes() to be either equal to zero or
2023 * equal to the ones of the source key. So it is safe to inherit
2024 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302025 * */
Archana9d17bf42021-09-10 06:22:44 +05302026 actual_attributes.core.bits = source_slot->attr.bits;
2027 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302028
2029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 status = psa_restrict_key_policy(source_slot->attr.type,
2031 &actual_attributes.core.policy,
2032 &source_slot->attr.policy);
2033 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002034 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2038 &target_slot, &driver);
2039 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002040 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 }
2042 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2043 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302044 /*
Archana9d17bf42021-09-10 06:22:44 +05302045 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302046 * the source key would need to be exported as plaintext and re-imported
2047 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302048 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302049 * appropriate API invocations from the application, if needed.
2050 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002051 status = PSA_ERROR_NOT_SUPPORTED;
2052 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002053 }
Archana8a180362021-07-05 02:18:48 +05302054 /*
2055 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302056 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302057 * - For opaque keys this translates to an invocation of the drivers'
2058 * copy_key entry point through the dispatch layer.
2059 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2061 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2062 &storage_size);
2063 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302064 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 }
Archana374fe5b2021-09-08 15:50:28 +05302066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2068 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302069 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 }
Archana374fe5b2021-09-08 15:50:28 +05302071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 status = psa_driver_wrapper_copy_key(&actual_attributes,
2073 source_slot->key.data,
2074 source_slot->key.bytes,
2075 target_slot->key.data,
2076 target_slot->key.bytes,
2077 &target_slot->key.bytes);
2078 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 }
2081 } else {
2082 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302083 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 source_slot->key.bytes);
2085 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302086 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 }
Archana8a180362021-07-05 02:18:48 +05302088 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002090exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (status != PSA_SUCCESS) {
2092 psa_fail_key_creation(target_slot, driver);
2093 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002098}
2099
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002100
2101
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002102/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002103/* Message digests */
2104/****************************************************************/
2105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002107{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002108 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (operation->id == 0) {
2110 return PSA_SUCCESS;
2111 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002114 operation->id = 0;
2115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002117}
2118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2120 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002122 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002123
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002124 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002126 status = PSA_ERROR_BAD_STATE;
2127 goto exit;
2128 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002131 status = PSA_ERROR_INVALID_ARGUMENT;
2132 goto exit;
2133 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002134
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002135 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2136 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002140
2141exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 if (status != PSA_SUCCESS) {
2143 psa_hash_abort(operation);
2144 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002145
2146 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002147}
2148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2150 const uint8_t *input,
2151 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002152{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002156 status = PSA_ERROR_BAD_STATE;
2157 goto exit;
2158 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002159
Steven Cooremanc8288352021-03-04 14:02:19 +01002160 /* Don't require hash implementations to behave correctly on a
2161 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 if (input_length == 0) {
2163 return PSA_SUCCESS;
2164 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002167
2168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (status != PSA_SUCCESS) {
2170 psa_hash_abort(operation);
2171 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002174}
2175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2177 uint8_t *hash,
2178 size_t hash_size,
2179 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002180{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002181 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if (operation->id == 0) {
2183 return PSA_ERROR_BAD_STATE;
2184 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002185
Steven Cooreman1e582352021-02-18 17:24:37 +01002186 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 operation, hash, hash_size, hash_length);
2188 psa_hash_abort(operation);
2189 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002190}
2191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2193 const uint8_t *hash,
2194 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195{
Ronald Cron69a63422021-10-18 09:47:58 +02002196 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002198 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 operation,
2200 actual_hash, sizeof(actual_hash),
2201 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002204 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002208 status = PSA_ERROR_INVALID_SIGNATURE;
2209 goto exit;
2210 }
2211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002213 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002215
2216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2218 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002219 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002223}
2224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225psa_status_t psa_hash_compute(psa_algorithm_t alg,
2226 const uint8_t *input, size_t input_length,
2227 uint8_t *hash, size_t hash_size,
2228 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002229{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002230 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 if (!PSA_ALG_IS_HASH(alg)) {
2232 return PSA_ERROR_INVALID_ARGUMENT;
2233 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2236 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002237}
2238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239psa_status_t psa_hash_compare(psa_algorithm_t alg,
2240 const uint8_t *input, size_t input_length,
2241 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002242{
Ronald Cron69a63422021-10-18 09:47:58 +02002243 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002244 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (!PSA_ALG_IS_HASH(alg)) {
2247 return PSA_ERROR_INVALID_ARGUMENT;
2248 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002249
Steven Cooreman1e582352021-02-18 17:24:37 +01002250 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 alg, input, input_length,
2252 actual_hash, sizeof(actual_hash),
2253 &actual_hash_length);
2254 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002255 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 }
2257 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002258 status = PSA_ERROR_INVALID_SIGNATURE;
2259 goto exit;
2260 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002262 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002264
2265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2267 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002268}
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2271 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002272{
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (source_operation->id == 0 ||
2274 target_operation->id != 0) {
2275 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002276 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2279 target_operation);
2280 if (status != PSA_SUCCESS) {
2281 psa_hash_abort(target_operation);
2282 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002285}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286
2287
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002289/* MAC */
2290/****************************************************************/
2291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002293{
Steven Cooremane6804192021-03-19 18:28:56 +01002294 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 if (operation->id == 0) {
2296 return PSA_SUCCESS;
2297 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002300 operation->mac_size = 0;
2301 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002302 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002305}
2306
Ronald Cron2dff3b22021-06-17 16:33:22 +02002307static psa_status_t psa_mac_finalize_alg_and_key_validation(
2308 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002309 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002311{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002312 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 psa_key_type_t key_type = psa_get_key_type(attributes);
2314 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (!PSA_ALG_IS_MAC(alg)) {
2317 return PSA_ERROR_INVALID_ARGUMENT;
2318 }
Steven Cooremane6804192021-03-19 18:28:56 +01002319
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002320 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 status = psa_mac_key_can_do(alg, key_type);
2322 if (status != PSA_SUCCESS) {
2323 return status;
2324 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002325
Steven Cooremand1a68f12021-05-10 11:13:41 +02002326 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002330 /* A very short MAC is too short for security since it can be
2331 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2332 * so we make this our minimum, even though 32 bits is still
2333 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002335 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2338 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002339 /* It's impossible to "truncate" to a larger length than the full length
2340 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002342 }
2343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002345 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2346 * that is disabled in the compile-time configuration. The result can
2347 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2348 * configuration into account. In this case, force a return of
2349 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2350 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2351 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2352 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2353 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002355 }
2356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002358}
2359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2361 mbedtls_svc_key_id_t key,
2362 psa_algorithm_t alg,
2363 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002364{
2365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2366 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002367 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002368
2369 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002371 status = PSA_ERROR_BAD_STATE;
2372 goto exit;
2373 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374
2375 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 key,
2377 &slot,
2378 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2379 alg);
2380 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002381 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002383
2384 psa_key_attributes_t attributes = {
2385 .core = slot->attr
2386 };
2387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2389 &operation->mac_size);
2390 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002391 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002393
2394 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002395 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (is_sign) {
2397 status = psa_driver_wrapper_mac_sign_setup(operation,
2398 &attributes,
2399 slot->key.data,
2400 slot->key.bytes,
2401 alg);
2402 } else {
2403 status = psa_driver_wrapper_mac_verify_setup(operation,
2404 &attributes,
2405 slot->key.data,
2406 slot->key.bytes,
2407 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002408 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002409
Gilles Peskinefbfac682018-07-08 20:51:54 +02002410exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (status != PSA_SUCCESS) {
2412 psa_mac_abort(operation);
2413 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418}
2419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2421 mbedtls_svc_key_id_t key,
2422 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002423{
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002425}
2426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2428 mbedtls_svc_key_id_t key,
2429 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002430{
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002432}
2433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2435 const uint8_t *input,
2436 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002437{
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if (operation->id == 0) {
2439 return PSA_ERROR_BAD_STATE;
2440 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002442 /* Don't require hash implementations to behave correctly on a
2443 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 if (input_length == 0) {
2445 return PSA_SUCCESS;
2446 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2449 input, input_length);
2450 if (status != PSA_SUCCESS) {
2451 psa_mac_abort(operation);
2452 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002455}
2456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2458 uint8_t *mac,
2459 size_t mac_size,
2460 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002461{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2463 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002466 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002467 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002468 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002471 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002472 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002473 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002474
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002475 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2476 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002478 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002479 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002480 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002483 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002484 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002485 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 status = psa_driver_wrapper_mac_sign_finish(operation,
2488 mac, operation->mac_size,
2489 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002490
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002491exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002492 /* In case of success, set the potential excess room in the output buffer
2493 * to an invalid value, to avoid potentially leaking a longer MAC.
2494 * In case of error, set the output length and content to a safe default,
2495 * such that in case the caller misses an error check, the output would be
2496 * an unachievable MAC.
2497 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002499 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002500 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002501 }
mohammad16036df908f2018-04-02 08:34:15 -07002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (mac_size > operation->mac_size) {
2504 memset(&mac[operation->mac_size], '!',
2505 mac_size - operation->mac_size);
2506 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002511}
2512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2514 const uint8_t *mac,
2515 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002516{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002517 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2518 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002521 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002522 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002523 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002526 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002527 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002528 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002531 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002532 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002533 }
2534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 status = psa_driver_wrapper_mac_verify_finish(operation,
2536 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002537
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002538exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542}
2543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2545 psa_algorithm_t alg,
2546 const uint8_t *input,
2547 size_t input_length,
2548 uint8_t *mac,
2549 size_t mac_size,
2550 size_t *mac_length,
2551 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002552{
2553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002554 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2555 psa_key_slot_t *slot;
2556 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557
Ronald Cron51131b52021-06-17 17:17:20 +02002558 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 key,
2560 &slot,
2561 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2562 alg);
2563 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002564 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002566
Ronald Cron51131b52021-06-17 17:17:20 +02002567 psa_key_attributes_t attributes = {
2568 .core = slot->attr
2569 };
2570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2572 &operation_mac_size);
2573 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002574 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002578 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002579 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002580 }
2581
2582 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 &attributes,
2584 slot->key.data, slot->key.bytes,
2585 alg,
2586 input, input_length,
2587 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002588
2589exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002590 /* In case of success, set the potential excess room in the output buffer
2591 * to an invalid value, to avoid potentially leaking a longer MAC.
2592 * In case of error, set the output length and content to a safe default,
2593 * such that in case the caller misses an error check, the output would be
2594 * an unachievable MAC.
2595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002597 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002598 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002599 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (mac_size > operation_mac_size) {
2601 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2602 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002607}
2608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002610 psa_algorithm_t alg,
2611 const uint8_t *input,
2612 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 uint8_t *mac,
2614 size_t mac_size,
2615 size_t *mac_length)
2616{
2617 return psa_mac_compute_internal(key, alg,
2618 input, input_length,
2619 mac, mac_size, mac_length, 1);
2620}
2621
2622psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2623 psa_algorithm_t alg,
2624 const uint8_t *input,
2625 size_t input_length,
2626 const uint8_t *mac,
2627 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002628{
2629 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002630 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2631 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 status = psa_mac_compute_internal(key, alg,
2634 input, input_length,
2635 actual_mac, sizeof(actual_mac),
2636 &actual_mac_length, 0);
2637 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002642 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002643 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002644 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002646 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002647 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002648 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002649
2650exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002654}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002655
Gilles Peskinee59236f2018-01-27 23:32:46 +01002656/****************************************************************/
2657/* Asymmetric cryptography */
2658/****************************************************************/
2659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2661 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002662{
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 if (input_is_message) {
2664 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2665 return PSA_ERROR_INVALID_ARGUMENT;
2666 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2669 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2670 return PSA_ERROR_INVALID_ARGUMENT;
2671 }
2672 }
2673 } else {
2674 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2675 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002676 }
2677 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002680}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2683 int input_is_message,
2684 psa_algorithm_t alg,
2685 const uint8_t *input,
2686 size_t input_length,
2687 uint8_t *signature,
2688 size_t signature_size,
2689 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002690{
2691 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2692 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2693 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002694
2695 *signature_length = 0;
2696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 status = psa_sign_verify_check_alg(input_is_message, alg);
2698 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002699 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002701
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002702 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002703 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002704 * buffer can in principle be empty since it doesn't actually have
2705 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 if (signature_size == 0) {
2707 return PSA_ERROR_BUFFER_TOO_SMALL;
2708 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002709
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002710 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 key, &slot,
2712 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2713 PSA_KEY_USAGE_SIGN_HASH,
2714 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002717 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002721 status = PSA_ERROR_INVALID_ARGUMENT;
2722 goto exit;
2723 }
2724
2725 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002727 };
2728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002730 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002731 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002732 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 signature, signature_size, signature_length);
2734 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002735
2736 status = psa_driver_wrapper_sign_hash(
2737 &attributes, slot->key.data, slot->key.bytes,
2738 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002740 }
2741
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002742
2743exit:
2744 /* Fill the unused part of the output buffer (the whole buffer on error,
2745 * the trailing part on success) with something that isn't a valid signature
2746 * (barring an attack on the signature and deliberately-crafted input),
2747 * in case the caller doesn't check the return status properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 if (status == PSA_SUCCESS) {
2749 memset(signature + *signature_length, '!',
2750 signature_size - *signature_length);
2751 } else {
2752 memset(signature, '!', signature_size);
2753 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002754 /* If signature_size is 0 then we have nothing to do. We must not call
2755 * memset because signature may be NULL in this case. */
2756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002760}
2761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2763 int input_is_message,
2764 psa_algorithm_t alg,
2765 const uint8_t *input,
2766 size_t input_length,
2767 const uint8_t *signature,
2768 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002769{
2770 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2771 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2772 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 status = psa_sign_verify_check_alg(input_is_message, alg);
2775 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002776 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002778
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002779 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 key, &slot,
2781 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2782 PSA_KEY_USAGE_VERIFY_HASH,
2783 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 if (status != PSA_SUCCESS) {
2786 return status;
2787 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002788
2789 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002791 };
2792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002794 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002795 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002796 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 signature, signature_length);
2798 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002799 status = psa_driver_wrapper_verify_hash(
2800 &attributes, slot->key.data, slot->key.bytes,
2801 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002803 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002808
2809}
2810
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002811psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002812 const psa_key_attributes_t *attributes,
2813 const uint8_t *key_buffer,
2814 size_t key_buffer_size,
2815 psa_algorithm_t alg,
2816 const uint8_t *input,
2817 size_t input_length,
2818 uint8_t *signature,
2819 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002821{
2822 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002825 size_t hash_length;
2826 uint8_t hash[PSA_HASH_MAX_SIZE];
2827
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002828 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 PSA_ALG_SIGN_GET_HASH(alg),
2830 input, input_length,
2831 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002834 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002836
2837 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 attributes, key_buffer, key_buffer_size,
2839 alg, hash, hash_length,
2840 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002841 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002844}
2845
Gilles Peskine449bd832023-01-11 14:50:10 +01002846psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2847 psa_algorithm_t alg,
2848 const uint8_t *input,
2849 size_t input_length,
2850 uint8_t *signature,
2851 size_t signature_size,
2852 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002853{
2854 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002855 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002857}
2858
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002859psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002860 const psa_key_attributes_t *attributes,
2861 const uint8_t *key_buffer,
2862 size_t key_buffer_size,
2863 psa_algorithm_t alg,
2864 const uint8_t *input,
2865 size_t input_length,
2866 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002868{
2869 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002872 size_t hash_length;
2873 uint8_t hash[PSA_HASH_MAX_SIZE];
2874
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002875 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 PSA_ALG_SIGN_GET_HASH(alg),
2877 input, input_length,
2878 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002881 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002883
2884 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 attributes, key_buffer, key_buffer_size,
2886 alg, hash, hash_length,
2887 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002888 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002891}
2892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2894 psa_algorithm_t alg,
2895 const uint8_t *input,
2896 size_t input_length,
2897 const uint8_t *signature,
2898 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002899{
2900 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002901 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002903}
2904
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002905psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002906 const psa_key_attributes_t *attributes,
2907 const uint8_t *key_buffer, size_t key_buffer_size,
2908 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002910{
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2912 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2913 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002914#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2916 return mbedtls_psa_rsa_sign_hash(
2917 attributes,
2918 key_buffer, key_buffer_size,
2919 alg, hash, hash_length,
2920 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002921#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2922 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 } else {
2924 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002925 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2927 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002928#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2930 return mbedtls_psa_ecdsa_sign_hash(
2931 attributes,
2932 key_buffer, key_buffer_size,
2933 alg, hash, hash_length,
2934 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002935#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2936 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 } else {
2938 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002939 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002940 }
Ronald Cron4501c982021-03-19 20:09:32 +01002941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 (void) key_buffer;
2943 (void) key_buffer_size;
2944 (void) hash;
2945 (void) hash_length;
2946 (void) signature;
2947 (void) signature_size;
2948 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002951}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2954 psa_algorithm_t alg,
2955 const uint8_t *hash,
2956 size_t hash_length,
2957 uint8_t *signature,
2958 size_t signature_size,
2959 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002960{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002961 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002962 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002964}
2965
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002966psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002967 const psa_key_attributes_t *attributes,
2968 const uint8_t *key_buffer, size_t key_buffer_size,
2969 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002971{
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
2973 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2974 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002975#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2977 return mbedtls_psa_rsa_verify_hash(
2978 attributes,
2979 key_buffer, key_buffer_size,
2980 alg, hash, hash_length,
2981 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002982#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2983 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 } else {
2985 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002986 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2988 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002989#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2991 return mbedtls_psa_ecdsa_verify_hash(
2992 attributes,
2993 key_buffer, key_buffer_size,
2994 alg, hash, hash_length,
2995 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002996#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2997 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 } else {
2999 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003000 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003001 }
Ronald Cron4501c982021-03-19 20:09:32 +01003002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 (void) key_buffer;
3004 (void) key_buffer_size;
3005 (void) hash;
3006 (void) hash_length;
3007 (void) signature;
3008 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003011}
3012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3014 psa_algorithm_t alg,
3015 const uint8_t *hash,
3016 size_t hash_length,
3017 const uint8_t *signature,
3018 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003019{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003020 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003021 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003023}
3024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3026 psa_algorithm_t alg,
3027 const uint8_t *input,
3028 size_t input_length,
3029 const uint8_t *salt,
3030 size_t salt_length,
3031 uint8_t *output,
3032 size_t output_size,
3033 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003034{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003035 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003036 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003037 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003038
Darryl Green5cc689a2018-07-24 15:34:10 +01003039 (void) input;
3040 (void) input_length;
3041 (void) salt;
3042 (void) output;
3043 (void) output_size;
3044
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003045 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3048 return PSA_ERROR_INVALID_ARGUMENT;
3049 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003050
Ronald Cron5c522922020-11-14 16:35:34 +01003051 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3053 if (status != PSA_SUCCESS) {
3054 return status;
3055 }
3056 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3057 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003058 status = PSA_ERROR_INVALID_ARGUMENT;
3059 goto exit;
3060 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003061
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003062 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003064 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003065
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003066 status = psa_driver_wrapper_asymmetric_encrypt(
3067 &attributes, slot->key.data, slot->key.bytes,
3068 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003070exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003074}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3077 psa_algorithm_t alg,
3078 const uint8_t *input,
3079 size_t input_length,
3080 const uint8_t *salt,
3081 size_t salt_length,
3082 uint8_t *output,
3083 size_t output_size,
3084 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003086 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003087 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003088 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003089
Darryl Green5cc689a2018-07-24 15:34:10 +01003090 (void) input;
3091 (void) input_length;
3092 (void) salt;
3093 (void) output;
3094 (void) output_size;
3095
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003096 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3099 return PSA_ERROR_INVALID_ARGUMENT;
3100 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003101
Ronald Cron5c522922020-11-14 16:35:34 +01003102 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3104 if (status != PSA_SUCCESS) {
3105 return status;
3106 }
3107 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003108 status = PSA_ERROR_INVALID_ARGUMENT;
3109 goto exit;
3110 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003111
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003112 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003114 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003116 status = psa_driver_wrapper_asymmetric_decrypt(
3117 &attributes, slot->key.data, slot->key.bytes,
3118 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003120
3121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003123
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003125}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003126
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003127
3128
mohammad1603503973b2018-03-12 15:59:30 +02003129/****************************************************************/
3130/* Symmetric cryptography */
3131/****************************************************************/
3132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3134 mbedtls_svc_key_id_t key,
3135 psa_algorithm_t alg,
3136 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003137{
3138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3139 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003140 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3142 PSA_KEY_USAGE_ENCRYPT :
3143 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003144
3145 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003147 status = PSA_ERROR_BAD_STATE;
3148 goto exit;
3149 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003152 status = PSA_ERROR_INVALID_ARGUMENT;
3153 goto exit;
3154 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003155
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3157 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003158 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003160
Ronald Cron49fafa92021-03-10 08:34:23 +01003161 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003162 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003163 * so we only set it (in the driver wrapper) after resources have been
3164 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003165 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003167 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003169 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 }
3171 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003172
Ronald Crona4af55f2020-12-14 14:36:06 +01003173 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003175 };
3176
Ronald Cronab99ac22020-10-01 16:38:28 +02003177 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 if (cipher_operation == MBEDTLS_ENCRYPT) {
3179 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3180 &attributes,
3181 slot->key.data,
3182 slot->key.bytes,
3183 alg);
3184 } else {
3185 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3186 &attributes,
3187 slot->key.data,
3188 slot->key.bytes,
3189 alg);
3190 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003191
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003192exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 if (status != PSA_SUCCESS) {
3194 psa_cipher_abort(operation);
3195 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003196
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003198
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003200}
3201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3203 mbedtls_svc_key_id_t key,
3204 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003205{
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003207}
3208
Gilles Peskine449bd832023-01-11 14:50:10 +01003209psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3210 mbedtls_svc_key_id_t key,
3211 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003212{
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003214}
3215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3217 uint8_t *iv,
3218 size_t iv_size,
3219 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003220{
Ronald Cron6d051732020-10-01 14:10:20 +02003221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003222 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3223 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01003224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003226 status = PSA_ERROR_BAD_STATE;
3227 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003228 }
3229
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003231 status = PSA_ERROR_BAD_STATE;
3232 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003233 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003234
Ronald Cron2fb90522021-07-05 12:31:44 +02003235 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003237 status = PSA_ERROR_BUFFER_TOO_SMALL;
3238 goto exit;
3239 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003240
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02003242 status = PSA_ERROR_GENERIC_ERROR;
3243 goto exit;
3244 }
3245
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 status = psa_generate_random(local_iv, default_iv_length);
3247 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003248 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 }
Ronald Cron5618a392021-03-26 09:52:26 +01003250
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 status = psa_driver_wrapper_cipher_set_iv(operation,
3252 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003253
3254exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 if (status == PSA_SUCCESS) {
3256 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02003257 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003258 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02003260 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02003262 }
Ronald Cron6d051732020-10-01 14:10:20 +02003263
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003265}
3266
Gilles Peskine449bd832023-01-11 14:50:10 +01003267psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3268 const uint8_t *iv,
3269 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003270{
Ronald Cron6d051732020-10-01 14:10:20 +02003271 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003272
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003274 status = PSA_ERROR_BAD_STATE;
3275 goto exit;
3276 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003279 status = PSA_ERROR_BAD_STATE;
3280 goto exit;
3281 }
Ronald Crona0d68172021-03-26 10:15:08 +01003282
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003284 status = PSA_ERROR_INVALID_ARGUMENT;
3285 goto exit;
3286 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003287
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 status = psa_driver_wrapper_cipher_set_iv(operation,
3289 iv,
3290 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003291
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003292exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003294 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 } else {
3296 psa_cipher_abort(operation);
3297 }
3298 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003299}
3300
Gilles Peskine449bd832023-01-11 14:50:10 +01003301psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3302 const uint8_t *input,
3303 size_t input_length,
3304 uint8_t *output,
3305 size_t output_size,
3306 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003307{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003308 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003309
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003311 status = PSA_ERROR_BAD_STATE;
3312 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003313 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003314
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003316 status = PSA_ERROR_BAD_STATE;
3317 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003318 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003319
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 status = psa_driver_wrapper_cipher_update(operation,
3321 input,
3322 input_length,
3323 output,
3324 output_size,
3325 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003326
3327exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 if (status != PSA_SUCCESS) {
3329 psa_cipher_abort(operation);
3330 }
Ronald Cron6d051732020-10-01 14:10:20 +02003331
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003333}
3334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3336 uint8_t *output,
3337 size_t output_size,
3338 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003339{
David Saadab4ecc272019-02-14 13:48:10 +02003340 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003341
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003343 status = PSA_ERROR_BAD_STATE;
3344 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003345 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003346
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003348 status = PSA_ERROR_BAD_STATE;
3349 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003350 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003351
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 status = psa_driver_wrapper_cipher_finish(operation,
3353 output,
3354 output_size,
3355 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003356
3357exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 if (status == PSA_SUCCESS) {
3359 return psa_cipher_abort(operation);
3360 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003361 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003365 }
mohammad1603503973b2018-03-12 15:59:30 +02003366}
3367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003369{
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003371 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003372 * in use. It's ok to call abort on such an object, and there's
3373 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003375 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003378
Ronald Cron49fafa92021-03-10 08:34:23 +01003379 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003380 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003381 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003382
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003384}
3385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3387 psa_algorithm_t alg,
3388 const uint8_t *input,
3389 size_t input_length,
3390 uint8_t *output,
3391 size_t output_size,
3392 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003393{
3394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003395 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02003396 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02003397 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3398 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003399
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02003401 status = PSA_ERROR_INVALID_ARGUMENT;
3402 goto exit;
3403 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003404
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3406 PSA_KEY_USAGE_ENCRYPT,
3407 alg);
3408 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02003409 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003411
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003412 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003414 };
3415
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3417 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02003418 status = PSA_ERROR_GENERIC_ERROR;
3419 goto exit;
3420 }
3421
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 if (default_iv_length > 0) {
3423 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003424 status = PSA_ERROR_BUFFER_TOO_SMALL;
3425 goto exit;
3426 }
3427
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 status = psa_generate_random(local_iv, default_iv_length);
3429 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003430 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003432 }
3433
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003434 status = psa_driver_wrapper_cipher_encrypt(
3435 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02003436 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 mbedtls_buffer_offset(output, default_iv_length),
3438 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003439
3440exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 unlock_status = psa_unlock_key_slot(slot);
3442 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02003443 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02003444 }
Ronald Cron23919522021-07-08 19:00:07 +02003445
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 if (status == PSA_SUCCESS) {
3447 if (default_iv_length > 0) {
3448 memcpy(output, local_iv, default_iv_length);
3449 }
3450 *output_length += default_iv_length;
3451 } else {
3452 *output_length = 0;
3453 }
3454
3455 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003456}
3457
Gilles Peskine449bd832023-01-11 14:50:10 +01003458psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3459 psa_algorithm_t alg,
3460 const uint8_t *input,
3461 size_t input_length,
3462 uint8_t *output,
3463 size_t output_size,
3464 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003465{
3466 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003467 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02003468 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003469
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02003471 status = PSA_ERROR_INVALID_ARGUMENT;
3472 goto exit;
3473 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003474
Gilles Peskine449bd832023-01-11 14:50:10 +01003475 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3476 PSA_KEY_USAGE_DECRYPT,
3477 alg);
3478 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02003479 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003481
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003482 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003484 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003485
Gilles Peskine449bd832023-01-11 14:50:10 +01003486 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
3487 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02003488 status = PSA_ERROR_INVALID_ARGUMENT;
3489 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003491 status = PSA_ERROR_INVALID_ARGUMENT;
3492 goto exit;
3493 }
3494
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003495 status = psa_driver_wrapper_cipher_decrypt(
3496 &attributes, slot->key.data, slot->key.bytes,
3497 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003499
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003500exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 unlock_status = psa_unlock_key_slot(slot);
3502 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02003503 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003504 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003505
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02003507 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003508 }
Ronald Cron23919522021-07-08 19:00:07 +02003509
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003511}
3512
3513
mohammad16035955c982018-04-26 00:53:03 +03003514/****************************************************************/
3515/* AEAD */
3516/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003517
Paul Elliottbaff51c2021-09-28 17:44:45 +01003518/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003519static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01003520{
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01003522}
3523
3524/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003525static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
3526 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003527{
Gilles Peskine449bd832023-01-11 14:50:10 +01003528 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01003529
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003531#if defined(PSA_WANT_ALG_GCM)
3532 case PSA_ALG_GCM:
3533 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 * arbitrarily large nonces. Please note that we do not generally
3535 * recommend the usage of nonces of greater length than
3536 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
3537 * size, which can then lead to collisions if you encrypt a very
3538 * large number of messages.*/
3539 if (nonce_length != 0) {
3540 return PSA_SUCCESS;
3541 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003542 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003543#endif /* PSA_WANT_ALG_GCM */
3544#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003545 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01003546 if (nonce_length >= 7 && nonce_length <= 13) {
3547 return PSA_SUCCESS;
3548 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003549 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003550#endif /* PSA_WANT_ALG_CCM */
3551#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003552 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01003553 if (nonce_length == 12) {
3554 return PSA_SUCCESS;
3555 } else if (nonce_length == 8) {
3556 return PSA_ERROR_NOT_SUPPORTED;
3557 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01003558 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003559#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003560 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02003561 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003562 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003563 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003564
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003566}
3567
Gilles Peskine449bd832023-01-11 14:50:10 +01003568static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01003569{
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3571 return PSA_ERROR_INVALID_ARGUMENT;
3572 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01003573
Gilles Peskine449bd832023-01-11 14:50:10 +01003574 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01003575}
3576
Gilles Peskine449bd832023-01-11 14:50:10 +01003577psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3578 psa_algorithm_t alg,
3579 const uint8_t *nonce,
3580 size_t nonce_length,
3581 const uint8_t *additional_data,
3582 size_t additional_data_length,
3583 const uint8_t *plaintext,
3584 size_t plaintext_length,
3585 uint8_t *ciphertext,
3586 size_t ciphertext_size,
3587 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003588{
Ronald Cron215633c2021-03-16 17:15:37 +01003589 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3590 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003591
mohammad1603f08a5502018-06-03 15:05:47 +03003592 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003593
Gilles Peskine449bd832023-01-11 14:50:10 +01003594 status = psa_aead_check_algorithm(alg);
3595 if (status != PSA_SUCCESS) {
3596 return status;
3597 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003598
Ronald Cron9a986162021-03-26 12:40:07 +01003599 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3601 if (status != PSA_SUCCESS) {
3602 return status;
3603 }
mohammad16035955c982018-04-26 00:53:03 +03003604
Ronald Cron215633c2021-03-16 17:15:37 +01003605 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003606 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003607 };
mohammad16035955c982018-04-26 00:53:03 +03003608
Gilles Peskine449bd832023-01-11 14:50:10 +01003609 status = psa_aead_check_nonce_length(alg, nonce_length);
3610 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003611 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003613
Ronald Cronde822812021-03-17 16:08:20 +01003614 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003615 &attributes, slot->key.data, slot->key.bytes,
3616 alg,
3617 nonce, nonce_length,
3618 additional_data, additional_data_length,
3619 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003620 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003621
Gilles Peskine449bd832023-01-11 14:50:10 +01003622 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3623 memset(ciphertext, 0, ciphertext_size);
3624 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003625
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003626exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003627 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003628
Gilles Peskine449bd832023-01-11 14:50:10 +01003629 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003630}
3631
Gilles Peskine449bd832023-01-11 14:50:10 +01003632psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3633 psa_algorithm_t alg,
3634 const uint8_t *nonce,
3635 size_t nonce_length,
3636 const uint8_t *additional_data,
3637 size_t additional_data_length,
3638 const uint8_t *ciphertext,
3639 size_t ciphertext_length,
3640 uint8_t *plaintext,
3641 size_t plaintext_size,
3642 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003643{
Ronald Cron215633c2021-03-16 17:15:37 +01003644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3645 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003646
Gilles Peskineee652a32018-06-01 19:23:52 +02003647 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003648
Gilles Peskine449bd832023-01-11 14:50:10 +01003649 status = psa_aead_check_algorithm(alg);
3650 if (status != PSA_SUCCESS) {
3651 return status;
3652 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003653
Ronald Cron9a986162021-03-26 12:40:07 +01003654 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003655 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3656 if (status != PSA_SUCCESS) {
3657 return status;
3658 }
mohammad16035955c982018-04-26 00:53:03 +03003659
Ronald Cron215633c2021-03-16 17:15:37 +01003660 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003661 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003662 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003663
Gilles Peskine449bd832023-01-11 14:50:10 +01003664 status = psa_aead_check_nonce_length(alg, nonce_length);
3665 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003667 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003668
Ronald Cronde822812021-03-17 16:08:20 +01003669 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003670 &attributes, slot->key.data, slot->key.bytes,
3671 alg,
3672 nonce, nonce_length,
3673 additional_data, additional_data_length,
3674 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003675 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003676
Gilles Peskine449bd832023-01-11 14:50:10 +01003677 if (status != PSA_SUCCESS && plaintext_size != 0) {
3678 memset(plaintext, 0, plaintext_size);
3679 }
mohammad160360a64d02018-06-03 17:20:42 +03003680
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003681exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003682 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003683
Gilles Peskine449bd832023-01-11 14:50:10 +01003684 return status;
mohammad16035955c982018-04-26 00:53:03 +03003685}
3686
Gilles Peskine449bd832023-01-11 14:50:10 +01003687static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
3688{
3689 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01003690
Gilles Peskine449bd832023-01-11 14:50:10 +01003691 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02003692#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01003693 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01003694 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01003695 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
3696 return PSA_ERROR_INVALID_ARGUMENT;
3697 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01003698 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003699#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003700
Przemek Stekiel86679c72022-10-06 17:06:56 +02003701#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01003702 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01003703 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003704 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
3705 return PSA_ERROR_INVALID_ARGUMENT;
3706 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01003707 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003708#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003709
Przemek Stekiel86679c72022-10-06 17:06:56 +02003710#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01003711 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01003712 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003713 if (tag_len != 16) {
3714 return PSA_ERROR_INVALID_ARGUMENT;
3715 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01003716 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003717#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003718
3719 default:
3720 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003721 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01003722 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003723 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01003724}
3725
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003726/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003727static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
3728 int is_encrypt,
3729 mbedtls_svc_key_id_t key,
3730 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01003731{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003732 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3733 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3734 psa_key_slot_t *slot = NULL;
3735 psa_key_usage_t key_usage = 0;
3736
Gilles Peskine449bd832023-01-11 14:50:10 +01003737 status = psa_aead_check_algorithm(alg);
3738 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003739 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 }
Paul Elliottc2b71442021-06-24 18:17:52 +01003741
Gilles Peskine449bd832023-01-11 14:50:10 +01003742 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003743 status = PSA_ERROR_BAD_STATE;
3744 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003745 }
3746
Gilles Peskine449bd832023-01-11 14:50:10 +01003747 if (operation->nonce_set || operation->lengths_set ||
3748 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003749 status = PSA_ERROR_BAD_STATE;
3750 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003751 }
3752
Gilles Peskine449bd832023-01-11 14:50:10 +01003753 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003754 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01003755 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003756 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01003757 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003758
Gilles Peskine449bd832023-01-11 14:50:10 +01003759 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
3760 alg);
3761 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003762 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003764
3765 psa_key_attributes_t attributes = {
3766 .core = slot->attr
3767 };
3768
Gilles Peskine449bd832023-01-11 14:50:10 +01003769 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02003770 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003771 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02003772
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 if (is_encrypt) {
3774 status = psa_driver_wrapper_aead_encrypt_setup(operation,
3775 &attributes,
3776 slot->key.data,
3777 slot->key.bytes,
3778 alg);
3779 } else {
3780 status = psa_driver_wrapper_aead_decrypt_setup(operation,
3781 &attributes,
3782 slot->key.data,
3783 slot->key.bytes,
3784 alg);
3785 }
3786 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003787 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003788 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003789
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003791
3792exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003794
Gilles Peskine449bd832023-01-11 14:50:10 +01003795 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003796 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003797 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01003798 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01003799 } else {
3800 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003801 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003802
Gilles Peskine449bd832023-01-11 14:50:10 +01003803 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01003804}
3805
Paul Elliott302ff6b2021-04-20 18:10:30 +01003806/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003807psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
3808 mbedtls_svc_key_id_t key,
3809 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01003810{
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01003812}
3813
3814/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003815psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
3816 mbedtls_svc_key_id_t key,
3817 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01003818{
Gilles Peskine449bd832023-01-11 14:50:10 +01003819 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01003820}
3821
3822/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01003823psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
3824 uint8_t *nonce,
3825 size_t nonce_size,
3826 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01003827{
3828 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01003829 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01003830 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003831
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003832 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003833
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01003835 status = PSA_ERROR_BAD_STATE;
3836 goto exit;
3837 }
3838
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003840 status = PSA_ERROR_BAD_STATE;
3841 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003842 }
3843
Paul Elliotte193ea82021-10-01 13:00:16 +01003844 /* For CCM, this size may not be correct according to the PSA
3845 * specification. The PSA Crypto 1.0.1 specification states:
3846 *
3847 * CCM encodes the plaintext length pLen in L octets, with L the smallest
3848 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
3849 *
3850 * However this restriction that L has to be the smallest integer is not
3851 * applied in practice, and it is not implementable here since the
3852 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003853 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
3854 operation->alg);
3855 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003856 status = PSA_ERROR_BUFFER_TOO_SMALL;
3857 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003858 }
3859
Gilles Peskine449bd832023-01-11 14:50:10 +01003860 status = psa_generate_random(local_nonce, required_nonce_size);
3861 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003862 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003863 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01003864
Gilles Peskine449bd832023-01-11 14:50:10 +01003865 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01003866
Paul Elliott39dc6b82021-05-11 19:16:09 +01003867exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003868 if (status == PSA_SUCCESS) {
3869 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01003870 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01003871 } else {
3872 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01003873 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01003874
Gilles Peskine449bd832023-01-11 14:50:10 +01003875 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003876}
3877
3878/* Set the nonce for a multipart authenticated encryption or decryption
3879 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01003880psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
3881 const uint8_t *nonce,
3882 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01003883{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003884 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3885
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01003887 status = PSA_ERROR_BAD_STATE;
3888 goto exit;
3889 }
3890
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003892 status = PSA_ERROR_BAD_STATE;
3893 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003894 }
3895
Gilles Peskine449bd832023-01-11 14:50:10 +01003896 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
3897 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003898 status = PSA_ERROR_INVALID_ARGUMENT;
3899 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01003900 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01003901
Gilles Peskine449bd832023-01-11 14:50:10 +01003902 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
3903 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003904
Paul Elliott39dc6b82021-05-11 19:16:09 +01003905exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003906 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003907 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 } else {
3909 psa_aead_abort(operation);
3910 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003911
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003913}
3914
3915/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003916psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
3917 size_t ad_length,
3918 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01003919{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3921
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01003923 status = PSA_ERROR_BAD_STATE;
3924 goto exit;
3925 }
3926
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 if (operation->lengths_set || operation->ad_started ||
3928 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003929 status = PSA_ERROR_BAD_STATE;
3930 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003931 }
3932
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003934#if defined(PSA_WANT_ALG_GCM)
3935 case PSA_ALG_GCM:
3936 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 * bits. Without the guard this code will generate warnings on 32bit
3938 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01003939#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 if (((uint64_t) ad_length) >> 61 != 0 ||
3941 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003942 status = PSA_ERROR_INVALID_ARGUMENT;
3943 goto exit;
3944 }
Paul Elliott325d3742021-09-27 17:56:28 +01003945#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003946 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003947#endif /* PSA_WANT_ALG_GCM */
3948#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003949 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003951 status = PSA_ERROR_INVALID_ARGUMENT;
3952 goto exit;
3953 }
3954 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003955#endif /* PSA_WANT_ALG_CCM */
3956#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003957 case PSA_ALG_CHACHA20_POLY1305:
3958 /* No length restrictions for ChaChaPoly. */
3959 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003960#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003961 default:
3962 break;
3963 }
Paul Elliott325d3742021-09-27 17:56:28 +01003964
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
3966 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003967
Paul Elliott39dc6b82021-05-11 19:16:09 +01003968exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01003970 operation->ad_remaining = ad_length;
3971 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01003972 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 } else {
3974 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01003975 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01003976
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003978}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01003979
3980/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003981psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
3982 const uint8_t *input,
3983 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01003984{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003985 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3986
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01003988 status = PSA_ERROR_BAD_STATE;
3989 goto exit;
3990 }
3991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003993 status = PSA_ERROR_BAD_STATE;
3994 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003995 }
3996
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 if (operation->lengths_set) {
3998 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01003999 status = PSA_ERROR_INVALID_ARGUMENT;
4000 goto exit;
4001 }
4002
4003 operation->ad_remaining -= input_length;
4004 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004005#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004007 status = PSA_ERROR_BAD_STATE;
4008 goto exit;
4009 }
4010#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004011
Gilles Peskine449bd832023-01-11 14:50:10 +01004012 status = psa_driver_wrapper_aead_update_ad(operation, input,
4013 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004014
Paul Elliott39dc6b82021-05-11 19:16:09 +01004015exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004017 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 } else {
4019 psa_aead_abort(operation);
4020 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004021
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004023}
4024
4025/* Encrypt or decrypt a message fragment in an active multipart AEAD
4026 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004027psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4028 const uint8_t *input,
4029 size_t input_length,
4030 uint8_t *output,
4031 size_t output_size,
4032 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004033{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004034 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004035
4036 *output_length = 0;
4037
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004039 status = PSA_ERROR_BAD_STATE;
4040 goto exit;
4041 }
4042
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004044 status = PSA_ERROR_BAD_STATE;
4045 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004046 }
4047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004049 /* Additional data length was supplied, but not all the additional
4050 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004052 status = PSA_ERROR_INVALID_ARGUMENT;
4053 goto exit;
4054 }
4055
4056 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004058 status = PSA_ERROR_INVALID_ARGUMENT;
4059 goto exit;
4060 }
4061
4062 operation->body_remaining -= input_length;
4063 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004064#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004065 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004066 status = PSA_ERROR_BAD_STATE;
4067 goto exit;
4068 }
4069#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004070
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4072 output, output_size,
4073 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004074
Paul Elliott39dc6b82021-05-11 19:16:09 +01004075exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004077 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 } else {
4079 psa_aead_abort(operation);
4080 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004081
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004083}
4084
Gilles Peskine449bd832023-01-11 14:50:10 +01004085static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004086{
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 if (operation->id == 0 || !operation->nonce_set) {
4088 return PSA_ERROR_BAD_STATE;
4089 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4092 operation->body_remaining != 0)) {
4093 return PSA_ERROR_INVALID_ARGUMENT;
4094 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004095
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004097}
4098
Paul Elliott302ff6b2021-04-20 18:10:30 +01004099/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004100psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4101 uint8_t *ciphertext,
4102 size_t ciphertext_size,
4103 size_t *ciphertext_length,
4104 uint8_t *tag,
4105 size_t tag_size,
4106 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004107{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004108 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4109
Paul Elliott302ff6b2021-04-20 18:10:30 +01004110 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004111 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 status = psa_aead_final_checks(operation);
4114 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004119 status = PSA_ERROR_BAD_STATE;
4120 goto exit;
4121 }
4122
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4124 ciphertext_size,
4125 ciphertext_length,
4126 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004127
Paul Elliott39dc6b82021-05-11 19:16:09 +01004128exit:
Paul Elliottf47b0952021-05-21 18:02:33 +01004129 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004130 * the zero tag size, make sure the tag is set to something implausible.
4131 * Even if the operation succeeds, make sure we clear the rest of the
4132 * buffer to prevent potential leakage of anything previously placed in
4133 * the same buffer.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 if (tag != NULL) {
4135 if (status != PSA_SUCCESS) {
4136 memset(tag, '!', tag_size);
4137 } else if (*tag_length < tag_size) {
4138 memset(tag + *tag_length, '!', (tag_size - *tag_length));
4139 }
Paul Elliottec95cc92021-09-19 22:33:09 +01004140 }
Paul Elliottf47b0952021-05-21 18:02:33 +01004141
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004143
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004145}
4146
4147/* Finish authenticating and decrypting a message in a multipart AEAD
4148 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004149psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4150 uint8_t *plaintext,
4151 size_t plaintext_size,
4152 size_t *plaintext_length,
4153 const uint8_t *tag,
4154 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004155{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004156 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4157
Paul Elliott302ff6b2021-04-20 18:10:30 +01004158 *plaintext_length = 0;
4159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 status = psa_aead_final_checks(operation);
4161 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004166 status = PSA_ERROR_BAD_STATE;
4167 goto exit;
4168 }
4169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4171 plaintext_size,
4172 plaintext_length,
4173 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004174
4175exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004179}
4180
4181/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004182psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004183{
4184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004187 /* The object has (apparently) been initialized but it is not (yet)
4188 * in use. It's ok to call abort on such an object, and there's
4189 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004191 }
4192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004194
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004198}
4199
Gilles Peskinee59236f2018-01-27 23:32:46 +01004200/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004201/* Generators */
4202/****************************************************************/
4203
Przemek Stekiel69c46792022-06-10 12:59:51 +02004204#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004205 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004206 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4207 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004208#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004209#endif /* At least one builtin KDF */
4210
Przemek Stekiel69c46792022-06-10 12:59:51 +02004211#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004212 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4213 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4214static psa_status_t psa_key_derivation_start_hmac(
4215 psa_mac_operation_t *operation,
4216 psa_algorithm_t hash_alg,
4217 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004219{
4220 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4223 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4224 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004225
Steven Cooreman72f736a2021-05-07 14:14:37 +02004226 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02004228
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 status = psa_driver_wrapper_mac_sign_setup(operation,
4230 &attributes,
4231 hmac_key, hmac_key_length,
4232 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 psa_reset_key_attributes(&attributes);
4235 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02004236}
4237#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004238
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004239#define HKDF_STATE_INIT 0 /* no input yet */
4240#define HKDF_STATE_STARTED 1 /* got salt */
4241#define HKDF_STATE_KEYED 2 /* got key */
4242#define HKDF_STATE_OUTPUT 3 /* output started */
4243
Gilles Peskinecbe66502019-05-16 16:59:18 +02004244static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004246{
Gilles Peskine449bd832023-01-11 14:50:10 +01004247 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4248 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4249 } else {
4250 return operation->alg;
4251 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004252}
4253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004255{
4256 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4258 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004259 /* The object has (apparently) been initialized but it is not
4260 * in use. It's ok to call abort on such an object, and there's
4261 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004263#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4265 mbedtls_free(operation->ctx.hkdf.info);
4266 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4267 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004268#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004269#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4270 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4272 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4273 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4274 if (operation->ctx.tls12_prf.secret != NULL) {
4275 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4276 operation->ctx.tls12_prf.secret_length);
4277 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02004278 }
4279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 if (operation->ctx.tls12_prf.seed != NULL) {
4281 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4282 operation->ctx.tls12_prf.seed_length);
4283 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004284 }
4285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (operation->ctx.tls12_prf.label != NULL) {
4287 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4288 operation->ctx.tls12_prf.label_length);
4289 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004290 }
4291
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 if (operation->ctx.tls12_prf.other_secret != NULL) {
4293 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
4294 operation->ctx.tls12_prf.other_secret_length);
4295 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02004296 }
4297
Steven Cooremana6df6042021-04-29 19:32:25 +02004298 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004299
4300 /* We leave the fields Ai and output_block to be erased safely by the
4301 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004303#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4304 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004305#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
4307 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
4308 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
4309 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004310#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004311 {
4312 status = PSA_ERROR_BAD_STATE;
4313 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 mbedtls_platform_zeroize(operation, sizeof(*operation));
4315 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004316}
4317
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004318psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004320{
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004322 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004324 }
4325
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004326 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004328}
4329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4331 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004332{
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 if (operation->alg == 0) {
4334 return PSA_ERROR_BAD_STATE;
4335 }
4336 if (capacity > operation->capacity) {
4337 return PSA_ERROR_INVALID_ARGUMENT;
4338 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004339 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004341}
4342
Przemek Stekiel69c46792022-06-10 12:59:51 +02004343#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004344/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004345static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4346 psa_algorithm_t kdf_alg,
4347 uint8_t *output,
4348 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004349{
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4351 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004352 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004353 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004354#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004356#else
4357 const uint8_t last_block = 0xff;
4358#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 if (hkdf->state < HKDF_STATE_KEYED ||
4361 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004362#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004364#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 )) {
4366 return PSA_ERROR_BAD_STATE;
4367 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004368 hkdf->state = HKDF_STATE_OUTPUT;
4369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004371 /* Copy what remains of the current block */
4372 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004374 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 }
4376 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004377 output += n;
4378 output_length -= n;
4379 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004381 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004383 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004384 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004385 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004386 * object was corrupted or if this function is called directly
4387 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 if (hkdf->block_number == last_block) {
4389 return PSA_ERROR_BAD_STATE;
4390 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004391
4392 /* We need a new block */
4393 ++hkdf->block_number;
4394 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004395
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4397 hash_alg,
4398 hkdf->prk,
4399 hash_length);
4400 if (status != PSA_SUCCESS) {
4401 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004402 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004403
4404 if (hkdf->block_number != 1) {
4405 status = psa_mac_update(&hkdf->hmac,
4406 hkdf->output_block,
4407 hash_length);
4408 if (status != PSA_SUCCESS) {
4409 return status;
4410 }
4411 }
4412 status = psa_mac_update(&hkdf->hmac,
4413 hkdf->info,
4414 hkdf->info_length);
4415 if (status != PSA_SUCCESS) {
4416 return status;
4417 }
4418 status = psa_mac_update(&hkdf->hmac,
4419 &hkdf->block_number, 1);
4420 if (status != PSA_SUCCESS) {
4421 return status;
4422 }
4423 status = psa_mac_sign_finish(&hkdf->hmac,
4424 hkdf->output_block,
4425 sizeof(hkdf->output_block),
4426 &hmac_output_length);
4427 if (status != PSA_SUCCESS) {
4428 return status;
4429 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004430 }
4431
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004433}
Przemek Stekiel69c46792022-06-10 12:59:51 +02004434#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004435
John Durkop07cc04a2020-11-16 22:08:34 -08004436#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4437 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004438static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4439 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004441{
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4443 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02004444 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4445 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004446 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004447
Janos Follath7742fee2019-06-17 12:58:10 +01004448 /* We can't be wanting more output after block 0xff, otherwise
4449 * the capacity check in psa_key_derivation_output_bytes() would have
4450 * prevented this call. It could happen only if the operation
4451 * object was corrupted or if this function is called directly
4452 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 if (tls12_prf->block_number == 0xff) {
4454 return PSA_ERROR_CORRUPTION_DETECTED;
4455 }
Janos Follath7742fee2019-06-17 12:58:10 +01004456
4457 /* We need a new block */
4458 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004459 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004460
4461 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4462 *
4463 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4464 *
4465 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4466 * HMAC_hash(secret, A(2) + seed) +
4467 * HMAC_hash(secret, A(3) + seed) + ...
4468 *
4469 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004470 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004471 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004472 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004473 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004474 * is currently extracted as `output_block` and where i is
4475 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004476 */
4477
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 status = psa_key_derivation_start_hmac(&hmac,
4479 hash_alg,
4480 tls12_prf->secret,
4481 tls12_prf->secret_length);
4482 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004483 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004485
4486 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004488 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4489 * the variable seed and in this instance means it in the context of the
4490 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 status = psa_mac_update(&hmac,
4492 tls12_prf->label,
4493 tls12_prf->label_length);
4494 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004495 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 }
4497 status = psa_mac_update(&hmac,
4498 tls12_prf->seed,
4499 tls12_prf->seed_length);
4500 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004501 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 }
4503 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004504 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4506 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004507 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004509 }
4510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 status = psa_mac_sign_finish(&hmac,
4512 tls12_prf->Ai, hash_length,
4513 &hmac_output_length);
4514 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02004515 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 }
4517 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004518 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004520
4521 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 status = psa_key_derivation_start_hmac(&hmac,
4523 hash_alg,
4524 tls12_prf->secret,
4525 tls12_prf->secret_length);
4526 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004527 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 }
4529 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4530 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004531 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 }
4533 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4534 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004535 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 }
4537 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4538 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004539 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 }
4541 status = psa_mac_sign_finish(&hmac,
4542 tls12_prf->output_block, hash_length,
4543 &hmac_output_length);
4544 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004545 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004547
Janos Follath7742fee2019-06-17 12:58:10 +01004548
4549cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 cleanup_status = psa_mac_abort(&hmac);
4551 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004552 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004556}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004557
Janos Follath844eb0e2019-06-19 12:10:49 +01004558static psa_status_t psa_key_derivation_tls12_prf_read(
4559 psa_tls12_prf_key_derivation_t *tls12_prf,
4560 psa_algorithm_t alg,
4561 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004563{
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4565 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004566 psa_status_t status;
4567 uint8_t offset, length;
4568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02004570 case PSA_TLS12_PRF_STATE_LABEL_SET:
4571 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4572 break;
4573 case PSA_TLS12_PRF_STATE_OUTPUT:
4574 break;
4575 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02004577 }
4578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004580 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 if (tls12_prf->left_in_block == 0) {
4582 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4583 alg);
4584 if (status != PSA_SUCCESS) {
4585 return status;
4586 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004587
4588 continue;
4589 }
4590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004592 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004594 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004596
4597 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004599 output += length;
4600 output_length -= length;
4601 tls12_prf->left_in_block -= length;
4602 }
4603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004605}
John Durkop07cc04a2020-11-16 22:08:34 -08004606#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4607 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004608
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004609#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
4610static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
4611 psa_tls12_ecjpake_to_pms_t *ecjpake,
4612 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004614{
4615 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04004616 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004617
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 if (output_length != 32) {
4619 return PSA_ERROR_INVALID_ARGUMENT;
4620 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
4623 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
4624 &output_size);
4625 if (status != PSA_SUCCESS) {
4626 return status;
4627 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 if (output_size != output_length) {
4630 return PSA_ERROR_GENERIC_ERROR;
4631 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004634}
4635#endif
4636
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004637psa_status_t psa_key_derivation_output_bytes(
4638 psa_key_derivation_operation_t *operation,
4639 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004641{
4642 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004646 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004648 }
4649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004651 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004652 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004653 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004654 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004655 goto exit;
4656 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004658 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004659 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004660 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4661 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004663 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004665 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004667
Przemek Stekiel69c46792022-06-10 12:59:51 +02004668#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4670 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
4671 output, output_length);
4672 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004673#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004674#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4675 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4677 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4678 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4679 kdf_alg, output,
4680 output_length);
4681 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004682#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4683 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004684#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004686 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
4688 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004689#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
4690
Gilles Peskineeab56e42018-07-12 17:12:33 +02004691 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004692 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004694 }
4695
4696exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004698 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004699 * This allows us to differentiate between exhausted operations and
4700 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4701 * operations. */
4702 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004704 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004706 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004708}
4709
David Brown7807bf72021-02-09 16:28:23 -07004710#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01004711static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004712{
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 if (data_size >= 8) {
4714 mbedtls_des_key_set_parity(data);
4715 }
4716 if (data_size >= 16) {
4717 mbedtls_des_key_set_parity(data + 8);
4718 }
4719 if (data_size >= 24) {
4720 mbedtls_des_key_set_parity(data + 16);
4721 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004722}
David Brown7807bf72021-02-09 16:28:23 -07004723#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004724
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004725/*
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 * ECC keys on a Weierstrass elliptic curve require the generation
4727 * of a private key which is an integer
4728 * in the range [1, N - 1], where N is the boundary of the private key domain:
4729 * N is the prime p for Diffie-Hellman, or the order of the
4730 * curve’s base point for ECC.
4731 *
4732 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
4733 * This function generates the private key using the following process:
4734 *
4735 * 1. Draw a byte string of length ceiling(m/8) bytes.
4736 * 2. If m is not a multiple of 8, set the most significant
4737 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
4738 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
4739 * 4. If k > N - 2, discard the result and return to step 1.
4740 * 5. Output k + 1 as the private key.
4741 *
4742 * This method allows compliance to NIST standards, specifically the methods titled
4743 * Key-Pair Generation by Testing Candidates in the following publications:
4744 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
4745 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
4746 * Diffie-Hellman keys.
4747 *
4748 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
4749 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
4750 *
4751 * Note: Function allocates memory for *data buffer, so given *data should be
4752 * always NULL.
4753 */
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004754#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4755 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4756 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4757 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
4758 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004759static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004760 psa_key_slot_t *slot,
4761 size_t bits,
4762 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004763 uint8_t **data
4764 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004765{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004766 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004767 mbedtls_mpi k;
4768 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004770 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004771
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 mbedtls_mpi_init(&k);
4773 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004774
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004775 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004777 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004779
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004781 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01004782 goto cleanup;
4783 }
4784
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004785 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004787
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004789
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004790 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004791 /* Let m be the bit size of N. */
4792 size_t m = ecp_group.nbits;
4793
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004795
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004796 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004798
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004799 /* Note: This function is always called with *data == NULL and it
4800 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 *data = mbedtls_calloc(1, m_bytes);
4802 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004803 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004804 goto cleanup;
4805 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004808 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004810 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004812
4813 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004815 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004816 * (8 * ceiling(m/8) - m) bits of the first byte in
4817 * the string to zero.
4818 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004820 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004821 }
4822
4823 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 * big-endian byte string.
4825 */
4826 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004827
4828 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 * Result of comparison is returned. When it indicates error
4830 * then this function is called again.
4831 */
4832 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004833 }
4834
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004835 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
4837 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004838cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 if (ret != 0) {
4840 status = mbedtls_to_psa_error(ret);
4841 }
4842 if (status != PSA_SUCCESS) {
4843 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004844 *data = NULL;
4845 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 mbedtls_mpi_free(&k);
4847 mbedtls_mpi_free(&diff_N_2);
4848 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004849}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004850
4851/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
4852 * is determined by the curve, and sets the mandatory bits accordingly. That is:
4853 *
4854 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
4855 * draw a 32-byte string and process it as specified in
4856 * Elliptic Curves for Security [RFC7748] §5.
4857 *
4858 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
4859 * draw a 56-byte string and process it as specified in [RFC7748] §5.
4860 *
4861 * Note: Function allocates memory for *data buffer, so given *data should be
4862 * always NULL.
4863 */
4864
4865static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
4866 size_t bits,
4867 psa_key_derivation_operation_t *operation,
4868 uint8_t **data
4869 )
4870{
4871 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004872 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004875 case 255:
4876 output_length = 32;
4877 break;
4878 case 448:
4879 output_length = 56;
4880 break;
4881 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004883 break;
4884 }
4885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 if (*data == NULL) {
4889 return PSA_ERROR_INSUFFICIENT_MEMORY;
4890 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004895 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004897
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004899 case 255:
4900 (*data)[0] &= 248;
4901 (*data)[31] &= 127;
4902 (*data)[31] |= 64;
4903 break;
4904 case 448:
4905 (*data)[0] &= 252;
4906 (*data)[55] |= 128;
4907 break;
4908 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004910 break;
4911 }
4912
4913 return status;
4914}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004915#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
4916 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
4917 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4918 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
4919 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004920
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004921static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004922 psa_key_slot_t *slot,
4923 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004925{
4926 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05304928 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004929 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004930
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
4932 return PSA_ERROR_INVALID_ARGUMENT;
4933 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004934
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004935#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4936 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4937 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4938 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
4939 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
4941 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
4942 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004943 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
4945 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004946 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 }
4948 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004949 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
4951 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004952 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004954 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004955 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004956#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
4957 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
4958 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4959 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
4960 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 if (key_type_is_raw_bytes(slot->attr.type)) {
4962 if (bits % 8 != 0) {
4963 return PSA_ERROR_INVALID_ARGUMENT;
4964 }
4965 data = mbedtls_calloc(1, bytes);
4966 if (data == NULL) {
4967 return PSA_ERROR_INSUFFICIENT_MEMORY;
4968 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 status = psa_key_derivation_output_bytes(operation, data, bytes);
4971 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004972 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 }
David Brown12ca5032021-02-11 11:02:00 -07004974#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4976 psa_des_set_key_parity(data, bytes);
4977 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01004978#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 } else {
4980 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004981 }
Ronald Crondd04d422020-11-28 15:14:42 +01004982
Ronald Cronbf33c932020-11-28 18:06:53 +01004983 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004984 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004986 };
4987
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
4989 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
4990 &storage_size);
4991 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05304992 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 }
Archanad8a83dc2021-06-14 10:04:16 +05304994 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 status = psa_allocate_buffer_to_slot(slot, storage_size);
4996 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05304997 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 }
Archanad8a83dc2021-06-14 10:04:16 +05304999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 status = psa_driver_wrapper_import_key(&attributes,
5001 data, bytes,
5002 slot->key.data,
5003 slot->key.bytes,
5004 &slot->key.bytes, &bits);
5005 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005006 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005008
5009exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 mbedtls_free(data);
5011 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005012}
5013
Gilles Peskine449bd832023-01-11 14:50:10 +01005014psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5015 psa_key_derivation_operation_t *operation,
5016 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005017{
5018 psa_status_t status;
5019 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005020 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005021
Ronald Cron81709fc2020-11-14 12:10:32 +01005022 *key = MBEDTLS_SVC_KEY_ID_INIT;
5023
Gilles Peskine0f84d622019-09-12 19:03:13 +02005024 /* Reject any attempt to create a zero-length key so that we don't
5025 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 if (psa_get_key_bits(attributes) == 0) {
5027 return PSA_ERROR_INVALID_ARGUMENT;
5028 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 if (operation->alg == PSA_ALG_NONE) {
5031 return PSA_ERROR_BAD_STATE;
5032 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005033
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 if (!operation->can_output_key) {
5035 return PSA_ERROR_NOT_PERMITTED;
5036 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005037
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5039 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005040#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005042 /* Deriving a key in a secure element is not implemented yet. */
5043 status = PSA_ERROR_NOT_SUPPORTED;
5044 }
5045#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 if (status == PSA_SUCCESS) {
5047 status = psa_generate_derived_key_internal(slot,
5048 attributes->core.bits,
5049 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005050 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 if (status == PSA_SUCCESS) {
5052 status = psa_finish_key_creation(slot, driver, key);
5053 }
5054 if (status != PSA_SUCCESS) {
5055 psa_fail_key_creation(slot, driver);
5056 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005059}
5060
Gilles Peskineeab56e42018-07-12 17:12:33 +02005061
5062
5063/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005064/* Key derivation */
5065/****************************************************************/
5066
Steven Cooreman932ffb72021-02-15 12:14:32 +01005067#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005068static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005069{
5070#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5072 return 1;
5073 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005074#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5077 return 1;
5078 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005079#endif
5080#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5082 return 1;
5083 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005084#endif
5085#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5087 return 1;
5088 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005089#endif
5090#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5092 return 1;
5093 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005094#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005095#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5097 return 1;
5098 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005099#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005101}
5102
Gilles Peskine449bd832023-01-11 14:50:10 +01005103static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005104{
5105 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 psa_status_t status = psa_hash_setup(&operation, alg);
5107 psa_hash_abort(&operation);
5108 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005109}
5110
Gilles Peskine969c5d62019-01-16 15:53:06 +01005111static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005114{
Janos Follath5fe19732019-06-20 15:09:30 +01005115 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5116 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005118
Gilles Peskine969c5d62019-01-16 15:53:06 +01005119 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 if (!is_kdf_alg_supported(kdf_alg)) {
5121 return PSA_ERROR_NOT_SUPPORTED;
5122 }
John Durkop07cc04a2020-11-16 22:08:34 -08005123
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005124 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005125 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5127 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5128 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5129 if (hash_size == 0) {
5130 return PSA_ERROR_NOT_SUPPORTED;
5131 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005132
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005133 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5134 * we might fail later, which is somewhat unfriendly and potentially
5135 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 psa_status_t status = psa_hash_try_support(hash_alg);
5137 if (status != PSA_SUCCESS) {
5138 return status;
5139 }
5140 } else {
5141 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005142 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5145 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5146 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5147 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005148 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005149#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005150 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5152 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005153 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005155#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5156 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 operation->capacity = 255 * hash_size;
5158 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005159}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005160
Gilles Peskine449bd832023-01-11 14:50:10 +01005161static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005162{
5163#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 if (alg == PSA_ALG_ECDH) {
5165 return PSA_SUCCESS;
5166 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005167#endif
5168 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005170}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005171
5172static int psa_key_derivation_allows_free_form_secret_input(
5173 psa_algorithm_t kdf_alg)
5174{
5175#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5176 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5177 return 0;
5178 }
5179#endif
5180 (void) kdf_alg;
5181 return 1;
5182}
John Durkop07cc04a2020-11-16 22:08:34 -08005183#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5186 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005187{
5188 psa_status_t status;
5189
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 if (operation->alg != 0) {
5191 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005192 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5195 return PSA_ERROR_INVALID_ARGUMENT;
5196 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5197#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5198 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5199 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5200 status = psa_key_agreement_try_support(ka_alg);
5201 if (status != PSA_SUCCESS) {
5202 return status;
5203 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005204 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
5205 return PSA_ERROR_INVALID_ARGUMENT;
5206 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
5208#else
5209 return PSA_ERROR_NOT_SUPPORTED;
5210#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5211 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
5212#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5213 status = psa_key_derivation_setup_kdf(operation, alg);
5214#else
5215 return PSA_ERROR_NOT_SUPPORTED;
5216#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5217 } else {
5218 return PSA_ERROR_INVALID_ARGUMENT;
5219 }
5220
5221 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005222 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 }
5224 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005225}
5226
Przemek Stekiel69c46792022-06-10 12:59:51 +02005227#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005228static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
5229 psa_algorithm_t kdf_alg,
5230 psa_key_derivation_step_t step,
5231 const uint8_t *data,
5232 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005233{
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005235 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02005237 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005238#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5240 return PSA_ERROR_INVALID_ARGUMENT;
5241 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005242#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 if (hkdf->state != HKDF_STATE_INIT) {
5244 return PSA_ERROR_BAD_STATE;
5245 } else {
5246 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5247 hash_alg,
5248 data, data_length);
5249 if (status != PSA_SUCCESS) {
5250 return status;
5251 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005252 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005254 }
Gilles Peskine03410b52019-05-16 16:05:19 +02005255 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005256#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005258 /* We shouldn't be in different state as HKDF_EXPAND only allows
5259 * two inputs: SECRET (this case) and INFO which does not modify
5260 * the state. It could happen only if the hkdf
5261 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 if (hkdf->state != HKDF_STATE_INIT) {
5263 return PSA_ERROR_BAD_STATE;
5264 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005265
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005266 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
5268 return PSA_ERROR_INVALID_ARGUMENT;
5269 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005270
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 memcpy(hkdf->prk, data, data_length);
5272 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005273#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005274 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005275 /* HKDF: If no salt was provided, use an empty salt.
5276 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005278#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5280 return PSA_ERROR_BAD_STATE;
5281 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005282#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5284 hash_alg,
5285 NULL, 0);
5286 if (status != PSA_SUCCESS) {
5287 return status;
5288 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005289 hkdf->state = HKDF_STATE_STARTED;
5290 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 if (hkdf->state != HKDF_STATE_STARTED) {
5292 return PSA_ERROR_BAD_STATE;
5293 }
5294 status = psa_mac_update(&hkdf->hmac,
5295 data, data_length);
5296 if (status != PSA_SUCCESS) {
5297 return status;
5298 }
5299 status = psa_mac_sign_finish(&hkdf->hmac,
5300 hkdf->prk,
5301 sizeof(hkdf->prk),
5302 &data_length);
5303 if (status != PSA_SUCCESS) {
5304 return status;
5305 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005306 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005307
Gilles Peskine2b522db2019-04-12 15:11:49 +02005308 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005309 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005310#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005312 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005314 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005316#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005317 {
5318 /* Block 0 is empty, and the next block will be
5319 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005321 }
5322
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02005324 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005325#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5327 return PSA_ERROR_INVALID_ARGUMENT;
5328 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005329#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02005330#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
5332 hkdf->state == HKDF_STATE_INIT) {
5333 return PSA_ERROR_BAD_STATE;
5334 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02005335#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 if (hkdf->state == HKDF_STATE_OUTPUT) {
5337 return PSA_ERROR_BAD_STATE;
5338 }
5339 if (hkdf->info_set) {
5340 return PSA_ERROR_BAD_STATE;
5341 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005342 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 if (data_length != 0) {
5344 hkdf->info = mbedtls_calloc(1, data_length);
5345 if (hkdf->info == NULL) {
5346 return PSA_ERROR_INSUFFICIENT_MEMORY;
5347 }
5348 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005349 }
5350 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005352 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005354 }
5355}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005356#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005357
John Durkop07cc04a2020-11-16 22:08:34 -08005358#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5359 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005360static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
5361 const uint8_t *data,
5362 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01005363{
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
5365 return PSA_ERROR_BAD_STATE;
5366 }
Janos Follathf08e2652019-06-13 09:05:41 +01005367
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 if (data_length != 0) {
5369 prf->seed = mbedtls_calloc(1, data_length);
5370 if (prf->seed == NULL) {
5371 return PSA_ERROR_INSUFFICIENT_MEMORY;
5372 }
Janos Follathf08e2652019-06-13 09:05:41 +01005373
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005375 prf->seed_length = data_length;
5376 }
Janos Follathf08e2652019-06-13 09:05:41 +01005377
Gilles Peskine43f958b2020-12-13 14:55:14 +01005378 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005379
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01005381}
5382
Gilles Peskine449bd832023-01-11 14:50:10 +01005383static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
5384 const uint8_t *data,
5385 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01005386{
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
5388 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
5389 return PSA_ERROR_BAD_STATE;
5390 }
Janos Follath81550542019-06-13 14:26:34 +01005391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 if (data_length != 0) {
5393 prf->secret = mbedtls_calloc(1, data_length);
5394 if (prf->secret == NULL) {
5395 return PSA_ERROR_INSUFFICIENT_MEMORY;
5396 }
Steven Cooremana6df6042021-04-29 19:32:25 +02005397
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005399 prf->secret_length = data_length;
5400 }
Janos Follath81550542019-06-13 14:26:34 +01005401
Gilles Peskine43f958b2020-12-13 14:55:14 +01005402 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01005405}
5406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
5408 const uint8_t *data,
5409 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01005410{
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
5412 return PSA_ERROR_BAD_STATE;
5413 }
Janos Follath63028dd2019-06-13 09:15:47 +01005414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 if (data_length != 0) {
5416 prf->label = mbedtls_calloc(1, data_length);
5417 if (prf->label == NULL) {
5418 return PSA_ERROR_INSUFFICIENT_MEMORY;
5419 }
Janos Follath63028dd2019-06-13 09:15:47 +01005420
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005422 prf->label_length = data_length;
5423 }
Janos Follath63028dd2019-06-13 09:15:47 +01005424
Gilles Peskine43f958b2020-12-13 14:55:14 +01005425 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005426
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01005428}
5429
Gilles Peskine449bd832023-01-11 14:50:10 +01005430static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
5431 psa_key_derivation_step_t step,
5432 const uint8_t *data,
5433 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01005434{
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01005436 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01005438 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01005440 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01005442 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01005444 }
5445}
John Durkop07cc04a2020-11-16 22:08:34 -08005446#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5447 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5448
5449#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5450static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5451 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005452 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08005454{
5455 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
5457 4 + data_length + prf->other_secret_length :
5458 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005459
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
5461 return PSA_ERROR_INVALID_ARGUMENT;
5462 }
John Durkop07cc04a2020-11-16 22:08:34 -08005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 uint8_t *pms = mbedtls_calloc(1, pms_len);
5465 if (pms == NULL) {
5466 return PSA_ERROR_INSUFFICIENT_MEMORY;
5467 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005468 uint8_t *cur = pms;
5469
5470 /* pure-PSK:
5471 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08005472 *
5473 * The premaster secret is formed as follows: if the PSK is N octets
5474 * long, concatenate a uint16 with the value N, N zero octets, a second
5475 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005476 *
5477 * mixed-PSK:
5478 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
5479 * follows: concatenate a uint16 with the length of the other secret,
5480 * the other secret itself, uint16 with the length of PSK, and the
5481 * PSK itself.
5482 * For details please check:
5483 * - RFC 4279, Section 4 for the definition of RSA-PSK,
5484 * - RFC 4279, Section 3 for the definition of DHE-PSK,
5485 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08005486 */
5487
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
5489 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
5490 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
5491 if (prf->other_secret_length != 0) {
5492 memcpy(cur, prf->other_secret, prf->other_secret_length);
5493 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02005494 cur += prf->other_secret_length;
5495 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 } else {
5497 *cur++ = MBEDTLS_BYTE_1(data_length);
5498 *cur++ = MBEDTLS_BYTE_0(data_length);
5499 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005500 cur += data_length;
5501 }
5502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 *cur++ = MBEDTLS_BYTE_1(data_length);
5504 *cur++ = MBEDTLS_BYTE_0(data_length);
5505 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005506 cur += data_length;
5507
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08005509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 mbedtls_platform_zeroize(pms, pms_len);
5511 mbedtls_free(pms);
5512 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08005513}
Janos Follath6660f0e2019-06-17 08:44:03 +01005514
Przemek Stekiele47201b2022-04-19 13:53:28 +02005515static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
5516 psa_tls12_prf_key_derivation_t *prf,
5517 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02005519{
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
5521 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02005522 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005523
5524 if (data_length != 0) {
5525 prf->other_secret = mbedtls_calloc(1, data_length);
5526 if (prf->other_secret == NULL) {
5527 return PSA_ERROR_INSUFFICIENT_MEMORY;
5528 }
5529
5530 memcpy(prf->other_secret, data, data_length);
5531 prf->other_secret_length = data_length;
5532 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02005533 prf->other_secret_length = 0;
5534 }
5535
5536 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
5537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02005539}
5540
Janos Follath6660f0e2019-06-17 08:44:03 +01005541static psa_status_t psa_tls12_prf_psk_to_ms_input(
5542 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005543 psa_key_derivation_step_t step,
5544 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01005546{
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02005548 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 return psa_tls12_prf_psk_to_ms_set_key(prf,
5550 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02005551 break;
5552 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
5554 data,
5555 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02005556 break;
5557 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02005559 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01005560
Przemek Stekiele47201b2022-04-19 13:53:28 +02005561 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005562}
John Durkop07cc04a2020-11-16 22:08:34 -08005563#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005564
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005565#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5566static psa_status_t psa_tls12_ecjpake_to_pms_input(
5567 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04005568 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005569 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005571{
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
5573 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
5574 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005575 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005576
5577 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 if (data[0] != 0x04) {
5579 return PSA_ERROR_INVALID_ARGUMENT;
5580 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005581
5582 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005586}
5587#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02005588/** Check whether the given key type is acceptable for the given
5589 * input step of a key derivation.
5590 *
5591 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5592 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5593 * Both secret and non-secret inputs can alternatively have the type
5594 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5595 * that the input was passed as a buffer rather than via a key object.
5596 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005597static int psa_key_derivation_check_input_type(
5598 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02005600{
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005602 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 if (key_type == PSA_KEY_TYPE_DERIVE) {
5604 return PSA_SUCCESS;
5605 }
5606 if (key_type == PSA_KEY_TYPE_NONE) {
5607 return PSA_SUCCESS;
5608 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005609 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02005610 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 if (key_type == PSA_KEY_TYPE_DERIVE) {
5612 return PSA_SUCCESS;
5613 }
5614 if (key_type == PSA_KEY_TYPE_NONE) {
5615 return PSA_SUCCESS;
5616 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02005617 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005618 case PSA_KEY_DERIVATION_INPUT_LABEL:
5619 case PSA_KEY_DERIVATION_INPUT_SALT:
5620 case PSA_KEY_DERIVATION_INPUT_INFO:
5621 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
5623 return PSA_SUCCESS;
5624 }
5625 if (key_type == PSA_KEY_TYPE_NONE) {
5626 return PSA_SUCCESS;
5627 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005628 break;
5629 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005631}
5632
Janos Follathb80a94e2019-06-12 15:54:46 +01005633static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005634 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005635 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005636 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005637 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005639{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005640 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 status = psa_key_derivation_check_input_type(step, key_type);
5644 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005645 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005647
Przemek Stekiel69c46792022-06-10 12:59:51 +02005648#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5650 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
5651 step, data, data_length);
5652 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005653#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005654#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5656 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
5657 step, data, data_length);
5658 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005659#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5660#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5662 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
5663 step, data, data_length);
5664 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005665#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005666#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005668 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
5670 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005671#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005672 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005673 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005674 (void) data;
5675 (void) data_length;
5676 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005678 }
5679
Gilles Peskine224b0d62019-09-23 18:13:17 +02005680exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 if (status != PSA_SUCCESS) {
5682 psa_key_derivation_abort(operation);
5683 }
5684 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005685}
5686
Janos Follath51f4a0f2019-06-14 11:35:55 +01005687psa_status_t psa_key_derivation_input_bytes(
5688 psa_key_derivation_operation_t *operation,
5689 psa_key_derivation_step_t step,
5690 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005692{
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 return psa_key_derivation_input_internal(operation, step,
5694 PSA_KEY_TYPE_NONE,
5695 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005696}
5697
Janos Follath51f4a0f2019-06-14 11:35:55 +01005698psa_status_t psa_key_derivation_input_key(
5699 psa_key_derivation_operation_t *operation,
5700 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005702{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005703 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005704 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005705 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005706
Ronald Cron5c522922020-11-14 16:35:34 +01005707 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5709 if (status != PSA_SUCCESS) {
5710 psa_key_derivation_abort(operation);
5711 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02005712 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005713
5714 /* Passing a key object as a SECRET input unlocks the permission
5715 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005717 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 status = psa_key_derivation_input_internal(operation,
5721 step, slot->attr.type,
5722 slot->key.data,
5723 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005728}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005729
5730
5731
5732/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005733/* Key agreement */
5734/****************************************************************/
5735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
5737 const uint8_t *key_buffer,
5738 size_t key_buffer_size,
5739 psa_algorithm_t alg,
5740 const uint8_t *peer_key,
5741 size_t peer_key_length,
5742 uint8_t *shared_secret,
5743 size_t shared_secret_size,
5744 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005745{
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005747#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005748 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
5750 key_buffer_size, alg,
5751 peer_key, peer_key_length,
5752 shared_secret,
5753 shared_secret_size,
5754 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005755#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005756 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01005757 (void) attributes;
5758 (void) key_buffer;
5759 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01005760 (void) peer_key;
5761 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005762 (void) shared_secret;
5763 (void) shared_secret_size;
5764 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005766 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005767}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005768
Aditya Deshpande17845b82022-10-13 17:21:01 +01005769/** Internal function for raw key agreement
5770 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01005771 * to the driver's implementation if a driver is present.
5772 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01005773 * (psa_key_agreement_raw_builtin).
5774 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005775static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
5776 psa_key_slot_t *private_key,
5777 const uint8_t *peer_key,
5778 size_t peer_key_length,
5779 uint8_t *shared_secret,
5780 size_t shared_secret_size,
5781 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005782{
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5784 return PSA_ERROR_NOT_SUPPORTED;
5785 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01005786
5787 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01005789 };
5790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 return psa_driver_wrapper_key_agreement(&attributes,
5792 private_key->key.data,
5793 private_key->key.bytes, alg,
5794 peer_key, peer_key_length,
5795 shared_secret,
5796 shared_secret_size,
5797 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005798}
5799
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005800/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005801 * to potentially free embedded data structures and wipe confidential data.
5802 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005803static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5804 psa_key_derivation_step_t step,
5805 psa_key_slot_t *private_key,
5806 const uint8_t *peer_key,
5807 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005808{
5809 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00005810 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02005811 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005813
5814 /* Step 1: run the secret agreement algorithm to generate the shared
5815 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 status = psa_key_agreement_raw_internal(ka_alg,
5817 private_key,
5818 peer_key, peer_key_length,
5819 shared_secret,
5820 sizeof(shared_secret),
5821 &shared_secret_length);
5822 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005823 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005825
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005826 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005827 * the shared secret. A shared secret is permitted wherever a key
5828 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 status = psa_key_derivation_input_internal(operation, step,
5830 PSA_KEY_TYPE_DERIVE,
5831 shared_secret,
5832 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005833exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5835 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005836}
5837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5839 psa_key_derivation_step_t step,
5840 mbedtls_svc_key_id_t private_key,
5841 const uint8_t *peer_key,
5842 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005843{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005844 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005845 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005846 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5849 return PSA_ERROR_INVALID_ARGUMENT;
5850 }
Ronald Cron5c522922020-11-14 16:35:34 +01005851 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5853 if (status != PSA_SUCCESS) {
5854 return status;
5855 }
5856 status = psa_key_agreement_internal(operation, step,
5857 slot,
5858 peer_key, peer_key_length);
5859 if (status != PSA_SUCCESS) {
5860 psa_key_derivation_abort(operation);
5861 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005862 /* If a private key has been added as SECRET, we allow the derived
5863 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005865 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005867 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005872}
5873
Gilles Peskine449bd832023-01-11 14:50:10 +01005874psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5875 mbedtls_svc_key_id_t private_key,
5876 const uint8_t *peer_key,
5877 size_t peer_key_length,
5878 uint8_t *output,
5879 size_t output_size,
5880 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005881{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005882 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005883 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005884 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005887 status = PSA_ERROR_INVALID_ARGUMENT;
5888 goto exit;
5889 }
Ronald Cron5c522922020-11-14 16:35:34 +01005890 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5892 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005893 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005895
Gilles Peskine3e561302022-04-14 00:17:15 +02005896 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5897 * for the output size. The PSA specification only guarantees that this
5898 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5899 * but it might be nice to allow smaller buffers if the output fits.
5900 * At the time of writing this comment, with only ECDH implemented,
5901 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5902 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5903 * be exact for it as well. */
5904 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5906 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02005907 status = PSA_ERROR_BUFFER_TOO_SMALL;
5908 goto exit;
5909 }
5910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 status = psa_key_agreement_raw_internal(alg, slot,
5912 peer_key, peer_key_length,
5913 output, output_size,
5914 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005915
5916exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005918 /* If an error happens and is not handled properly, the output
5919 * may be used as a key to protect sensitive data. Arrange for such
5920 * a key to be random, which is likely to result in decryption or
5921 * verification errors. This is better than filling the buffer with
5922 * some constant data such as zeros, which would result in the data
5923 * being protected with a reproducible, easily knowable key.
5924 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005926 *output_length = output_size;
5927 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005928
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005932}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005933
5934
Gilles Peskine30524eb2020-11-13 17:02:26 +01005935
Gilles Peskine86a440b2018-11-12 18:39:40 +01005936/****************************************************************/
5937/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005938/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005939
Gilles Peskine30524eb2020-11-13 17:02:26 +01005940/** Initialize the PSA random generator.
5941 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005942static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005943{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005944#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005946#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5947
Gilles Peskine30524eb2020-11-13 17:02:26 +01005948 /* Set default configuration if
5949 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005951 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 }
5953 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005954 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005958#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5959 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5960 /* The PSA entropy injection feature depends on using NV seed as an entropy
5961 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 mbedtls_entropy_add_source(&rng->entropy,
5963 mbedtls_nv_seed_poll, NULL,
5964 MBEDTLS_ENTROPY_BLOCK_SIZE,
5965 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005966#endif
5967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005969#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005970}
5971
5972/** Deinitialize the PSA random generator.
5973 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005974static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005975{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005976#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005978#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5980 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005981#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005982}
5983
5984/** Seed the PSA random generator.
5985 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005986static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005987{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005988#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5989 /* Do nothing: the external RNG seeds itself. */
5990 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005992#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005993 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5995 drbg_seed, sizeof(drbg_seed) - 1);
5996 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005997#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005998}
5999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000psa_status_t psa_generate_random(uint8_t *output,
6001 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006002{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006003 GUARD_MODULE_INITIALIZED;
6004
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006005#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6006
6007 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6009 output, output_size,
6010 &output_length);
6011 if (status != PSA_SUCCESS) {
6012 return status;
6013 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006014 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006015 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 if (output_length != output_size) {
6017 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6018 }
6019 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006020
6021#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6022
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006024 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6026 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6027 output_size);
6028 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6029 output, request_size);
6030 if (ret != 0) {
6031 return mbedtls_to_psa_error(ret);
6032 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006033 output_size -= request_size;
6034 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006035 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006037#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006038}
6039
Gilles Peskine8814fc42020-12-14 15:33:44 +01006040/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006041 *
6042 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6043 * `psa_generate_random(...)`. The state parameter is ignored since the
6044 * PSA API doesn't support passing an explicit state.
6045 *
6046 * In the non-external case, psa_generate_random() calls an
6047 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6048 * and semantics as mbedtls_psa_get_random(). As an optimization,
6049 * instead of doing this back-and-forth between the PSA API and the
6050 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6051 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006052 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006053#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6054int mbedtls_psa_get_random(void *p_rng,
6055 unsigned char *output,
6056 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006057{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006058 /* This function takes a pointer to the RNG state because that's what
6059 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6060 * its own state internally and doesn't let the caller access that state.
6061 * So we just ignore the state parameter, and in practice we'll pass
6062 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006063 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 psa_status_t status = psa_generate_random(output, output_size);
6065 if (status == PSA_SUCCESS) {
6066 return 0;
6067 } else {
6068 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6069 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006070}
6071#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6072
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006073#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006074#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6077 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006078{
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 if (global_data.initialized) {
6080 return PSA_ERROR_NOT_PERMITTED;
6081 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6084 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6085 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6086 return PSA_ERROR_INVALID_ARGUMENT;
6087 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006090}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006091#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006092
Ronald Cron3772afe2021-02-08 16:10:05 +01006093/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006094 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006095 * \param type The key type
6096 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006097 *
6098 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006099 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006100 * \retval #PSA_ERROR_INVALID_ARGUMENT
6101 * The size in bits of the key is not valid.
6102 * \retval #PSA_ERROR_NOT_SUPPORTED
6103 * The type and/or the size in bits of the key or the combination of
6104 * the two is not supported.
6105 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006106static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006108{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006110
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 if (key_type_is_raw_bytes(type)) {
6112 status = psa_validate_unstructured_key_bit_size(type, bits);
6113 if (status != PSA_SUCCESS) {
6114 return status;
6115 }
6116 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006117#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6119 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6120 return PSA_ERROR_NOT_SUPPORTED;
6121 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006122
Ronald Cron01b2aba2020-10-05 09:42:02 +02006123 /* Accept only byte-aligned keys, for the same reasons as
6124 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 if (bits % 8 != 0) {
6126 return PSA_ERROR_NOT_SUPPORTED;
6127 }
6128 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006129#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006130
Ronald Cron5c4d3862020-12-07 11:07:24 +01006131#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006133 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 return PSA_SUCCESS;
6135 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006136#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006137 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006139 }
6140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006142}
6143
Ronald Cron55ed0592020-10-05 10:30:40 +02006144psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006145 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006147{
6148 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006149 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006150
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 if ((attributes->domain_parameters == NULL) &&
6152 (attributes->domain_parameters_size != 0)) {
6153 return PSA_ERROR_INVALID_ARGUMENT;
6154 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006155
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 if (key_type_is_raw_bytes(type)) {
6157 status = psa_generate_random(key_buffer, key_buffer_size);
6158 if (status != PSA_SUCCESS) {
6159 return status;
6160 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006161
David Brown12ca5032021-02-11 11:02:00 -07006162#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 if (type == PSA_KEY_TYPE_DES) {
6164 psa_des_set_key_parity(key_buffer, key_buffer_size);
6165 }
David Brown8107e312021-02-12 08:08:46 -07006166#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006168
Jaeden Amero424fa932021-05-14 08:34:32 +01006169#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6170 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6172 return mbedtls_psa_rsa_generate_key(attributes,
6173 key_buffer,
6174 key_buffer_size,
6175 key_buffer_length);
6176 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006177#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6178 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006179
John Durkop9814fa22020-11-04 12:28:15 -08006180#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6182 return mbedtls_psa_ecp_generate_key(attributes,
6183 key_buffer,
6184 key_buffer_size,
6185 key_buffer_length);
6186 } else
John Durkop9814fa22020-11-04 12:28:15 -08006187#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006188 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 (void) key_buffer_length;
6190 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006191 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006194}
Darryl Green0c6575a2018-11-07 16:05:30 +00006195
Gilles Peskine449bd832023-01-11 14:50:10 +01006196psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6197 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006198{
6199 psa_status_t status;
6200 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006201 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006202 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006203
Ronald Cron81709fc2020-11-14 12:10:32 +01006204 *key = MBEDTLS_SVC_KEY_ID_INIT;
6205
Gilles Peskine0f84d622019-09-12 19:03:13 +02006206 /* Reject any attempt to create a zero-length key so that we don't
6207 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 if (psa_get_key_bits(attributes) == 0) {
6209 return PSA_ERROR_INVALID_ARGUMENT;
6210 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006211
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006212 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
6214 return PSA_ERROR_INVALID_ARGUMENT;
6215 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
6218 &slot, &driver);
6219 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02006220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 }
Gilles Peskine11792082019-08-06 18:36:36 +02006222
Ronald Cron977c2472020-10-13 08:32:21 +02006223 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05306224 * storage ( thus not in the case of generating a key in a secure element
6225 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
6226 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 if (slot->key.data == NULL) {
6228 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
6229 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01006230 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 attributes->core.type, attributes->core.bits);
6232 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01006233 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 }
Ronald Cron3772afe2021-02-08 16:10:05 +01006235
6236 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 attributes->core.type,
6238 attributes->core.bits);
6239 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02006240 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 attributes, &key_buffer_size);
6242 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01006243 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 }
Ronald Cron977c2472020-10-13 08:32:21 +02006245 }
Ronald Cron977c2472020-10-13 08:32:21 +02006246
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
6248 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02006249 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 }
Ronald Cron977c2472020-10-13 08:32:21 +02006251 }
6252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 status = psa_driver_wrapper_generate_key(attributes,
6254 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 if (status != PSA_SUCCESS) {
6257 psa_remove_key_data_from_memory(slot);
6258 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02006259
Gilles Peskine11792082019-08-06 18:36:36 +02006260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 if (status == PSA_SUCCESS) {
6262 status = psa_finish_key_creation(slot, driver, key);
6263 }
6264 if (status != PSA_SUCCESS) {
6265 psa_fail_key_creation(slot, driver);
6266 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006267
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006269}
6270
Gilles Peskinee59236f2018-01-27 23:32:46 +01006271/****************************************************************/
6272/* Module setup */
6273/****************************************************************/
6274
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006275#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006276psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 void (* entropy_init)(mbedtls_entropy_context *ctx),
6278 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01006279{
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
6281 return PSA_ERROR_BAD_STATE;
6282 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006283 global_data.rng.entropy_init = entropy_init;
6284 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01006286}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006287#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006290{
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 psa_wipe_all_key_slots();
6292 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
6293 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01006294 }
6295 /* Wipe all remaining data, including configuration.
6296 * In particular, this sets all state indicator to the value
6297 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02006299
6300 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01006302}
6303
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006304#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6305/** Recover a transaction that was interrupted by a power failure.
6306 *
6307 * This function is called during initialization, before psa_crypto_init()
6308 * returns. If this function returns a failure status, the initialization
6309 * fails.
6310 */
6311static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006313{
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006315 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6316 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 /* TODO - fall through to the failure case until this
6318 * is implemented.
6319 * https://github.com/ARMmbed/mbed-crypto/issues/218
6320 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006321 default:
6322 /* We found an unsupported transaction in the storage.
6323 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006325 }
6326}
6327#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006330{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006331 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006332
Gilles Peskinec6b69072018-11-20 21:42:52 +01006333 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 if (global_data.initialized != 0) {
6335 return PSA_SUCCESS;
6336 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006337
Gilles Peskine30524eb2020-11-13 17:02:26 +01006338 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01006340 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 status = mbedtls_psa_random_seed(&global_data.rng);
6342 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006343 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01006345 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 status = psa_initialize_key_slots();
6348 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01006349 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01006351
Ronald Cron9ba76912021-04-10 16:57:30 +02006352 /* Init drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 status = psa_driver_wrapper_init();
6354 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02006355 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 }
Gilles Peskined9348f22019-10-01 15:22:29 +02006357
Gilles Peskine4b734222019-07-24 15:56:31 +02006358#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 status = psa_crypto_load_transaction();
6360 if (status == PSA_SUCCESS) {
6361 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
6362 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006363 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 }
6365 status = psa_crypto_stop_transaction();
6366 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02006367 /* There's no transaction to complete. It's all good. */
6368 status = PSA_SUCCESS;
6369 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006370#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006371
Gilles Peskinec6b69072018-11-20 21:42:52 +01006372 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006373 global_data.initialized = 1;
6374
6375exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 if (status != PSA_SUCCESS) {
6377 mbedtls_psa_crypto_free();
6378 }
6379 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006380}
6381
6382#endif /* MBEDTLS_PSA_CRYPTO_C */