blob: e01ee474fca77bf5f87ae4f083cdc0a1a7dde76b [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 Peskine2f9c4dc2018-01-28 13:16:24 +010051#include <stdlib.h>
52#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010055#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000057#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020058#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020060#include "mbedtls/chacha20.h"
61#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/cipher.h"
63#include "mbedtls/ccm.h"
64#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020066#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010067#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010068#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/error.h"
70#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000073#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000075#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000077#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
Paul Elliott588f8ed2022-12-02 18:10:26 +000083#include "hash_info.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
Paul Elliott588f8ed2022-12-02 18:10:26 +0000313 case MBEDTLS_ERR_ECP_IN_PROGRESS:
314 return PSA_OPERATION_INCOMPLETE;
315
Janos Follatha13b9052019-11-22 12:48:59 +0000316 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300318
Gilles Peskinee59236f2018-01-27 23:32:46 +0100319 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100321 }
322}
323
Paul Elliottdc42ca82023-02-24 18:11:59 +0000324/**
325 * \brief For output buffers which contain "tags"
326 * (outputs that may be checked for validity like
327 * hashes, MACs and signatures), fill the unused
328 * part of the output buffer (the whole buffer on
329 * error, the trailing part on success) with
330 * something that isn't a valid tag (barring an
331 * attack on the tag and deliberately-crafted
332 * input), in case the caller doesn't check the
333 * return status properly.
334 *
335 * \param output_buffer Pointer to buffer to wipe. May not be NULL
336 * unless \p output_buffer_size is zero.
337 * \param status Status of function called to generate
338 * output_buffer originally
339 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
340 * could be NULL.
341 * \param output_buffer_length Length of data written to output_buffer, must be
342 * less than \p output_buffer_size
343 */
344static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000345 size_t output_buffer_size, size_t output_buffer_length)
346{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000347 size_t offset = 0;
348
349 if (output_buffer_size == 0) {
350 /* If output_buffer_size is 0 then we have nothing to do. We must not
351 call memset because output_buffer may be NULL in this case */
352 return;
353 }
354
355 if (status == PSA_SUCCESS) {
356 offset = output_buffer_length;
357 }
358
359 memset(output_buffer + offset, '!', output_buffer_size - offset);
360}
361
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200362
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200363
364
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100365/****************************************************************/
366/* Key management */
367/****************************************************************/
368
John Durkop9814fa22020-11-04 12:28:15 -0800369#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800370 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100371 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
372 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
373 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100374mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
375 size_t bits,
376 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200377{
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100379 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100381#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100382 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100384#endif
385#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100386 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100388#endif
389#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100392#endif
393#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100396#endif
397#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100398 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100400 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (bits_is_sloppy) {
402 return MBEDTLS_ECP_DP_SECP521R1;
403 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100404 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100406 }
407 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100408
Paul Elliott8ff510a2020-06-02 17:19:28 +0100409 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100411#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100412 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100414#endif
415#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100416 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100418#endif
419#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100422#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100423 }
424 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100425
Paul Elliott8ff510a2020-06-02 17:19:28 +0100426 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100428#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (bits_is_sloppy) {
433 return MBEDTLS_ECP_DP_CURVE25519;
434 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100436#endif
437#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100438 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100440#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100441 }
442 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100443
Paul Elliott8ff510a2020-06-02 17:19:28 +0100444 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100446#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100449#endif
450#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#endif
454#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 }
459 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200460 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100461
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100462 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200464}
John Durkop9814fa22020-11-04 12:28:15 -0800465#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100466 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
467 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
468 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
469 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
472 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200473{
474 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200476 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200478 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200479 case PSA_KEY_TYPE_PASSWORD:
480 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200481 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100482#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200483 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (bits != 128 && bits != 192 && bits != 256) {
485 return PSA_ERROR_INVALID_ARGUMENT;
486 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 break;
488#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200489#if defined(PSA_WANT_KEY_TYPE_ARIA)
490 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (bits != 128 && bits != 192 && bits != 256) {
492 return PSA_ERROR_INVALID_ARGUMENT;
493 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200494 break;
495#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100496#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (bits != 128 && bits != 192 && bits != 256) {
499 return PSA_ERROR_INVALID_ARGUMENT;
500 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501 break;
502#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100503#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (bits != 64 && bits != 128 && bits != 192) {
506 return PSA_ERROR_INVALID_ARGUMENT;
507 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508 break;
509#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100510#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200511 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (bits != 256) {
513 return PSA_ERROR_INVALID_ARGUMENT;
514 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200515 break;
516#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200519 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (bits % 8 != 0) {
521 return PSA_ERROR_INVALID_ARGUMENT;
522 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200525}
526
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100527/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100528 *
Steven Cooremancd640932021-03-08 12:00:27 +0100529 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
530 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100531 *
532 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100533 * \param[in] key_type The key type of the key to be used with the
534 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100535 *
536 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100537 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100538 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100539 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100540 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100541MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100542 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100544{
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (PSA_ALG_IS_HMAC(algorithm)) {
546 if (key_type == PSA_KEY_TYPE_HMAC) {
547 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
552 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
553 * key. */
554 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
555 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
556 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
557 * the block length (larger than 1) for block ciphers. */
558 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
559 return PSA_SUCCESS;
560 }
561 }
562 }
563
564 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100565}
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
568 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200569{
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (slot->key.data != NULL) {
571 return PSA_ERROR_ALREADY_EXISTS;
572 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 slot->key.data = mbedtls_calloc(1, buffer_length);
575 if (slot->key.data == NULL) {
576 return PSA_ERROR_INSUFFICIENT_MEMORY;
577 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200578
Ronald Cronea0f8a62020-11-25 17:52:23 +0100579 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200581}
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
584 const uint8_t *data,
585 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200586{
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 psa_status_t status = psa_allocate_buffer_to_slot(slot,
588 data_length);
589 if (status != PSA_SUCCESS) {
590 return status;
591 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 memcpy(slot->key.data, data, data_length);
594 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200595}
596
Ronald Crona0fe59f2020-11-29 11:14:53 +0100597psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100598 const psa_key_attributes_t *attributes,
599 const uint8_t *data, size_t data_length,
600 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100602{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100603 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
604 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200606 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (data_length == 0) {
608 return PSA_ERROR_NOT_SUPPORTED;
609 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (key_type_is_raw_bytes(type)) {
612 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
615 *bits);
616 if (status != PSA_SUCCESS) {
617 return status;
618 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200619
Ronald Crondd04d422020-11-28 15:14:42 +0100620 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100622 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 return PSA_SUCCESS;
626 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800627#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
629 if (PSA_KEY_TYPE_IS_ECC(type)) {
630 return mbedtls_psa_ecp_import_key(attributes,
631 data, data_length,
632 key_buffer, key_buffer_size,
633 key_buffer_length,
634 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100635 }
John Durkop9814fa22020-11-04 12:28:15 -0800636#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
637 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700638#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
640 if (PSA_KEY_TYPE_IS_RSA(type)) {
641 return mbedtls_psa_rsa_import_key(attributes,
642 data, data_length,
643 key_buffer, key_buffer_size,
644 key_buffer_length,
645 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200646 }
John Durkop9814fa22020-11-04 12:28:15 -0800647#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
648 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100649 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100652}
653
Gilles Peskinef603c712019-01-19 13:40:11 +0100654/** Calculate the intersection of two algorithm usage policies.
655 *
656 * Return 0 (which allows no operation) on incompatibility.
657 */
658static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100659 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100660 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100662{
Gilles Peskine549ea862019-05-22 11:45:59 +0200663 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (alg1 == alg2) {
665 return alg1;
666 }
Steven Cooreman03488022021-02-18 12:07:20 +0100667 /* If the policies are from the same hash-and-sign family, check
668 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
670 PSA_ALG_IS_SIGN_HASH(alg2) &&
671 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
672 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
673 return alg2;
674 }
675 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
676 return alg1;
677 }
Steven Cooreman03488022021-02-18 12:07:20 +0100678 }
679 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100680 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100681 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
683 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
684 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
685 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
686 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100687 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100688
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100689 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
691 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
692 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
693 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100694 }
695 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
697 (alg1_len <= alg2_len)) {
698 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100699 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
701 (alg2_len <= alg1_len)) {
702 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100703 }
704 }
705 /* If the policies are from the same MAC family, check whether one
706 * of them is a minimum-MAC-length policy. Calculate the most
707 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
709 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
710 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100711 /* Validate the combination of key type and algorithm. Since the base
712 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
714 return 0;
715 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100716
Steven Cooreman31a876d2021-03-03 20:47:40 +0100717 /* Get the (exact or at-least) output lengths for both sides of the
718 * requested intersection. None of the currently supported algorithms
719 * have an output length dependent on the actual key size, so setting it
720 * to a bogus value of 0 is currently OK.
721 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100722 * Note that for at-least-this-length wildcard algorithms, the output
723 * length is set to the shortest allowed length, which allows us to
724 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
726 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100727 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100728
Steven Cooremana1d83222021-02-25 10:20:29 +0100729 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
731 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
732 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100733 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100734
735 /* If only one is an at-least-this-length policy, the intersection would
736 * be the other (fixed-length) policy as long as said fixed length is
737 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
739 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100740 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
742 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100743 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100744
Steven Cooremancd640932021-03-08 12:00:27 +0100745 /* If none of them are wildcards, check whether they define the same tag
746 * length. This is still possible here when one is default-length and
747 * the other specific-length. Ensure to always return the
748 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (alg1_len == alg2_len) {
750 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
751 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100752 }
753 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100755}
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757static int psa_key_algorithm_permits(psa_key_type_t key_type,
758 psa_algorithm_t policy_alg,
759 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200760{
Gilles Peskine549ea862019-05-22 11:45:59 +0200761 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (requested_alg == policy_alg) {
763 return 1;
764 }
Steven Cooreman03488022021-02-18 12:07:20 +0100765 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
766 * and requested_alg is the same hash-and-sign family with any hash,
767 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
769 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
770 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
771 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100772 }
773 /* If policy_alg is a wildcard AEAD algorithm of the same base as
774 * the requested algorithm, check the requested tag length to be
775 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (PSA_ALG_IS_AEAD(policy_alg) &&
777 PSA_ALG_IS_AEAD(requested_alg) &&
778 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
779 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
780 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
781 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
782 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100783 }
Steven Cooremancd640932021-03-08 12:00:27 +0100784 /* If policy_alg is a MAC algorithm of the same base as the requested
785 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (PSA_ALG_IS_MAC(policy_alg) &&
787 PSA_ALG_IS_MAC(requested_alg) &&
788 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
789 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100790 /* Validate the combination of key type and algorithm. Since the policy
791 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
793 return 0;
794 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100795
Steven Cooreman31a876d2021-03-03 20:47:40 +0100796 /* Get both the requested output length for the algorithm which is to be
797 * verified, and the default output length for the base algorithm.
798 * Note that none of the currently supported algorithms have an output
799 * length dependent on actual key size, so setting it to a bogus value
800 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100801 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100803 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 key_type, 0,
805 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100806
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100807 /* If the policy is default-length, only allow an algorithm with
808 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
810 return requested_output_length == default_output_length;
811 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100812
813 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100814 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
816 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
817 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100818 }
819
Steven Cooremancd640932021-03-08 12:00:27 +0100820 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
821 * check for the requested MAC length to be equal to or longer than the
822 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
824 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
825 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100826 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200827 }
Steven Cooremance48e852020-10-05 16:02:45 +0200828 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200829 * a key derivation with that key agreement should also be allowed. This
830 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
832 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
833 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
834 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200835 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100836 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200838}
839
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100840/** Test whether a policy permits an algorithm.
841 *
842 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100843 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100844 * \note This function requires providing the key type for which the policy is
845 * being validated, since some algorithm policy definitions (e.g. MAC)
846 * have different properties depending on what kind of cipher it is
847 * combined with.
848 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100849 * \retval PSA_SUCCESS When \p alg is a specific algorithm
850 * allowed by the \p policy.
851 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
852 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
853 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100854 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
856 psa_key_type_t key_type,
857 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100858{
Steven Cooremand788fab2021-02-25 11:29:17 +0100859 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (alg == 0) {
861 return PSA_ERROR_INVALID_ARGUMENT;
862 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100863
Steven Cooreman7e39f052021-02-23 14:18:32 +0100864 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (PSA_ALG_IS_WILDCARD(alg)) {
866 return PSA_ERROR_INVALID_ARGUMENT;
867 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
870 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
871 return PSA_SUCCESS;
872 } else {
873 return PSA_ERROR_NOT_PERMITTED;
874 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100875}
876
Gilles Peskinef603c712019-01-19 13:40:11 +0100877/** Restrict a key policy based on a constraint.
878 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100879 * \note This function requires providing the key type for which the policy is
880 * being restricted, since some algorithm policy definitions (e.g. MAC)
881 * have different properties depending on what kind of cipher it is
882 * combined with.
883 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100884 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100885 * \param[in,out] policy The policy to restrict.
886 * \param[in] constraint The policy constraint to apply.
887 *
888 * \retval #PSA_SUCCESS
889 * \c *policy contains the intersection of the original value of
890 * \c *policy and \c *constraint.
891 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100892 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100893 * \c *policy is unchanged.
894 */
895static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100896 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100897 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100899{
900 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 psa_key_policy_algorithm_intersection(key_type, policy->alg,
902 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200903 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
905 constraint->alg2);
906 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
907 return PSA_ERROR_INVALID_ARGUMENT;
908 }
909 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
910 return PSA_ERROR_INVALID_ARGUMENT;
911 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100912 policy->usage &= constraint->usage;
913 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200914 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100916}
917
Przemek Stekiel348410f2022-11-15 22:22:07 +0100918psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100919 mbedtls_svc_key_id_t key,
920 psa_key_slot_t **p_slot,
921 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100923{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200924 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
925 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 status = psa_get_and_lock_key_slot(key, p_slot);
928 if (status != PSA_SUCCESS) {
929 return status;
930 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200931 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100932
933 /* Enforce that usage policy for the key slot contains all the flags
934 * required by the usage parameter. There is one exception: public
935 * keys can always be exported, so we treat public key objects as
936 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100938 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100942 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200943 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100944 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100945
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800946 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (alg != 0) {
948 status = psa_key_policy_permits(&slot->attr.policy,
949 slot->attr.type,
950 alg);
951 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100952 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100954 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200957
958error:
959 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100963}
Darryl Green940d72c2018-07-13 13:18:51 +0100964
Ronald Cron5c522922020-11-14 16:35:34 +0100965/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200966 *
967 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200968 * available, as opposed to a key in a secure element and/or to be used
969 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200970 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200971 * This is a temporary function that may be used instead of
972 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
973 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200974 *
Ronald Cron5c522922020-11-14 16:35:34 +0100975 * On success, the returned key slot is locked. It is the responsibility of the
976 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200977 */
Ronald Cron5c522922020-11-14 16:35:34 +0100978static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
979 mbedtls_svc_key_id_t key,
980 psa_key_slot_t **p_slot,
981 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +0200983{
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
985 usage, alg);
986 if (status != PSA_SUCCESS) {
987 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200988 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
991 psa_unlock_key_slot(*p_slot);
992 *p_slot = NULL;
993 return PSA_ERROR_NOT_SUPPORTED;
994 }
995
996 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +0200997}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001000{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001001 /* Data pointer will always be either a valid pointer or NULL in an
1002 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if (slot->key.data != NULL) {
1004 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1005 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001008 slot->key.data = NULL;
1009 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001012}
1013
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001014/** Completely wipe a slot in memory, including its policy.
1015 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001017{
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 /*
1021 * As the return error code may not be handled in case of multiple errors,
1022 * do our best to report an unexpected lock counter. Assert with
1023 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1024 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1025 * function is called as part of the execution of a test suite, the
1026 * execution of the test suite is stopped in error if the assertion fails.
1027 */
1028 if (slot->lock_count != 1) {
1029 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001030 status = PSA_ERROR_CORRUPTION_DETECTED;
1031 }
1032
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001033 /* Multipart operations may still be using the key. This is safe
1034 * because all multipart operation objects are independent from
1035 * the key slot: if they need to access the key after the setup
1036 * phase, they have a copy of the key. Note that this means that
1037 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001038 /* At this point, key material and other type-specific content has
1039 * been wiped. Clear remaining metadata. We can call memset and not
1040 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 memset(slot, 0, sizeof(*slot));
1042 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001043}
1044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001046{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001047 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001048 psa_status_t status; /* status of the last operation */
1049 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001050#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1051 psa_se_drv_table_entry_t *driver;
1052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (mbedtls_svc_key_id_is_null(key)) {
1055 return PSA_SUCCESS;
1056 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001057
Ronald Cronf2911112020-10-29 17:51:10 +01001058 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001059 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001060 * key, this will load the key description from persistent memory if not
1061 * done yet. We cannot avoid this loading as without it we don't know if
1062 * the key is operated by an SE or not and this information is needed by
1063 * the current implementation.
1064 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 status = psa_get_and_lock_key_slot(key, &slot);
1066 if (status != PSA_SUCCESS) {
1067 return status;
1068 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001069
Ronald Cronf2911112020-10-29 17:51:10 +01001070 /*
1071 * If the key slot containing the key description is under access by the
1072 * library (apart from the present access), the key cannot be destroyed
1073 * yet. For the time being, just return in error. Eventually (to be
1074 * implemented), the key should be destroyed when all accesses have
1075 * stopped.
1076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if (slot->lock_count > 1) {
1078 psa_unlock_key_slot(slot);
1079 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001080 }
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001083 /* Refuse the destruction of a read-only key (which may or may not work
1084 * if we attempt it, depending on whether the key is merely read-only
1085 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001086 * Just do the best we can, which is to wipe the copy in memory
1087 * (done in this function's cleanup code). */
1088 overall_status = PSA_ERROR_NOT_PERMITTED;
1089 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001090 }
1091
Gilles Peskine354f7672019-07-12 23:46:38 +02001092#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1094 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001095 /* For a key in a secure element, we need to do three things:
1096 * remove the key file in internal storage, destroy the
1097 * key inside the secure element, and update the driver's
1098 * persistent data. Start a transaction that will encompass these
1099 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001101 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001103 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 status = psa_crypto_save_transaction();
1105 if (status != PSA_SUCCESS) {
1106 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001107 /* We should still try to destroy the key in the secure
1108 * element and the key metadata in storage. This is especially
1109 * important if the error is that the storage is full.
1110 * But how to do it exactly without risking an inconsistent
1111 * state after a reset?
1112 * https://github.com/ARMmbed/mbed-crypto/issues/215
1113 */
1114 overall_status = status;
1115 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001116 }
1117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 status = psa_destroy_se_key(driver,
1119 psa_key_slot_get_slot_number(slot));
1120 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001121 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001123 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001124#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1125
Darryl Greend49a4992018-06-18 17:27:26 +01001126#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1128 status = psa_destroy_persistent_key(slot->attr.id);
1129 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001130 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001132
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001133 /* TODO: other slots may have a copy of the same key. We should
1134 * invalidate them.
1135 * https://github.com/ARMmbed/mbed-crypto/issues/214
1136 */
Darryl Greend49a4992018-06-18 17:27:26 +01001137 }
1138#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001139
Gilles Peskinefc762652019-07-22 19:30:34 +02001140#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (driver != NULL) {
1142 status = psa_save_se_persistent_data(driver);
1143 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001144 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 }
1146 status = psa_crypto_stop_transaction();
1147 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001148 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001150 }
1151#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1152
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001153exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001155 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001157 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
1159 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001160}
1161
John Durkop07cc04a2020-11-16 22:08:34 -08001162#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001163 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001164static psa_status_t psa_get_rsa_public_exponent(
1165 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001167{
1168 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001170 uint8_t *buffer = NULL;
1171 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1175 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001176 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 }
1178 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001179 /* It's the default value, which is reported as an empty string,
1180 * so there's nothing to do. */
1181 goto exit;
1182 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 buflen = mbedtls_mpi_size(&mpi);
1185 buffer = mbedtls_calloc(1, buflen);
1186 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001187 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1188 goto exit;
1189 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1191 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001194 attributes->domain_parameters = buffer;
1195 attributes->domain_parameters_size = buflen;
1196
1197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 mbedtls_mpi_free(&mpi);
1199 if (ret != 0) {
1200 mbedtls_free(buffer);
1201 }
1202 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001203}
John Durkop07cc04a2020-11-16 22:08:34 -08001204#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001205 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001206
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001207/** Retrieve all the publicly-accessible attributes of a key.
1208 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1210 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001211{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001212 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001213 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001214 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1219 if (status != PSA_SUCCESS) {
1220 return status;
1221 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001222
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001223 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1225 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001226
1227#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1229 psa_set_key_slot_number(attributes,
1230 psa_key_slot_get_slot_number(slot));
1231 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001232#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001235#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001236 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001237 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001238 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001239 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001240 * is not yet implemented.
1241 * https://github.com/ARMmbed/mbed-crypto/issues/216
1242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001244 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001245
Ronald Cron90857082020-11-25 14:58:33 +01001246 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 slot->attr.type,
1248 slot->key.data,
1249 slot->key.bytes,
1250 &rsa);
1251 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001252 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 status = psa_get_rsa_public_exponent(rsa,
1256 attributes);
1257 mbedtls_rsa_free(rsa);
1258 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001259 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001260 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001261#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001262 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001263 default:
1264 /* Nothing else to do. */
1265 break;
1266 }
1267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (status != PSA_SUCCESS) {
1269 psa_reset_key_attributes(attributes);
1270 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001275}
1276
Gilles Peskinec8000c02019-08-02 20:15:51 +02001277#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1278psa_status_t psa_get_key_slot_number(
1279 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001281{
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001283 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 return PSA_SUCCESS;
1285 } else {
1286 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001287 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001288}
1289#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1292 size_t key_buffer_size,
1293 uint8_t *data,
1294 size_t data_size,
1295 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001296{
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (key_buffer_size > data_size) {
1298 return PSA_ERROR_BUFFER_TOO_SMALL;
1299 }
1300 memcpy(data, key_buffer, key_buffer_size);
1301 memset(data + key_buffer_size, 0,
1302 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001303 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001305}
1306
Ronald Cron7285cda2020-11-26 14:46:37 +01001307psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001308 const psa_key_attributes_t *attributes,
1309 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001311{
Ronald Crond18b5f82020-11-25 16:46:05 +01001312 psa_key_type_t type = attributes->core.type;
1313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (key_type_is_raw_bytes(type) ||
1315 PSA_KEY_TYPE_IS_RSA(type) ||
1316 PSA_KEY_TYPE_IS_ECC(type)) {
1317 return psa_export_key_buffer_internal(
1318 key_buffer, key_buffer_size,
1319 data, data_size, data_length);
1320 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001321 /* This shouldn't happen in the reference implementation, but
1322 it is valid for a special-purpose implementation to omit
1323 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001325 }
1326}
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1329 uint8_t *data,
1330 size_t data_size,
1331 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001332{
1333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1334 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1335 psa_key_slot_t *slot;
1336
Ronald Crone907e552021-01-18 13:22:38 +01001337 /* Reject a zero-length output buffer now, since this can never be a
1338 * valid key representation. This way we know that data must be a valid
1339 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if (data_size == 0) {
1341 return PSA_ERROR_BUFFER_TOO_SMALL;
1342 }
Ronald Crone907e552021-01-18 13:22:38 +01001343
Ronald Cron9486f9d2020-11-26 13:36:23 +01001344 /* Set the key to empty now, so that even when there are errors, we always
1345 * set data_length to a value between 0 and data_size. On error, setting
1346 * the key to empty is a good choice because an empty key representation is
1347 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001348 *data_length = 0;
1349
Ronald Cron9486f9d2020-11-26 13:36:23 +01001350 /* Export requires the EXPORT flag. There is an exception for public keys,
1351 * which don't require any flag, but
1352 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1355 PSA_KEY_USAGE_EXPORT, 0);
1356 if (status != PSA_SUCCESS) {
1357 return status;
1358 }
mohammad160306e79202018-03-28 13:17:44 +03001359
Ronald Crond18b5f82020-11-25 16:46:05 +01001360 psa_key_attributes_t attributes = {
1361 .core = slot->attr
1362 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 status = psa_driver_wrapper_export_key(&attributes,
1364 slot->key.data, slot->key.bytes,
1365 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001370}
1371
Ronald Cron7285cda2020-11-26 14:46:37 +01001372psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001373 const psa_key_attributes_t *attributes,
1374 const uint8_t *key_buffer,
1375 size_t key_buffer_size,
1376 uint8_t *data,
1377 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001379{
Ronald Crond18b5f82020-11-25 16:46:05 +01001380 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1383 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001384 /* Exporting public -> public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 return psa_export_key_buffer_internal(
1386 key_buffer, key_buffer_size,
1387 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001388 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001391#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1393 return mbedtls_psa_rsa_export_public_key(attributes,
1394 key_buffer,
1395 key_buffer_size,
1396 data,
1397 data_size,
1398 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001399#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001400 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001402#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1403 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001405#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1407 return mbedtls_psa_ecp_export_public_key(attributes,
1408 key_buffer,
1409 key_buffer_size,
1410 data,
1411 data_size,
1412 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001413#else
1414 /* We don't know how to convert a private ECC key to public */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001416#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1417 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001418 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001420 /* This shouldn't happen in the reference implementation, but
1421 it is valid for a special-purpose implementation to omit
1422 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001424 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001425}
Ronald Crond18b5f82020-11-25 16:46:05 +01001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1428 uint8_t *data,
1429 size_t data_size,
1430 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001431{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001433 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001434 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001435
Ronald Crone907e552021-01-18 13:22:38 +01001436 /* Reject a zero-length output buffer now, since this can never be a
1437 * valid key representation. This way we know that data must be a valid
1438 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (data_size == 0) {
1440 return PSA_ERROR_BUFFER_TOO_SMALL;
1441 }
Ronald Crone907e552021-01-18 13:22:38 +01001442
Darryl Greendd8fb772018-11-07 16:00:44 +00001443 /* Set the key to empty now, so that even when there are errors, we always
1444 * set data_length to a value between 0 and data_size. On error, setting
1445 * the key to empty is a good choice because an empty key representation is
1446 * unlikely to be accepted anywhere. */
1447 *data_length = 0;
1448
1449 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1451 if (status != PSA_SUCCESS) {
1452 return status;
1453 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1456 status = PSA_ERROR_INVALID_ARGUMENT;
1457 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001458 }
1459
Ronald Crond18b5f82020-11-25 16:46:05 +01001460 psa_key_attributes_t attributes = {
1461 .core = slot->attr
1462 };
Ronald Cron67227982020-11-26 15:16:05 +01001463 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001464 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001466
1467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001471}
1472
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001473MBEDTLS_STATIC_ASSERT(
1474 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1475 "One or more key attribute flag is listed as both external-only and dual-use")
1476MBEDTLS_STATIC_ASSERT(
1477 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1478 "One or more key attribute flag is listed as both internal-only and dual-use")
1479MBEDTLS_STATIC_ASSERT(
1480 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1481 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001482
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001483/** Validate that a key policy is internally well-formed.
1484 *
1485 * This function only rejects invalid policies. It does not validate the
1486 * consistency of the policy with respect to other attributes of the key
1487 * such as the key type.
1488 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001489static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001490{
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1492 PSA_KEY_USAGE_COPY |
1493 PSA_KEY_USAGE_ENCRYPT |
1494 PSA_KEY_USAGE_DECRYPT |
1495 PSA_KEY_USAGE_SIGN_MESSAGE |
1496 PSA_KEY_USAGE_VERIFY_MESSAGE |
1497 PSA_KEY_USAGE_SIGN_HASH |
1498 PSA_KEY_USAGE_VERIFY_HASH |
1499 PSA_KEY_USAGE_VERIFY_DERIVATION |
1500 PSA_KEY_USAGE_DERIVE)) != 0) {
1501 return PSA_ERROR_INVALID_ARGUMENT;
1502 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001505}
1506
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001507/** Validate the internal consistency of key attributes.
1508 *
1509 * This function only rejects invalid attribute values. If does not
1510 * validate the consistency of the attributes with any key data that may
1511 * be involved in the creation of the key.
1512 *
1513 * Call this function early in the key creation process.
1514 *
1515 * \param[in] attributes Key attributes for the new key.
1516 * \param[out] p_drv On any return, the driver for the key, if any.
1517 * NULL for a transparent key.
1518 *
1519 */
1520static psa_status_t psa_validate_key_attributes(
1521 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001523{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001524 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1526 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 status = psa_validate_key_location(lifetime, p_drv);
1529 if (status != PSA_SUCCESS) {
1530 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001531 }
1532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 status = psa_validate_key_persistence(lifetime);
1534 if (status != PSA_SUCCESS) {
1535 return status;
1536 }
1537
1538 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1539 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1540 return PSA_ERROR_INVALID_ARGUMENT;
1541 }
1542 } else {
1543 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1544 return PSA_ERROR_INVALID_ARGUMENT;
1545 }
1546 }
1547
1548 status = psa_validate_key_policy(&attributes->core.policy);
1549 if (status != PSA_SUCCESS) {
1550 return status;
1551 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001552
1553 /* Refuse to create overly large keys.
1554 * Note that this doesn't trigger on import if the attributes don't
1555 * explicitly specify a size (so psa_get_key_bits returns 0), so
1556 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1558 return PSA_ERROR_NOT_SUPPORTED;
1559 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001560
Gilles Peskine91e8c332019-08-02 19:19:39 +02001561 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1563 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1564 return PSA_ERROR_INVALID_ARGUMENT;
1565 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001568}
1569
Gilles Peskine4747d192019-04-17 15:05:45 +02001570/** Prepare a key slot to receive key material.
1571 *
1572 * This function allocates a key slot and sets its metadata.
1573 *
1574 * If this function fails, call psa_fail_key_creation().
1575 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001576 * This function is intended to be used as follows:
1577 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001578 * it with the specified attributes, and in case of a volatile key assign it
1579 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001580 * -# Populate the slot with the key material.
1581 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1582 * In case of failure at any step, stop the sequence and call
1583 * psa_fail_key_creation().
1584 *
Ronald Cron5c522922020-11-14 16:35:34 +01001585 * On success, the key slot is locked. It is the responsibility of the caller
1586 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001587 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001588 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001589 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001590 * \param[out] p_slot On success, a pointer to the prepared slot.
1591 * \param[out] p_drv On any return, the driver for the key, if any.
1592 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001593 *
1594 * \retval #PSA_SUCCESS
1595 * The key slot is ready to receive key material.
1596 * \return If this function fails, the key slot is an invalid state.
1597 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001598 */
1599static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001600 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001601 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001602 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001604{
1605 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001606 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001607 psa_key_slot_t *slot;
1608
Gilles Peskinedf179142019-07-15 22:02:14 +02001609 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001610 *p_drv = NULL;
1611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 status = psa_validate_key_attributes(attributes, p_drv);
1613 if (status != PSA_SUCCESS) {
1614 return status;
1615 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1618 if (status != PSA_SUCCESS) {
1619 return status;
1620 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001621 slot = *p_slot;
1622
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001623 /* We're storing the declared bit-size of the key. It's up to each
1624 * creation mechanism to verify that this information is correct.
1625 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001626 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001627 * is optional (import, copy). In case of a volatile key, assign it the
1628 * volatile key identifier associated to the slot returned to contain its
1629 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001630
1631 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001633#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1634 slot->attr.id = volatile_key_id;
1635#else
1636 slot->attr.id.key_id = volatile_key_id;
1637#endif
1638 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001639
Gilles Peskine91e8c332019-08-02 19:19:39 +02001640 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001641 * external-only flags, query `attributes`. Thanks to the check
1642 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001643 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001644 * may have set. */
1645 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001646
Gilles Peskinecbaff462019-07-12 23:46:04 +02001647#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001648 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001649 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001650 * create the key file in internal storage, create the
1651 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001652 * persistent data. This is done by starting a transaction that will
1653 * encompass these three actions.
1654 * For registering a volatile key, we just need to find an appropriate
1655 * slot number inside the SE. Since the key is designated volatile, creating
1656 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001657 /* The first thing to do is to find a slot number for the new key.
1658 * We save the slot number in persistent storage as part of the
1659 * transaction data. It will be needed to recover if the power
1660 * fails during the key creation process, to clean up on the secure
1661 * element side after restarting. Obtaining a slot number from the
1662 * secure element driver updates its persistent state, but we do not yet
1663 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001664 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001666 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1668 &slot_number);
1669 if (status != PSA_SUCCESS) {
1670 return status;
1671 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1674 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001675 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001676 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001677 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 status = psa_crypto_save_transaction();
1679 if (status != PSA_SUCCESS) {
1680 (void) psa_crypto_stop_transaction();
1681 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001682 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001683 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001684
1685 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001687 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001690 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001692 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001693#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001696}
Gilles Peskine4747d192019-04-17 15:05:45 +02001697
1698/** Finalize the creation of a key once its key material has been set.
1699 *
1700 * This entails writing the key to persistent storage.
1701 *
1702 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001703 * See the documentation of psa_start_key_creation() for the intended use
1704 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001705 *
Ronald Cron5c522922020-11-14 16:35:34 +01001706 * If the finalization succeeds, the function unlocks the key slot (it was
1707 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1708 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001709 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001710 * \param[in,out] slot Pointer to the slot with key material.
1711 * \param[in] driver The secure element driver for the key,
1712 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001713 * \param[out] key On success, identifier of the key. Note that the
1714 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001715 *
1716 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001717 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001718 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1719 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1720 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1721 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1722 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1723 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001724 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001725 * \return If this function fails, the key slot is an invalid state.
1726 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001727 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001728static psa_status_t psa_finish_key_creation(
1729 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001730 psa_se_drv_table_entry_t *driver,
1731 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001732{
1733 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001734 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001735 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001736
1737#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001739#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001741 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001742 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001744
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001745 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1746 sizeof(data.slot_number),
1747 "Slot number size does not match psa_se_key_data_storage_t");
1748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1750 status = psa_save_persistent_key(&slot->attr,
1751 (uint8_t *) &data,
1752 sizeof(data));
1753 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001754#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1755 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001756 /* Key material is saved in export representation in the slot, so
1757 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 status = psa_save_persistent_key(&slot->attr,
1759 slot->key.data,
1760 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001761 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001762 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001763#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1764
Gilles Peskinecbaff462019-07-12 23:46:04 +02001765#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001766 /* Finish the transaction for a key creation. This does not
1767 * happen when registering an existing key. Detect this case
1768 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001769 * creation of a persistent key in a secure element requires a transaction,
1770 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (driver != NULL &&
1772 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1773 status = psa_save_se_persistent_data(driver);
1774 if (status != PSA_SUCCESS) {
1775 psa_destroy_persistent_key(slot->attr.id);
1776 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001777 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001779 }
1780#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001783 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 status = psa_unlock_key_slot(slot);
1785 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001786 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001788 }
Ronald Cron50972942020-11-14 11:28:25 +01001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001791}
1792
1793/** Abort the creation of a key.
1794 *
1795 * You may call this function after calling psa_start_key_creation(),
1796 * or after psa_finish_key_creation() fails. In other circumstances, this
1797 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001798 * See the documentation of psa_start_key_creation() for the intended use
1799 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001800 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001801 * \param[in,out] slot Pointer to the slot with key material.
1802 * \param[in] driver The secure element driver for the key,
1803 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001804 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001805static void psa_fail_key_creation(psa_key_slot_t *slot,
1806 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001807{
Gilles Peskine011e4282019-06-26 18:34:38 +02001808 (void) driver;
1809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001811 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001813
1814#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001815 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001816 * element, and the failure happened later (when saving metadata
1817 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001818 * element.
1819 * https://github.com/ARMmbed/mbed-crypto/issues/217
1820 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001821
Gilles Peskined7729582019-08-05 15:55:54 +02001822 /* Abort the ongoing transaction if any (there may not be one if
1823 * the creation process failed before starting one, or if the
1824 * key creation is a registration of a key in a secure element).
1825 * Earlier functions must already have done what it takes to undo any
1826 * partial creation. All that's left is to update the transaction data
1827 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001829#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1830
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001832}
1833
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001834/** Validate optional attributes during key creation.
1835 *
1836 * Some key attributes are optional during key creation. If they are
1837 * specified in the attributes structure, check that they are consistent
1838 * with the data in the slot.
1839 *
1840 * This function should be called near the end of key creation, after
1841 * the slot in memory is fully populated but before saving persistent data.
1842 */
1843static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001844 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001846{
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (attributes->core.type != 0) {
1848 if (attributes->core.type != slot->attr.type) {
1849 return PSA_ERROR_INVALID_ARGUMENT;
1850 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851 }
1852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001854#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1856 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001857 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001858 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001859 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001860
Ronald Cron90857082020-11-25 14:58:33 +01001861 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 slot->attr.type,
1863 slot->key.data,
1864 slot->key.bytes,
1865 &rsa);
1866 if (status != PSA_SUCCESS) {
1867 return status;
1868 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 mbedtls_mpi_init(&actual);
1871 mbedtls_mpi_init(&required);
1872 ret = mbedtls_rsa_export(rsa,
1873 NULL, NULL, NULL, NULL, &actual);
1874 mbedtls_rsa_free(rsa);
1875 mbedtls_free(rsa);
1876 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001877 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 }
1879 ret = mbedtls_mpi_read_binary(&required,
1880 attributes->domain_parameters,
1881 attributes->domain_parameters_size);
1882 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001883 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 }
1885 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001886 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 }
1888rsa_exit:
1889 mbedtls_mpi_free(&actual);
1890 mbedtls_mpi_free(&required);
1891 if (ret != 0) {
1892 return mbedtls_to_psa_error(ret);
1893 }
1894 } else
John Durkop9814fa22020-11-04 12:28:15 -08001895#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1896 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001897 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899 }
1900 }
1901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 if (attributes->core.bits != 0) {
1903 if (attributes->core.bits != slot->attr.bits) {
1904 return PSA_ERROR_INVALID_ARGUMENT;
1905 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001906 }
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001909}
1910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1912 const uint8_t *data,
1913 size_t data_length,
1914 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001915{
1916 psa_status_t status;
1917 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001918 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001919 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301920 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001921
Ronald Cron81709fc2020-11-14 12:10:32 +01001922 *key = MBEDTLS_SVC_KEY_ID_INIT;
1923
Gilles Peskine0f84d622019-09-12 19:03:13 +02001924 /* Reject zero-length symmetric keys (including raw data key objects).
1925 * This also rejects any key which might be encoded as an empty string,
1926 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 if (data_length == 0) {
1928 return PSA_ERROR_INVALID_ARGUMENT;
1929 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001930
Archana449608b2021-09-08 15:36:05 +05301931 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 if (data_length > SIZE_MAX / 8) {
1933 return PSA_ERROR_NOT_SUPPORTED;
1934 }
Archana6ed4bda2021-08-04 10:47:15 +05301935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1937 &slot, &driver);
1938 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001939 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001941
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001942 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301943 * storage ( thus not in the case of importing a key in a secure element
1944 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1945 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (slot->key.data == NULL) {
1947 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301948 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 attributes, data, data_length, &storage_size);
1950 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 }
Archanad8a83dc2021-06-14 10:04:16 +05301953 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 status = psa_allocate_buffer_to_slot(slot, storage_size);
1955 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001956 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001958 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001959
1960 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 status = psa_driver_wrapper_import_key(attributes,
1962 data, data_length,
1963 slot->key.data,
1964 slot->key.bytes,
1965 &slot->key.bytes, &bits);
1966 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001967 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001971 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001973 status = PSA_ERROR_INVALID_ARGUMENT;
1974 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001975 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001976
Archana6ed4bda2021-08-04 10:47:15 +05301977 /* Enforce a size limit, and in particular ensure that the bit
1978 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301980 status = PSA_ERROR_NOT_SUPPORTED;
1981 goto exit;
1982 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_validate_optional_attributes(slot, attributes);
1984 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001985 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 if (status != PSA_SUCCESS) {
1991 psa_fail_key_creation(slot, driver);
1992 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001995}
1996
Gilles Peskined7729582019-08-05 15:55:54 +02001997#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1998psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002000{
2001 psa_status_t status;
2002 psa_key_slot_t *slot = NULL;
2003 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002005
2006 /* Leaving attributes unspecified is not currently supported.
2007 * It could make sense to query the key type and size from the
2008 * secure element, but not all secure elements support this
2009 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2011 return PSA_ERROR_NOT_SUPPORTED;
2012 }
2013 if (psa_get_key_bits(attributes) == 0) {
2014 return PSA_ERROR_NOT_SUPPORTED;
2015 }
Gilles Peskined7729582019-08-05 15:55:54 +02002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2018 &slot, &driver);
2019 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002020 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 }
Gilles Peskined7729582019-08-05 15:55:54 +02002022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002024
2025exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (status != PSA_SUCCESS) {
2027 psa_fail_key_creation(slot, driver);
2028 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002029
Gilles Peskined7729582019-08-05 15:55:54 +02002030 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 psa_close_key(key);
2032 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002033}
2034#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2037 const psa_key_attributes_t *specified_attributes,
2038 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002039{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002040 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002041 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002042 psa_key_slot_t *source_slot = NULL;
2043 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002044 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002045 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302046 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002047
Ronald Cron81709fc2020-11-14 12:10:32 +01002048 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2049
Archana8a180362021-07-05 02:18:48 +05302050 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2052 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002053 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 status = psa_validate_optional_attributes(source_slot,
2057 specified_attributes);
2058 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002059 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002061
Archana9d17bf42021-09-10 06:22:44 +05302062 /* The target key type and number of bits have been validated by
2063 * psa_validate_optional_attributes() to be either equal to zero or
2064 * equal to the ones of the source key. So it is safe to inherit
2065 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302066 * */
Archana9d17bf42021-09-10 06:22:44 +05302067 actual_attributes.core.bits = source_slot->attr.bits;
2068 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302069
2070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 status = psa_restrict_key_policy(source_slot->attr.type,
2072 &actual_attributes.core.policy,
2073 &source_slot->attr.policy);
2074 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002075 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2079 &target_slot, &driver);
2080 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002081 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 }
2083 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2084 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302085 /*
Archana9d17bf42021-09-10 06:22:44 +05302086 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302087 * the source key would need to be exported as plaintext and re-imported
2088 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302089 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302090 * appropriate API invocations from the application, if needed.
2091 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002092 status = PSA_ERROR_NOT_SUPPORTED;
2093 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002094 }
Archana8a180362021-07-05 02:18:48 +05302095 /*
2096 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302097 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302098 * - For opaque keys this translates to an invocation of the drivers'
2099 * copy_key entry point through the dispatch layer.
2100 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2102 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2103 &storage_size);
2104 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302105 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 }
Archana374fe5b2021-09-08 15:50:28 +05302107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2109 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 }
Archana374fe5b2021-09-08 15:50:28 +05302112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 status = psa_driver_wrapper_copy_key(&actual_attributes,
2114 source_slot->key.data,
2115 source_slot->key.bytes,
2116 target_slot->key.data,
2117 target_slot->key.bytes,
2118 &target_slot->key.bytes);
2119 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 }
2122 } else {
2123 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302124 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 source_slot->key.bytes);
2126 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 }
Archana8a180362021-07-05 02:18:48 +05302129 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (status != PSA_SUCCESS) {
2133 psa_fail_key_creation(target_slot, driver);
2134 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002139}
2140
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002141
2142
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002143/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002144/* Message digests */
2145/****************************************************************/
2146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002148{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002149 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (operation->id == 0) {
2151 return PSA_SUCCESS;
2152 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002155 operation->id = 0;
2156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002158}
2159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2161 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002162{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002163 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002164
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002165 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002167 status = PSA_ERROR_BAD_STATE;
2168 goto exit;
2169 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002172 status = PSA_ERROR_INVALID_ARGUMENT;
2173 goto exit;
2174 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002175
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002176 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2177 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002181
2182exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (status != PSA_SUCCESS) {
2184 psa_hash_abort(operation);
2185 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002186
2187 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002188}
2189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2191 const uint8_t *input,
2192 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002194 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002197 status = PSA_ERROR_BAD_STATE;
2198 goto exit;
2199 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002200
Steven Cooremanc8288352021-03-04 14:02:19 +01002201 /* Don't require hash implementations to behave correctly on a
2202 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (input_length == 0) {
2204 return PSA_SUCCESS;
2205 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002208
2209exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (status != PSA_SUCCESS) {
2211 psa_hash_abort(operation);
2212 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002215}
2216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2218 uint8_t *hash,
2219 size_t hash_size,
2220 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002222 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if (operation->id == 0) {
2224 return PSA_ERROR_BAD_STATE;
2225 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002226
Steven Cooreman1e582352021-02-18 17:24:37 +01002227 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 operation, hash, hash_size, hash_length);
2229 psa_hash_abort(operation);
2230 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002231}
2232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2234 const uint8_t *hash,
2235 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236{
Ronald Cron69a63422021-10-18 09:47:58 +02002237 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002239 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 operation,
2241 actual_hash, sizeof(actual_hash),
2242 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002245 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002249 status = PSA_ERROR_INVALID_SIGNATURE;
2250 goto exit;
2251 }
2252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002254 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002256
2257exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2259 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002260 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002264}
2265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266psa_status_t psa_hash_compute(psa_algorithm_t alg,
2267 const uint8_t *input, size_t input_length,
2268 uint8_t *hash, size_t hash_size,
2269 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002270{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002271 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 if (!PSA_ALG_IS_HASH(alg)) {
2273 return PSA_ERROR_INVALID_ARGUMENT;
2274 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2277 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002278}
2279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280psa_status_t psa_hash_compare(psa_algorithm_t alg,
2281 const uint8_t *input, size_t input_length,
2282 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002283{
Ronald Cron69a63422021-10-18 09:47:58 +02002284 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002285 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 if (!PSA_ALG_IS_HASH(alg)) {
2288 return PSA_ERROR_INVALID_ARGUMENT;
2289 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002290
Steven Cooreman1e582352021-02-18 17:24:37 +01002291 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 alg, input, input_length,
2293 actual_hash, sizeof(actual_hash),
2294 &actual_hash_length);
2295 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002296 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 }
2298 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002299 status = PSA_ERROR_INVALID_SIGNATURE;
2300 goto exit;
2301 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002303 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002305
2306exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2308 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002309}
2310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2312 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002313{
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (source_operation->id == 0 ||
2315 target_operation->id != 0) {
2316 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002317 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2320 target_operation);
2321 if (status != PSA_SUCCESS) {
2322 psa_hash_abort(target_operation);
2323 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002326}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327
2328
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002330/* MAC */
2331/****************************************************************/
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002334{
Steven Cooremane6804192021-03-19 18:28:56 +01002335 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (operation->id == 0) {
2337 return PSA_SUCCESS;
2338 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002341 operation->mac_size = 0;
2342 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002343 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002346}
2347
Ronald Cron2dff3b22021-06-17 16:33:22 +02002348static psa_status_t psa_mac_finalize_alg_and_key_validation(
2349 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002350 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002352{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 psa_key_type_t key_type = psa_get_key_type(attributes);
2355 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if (!PSA_ALG_IS_MAC(alg)) {
2358 return PSA_ERROR_INVALID_ARGUMENT;
2359 }
Steven Cooremane6804192021-03-19 18:28:56 +01002360
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002361 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 status = psa_mac_key_can_do(alg, key_type);
2363 if (status != PSA_SUCCESS) {
2364 return status;
2365 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002366
Steven Cooremand1a68f12021-05-10 11:13:41 +02002367 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002371 /* A very short MAC is too short for security since it can be
2372 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2373 * so we make this our minimum, even though 32 bits is still
2374 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002376 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2379 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002380 /* It's impossible to "truncate" to a larger length than the full length
2381 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002383 }
2384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002386 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2387 * that is disabled in the compile-time configuration. The result can
2388 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2389 * configuration into account. In this case, force a return of
2390 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2391 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2392 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2393 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2394 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002396 }
2397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002399}
2400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2402 mbedtls_svc_key_id_t key,
2403 psa_algorithm_t alg,
2404 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002405{
2406 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2407 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002408 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409
2410 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002412 status = PSA_ERROR_BAD_STATE;
2413 goto exit;
2414 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415
2416 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 key,
2418 &slot,
2419 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2420 alg);
2421 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002422 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002424
2425 psa_key_attributes_t attributes = {
2426 .core = slot->attr
2427 };
2428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2430 &operation->mac_size);
2431 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002434
2435 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002436 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (is_sign) {
2438 status = psa_driver_wrapper_mac_sign_setup(operation,
2439 &attributes,
2440 slot->key.data,
2441 slot->key.bytes,
2442 alg);
2443 } else {
2444 status = psa_driver_wrapper_mac_verify_setup(operation,
2445 &attributes,
2446 slot->key.data,
2447 slot->key.bytes,
2448 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002450
Gilles Peskinefbfac682018-07-08 20:51:54 +02002451exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 if (status != PSA_SUCCESS) {
2453 psa_mac_abort(operation);
2454 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459}
2460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2462 mbedtls_svc_key_id_t key,
2463 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002464{
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002466}
2467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2469 mbedtls_svc_key_id_t key,
2470 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002471{
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002473}
2474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2476 const uint8_t *input,
2477 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478{
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (operation->id == 0) {
2480 return PSA_ERROR_BAD_STATE;
2481 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002482
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002483 /* Don't require hash implementations to behave correctly on a
2484 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (input_length == 0) {
2486 return PSA_SUCCESS;
2487 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2490 input, input_length);
2491 if (status != PSA_SUCCESS) {
2492 psa_mac_abort(operation);
2493 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496}
2497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2499 uint8_t *mac,
2500 size_t mac_size,
2501 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002502{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002503 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2504 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002507 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002508 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002509 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002512 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002513 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002514 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002515
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002516 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2517 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002519 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002520 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002521 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002524 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002525 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002526 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 status = psa_driver_wrapper_mac_sign_finish(operation,
2529 mac, operation->mac_size,
2530 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002531
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002532exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002533 /* In case of success, set the potential excess room in the output buffer
2534 * to an invalid value, to avoid potentially leaking a longer MAC.
2535 * In case of error, set the output length and content to a safe default,
2536 * such that in case the caller misses an error check, the output would be
2537 * an unachievable MAC.
2538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002540 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002541 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002542 }
mohammad16036df908f2018-04-02 08:34:15 -07002543
Paul Elliottdc42ca82023-02-24 18:11:59 +00002544 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002549}
2550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2552 const uint8_t *mac,
2553 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002555 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2556 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002559 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002560 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002561 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002564 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002565 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002566 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002569 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002570 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002571 }
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 status = psa_driver_wrapper_mac_verify_finish(operation,
2574 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002575
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580}
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2583 psa_algorithm_t alg,
2584 const uint8_t *input,
2585 size_t input_length,
2586 uint8_t *mac,
2587 size_t mac_size,
2588 size_t *mac_length,
2589 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002590{
2591 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002592 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2593 psa_key_slot_t *slot;
2594 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595
Ronald Cron51131b52021-06-17 17:17:20 +02002596 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 key,
2598 &slot,
2599 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2600 alg);
2601 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002602 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002604
Ronald Cron51131b52021-06-17 17:17:20 +02002605 psa_key_attributes_t attributes = {
2606 .core = slot->attr
2607 };
2608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2610 &operation_mac_size);
2611 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002612 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002616 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002617 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002618 }
2619
2620 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 &attributes,
2622 slot->key.data, slot->key.bytes,
2623 alg,
2624 input, input_length,
2625 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002626
2627exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002628 /* In case of success, set the potential excess room in the output buffer
2629 * to an invalid value, to avoid potentially leaking a longer MAC.
2630 * In case of error, set the output length and content to a safe default,
2631 * such that in case the caller misses an error check, the output would be
2632 * an unachievable MAC.
2633 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002635 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002636 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002637 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002638
2639 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002644}
2645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002647 psa_algorithm_t alg,
2648 const uint8_t *input,
2649 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 uint8_t *mac,
2651 size_t mac_size,
2652 size_t *mac_length)
2653{
2654 return psa_mac_compute_internal(key, alg,
2655 input, input_length,
2656 mac, mac_size, mac_length, 1);
2657}
2658
2659psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2660 psa_algorithm_t alg,
2661 const uint8_t *input,
2662 size_t input_length,
2663 const uint8_t *mac,
2664 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002665{
2666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002667 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2668 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 status = psa_mac_compute_internal(key, alg,
2671 input, input_length,
2672 actual_mac, sizeof(actual_mac),
2673 &actual_mac_length, 0);
2674 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002675 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002679 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002680 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002681 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002683 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002684 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002685 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002686
2687exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002691}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002692
Gilles Peskinee59236f2018-01-27 23:32:46 +01002693/****************************************************************/
2694/* Asymmetric cryptography */
2695/****************************************************************/
2696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2698 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002699{
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 if (input_is_message) {
2701 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2702 return PSA_ERROR_INVALID_ARGUMENT;
2703 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2706 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2707 return PSA_ERROR_INVALID_ARGUMENT;
2708 }
2709 }
2710 } else {
2711 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2712 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002713 }
2714 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002717}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2720 int input_is_message,
2721 psa_algorithm_t alg,
2722 const uint8_t *input,
2723 size_t input_length,
2724 uint8_t *signature,
2725 size_t signature_size,
2726 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002727{
2728 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2729 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2730 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002731
2732 *signature_length = 0;
2733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 status = psa_sign_verify_check_alg(input_is_message, alg);
2735 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002736 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002738
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002739 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002740 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002741 * buffer can in principle be empty since it doesn't actually have
2742 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 if (signature_size == 0) {
2744 return PSA_ERROR_BUFFER_TOO_SMALL;
2745 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002746
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002747 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 key, &slot,
2749 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2750 PSA_KEY_USAGE_SIGN_HASH,
2751 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002754 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002758 status = PSA_ERROR_INVALID_ARGUMENT;
2759 goto exit;
2760 }
2761
2762 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002764 };
2765
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002767 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002768 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002769 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 signature, signature_size, signature_length);
2771 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002772
2773 status = psa_driver_wrapper_sign_hash(
2774 &attributes, slot->key.data, slot->key.bytes,
2775 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002777 }
2778
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002779
2780exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002781 psa_wipe_tag_output_buffer(signature, status, signature_size,
2782 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002787}
2788
Gilles Peskine449bd832023-01-11 14:50:10 +01002789static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2790 int input_is_message,
2791 psa_algorithm_t alg,
2792 const uint8_t *input,
2793 size_t input_length,
2794 const uint8_t *signature,
2795 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002796{
2797 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2798 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2799 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 status = psa_sign_verify_check_alg(input_is_message, alg);
2802 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002803 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002805
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002806 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 key, &slot,
2808 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2809 PSA_KEY_USAGE_VERIFY_HASH,
2810 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 if (status != PSA_SUCCESS) {
2813 return status;
2814 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002815
2816 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002818 };
2819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002821 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002822 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002823 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 signature, signature_length);
2825 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002826 status = psa_driver_wrapper_verify_hash(
2827 &attributes, slot->key.data, slot->key.bytes,
2828 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002830 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002831
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002835
2836}
2837
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002838psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002839 const psa_key_attributes_t *attributes,
2840 const uint8_t *key_buffer,
2841 size_t key_buffer_size,
2842 psa_algorithm_t alg,
2843 const uint8_t *input,
2844 size_t input_length,
2845 uint8_t *signature,
2846 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002848{
2849 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002852 size_t hash_length;
2853 uint8_t hash[PSA_HASH_MAX_SIZE];
2854
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002855 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 PSA_ALG_SIGN_GET_HASH(alg),
2857 input, input_length,
2858 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002861 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002863
2864 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 attributes, key_buffer, key_buffer_size,
2866 alg, hash, hash_length,
2867 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002868 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002871}
2872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2874 psa_algorithm_t alg,
2875 const uint8_t *input,
2876 size_t input_length,
2877 uint8_t *signature,
2878 size_t signature_size,
2879 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002880{
2881 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002882 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002884}
2885
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002886psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002887 const psa_key_attributes_t *attributes,
2888 const uint8_t *key_buffer,
2889 size_t key_buffer_size,
2890 psa_algorithm_t alg,
2891 const uint8_t *input,
2892 size_t input_length,
2893 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002895{
2896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002899 size_t hash_length;
2900 uint8_t hash[PSA_HASH_MAX_SIZE];
2901
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002902 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 PSA_ALG_SIGN_GET_HASH(alg),
2904 input, input_length,
2905 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002908 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002910
2911 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 attributes, key_buffer, key_buffer_size,
2913 alg, hash, hash_length,
2914 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002915 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002918}
2919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2921 psa_algorithm_t alg,
2922 const uint8_t *input,
2923 size_t input_length,
2924 const uint8_t *signature,
2925 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002926{
2927 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002928 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002930}
2931
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002932psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002933 const psa_key_attributes_t *attributes,
2934 const uint8_t *key_buffer, size_t key_buffer_size,
2935 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002937{
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2939 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2940 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002941#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2943 return mbedtls_psa_rsa_sign_hash(
2944 attributes,
2945 key_buffer, key_buffer_size,
2946 alg, hash, hash_length,
2947 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002948#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2949 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 } else {
2951 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002952 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2954 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002955#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2957 return mbedtls_psa_ecdsa_sign_hash(
2958 attributes,
2959 key_buffer, key_buffer_size,
2960 alg, hash, hash_length,
2961 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002962#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2963 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 } else {
2965 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002966 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002967 }
Ronald Cron4501c982021-03-19 20:09:32 +01002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 (void) key_buffer;
2970 (void) key_buffer_size;
2971 (void) hash;
2972 (void) hash_length;
2973 (void) signature;
2974 (void) signature_size;
2975 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002976
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002978}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2981 psa_algorithm_t alg,
2982 const uint8_t *hash,
2983 size_t hash_length,
2984 uint8_t *signature,
2985 size_t signature_size,
2986 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002987{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002988 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002989 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002991}
2992
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002993psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002994 const psa_key_attributes_t *attributes,
2995 const uint8_t *key_buffer, size_t key_buffer_size,
2996 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002998{
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3000 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3001 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003002#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3004 return mbedtls_psa_rsa_verify_hash(
3005 attributes,
3006 key_buffer, key_buffer_size,
3007 alg, hash, hash_length,
3008 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003009#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3010 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 } else {
3012 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003013 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3015 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003016#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3018 return mbedtls_psa_ecdsa_verify_hash(
3019 attributes,
3020 key_buffer, key_buffer_size,
3021 alg, hash, hash_length,
3022 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003023#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3024 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 } else {
3026 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003027 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003028 }
Ronald Cron4501c982021-03-19 20:09:32 +01003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 (void) key_buffer;
3031 (void) key_buffer_size;
3032 (void) hash;
3033 (void) hash_length;
3034 (void) signature;
3035 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003038}
3039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3041 psa_algorithm_t alg,
3042 const uint8_t *hash,
3043 size_t hash_length,
3044 const uint8_t *signature,
3045 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003046{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003047 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003048 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003050}
3051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3053 psa_algorithm_t alg,
3054 const uint8_t *input,
3055 size_t input_length,
3056 const uint8_t *salt,
3057 size_t salt_length,
3058 uint8_t *output,
3059 size_t output_size,
3060 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003061{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003062 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003063 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003064 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003065
Darryl Green5cc689a2018-07-24 15:34:10 +01003066 (void) input;
3067 (void) input_length;
3068 (void) salt;
3069 (void) output;
3070 (void) output_size;
3071
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003072 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3075 return PSA_ERROR_INVALID_ARGUMENT;
3076 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003077
Ronald Cron5c522922020-11-14 16:35:34 +01003078 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3080 if (status != PSA_SUCCESS) {
3081 return status;
3082 }
3083 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3084 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003085 status = PSA_ERROR_INVALID_ARGUMENT;
3086 goto exit;
3087 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003088
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003089 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003091 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003092
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003093 status = psa_driver_wrapper_asymmetric_encrypt(
3094 &attributes, slot->key.data, slot->key.bytes,
3095 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003097exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003099
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003101}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3104 psa_algorithm_t alg,
3105 const uint8_t *input,
3106 size_t input_length,
3107 const uint8_t *salt,
3108 size_t salt_length,
3109 uint8_t *output,
3110 size_t output_size,
3111 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003112{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003113 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003114 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003115 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003116
Darryl Green5cc689a2018-07-24 15:34:10 +01003117 (void) input;
3118 (void) input_length;
3119 (void) salt;
3120 (void) output;
3121 (void) output_size;
3122
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003123 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3126 return PSA_ERROR_INVALID_ARGUMENT;
3127 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003128
Ronald Cron5c522922020-11-14 16:35:34 +01003129 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3131 if (status != PSA_SUCCESS) {
3132 return status;
3133 }
3134 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003135 status = PSA_ERROR_INVALID_ARGUMENT;
3136 goto exit;
3137 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003138
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003139 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003141 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003143 status = psa_driver_wrapper_asymmetric_decrypt(
3144 &attributes, slot->key.data, slot->key.bytes,
3145 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003147
3148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003152}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003153
Paul Elliott9fe12f62022-11-30 19:16:02 +00003154/****************************************************************/
3155/* Asymmetric interruptible cryptography */
3156/****************************************************************/
3157
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003158static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3159
Paul Elliott9fe12f62022-11-30 19:16:02 +00003160void psa_interruptible_set_max_ops(uint32_t max_ops)
3161{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003162 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003163}
3164
3165uint32_t psa_interruptible_get_max_ops(void)
3166{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003167 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003168}
3169
Paul Elliott9fe12f62022-11-30 19:16:02 +00003170uint32_t psa_sign_hash_get_num_ops(
3171 const psa_sign_hash_interruptible_operation_t *operation)
3172{
Paul Elliott296ede92022-12-15 17:00:30 +00003173 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003174}
3175
3176uint32_t psa_verify_hash_get_num_ops(
3177 const psa_verify_hash_interruptible_operation_t *operation)
3178{
Paul Elliott296ede92022-12-15 17:00:30 +00003179 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003180}
3181
Paul Elliott59ad9452022-12-18 15:09:02 +00003182static psa_status_t psa_sign_hash_abort_internal(
3183 psa_sign_hash_interruptible_operation_t *operation)
3184{
3185 if (operation->id == 0) {
3186 /* The object has (apparently) been initialized but it is not (yet)
3187 * in use. It's ok to call abort on such an object, and there's
3188 * nothing to do. */
3189 return PSA_SUCCESS;
3190 }
3191
3192 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3193
3194 status = psa_driver_wrapper_sign_hash_abort(operation);
3195
3196 operation->id = 0;
3197
Paul Elliotte6145dc2023-02-07 12:51:21 +00003198 /* Do not clear either the error_occurred or num_ops elements here as they
3199 * only want to be cleared by the application calling abort, not by abort
3200 * being called at completion of an operation. */
3201
Paul Elliott59ad9452022-12-18 15:09:02 +00003202 return status;
3203}
3204
Paul Elliott9fe12f62022-11-30 19:16:02 +00003205psa_status_t psa_sign_hash_start(
3206 psa_sign_hash_interruptible_operation_t *operation,
3207 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3208 const uint8_t *hash, size_t hash_length)
3209{
3210 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3211 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3212 psa_key_slot_t *slot;
3213
Paul Elliottc9774412023-02-06 15:14:07 +00003214 /* Check that start has not been previously called, or operation has not
3215 * previously errored. */
3216 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003217 return PSA_ERROR_BAD_STATE;
3218 }
3219
Paul Elliott9fe12f62022-11-30 19:16:02 +00003220 status = psa_sign_verify_check_alg(0, alg);
3221 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003222 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003223 return status;
3224 }
3225
3226 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3227 PSA_KEY_USAGE_SIGN_HASH,
3228 alg);
3229
3230 if (status != PSA_SUCCESS) {
3231 goto exit;
3232 }
3233
3234 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3235 status = PSA_ERROR_INVALID_ARGUMENT;
3236 goto exit;
3237 }
3238
3239 psa_key_attributes_t attributes = {
3240 .core = slot->attr
3241 };
3242
Paul Elliott296ede92022-12-15 17:00:30 +00003243 /* Ensure ops count gets reset, in case of operation re-use. */
3244 operation->num_ops = 0;
3245
Paul Elliott9fe12f62022-11-30 19:16:02 +00003246 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3247 slot->key.data,
3248 slot->key.bytes, alg,
3249 hash, hash_length);
3250exit:
3251
3252 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003253 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003254 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003255 }
3256
3257 unlock_status = psa_unlock_key_slot(slot);
3258
Paul Elliottc9774412023-02-06 15:14:07 +00003259 if (unlock_status != PSA_SUCCESS) {
3260 operation->error_occurred = 1;
3261 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003262
Paul Elliottc9774412023-02-06 15:14:07 +00003263 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003264}
3265
3266
3267psa_status_t psa_sign_hash_complete(
3268 psa_sign_hash_interruptible_operation_t *operation,
3269 uint8_t *signature, size_t signature_size,
3270 size_t *signature_length)
3271{
3272 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3273
3274 *signature_length = 0;
3275
Paul Elliottc9774412023-02-06 15:14:07 +00003276 /* Check that start has been called first, and that operation has not
3277 * previously errored. */
3278 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003279 status = PSA_ERROR_BAD_STATE;
3280 goto exit;
3281 }
3282
Paul Elliott096abc42023-02-03 18:33:23 +00003283 /* Immediately reject a zero-length signature buffer. This guarantees that
3284 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003285 if (signature_size == 0) {
3286 status = PSA_ERROR_BUFFER_TOO_SMALL;
3287 goto exit;
3288 }
3289
3290 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3291 signature_size,
3292 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003293
Paul Elliott296ede92022-12-15 17:00:30 +00003294 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003295 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003296
Paul Elliottebe225c2023-02-10 14:32:53 +00003297exit:
3298
Paul Elliott59200a22023-02-21 15:07:40 +00003299 psa_wipe_tag_output_buffer(signature, status, signature_size,
3300 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003301
Paul Elliott53bb3122023-02-10 14:22:22 +00003302 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003303 if (status != PSA_SUCCESS) {
3304 operation->error_occurred = 1;
3305 }
3306
Paul Elliott59ad9452022-12-18 15:09:02 +00003307 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003308 }
3309
3310 return status;
3311}
3312
3313psa_status_t psa_sign_hash_abort(
3314 psa_sign_hash_interruptible_operation_t *operation)
3315{
Paul Elliott59ad9452022-12-18 15:09:02 +00003316 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3317
3318 status = psa_sign_hash_abort_internal(operation);
3319
3320 /* We clear the number of ops done here, so that it is not cleared when
3321 * the operation fails or succeeds, only on manual abort. */
3322 operation->num_ops = 0;
3323
Paul Elliottc9774412023-02-06 15:14:07 +00003324 /* Likewise, failure state. */
3325 operation->error_occurred = 0;
3326
Paul Elliott59ad9452022-12-18 15:09:02 +00003327 return status;
3328}
3329
3330static psa_status_t psa_verify_hash_abort_internal(
3331 psa_verify_hash_interruptible_operation_t *operation)
3332{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003333 if (operation->id == 0) {
3334 /* The object has (apparently) been initialized but it is not (yet)
3335 * in use. It's ok to call abort on such an object, and there's
3336 * nothing to do. */
3337 return PSA_SUCCESS;
3338 }
3339
Paul Elliott59ad9452022-12-18 15:09:02 +00003340 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3341
3342 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003343
3344 operation->id = 0;
3345
Paul Elliotte6145dc2023-02-07 12:51:21 +00003346 /* Do not clear either the error_occurred or num_ops elements here as they
3347 * only want to be cleared by the application calling abort, not by abort
3348 * being called at completion of an operation. */
3349
Paul Elliott59ad9452022-12-18 15:09:02 +00003350 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003351}
3352
3353psa_status_t psa_verify_hash_start(
3354 psa_verify_hash_interruptible_operation_t *operation,
3355 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3356 const uint8_t *hash, size_t hash_length,
3357 const uint8_t *signature, size_t signature_length)
3358{
3359 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3360 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3361 psa_key_slot_t *slot;
3362
Paul Elliottc9774412023-02-06 15:14:07 +00003363 /* Check that start has not been previously called, or operation has not
3364 * previously errored. */
3365 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003366 return PSA_ERROR_BAD_STATE;
3367 }
3368
3369 status = psa_sign_verify_check_alg(0, alg);
3370 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003371 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003372 return status;
3373 }
3374
3375 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3376 PSA_KEY_USAGE_VERIFY_HASH,
3377 alg);
3378
3379 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003380 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003381 return status;
3382 }
3383
3384 psa_key_attributes_t attributes = {
3385 .core = slot->attr
3386 };
3387
Paul Elliott296ede92022-12-15 17:00:30 +00003388 /* Ensure ops count gets reset, in case of operation re-use. */
3389 operation->num_ops = 0;
3390
Paul Elliott9fe12f62022-11-30 19:16:02 +00003391 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3392 slot->key.data,
3393 slot->key.bytes,
3394 alg, hash, hash_length,
3395 signature, signature_length);
3396
3397 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003398 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003399 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003400 }
3401
3402 unlock_status = psa_unlock_key_slot(slot);
3403
Paul Elliottc9774412023-02-06 15:14:07 +00003404 if (unlock_status != PSA_SUCCESS) {
3405 operation->error_occurred = 1;
3406 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003407
Paul Elliottc9774412023-02-06 15:14:07 +00003408 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003409}
3410
3411psa_status_t psa_verify_hash_complete(
3412 psa_verify_hash_interruptible_operation_t *operation)
3413{
3414 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3415
Paul Elliottc9774412023-02-06 15:14:07 +00003416 /* Check that start has been called first, and that operation has not
3417 * previously errored. */
3418 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003419 status = PSA_ERROR_BAD_STATE;
3420 goto exit;
3421 }
3422
3423 status = psa_driver_wrapper_verify_hash_complete(operation);
3424
Paul Elliott296ede92022-12-15 17:00:30 +00003425 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003426 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003427 operation);
3428
Paul Elliottebe225c2023-02-10 14:32:53 +00003429exit:
3430
Paul Elliott9fe12f62022-11-30 19:16:02 +00003431 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003432 if (status != PSA_SUCCESS) {
3433 operation->error_occurred = 1;
3434 }
3435
Paul Elliott59ad9452022-12-18 15:09:02 +00003436 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003437 }
3438
3439 return status;
3440}
3441
3442psa_status_t psa_verify_hash_abort(
3443 psa_verify_hash_interruptible_operation_t *operation)
3444{
Paul Elliott59ad9452022-12-18 15:09:02 +00003445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003446
Paul Elliott59ad9452022-12-18 15:09:02 +00003447 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003448
Paul Elliott59ad9452022-12-18 15:09:02 +00003449 /* We clear the number of ops done here, so that it is not cleared when
3450 * the operation fails or succeeds, only on manual abort. */
3451 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003452
Paul Elliottc9774412023-02-06 15:14:07 +00003453 /* Likewise, failure state. */
3454 operation->error_occurred = 0;
3455
Paul Elliott59ad9452022-12-18 15:09:02 +00003456 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003457}
3458
Paul Elliott588f8ed2022-12-02 18:10:26 +00003459/****************************************************************/
3460/* Asymmetric interruptible cryptography internal */
3461/* implementations */
3462/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003463
Paul Elliott588f8ed2022-12-02 18:10:26 +00003464void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3465{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003466
Paul Elliott588f8ed2022-12-02 18:10:26 +00003467#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3468 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3469 defined(MBEDTLS_ECP_RESTARTABLE)
3470
3471 /* Internal implementation uses zero to indicate infinite number max ops,
3472 * therefore avoid this value, and set to minimum possible. */
3473 if (max_ops == 0) {
3474 max_ops = 1;
3475 }
3476
Paul Elliott588f8ed2022-12-02 18:10:26 +00003477 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003478#else
3479 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003480#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3481 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3482 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3483}
3484
Paul Elliott588f8ed2022-12-02 18:10:26 +00003485uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003486 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003487{
3488#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3489 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3490 defined(MBEDTLS_ECP_RESTARTABLE)
3491
Paul Elliott93d9ca82023-02-15 18:14:21 +00003492 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003493#else
3494 (void) operation;
3495 return 0;
3496#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3497 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3498 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3499}
3500
3501uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003502 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003503{
3504 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3505 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3506 defined(MBEDTLS_ECP_RESTARTABLE)
3507
Paul Elliott93d9ca82023-02-15 18:14:21 +00003508 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003509#else
3510 (void) operation;
3511 return 0;
3512#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3513 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3514 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3515}
3516
3517psa_status_t mbedtls_psa_sign_hash_start(
3518 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3519 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3520 size_t key_buffer_size, psa_algorithm_t alg,
3521 const uint8_t *hash, size_t hash_length)
3522{
3523 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003524 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003525
Paul Elliott068fe072023-01-16 13:59:15 +00003526 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3527 return PSA_ERROR_NOT_SUPPORTED;
3528 }
3529
3530 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003531 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003532 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003533
3534#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003535 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3536 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003537
Paul Elliotta1c94092023-02-15 16:38:04 +00003538 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3539
Paul Elliott93d9ca82023-02-15 18:14:21 +00003540 /* Ensure num_ops is zero'ed in case of context re-use. */
3541 operation->num_ops = 0;
3542
Paul Elliott068fe072023-01-16 13:59:15 +00003543 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3544 attributes->core.bits,
3545 key_buffer,
3546 key_buffer_size,
3547 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003548
Paul Elliott068fe072023-01-16 13:59:15 +00003549 if (status != PSA_SUCCESS) {
3550 return status;
3551 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003552
Paul Elliott1bc59df2023-02-05 13:41:57 +00003553 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003554 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003555
Paul Elliott068fe072023-01-16 13:59:15 +00003556 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3557 operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
3558 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003559
Paul Elliott0290a762023-02-09 14:30:24 +00003560 /* We only need to store the same length of hash as the private key size
3561 * here, it would be truncated by the internal implementation anyway. */
3562 required_hash_length = (hash_length < operation->coordinate_bytes ?
3563 hash_length : operation->coordinate_bytes);
3564
Paul Elliottba70ad42023-02-15 18:23:53 +00003565 if (required_hash_length > sizeof(operation->hash)) {
3566 /* Shouldn't happen, but better safe than sorry. */
3567 return PSA_ERROR_CORRUPTION_DETECTED;
3568 }
3569
Paul Elliott0290a762023-02-09 14:30:24 +00003570 memcpy(operation->hash, hash, required_hash_length);
3571 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003572
3573 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003574
3575#else
Paul Elliott068fe072023-01-16 13:59:15 +00003576 (void) operation;
3577 (void) key_buffer;
3578 (void) key_buffer_size;
3579 (void) alg;
3580 (void) hash;
3581 (void) hash_length;
3582 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003583 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003584
Paul Elliott068fe072023-01-16 13:59:15 +00003585 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003586#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3587 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3588 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003589}
3590
3591psa_status_t mbedtls_psa_sign_hash_complete(
3592 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3593 uint8_t *signature, size_t signature_size,
3594 size_t *signature_length)
3595{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003596#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3597 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3598 defined(MBEDTLS_ECP_RESTARTABLE)
3599
Paul Elliotta1c94092023-02-15 16:38:04 +00003600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003601 mbedtls_mpi r;
3602 mbedtls_mpi s;
3603
Paul Elliott46845252023-02-03 14:59:11 +00003604 mbedtls_mpi_init(&r);
3605 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003606
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003607 /* Ensure max_ops is set to the current value (or default). */
3608 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3609
Paul Elliotta1c94092023-02-15 16:38:04 +00003610 if (signature_size < 2 * operation->coordinate_bytes) {
3611 status = PSA_ERROR_BUFFER_TOO_SMALL;
3612 goto exit;
3613 }
3614
Paul Elliott588f8ed2022-12-02 18:10:26 +00003615 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003616
Paul Elliott588f8ed2022-12-02 18:10:26 +00003617#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3618 status = mbedtls_to_psa_error(
3619 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003620 &r,
3621 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003622 &operation->ctx->d,
3623 operation->hash,
3624 operation->hash_length,
3625 operation->md_alg,
3626 mbedtls_psa_get_random,
3627 MBEDTLS_PSA_RANDOM_STATE,
3628 &operation->restart_ctx));
3629#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003630 status = PSA_ERROR_NOT_SUPPORTED;
3631 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003632#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3633 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003634 status = mbedtls_to_psa_error(
3635 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003636 &r,
3637 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638 &operation->ctx->d,
3639 operation->hash,
3640 operation->hash_length,
3641 mbedtls_psa_get_random,
3642 MBEDTLS_PSA_RANDOM_STATE,
3643 mbedtls_psa_get_random,
3644 MBEDTLS_PSA_RANDOM_STATE,
3645 &operation->restart_ctx));
3646 }
3647
Paul Elliottf8e5b562023-02-19 18:43:45 +00003648 /* Hide the fact that the restart context only holds a delta of number of
3649 * ops done during the last operation, not an absolute value. */
3650 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3651
Paul Elliott724bd252023-02-08 12:35:08 +00003652 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003653 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003654 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003655 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003656 operation->coordinate_bytes)
3657 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003658
3659 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003660 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003661 }
3662
3663 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003664 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003665 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003666 operation->coordinate_bytes,
3667 operation->coordinate_bytes)
3668 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669
3670 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003671 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672 }
3673
Paul Elliott1bc59df2023-02-05 13:41:57 +00003674 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675
Paul Elliott724bd252023-02-08 12:35:08 +00003676 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003677 }
Paul Elliott724bd252023-02-08 12:35:08 +00003678
3679exit:
3680
3681 mbedtls_mpi_free(&r);
3682 mbedtls_mpi_free(&s);
3683 return status;
3684
Paul Elliott588f8ed2022-12-02 18:10:26 +00003685 #else
3686
3687 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688 (void) signature;
3689 (void) signature_size;
3690 (void) signature_length;
3691
3692 return PSA_ERROR_NOT_SUPPORTED;
3693
3694#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3695 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3696 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3697}
3698
3699psa_status_t mbedtls_psa_sign_hash_abort(
3700 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3701{
3702
3703#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3704 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3705 defined(MBEDTLS_ECP_RESTARTABLE)
3706
3707 if (operation->ctx) {
3708 mbedtls_ecdsa_free(operation->ctx);
3709 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003710 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711 }
3712
3713 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3714
Paul Elliott93d9ca82023-02-15 18:14:21 +00003715 operation->num_ops = 0;
3716
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717 return PSA_SUCCESS;
3718
3719#else
3720
3721 (void) operation;
3722
3723 return PSA_ERROR_NOT_SUPPORTED;
3724
3725#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3726 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3727 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3728}
3729
3730psa_status_t mbedtls_psa_verify_hash_start(
3731 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3732 const psa_key_attributes_t *attributes,
3733 const uint8_t *key_buffer, size_t key_buffer_size,
3734 psa_algorithm_t alg,
3735 const uint8_t *hash, size_t hash_length,
3736 const uint8_t *signature, size_t signature_length)
3737{
3738 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003739 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003740 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003741
Paul Elliott068fe072023-01-16 13:59:15 +00003742 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3743 return PSA_ERROR_NOT_SUPPORTED;
3744 }
3745
3746 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003747 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003748 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003749
3750#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003751 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3752 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003753
Paul Elliotta1c94092023-02-15 16:38:04 +00003754 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3755 mbedtls_mpi_init(&operation->r);
3756 mbedtls_mpi_init(&operation->s);
3757
Paul Elliott93d9ca82023-02-15 18:14:21 +00003758 /* Ensure num_ops is zero'ed in case of context re-use. */
3759 operation->num_ops = 0;
3760
Paul Elliott068fe072023-01-16 13:59:15 +00003761 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3762 attributes->core.bits,
3763 key_buffer,
3764 key_buffer_size,
3765 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003766
Paul Elliott068fe072023-01-16 13:59:15 +00003767 if (status != PSA_SUCCESS) {
3768 return status;
3769 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770
Paul Elliottc569fc22023-02-10 13:02:54 +00003771 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003772
Paul Elliott1bc59df2023-02-05 13:41:57 +00003773 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003774 return PSA_ERROR_INVALID_SIGNATURE;
3775 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003776
Paul Elliott068fe072023-01-16 13:59:15 +00003777 status = mbedtls_to_psa_error(
3778 mbedtls_mpi_read_binary(&operation->r,
3779 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003780 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003781
Paul Elliott068fe072023-01-16 13:59:15 +00003782 if (status != PSA_SUCCESS) {
3783 return status;
3784 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003785
Paul Elliott068fe072023-01-16 13:59:15 +00003786 status = mbedtls_to_psa_error(
3787 mbedtls_mpi_read_binary(&operation->s,
3788 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003789 coordinate_bytes,
3790 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003791
Paul Elliott068fe072023-01-16 13:59:15 +00003792 if (status != PSA_SUCCESS) {
3793 return status;
3794 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003795
Paul Elliott2c9843f2023-02-15 17:32:42 +00003796 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003797
Paul Elliott2c9843f2023-02-15 17:32:42 +00003798 if (status != PSA_SUCCESS) {
3799 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003800 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003801
Paul Elliott0290a762023-02-09 14:30:24 +00003802 /* We only need to store the same length of hash as the private key size
3803 * here, it would be truncated by the internal implementation anyway. */
3804 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3805 coordinate_bytes);
3806
Paul Elliottba70ad42023-02-15 18:23:53 +00003807 if (required_hash_length > sizeof(operation->hash)) {
3808 /* Shouldn't happen, but better safe than sorry. */
3809 return PSA_ERROR_CORRUPTION_DETECTED;
3810 }
3811
Paul Elliott0290a762023-02-09 14:30:24 +00003812 memcpy(operation->hash, hash, required_hash_length);
3813 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003814
3815 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003816#else
Paul Elliott068fe072023-01-16 13:59:15 +00003817 (void) operation;
3818 (void) key_buffer;
3819 (void) key_buffer_size;
3820 (void) alg;
3821 (void) hash;
3822 (void) hash_length;
3823 (void) signature;
3824 (void) signature_length;
3825 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003826 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003827 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003828
Paul Elliott068fe072023-01-16 13:59:15 +00003829 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003830#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3831 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3832 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003833}
3834
3835psa_status_t mbedtls_psa_verify_hash_complete(
3836 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3837{
3838
3839#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3840 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3841 defined(MBEDTLS_ECP_RESTARTABLE)
3842
Paul Elliottf8e5b562023-02-19 18:43:45 +00003843 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3844
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003845 /* Ensure max_ops is set to the current value (or default). */
3846 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3847
Paul Elliottf8e5b562023-02-19 18:43:45 +00003848 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3850 operation->hash,
3851 operation->hash_length,
3852 &operation->ctx->Q,
3853 &operation->r,
3854 &operation->s,
3855 &operation->restart_ctx));
3856
Paul Elliottf8e5b562023-02-19 18:43:45 +00003857 /* Hide the fact that the restart context only holds a delta of number of
3858 * ops done during the last operation, not an absolute value. */
3859 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3860
3861 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003862#else
3863 (void) operation;
3864
3865 return PSA_ERROR_NOT_SUPPORTED;
3866
3867#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3868 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3869 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3870}
3871
3872psa_status_t mbedtls_psa_verify_hash_abort(
3873 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3874{
3875
3876#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3877 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3878 defined(MBEDTLS_ECP_RESTARTABLE)
3879
3880 if (operation->ctx) {
3881 mbedtls_ecdsa_free(operation->ctx);
3882 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003883 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884 }
3885
3886 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3887
Paul Elliott93d9ca82023-02-15 18:14:21 +00003888 operation->num_ops = 0;
3889
Paul Elliott588f8ed2022-12-02 18:10:26 +00003890 mbedtls_mpi_free(&operation->r);
3891 mbedtls_mpi_free(&operation->s);
3892
3893 return PSA_SUCCESS;
3894
3895#else
3896 (void) operation;
3897
3898 return PSA_ERROR_NOT_SUPPORTED;
3899
3900#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3901 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3902 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3903}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003904
mohammad1603503973b2018-03-12 15:59:30 +02003905/****************************************************************/
3906/* Symmetric cryptography */
3907/****************************************************************/
3908
Gilles Peskine449bd832023-01-11 14:50:10 +01003909static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3910 mbedtls_svc_key_id_t key,
3911 psa_algorithm_t alg,
3912 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003913{
3914 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3915 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003916 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3918 PSA_KEY_USAGE_ENCRYPT :
3919 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003920
3921 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003923 status = PSA_ERROR_BAD_STATE;
3924 goto exit;
3925 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003926
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003928 status = PSA_ERROR_INVALID_ARGUMENT;
3929 goto exit;
3930 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003931
Gilles Peskine449bd832023-01-11 14:50:10 +01003932 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3933 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003934 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003936
Ronald Cron49fafa92021-03-10 08:34:23 +01003937 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003938 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003939 * so we only set it (in the driver wrapper) after resources have been
3940 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003941 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003943 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003945 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003946 }
3947 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003948
Ronald Crona4af55f2020-12-14 14:36:06 +01003949 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003951 };
3952
Ronald Cronab99ac22020-10-01 16:38:28 +02003953 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 if (cipher_operation == MBEDTLS_ENCRYPT) {
3955 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3956 &attributes,
3957 slot->key.data,
3958 slot->key.bytes,
3959 alg);
3960 } else {
3961 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3962 &attributes,
3963 slot->key.data,
3964 slot->key.bytes,
3965 alg);
3966 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003967
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003968exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 if (status != PSA_SUCCESS) {
3970 psa_cipher_abort(operation);
3971 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003972
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003974
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003976}
3977
Gilles Peskine449bd832023-01-11 14:50:10 +01003978psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3979 mbedtls_svc_key_id_t key,
3980 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003981{
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003983}
3984
Gilles Peskine449bd832023-01-11 14:50:10 +01003985psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3986 mbedtls_svc_key_id_t key,
3987 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003988{
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003990}
3991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3993 uint8_t *iv,
3994 size_t iv_size,
3995 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003996{
Ronald Cron6d051732020-10-01 14:10:20 +02003997 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003998 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3999 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004000
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004002 status = PSA_ERROR_BAD_STATE;
4003 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004004 }
4005
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004007 status = PSA_ERROR_BAD_STATE;
4008 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004009 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004010
Ronald Cron2fb90522021-07-05 12:31:44 +02004011 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004012 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004013 status = PSA_ERROR_BUFFER_TOO_SMALL;
4014 goto exit;
4015 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004016
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004018 status = PSA_ERROR_GENERIC_ERROR;
4019 goto exit;
4020 }
4021
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 status = psa_generate_random(local_iv, default_iv_length);
4023 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004024 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 }
Ronald Cron5618a392021-03-26 09:52:26 +01004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 status = psa_driver_wrapper_cipher_set_iv(operation,
4028 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004029
4030exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 if (status == PSA_SUCCESS) {
4032 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004033 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004034 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004036 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004038 }
Ronald Cron6d051732020-10-01 14:10:20 +02004039
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004041}
4042
Gilles Peskine449bd832023-01-11 14:50:10 +01004043psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4044 const uint8_t *iv,
4045 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004046{
Ronald Cron6d051732020-10-01 14:10:20 +02004047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004048
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004050 status = PSA_ERROR_BAD_STATE;
4051 goto exit;
4052 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004053
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004055 status = PSA_ERROR_BAD_STATE;
4056 goto exit;
4057 }
Ronald Crona0d68172021-03-26 10:15:08 +01004058
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004060 status = PSA_ERROR_INVALID_ARGUMENT;
4061 goto exit;
4062 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004063
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 status = psa_driver_wrapper_cipher_set_iv(operation,
4065 iv,
4066 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004067
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004068exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004070 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 } else {
4072 psa_cipher_abort(operation);
4073 }
4074 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004075}
4076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4078 const uint8_t *input,
4079 size_t input_length,
4080 uint8_t *output,
4081 size_t output_size,
4082 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004083{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004084 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004085
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004087 status = PSA_ERROR_BAD_STATE;
4088 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004089 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004092 status = PSA_ERROR_BAD_STATE;
4093 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004094 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004095
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 status = psa_driver_wrapper_cipher_update(operation,
4097 input,
4098 input_length,
4099 output,
4100 output_size,
4101 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004102
4103exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (status != PSA_SUCCESS) {
4105 psa_cipher_abort(operation);
4106 }
Ronald Cron6d051732020-10-01 14:10:20 +02004107
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004109}
4110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4112 uint8_t *output,
4113 size_t output_size,
4114 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004115{
David Saadab4ecc272019-02-14 13:48:10 +02004116 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004119 status = PSA_ERROR_BAD_STATE;
4120 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004121 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004122
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004124 status = PSA_ERROR_BAD_STATE;
4125 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004126 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004127
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 status = psa_driver_wrapper_cipher_finish(operation,
4129 output,
4130 output_size,
4131 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004132
4133exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 if (status == PSA_SUCCESS) {
4135 return psa_cipher_abort(operation);
4136 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004137 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004141 }
mohammad1603503973b2018-03-12 15:59:30 +02004142}
4143
Gilles Peskine449bd832023-01-11 14:50:10 +01004144psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004145{
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004147 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004148 * in use. It's ok to call abort on such an object, and there's
4149 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004151 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004154
Ronald Cron49fafa92021-03-10 08:34:23 +01004155 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004156 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004157 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004160}
4161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4163 psa_algorithm_t alg,
4164 const uint8_t *input,
4165 size_t input_length,
4166 uint8_t *output,
4167 size_t output_size,
4168 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004169{
4170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004171 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004172 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004173 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4174 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004177 status = PSA_ERROR_INVALID_ARGUMENT;
4178 goto exit;
4179 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4182 PSA_KEY_USAGE_ENCRYPT,
4183 alg);
4184 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004185 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004187
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004188 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004190 };
4191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4193 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004194 status = PSA_ERROR_GENERIC_ERROR;
4195 goto exit;
4196 }
4197
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 if (default_iv_length > 0) {
4199 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004200 status = PSA_ERROR_BUFFER_TOO_SMALL;
4201 goto exit;
4202 }
4203
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 status = psa_generate_random(local_iv, default_iv_length);
4205 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004206 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004208 }
4209
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004210 status = psa_driver_wrapper_cipher_encrypt(
4211 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004212 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 mbedtls_buffer_offset(output, default_iv_length),
4214 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004215
4216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 unlock_status = psa_unlock_key_slot(slot);
4218 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004219 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004220 }
Ronald Cron23919522021-07-08 19:00:07 +02004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 if (status == PSA_SUCCESS) {
4223 if (default_iv_length > 0) {
4224 memcpy(output, local_iv, default_iv_length);
4225 }
4226 *output_length += default_iv_length;
4227 } else {
4228 *output_length = 0;
4229 }
4230
4231 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004232}
4233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4235 psa_algorithm_t alg,
4236 const uint8_t *input,
4237 size_t input_length,
4238 uint8_t *output,
4239 size_t output_size,
4240 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004241{
4242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004243 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004244 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004247 status = PSA_ERROR_INVALID_ARGUMENT;
4248 goto exit;
4249 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4252 PSA_KEY_USAGE_DECRYPT,
4253 alg);
4254 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004255 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004257
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004258 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004260 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4263 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004264 status = PSA_ERROR_INVALID_ARGUMENT;
4265 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004267 status = PSA_ERROR_INVALID_ARGUMENT;
4268 goto exit;
4269 }
4270
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004271 status = psa_driver_wrapper_cipher_decrypt(
4272 &attributes, slot->key.data, slot->key.bytes,
4273 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004275
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004276exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 unlock_status = psa_unlock_key_slot(slot);
4278 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004279 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004281
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004283 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 }
Ronald Cron23919522021-07-08 19:00:07 +02004285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004287}
4288
4289
mohammad16035955c982018-04-26 00:53:03 +03004290/****************************************************************/
4291/* AEAD */
4292/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004293
Paul Elliottbaff51c2021-09-28 17:44:45 +01004294/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004295static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004296{
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004298}
4299
4300/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004301static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4302 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004303{
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004305
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004307#if defined(PSA_WANT_ALG_GCM)
4308 case PSA_ALG_GCM:
4309 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 * arbitrarily large nonces. Please note that we do not generally
4311 * recommend the usage of nonces of greater length than
4312 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4313 * size, which can then lead to collisions if you encrypt a very
4314 * large number of messages.*/
4315 if (nonce_length != 0) {
4316 return PSA_SUCCESS;
4317 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004318 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004319#endif /* PSA_WANT_ALG_GCM */
4320#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004321 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 if (nonce_length >= 7 && nonce_length <= 13) {
4323 return PSA_SUCCESS;
4324 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004325 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004326#endif /* PSA_WANT_ALG_CCM */
4327#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004328 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 if (nonce_length == 12) {
4330 return PSA_SUCCESS;
4331 } else if (nonce_length == 8) {
4332 return PSA_ERROR_NOT_SUPPORTED;
4333 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004334 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004335#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004336 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004337 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004339 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004342}
4343
Gilles Peskine449bd832023-01-11 14:50:10 +01004344static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004345{
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4347 return PSA_ERROR_INVALID_ARGUMENT;
4348 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004349
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004351}
4352
Gilles Peskine449bd832023-01-11 14:50:10 +01004353psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4354 psa_algorithm_t alg,
4355 const uint8_t *nonce,
4356 size_t nonce_length,
4357 const uint8_t *additional_data,
4358 size_t additional_data_length,
4359 const uint8_t *plaintext,
4360 size_t plaintext_length,
4361 uint8_t *ciphertext,
4362 size_t ciphertext_size,
4363 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004364{
Ronald Cron215633c2021-03-16 17:15:37 +01004365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4366 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004367
mohammad1603f08a5502018-06-03 15:05:47 +03004368 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 status = psa_aead_check_algorithm(alg);
4371 if (status != PSA_SUCCESS) {
4372 return status;
4373 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004374
Ronald Cron9a986162021-03-26 12:40:07 +01004375 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4377 if (status != PSA_SUCCESS) {
4378 return status;
4379 }
mohammad16035955c982018-04-26 00:53:03 +03004380
Ronald Cron215633c2021-03-16 17:15:37 +01004381 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004383 };
mohammad16035955c982018-04-26 00:53:03 +03004384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 status = psa_aead_check_nonce_length(alg, nonce_length);
4386 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004387 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004389
Ronald Cronde822812021-03-17 16:08:20 +01004390 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004391 &attributes, slot->key.data, slot->key.bytes,
4392 alg,
4393 nonce, nonce_length,
4394 additional_data, additional_data_length,
4395 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4399 memset(ciphertext, 0, ciphertext_size);
4400 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004401
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004402exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004406}
4407
Gilles Peskine449bd832023-01-11 14:50:10 +01004408psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4409 psa_algorithm_t alg,
4410 const uint8_t *nonce,
4411 size_t nonce_length,
4412 const uint8_t *additional_data,
4413 size_t additional_data_length,
4414 const uint8_t *ciphertext,
4415 size_t ciphertext_length,
4416 uint8_t *plaintext,
4417 size_t plaintext_size,
4418 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004419{
Ronald Cron215633c2021-03-16 17:15:37 +01004420 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4421 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004422
Gilles Peskineee652a32018-06-01 19:23:52 +02004423 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 status = psa_aead_check_algorithm(alg);
4426 if (status != PSA_SUCCESS) {
4427 return status;
4428 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004429
Ronald Cron9a986162021-03-26 12:40:07 +01004430 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4432 if (status != PSA_SUCCESS) {
4433 return status;
4434 }
mohammad16035955c982018-04-26 00:53:03 +03004435
Ronald Cron215633c2021-03-16 17:15:37 +01004436 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004438 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004439
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 status = psa_aead_check_nonce_length(alg, nonce_length);
4441 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004442 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004444
Ronald Cronde822812021-03-17 16:08:20 +01004445 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004446 &attributes, slot->key.data, slot->key.bytes,
4447 alg,
4448 nonce, nonce_length,
4449 additional_data, additional_data_length,
4450 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 if (status != PSA_SUCCESS && plaintext_size != 0) {
4454 memset(plaintext, 0, plaintext_size);
4455 }
mohammad160360a64d02018-06-03 17:20:42 +03004456
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004457exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004459
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 return status;
mohammad16035955c982018-04-26 00:53:03 +03004461}
4462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4464{
4465 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004468#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004470 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4472 return PSA_ERROR_INVALID_ARGUMENT;
4473 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004474 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004475#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004476
Przemek Stekiel86679c72022-10-06 17:06:56 +02004477#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004479 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4481 return PSA_ERROR_INVALID_ARGUMENT;
4482 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004483 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004484#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004485
Przemek Stekiel86679c72022-10-06 17:06:56 +02004486#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004488 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 if (tag_len != 16) {
4490 return PSA_ERROR_INVALID_ARGUMENT;
4491 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004492 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004493#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004494
4495 default:
4496 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004498 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004500}
4501
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004502/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004503static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4504 int is_encrypt,
4505 mbedtls_svc_key_id_t key,
4506 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004507{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004508 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4509 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4510 psa_key_slot_t *slot = NULL;
4511 psa_key_usage_t key_usage = 0;
4512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 status = psa_aead_check_algorithm(alg);
4514 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004515 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004517
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004519 status = PSA_ERROR_BAD_STATE;
4520 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004521 }
4522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 if (operation->nonce_set || operation->lengths_set ||
4524 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004525 status = PSA_ERROR_BAD_STATE;
4526 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004527 }
4528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004530 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004532 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4536 alg);
4537 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004538 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004540
4541 psa_key_attributes_t attributes = {
4542 .core = slot->attr
4543 };
4544
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004546 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004548
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 if (is_encrypt) {
4550 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4551 &attributes,
4552 slot->key.data,
4553 slot->key.bytes,
4554 alg);
4555 } else {
4556 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4557 &attributes,
4558 slot->key.data,
4559 slot->key.bytes,
4560 alg);
4561 }
4562 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004563 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004567
4568exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004572 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004574 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 } else {
4576 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004577 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004580}
4581
Paul Elliott302ff6b2021-04-20 18:10:30 +01004582/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004583psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4584 mbedtls_svc_key_id_t key,
4585 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004586{
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004588}
4589
4590/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004591psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4592 mbedtls_svc_key_id_t key,
4593 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004594{
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004596}
4597
4598/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004599psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4600 uint8_t *nonce,
4601 size_t nonce_size,
4602 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004603{
4604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004605 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004606 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004607
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004608 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004611 status = PSA_ERROR_BAD_STATE;
4612 goto exit;
4613 }
4614
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004616 status = PSA_ERROR_BAD_STATE;
4617 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004618 }
4619
Paul Elliotte193ea82021-10-01 13:00:16 +01004620 /* For CCM, this size may not be correct according to the PSA
4621 * specification. The PSA Crypto 1.0.1 specification states:
4622 *
4623 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4624 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4625 *
4626 * However this restriction that L has to be the smallest integer is not
4627 * applied in practice, and it is not implementable here since the
4628 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4630 operation->alg);
4631 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004632 status = PSA_ERROR_BUFFER_TOO_SMALL;
4633 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004634 }
4635
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 status = psa_generate_random(local_nonce, required_nonce_size);
4637 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004642
Paul Elliott39dc6b82021-05-11 19:16:09 +01004643exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 if (status == PSA_SUCCESS) {
4645 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004646 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 } else {
4648 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004649 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004652}
4653
4654/* Set the nonce for a multipart authenticated encryption or decryption
4655 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004656psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4657 const uint8_t *nonce,
4658 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004659{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004660 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4661
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004663 status = PSA_ERROR_BAD_STATE;
4664 goto exit;
4665 }
4666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004668 status = PSA_ERROR_BAD_STATE;
4669 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004670 }
4671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4673 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004674 status = PSA_ERROR_INVALID_ARGUMENT;
4675 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004676 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4679 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004680
Paul Elliott39dc6b82021-05-11 19:16:09 +01004681exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004683 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 } else {
4685 psa_aead_abort(operation);
4686 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004689}
4690
4691/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004692psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4693 size_t ad_length,
4694 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004695{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004699 status = PSA_ERROR_BAD_STATE;
4700 goto exit;
4701 }
4702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 if (operation->lengths_set || operation->ad_started ||
4704 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004705 status = PSA_ERROR_BAD_STATE;
4706 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004707 }
4708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004710#if defined(PSA_WANT_ALG_GCM)
4711 case PSA_ALG_GCM:
4712 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 * bits. Without the guard this code will generate warnings on 32bit
4714 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004715#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if (((uint64_t) ad_length) >> 61 != 0 ||
4717 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004718 status = PSA_ERROR_INVALID_ARGUMENT;
4719 goto exit;
4720 }
Paul Elliott325d3742021-09-27 17:56:28 +01004721#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004722 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004723#endif /* PSA_WANT_ALG_GCM */
4724#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004725 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004727 status = PSA_ERROR_INVALID_ARGUMENT;
4728 goto exit;
4729 }
4730 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004731#endif /* PSA_WANT_ALG_CCM */
4732#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004733 case PSA_ALG_CHACHA20_POLY1305:
4734 /* No length restrictions for ChaChaPoly. */
4735 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004736#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004737 default:
4738 break;
4739 }
Paul Elliott325d3742021-09-27 17:56:28 +01004740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4742 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004743
Paul Elliott39dc6b82021-05-11 19:16:09 +01004744exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004746 operation->ad_remaining = ad_length;
4747 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004748 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 } else {
4750 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004751 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004754}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004755
4756/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004757psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4758 const uint8_t *input,
4759 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004760{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004761 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004764 status = PSA_ERROR_BAD_STATE;
4765 goto exit;
4766 }
4767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004769 status = PSA_ERROR_BAD_STATE;
4770 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771 }
4772
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 if (operation->lengths_set) {
4774 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004775 status = PSA_ERROR_INVALID_ARGUMENT;
4776 goto exit;
4777 }
4778
4779 operation->ad_remaining -= input_length;
4780 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004781#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004783 status = PSA_ERROR_BAD_STATE;
4784 goto exit;
4785 }
4786#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004787
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 status = psa_driver_wrapper_aead_update_ad(operation, input,
4789 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004790
Paul Elliott39dc6b82021-05-11 19:16:09 +01004791exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004793 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 } else {
4795 psa_aead_abort(operation);
4796 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004799}
4800
4801/* Encrypt or decrypt a message fragment in an active multipart AEAD
4802 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004803psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4804 const uint8_t *input,
4805 size_t input_length,
4806 uint8_t *output,
4807 size_t output_size,
4808 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004809{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004810 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004811
4812 *output_length = 0;
4813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004815 status = PSA_ERROR_BAD_STATE;
4816 goto exit;
4817 }
4818
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004820 status = PSA_ERROR_BAD_STATE;
4821 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004822 }
4823
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004825 /* Additional data length was supplied, but not all the additional
4826 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004828 status = PSA_ERROR_INVALID_ARGUMENT;
4829 goto exit;
4830 }
4831
4832 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004834 status = PSA_ERROR_INVALID_ARGUMENT;
4835 goto exit;
4836 }
4837
4838 operation->body_remaining -= input_length;
4839 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004840#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004842 status = PSA_ERROR_BAD_STATE;
4843 goto exit;
4844 }
4845#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4848 output, output_size,
4849 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004850
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004853 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 } else {
4855 psa_aead_abort(operation);
4856 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004859}
4860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004862{
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 if (operation->id == 0 || !operation->nonce_set) {
4864 return PSA_ERROR_BAD_STATE;
4865 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4868 operation->body_remaining != 0)) {
4869 return PSA_ERROR_INVALID_ARGUMENT;
4870 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004873}
4874
Paul Elliott302ff6b2021-04-20 18:10:30 +01004875/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004876psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4877 uint8_t *ciphertext,
4878 size_t ciphertext_size,
4879 size_t *ciphertext_length,
4880 uint8_t *tag,
4881 size_t tag_size,
4882 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004883{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004884 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4885
Paul Elliott302ff6b2021-04-20 18:10:30 +01004886 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004887 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 status = psa_aead_final_checks(operation);
4890 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004891 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004895 status = PSA_ERROR_BAD_STATE;
4896 goto exit;
4897 }
4898
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4900 ciphertext_size,
4901 ciphertext_length,
4902 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004903
Paul Elliott39dc6b82021-05-11 19:16:09 +01004904exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004905
4906
Paul Elliottf47b0952021-05-21 18:02:33 +01004907 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004908 * the zero tag size, make sure the tag is set to something implausible.
4909 * Even if the operation succeeds, make sure we clear the rest of the
4910 * buffer to prevent potential leakage of anything previously placed in
4911 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004912 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004913
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004917}
4918
4919/* Finish authenticating and decrypting a message in a multipart AEAD
4920 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004921psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4922 uint8_t *plaintext,
4923 size_t plaintext_size,
4924 size_t *plaintext_length,
4925 const uint8_t *tag,
4926 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004927{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004928 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4929
Paul Elliott302ff6b2021-04-20 18:10:30 +01004930 *plaintext_length = 0;
4931
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 status = psa_aead_final_checks(operation);
4933 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004934 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004936
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004938 status = PSA_ERROR_BAD_STATE;
4939 goto exit;
4940 }
4941
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4943 plaintext_size,
4944 plaintext_length,
4945 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004946
4947exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004951}
4952
4953/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004954psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004955{
4956 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4957
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004959 /* The object has (apparently) been initialized but it is not (yet)
4960 * in use. It's ok to call abort on such an object, and there's
4961 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004963 }
4964
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004966
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004968
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004970}
4971
Gilles Peskinee59236f2018-01-27 23:32:46 +01004972/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004973/* Generators */
4974/****************************************************************/
4975
Przemek Stekiel69c46792022-06-10 12:59:51 +02004976#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004977 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004978 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4979 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004980#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004981#endif /* At least one builtin KDF */
4982
Przemek Stekiel69c46792022-06-10 12:59:51 +02004983#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004984 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4985 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4986static psa_status_t psa_key_derivation_start_hmac(
4987 psa_mac_operation_t *operation,
4988 psa_algorithm_t hash_alg,
4989 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004991{
4992 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4995 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4996 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004997
Steven Cooreman72f736a2021-05-07 14:14:37 +02004998 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005000
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 status = psa_driver_wrapper_mac_sign_setup(operation,
5002 &attributes,
5003 hmac_key, hmac_key_length,
5004 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005005
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 psa_reset_key_attributes(&attributes);
5007 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005008}
5009#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005010
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005011#define HKDF_STATE_INIT 0 /* no input yet */
5012#define HKDF_STATE_STARTED 1 /* got salt */
5013#define HKDF_STATE_KEYED 2 /* got key */
5014#define HKDF_STATE_OUTPUT 3 /* output started */
5015
Gilles Peskinecbe66502019-05-16 16:59:18 +02005016static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005018{
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5020 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5021 } else {
5022 return operation->alg;
5023 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005024}
5025
Gilles Peskine449bd832023-01-11 14:50:10 +01005026psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005027{
5028 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5030 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005031 /* The object has (apparently) been initialized but it is not
5032 * in use. It's ok to call abort on such an object, and there's
5033 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005035#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5037 mbedtls_free(operation->ctx.hkdf.info);
5038 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5039 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005040#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005041#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5042 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5044 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5045 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5046 if (operation->ctx.tls12_prf.secret != NULL) {
5047 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5048 operation->ctx.tls12_prf.secret_length);
5049 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005050 }
5051
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 if (operation->ctx.tls12_prf.seed != NULL) {
5053 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5054 operation->ctx.tls12_prf.seed_length);
5055 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005056 }
5057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 if (operation->ctx.tls12_prf.label != NULL) {
5059 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5060 operation->ctx.tls12_prf.label_length);
5061 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005062 }
5063
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 if (operation->ctx.tls12_prf.other_secret != NULL) {
5065 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5066 operation->ctx.tls12_prf.other_secret_length);
5067 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005068 }
5069
Steven Cooremana6df6042021-04-29 19:32:25 +02005070 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005071
5072 /* We leave the fields Ai and output_block to be erased safely by the
5073 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005075#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5076 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005077#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5079 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5080 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5081 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005082#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005083 {
5084 status = PSA_ERROR_BAD_STATE;
5085 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 mbedtls_platform_zeroize(operation, sizeof(*operation));
5087 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005088}
5089
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005090psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005092{
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005096 }
5097
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005098 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005100}
5101
Gilles Peskine449bd832023-01-11 14:50:10 +01005102psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5103 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005104{
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 if (operation->alg == 0) {
5106 return PSA_ERROR_BAD_STATE;
5107 }
5108 if (capacity > operation->capacity) {
5109 return PSA_ERROR_INVALID_ARGUMENT;
5110 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005111 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005113}
5114
Przemek Stekiel69c46792022-06-10 12:59:51 +02005115#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005116/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005117static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5118 psa_algorithm_t kdf_alg,
5119 uint8_t *output,
5120 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005121{
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5123 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005124 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005125 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005126#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005128#else
5129 const uint8_t last_block = 0xff;
5130#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005131
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 if (hkdf->state < HKDF_STATE_KEYED ||
5133 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005134#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005136#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 )) {
5138 return PSA_ERROR_BAD_STATE;
5139 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005140 hkdf->state = HKDF_STATE_OUTPUT;
5141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005143 /* Copy what remains of the current block */
5144 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005146 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 }
5148 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005149 output += n;
5150 output_length -= n;
5151 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005153 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005155 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005156 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005157 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005158 * object was corrupted or if this function is called directly
5159 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 if (hkdf->block_number == last_block) {
5161 return PSA_ERROR_BAD_STATE;
5162 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005163
5164 /* We need a new block */
5165 ++hkdf->block_number;
5166 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005167
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5169 hash_alg,
5170 hkdf->prk,
5171 hash_length);
5172 if (status != PSA_SUCCESS) {
5173 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005174 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005175
5176 if (hkdf->block_number != 1) {
5177 status = psa_mac_update(&hkdf->hmac,
5178 hkdf->output_block,
5179 hash_length);
5180 if (status != PSA_SUCCESS) {
5181 return status;
5182 }
5183 }
5184 status = psa_mac_update(&hkdf->hmac,
5185 hkdf->info,
5186 hkdf->info_length);
5187 if (status != PSA_SUCCESS) {
5188 return status;
5189 }
5190 status = psa_mac_update(&hkdf->hmac,
5191 &hkdf->block_number, 1);
5192 if (status != PSA_SUCCESS) {
5193 return status;
5194 }
5195 status = psa_mac_sign_finish(&hkdf->hmac,
5196 hkdf->output_block,
5197 sizeof(hkdf->output_block),
5198 &hmac_output_length);
5199 if (status != PSA_SUCCESS) {
5200 return status;
5201 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005202 }
5203
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005205}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005206#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005207
John Durkop07cc04a2020-11-16 22:08:34 -08005208#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5209 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005210static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5211 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005213{
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5215 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005216 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5217 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005218 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005219
Janos Follath7742fee2019-06-17 12:58:10 +01005220 /* We can't be wanting more output after block 0xff, otherwise
5221 * the capacity check in psa_key_derivation_output_bytes() would have
5222 * prevented this call. It could happen only if the operation
5223 * object was corrupted or if this function is called directly
5224 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 if (tls12_prf->block_number == 0xff) {
5226 return PSA_ERROR_CORRUPTION_DETECTED;
5227 }
Janos Follath7742fee2019-06-17 12:58:10 +01005228
5229 /* We need a new block */
5230 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005231 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005232
5233 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5234 *
5235 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5236 *
5237 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5238 * HMAC_hash(secret, A(2) + seed) +
5239 * HMAC_hash(secret, A(3) + seed) + ...
5240 *
5241 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005242 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005243 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005244 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005245 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005246 * is currently extracted as `output_block` and where i is
5247 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005248 */
5249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 status = psa_key_derivation_start_hmac(&hmac,
5251 hash_alg,
5252 tls12_prf->secret,
5253 tls12_prf->secret_length);
5254 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005255 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005257
5258 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005260 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5261 * the variable seed and in this instance means it in the context of the
5262 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 status = psa_mac_update(&hmac,
5264 tls12_prf->label,
5265 tls12_prf->label_length);
5266 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005267 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 }
5269 status = psa_mac_update(&hmac,
5270 tls12_prf->seed,
5271 tls12_prf->seed_length);
5272 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005273 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 }
5275 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005276 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5278 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005279 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005281 }
5282
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 status = psa_mac_sign_finish(&hmac,
5284 tls12_prf->Ai, hash_length,
5285 &hmac_output_length);
5286 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005287 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 }
5289 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005290 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005292
5293 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 status = psa_key_derivation_start_hmac(&hmac,
5295 hash_alg,
5296 tls12_prf->secret,
5297 tls12_prf->secret_length);
5298 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005299 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 }
5301 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5302 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005303 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 }
5305 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5306 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005307 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 }
5309 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5310 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005311 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 }
5313 status = psa_mac_sign_finish(&hmac,
5314 tls12_prf->output_block, hash_length,
5315 &hmac_output_length);
5316 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005317 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005319
Janos Follath7742fee2019-06-17 12:58:10 +01005320
5321cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 cleanup_status = psa_mac_abort(&hmac);
5323 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005324 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005326
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005328}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005329
Janos Follath844eb0e2019-06-19 12:10:49 +01005330static psa_status_t psa_key_derivation_tls12_prf_read(
5331 psa_tls12_prf_key_derivation_t *tls12_prf,
5332 psa_algorithm_t alg,
5333 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005335{
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5337 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005338 psa_status_t status;
5339 uint8_t offset, length;
5340
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005342 case PSA_TLS12_PRF_STATE_LABEL_SET:
5343 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5344 break;
5345 case PSA_TLS12_PRF_STATE_OUTPUT:
5346 break;
5347 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005349 }
5350
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005352 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 if (tls12_prf->left_in_block == 0) {
5354 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5355 alg);
5356 if (status != PSA_SUCCESS) {
5357 return status;
5358 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005359
5360 continue;
5361 }
5362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005364 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005366 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005368
5369 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005371 output += length;
5372 output_length -= length;
5373 tls12_prf->left_in_block -= length;
5374 }
5375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005377}
John Durkop07cc04a2020-11-16 22:08:34 -08005378#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5379 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005380
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005381#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5382static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5383 psa_tls12_ecjpake_to_pms_t *ecjpake,
5384 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005386{
5387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005388 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 if (output_length != 32) {
5391 return PSA_ERROR_INVALID_ARGUMENT;
5392 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5395 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5396 &output_size);
5397 if (status != PSA_SUCCESS) {
5398 return status;
5399 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 if (output_size != output_length) {
5402 return PSA_ERROR_GENERIC_ERROR;
5403 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005406}
5407#endif
5408
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005409psa_status_t psa_key_derivation_output_bytes(
5410 psa_key_derivation_operation_t *operation,
5411 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005413{
5414 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005418 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005420 }
5421
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005423 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005424 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005425 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005426 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005427 goto exit;
5428 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005430 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005431 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005432 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5433 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005434 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005435 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005437 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005438 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005439
Przemek Stekiel69c46792022-06-10 12:59:51 +02005440#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5442 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5443 output, output_length);
5444 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005445#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005446#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5447 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5449 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5450 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5451 kdf_alg, output,
5452 output_length);
5453 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005454#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5455 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005456#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005458 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5460 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005461#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
5462
Gilles Peskineeab56e42018-07-12 17:12:33 +02005463 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005464 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005466 }
5467
5468exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005470 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005471 * This allows us to differentiate between exhausted operations and
5472 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5473 * operations. */
5474 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005476 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005478 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005480}
5481
David Brown7807bf72021-02-09 16:28:23 -07005482#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005483static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005484{
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 if (data_size >= 8) {
5486 mbedtls_des_key_set_parity(data);
5487 }
5488 if (data_size >= 16) {
5489 mbedtls_des_key_set_parity(data + 8);
5490 }
5491 if (data_size >= 24) {
5492 mbedtls_des_key_set_parity(data + 16);
5493 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005494}
David Brown7807bf72021-02-09 16:28:23 -07005495#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005496
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005497/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 * ECC keys on a Weierstrass elliptic curve require the generation
5499 * of a private key which is an integer
5500 * in the range [1, N - 1], where N is the boundary of the private key domain:
5501 * N is the prime p for Diffie-Hellman, or the order of the
5502 * curve’s base point for ECC.
5503 *
5504 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5505 * This function generates the private key using the following process:
5506 *
5507 * 1. Draw a byte string of length ceiling(m/8) bytes.
5508 * 2. If m is not a multiple of 8, set the most significant
5509 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5510 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5511 * 4. If k > N - 2, discard the result and return to step 1.
5512 * 5. Output k + 1 as the private key.
5513 *
5514 * This method allows compliance to NIST standards, specifically the methods titled
5515 * Key-Pair Generation by Testing Candidates in the following publications:
5516 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5517 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5518 * Diffie-Hellman keys.
5519 *
5520 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5521 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5522 *
5523 * Note: Function allocates memory for *data buffer, so given *data should be
5524 * always NULL.
5525 */
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005526#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5527 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5528 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5529 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5530 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005531static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005532 psa_key_slot_t *slot,
5533 size_t bits,
5534 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005535 uint8_t **data
5536 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005537{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005538 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005539 mbedtls_mpi k;
5540 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005542 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 mbedtls_mpi_init(&k);
5545 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005546
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005547 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005549 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005553 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005554 goto cleanup;
5555 }
5556
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005557 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005559
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005561
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005562 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005563 /* Let m be the bit size of N. */
5564 size_t m = ecp_group.nbits;
5565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 size_t m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005567
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005568 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005570
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005571 /* Note: This function is always called with *data == NULL and it
5572 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 *data = mbedtls_calloc(1, m_bytes);
5574 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005575 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005576 goto cleanup;
5577 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005578
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005580 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005582 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005584
5585 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005587 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005588 * (8 * ceiling(m/8) - m) bits of the first byte in
5589 * the string to zero.
5590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005592 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005593 }
5594
5595 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 * big-endian byte string.
5597 */
5598 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005599
5600 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 * Result of comparison is returned. When it indicates error
5602 * then this function is called again.
5603 */
5604 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005605 }
5606
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005607 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5609 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005610cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 if (ret != 0) {
5612 status = mbedtls_to_psa_error(ret);
5613 }
5614 if (status != PSA_SUCCESS) {
5615 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005616 *data = NULL;
5617 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 mbedtls_mpi_free(&k);
5619 mbedtls_mpi_free(&diff_N_2);
5620 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005621}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005622
5623/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5624 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5625 *
5626 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5627 * draw a 32-byte string and process it as specified in
5628 * Elliptic Curves for Security [RFC7748] §5.
5629 *
5630 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5631 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5632 *
5633 * Note: Function allocates memory for *data buffer, so given *data should be
5634 * always NULL.
5635 */
5636
5637static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5638 size_t bits,
5639 psa_key_derivation_operation_t *operation,
5640 uint8_t **data
5641 )
5642{
5643 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005645
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005647 case 255:
5648 output_length = 32;
5649 break;
5650 case 448:
5651 output_length = 56;
5652 break;
5653 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005655 break;
5656 }
5657
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005659
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 if (*data == NULL) {
5661 return PSA_ERROR_INSUFFICIENT_MEMORY;
5662 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005663
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005665
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005667 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005669
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005671 case 255:
5672 (*data)[0] &= 248;
5673 (*data)[31] &= 127;
5674 (*data)[31] |= 64;
5675 break;
5676 case 448:
5677 (*data)[0] &= 252;
5678 (*data)[55] |= 128;
5679 break;
5680 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005682 break;
5683 }
5684
5685 return status;
5686}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005687#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5688 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5689 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5690 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5691 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005692
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005693static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005694 psa_key_slot_t *slot,
5695 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005697{
5698 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305700 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005701 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005702
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5704 return PSA_ERROR_INVALID_ARGUMENT;
5705 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005706
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005707#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
5708 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
5709 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
5710 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
5711 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005712 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5713 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5714 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005715 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5717 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005718 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 }
5720 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005721 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5723 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005724 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005726 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005727 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01005728#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
5729 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
5730 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
5731 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
5732 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 if (key_type_is_raw_bytes(slot->attr.type)) {
5734 if (bits % 8 != 0) {
5735 return PSA_ERROR_INVALID_ARGUMENT;
5736 }
5737 data = mbedtls_calloc(1, bytes);
5738 if (data == NULL) {
5739 return PSA_ERROR_INSUFFICIENT_MEMORY;
5740 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005741
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 status = psa_key_derivation_output_bytes(operation, data, bytes);
5743 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005744 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 }
David Brown12ca5032021-02-11 11:02:00 -07005746#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5748 psa_des_set_key_parity(data, bytes);
5749 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005750#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 } else {
5752 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005753 }
Ronald Crondd04d422020-11-28 15:14:42 +01005754
Ronald Cronbf33c932020-11-28 18:06:53 +01005755 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005756 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005758 };
5759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
5761 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
5762 &storage_size);
5763 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305764 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 }
Archanad8a83dc2021-06-14 10:04:16 +05305766 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 status = psa_allocate_buffer_to_slot(slot, storage_size);
5768 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305769 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 }
Archanad8a83dc2021-06-14 10:04:16 +05305771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 status = psa_driver_wrapper_import_key(&attributes,
5773 data, bytes,
5774 slot->key.data,
5775 slot->key.bytes,
5776 &slot->key.bytes, &bits);
5777 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005778 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005780
5781exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 mbedtls_free(data);
5783 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005784}
5785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
5787 psa_key_derivation_operation_t *operation,
5788 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005789{
5790 psa_status_t status;
5791 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005792 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005793
Ronald Cron81709fc2020-11-14 12:10:32 +01005794 *key = MBEDTLS_SVC_KEY_ID_INIT;
5795
Gilles Peskine0f84d622019-09-12 19:03:13 +02005796 /* Reject any attempt to create a zero-length key so that we don't
5797 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 if (psa_get_key_bits(attributes) == 0) {
5799 return PSA_ERROR_INVALID_ARGUMENT;
5800 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 if (operation->alg == PSA_ALG_NONE) {
5803 return PSA_ERROR_BAD_STATE;
5804 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 if (!operation->can_output_key) {
5807 return PSA_ERROR_NOT_PERMITTED;
5808 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5811 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005812#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005814 /* Deriving a key in a secure element is not implemented yet. */
5815 status = PSA_ERROR_NOT_SUPPORTED;
5816 }
5817#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 if (status == PSA_SUCCESS) {
5819 status = psa_generate_derived_key_internal(slot,
5820 attributes->core.bits,
5821 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005822 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 if (status == PSA_SUCCESS) {
5824 status = psa_finish_key_creation(slot, driver, key);
5825 }
5826 if (status != PSA_SUCCESS) {
5827 psa_fail_key_creation(slot, driver);
5828 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005829
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005831}
5832
Gilles Peskineeab56e42018-07-12 17:12:33 +02005833
5834
5835/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005836/* Key derivation */
5837/****************************************************************/
5838
Steven Cooreman932ffb72021-02-15 12:14:32 +01005839#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005840static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005841{
5842#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5844 return 1;
5845 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005846#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005847#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5849 return 1;
5850 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005851#endif
5852#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5854 return 1;
5855 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005856#endif
5857#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5859 return 1;
5860 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005861#endif
5862#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5864 return 1;
5865 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005866#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005867#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5869 return 1;
5870 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005871#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005873}
5874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005876{
5877 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 psa_status_t status = psa_hash_setup(&operation, alg);
5879 psa_hash_abort(&operation);
5880 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005881}
5882
Gilles Peskine969c5d62019-01-16 15:53:06 +01005883static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005884 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005886{
Janos Follath5fe19732019-06-20 15:09:30 +01005887 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5888 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01005890
Gilles Peskine969c5d62019-01-16 15:53:06 +01005891 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 if (!is_kdf_alg_supported(kdf_alg)) {
5893 return PSA_ERROR_NOT_SUPPORTED;
5894 }
John Durkop07cc04a2020-11-16 22:08:34 -08005895
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005896 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005897 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5899 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
5900 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5901 if (hash_size == 0) {
5902 return PSA_ERROR_NOT_SUPPORTED;
5903 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005904
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005905 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5906 * we might fail later, which is somewhat unfriendly and potentially
5907 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 psa_status_t status = psa_hash_try_support(hash_alg);
5909 if (status != PSA_SUCCESS) {
5910 return status;
5911 }
5912 } else {
5913 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005914 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5917 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
5918 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
5919 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005920 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005921#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005922 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
5924 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005925 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005927#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5928 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 operation->capacity = 255 * hash_size;
5930 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005931}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005934{
5935#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 if (alg == PSA_ALG_ECDH) {
5937 return PSA_SUCCESS;
5938 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005939#endif
5940 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005942}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005943
5944static int psa_key_derivation_allows_free_form_secret_input(
5945 psa_algorithm_t kdf_alg)
5946{
5947#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5948 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5949 return 0;
5950 }
5951#endif
5952 (void) kdf_alg;
5953 return 1;
5954}
John Durkop07cc04a2020-11-16 22:08:34 -08005955#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
5958 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005959{
5960 psa_status_t status;
5961
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 if (operation->alg != 0) {
5963 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005964 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005965
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
5967 return PSA_ERROR_INVALID_ARGUMENT;
5968 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
5969#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5970 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
5971 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
5972 status = psa_key_agreement_try_support(ka_alg);
5973 if (status != PSA_SUCCESS) {
5974 return status;
5975 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01005976 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
5977 return PSA_ERROR_INVALID_ARGUMENT;
5978 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
5980#else
5981 return PSA_ERROR_NOT_SUPPORTED;
5982#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5983 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
5984#if defined(AT_LEAST_ONE_BUILTIN_KDF)
5985 status = psa_key_derivation_setup_kdf(operation, alg);
5986#else
5987 return PSA_ERROR_NOT_SUPPORTED;
5988#endif /* AT_LEAST_ONE_BUILTIN_KDF */
5989 } else {
5990 return PSA_ERROR_INVALID_ARGUMENT;
5991 }
5992
5993 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005994 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 }
5996 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005997}
5998
Przemek Stekiel69c46792022-06-10 12:59:51 +02005999#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006000static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6001 psa_algorithm_t kdf_alg,
6002 psa_key_derivation_step_t step,
6003 const uint8_t *data,
6004 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006005{
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006007 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006009 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006010#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6012 return PSA_ERROR_INVALID_ARGUMENT;
6013 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006014#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 if (hkdf->state != HKDF_STATE_INIT) {
6016 return PSA_ERROR_BAD_STATE;
6017 } else {
6018 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6019 hash_alg,
6020 data, data_length);
6021 if (status != PSA_SUCCESS) {
6022 return status;
6023 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006024 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006026 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006027 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006028#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006030 /* We shouldn't be in different state as HKDF_EXPAND only allows
6031 * two inputs: SECRET (this case) and INFO which does not modify
6032 * the state. It could happen only if the hkdf
6033 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 if (hkdf->state != HKDF_STATE_INIT) {
6035 return PSA_ERROR_BAD_STATE;
6036 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006037
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006038 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6040 return PSA_ERROR_INVALID_ARGUMENT;
6041 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006042
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 memcpy(hkdf->prk, data, data_length);
6044 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006045#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006046 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006047 /* HKDF: If no salt was provided, use an empty salt.
6048 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006050#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6052 return PSA_ERROR_BAD_STATE;
6053 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006054#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6056 hash_alg,
6057 NULL, 0);
6058 if (status != PSA_SUCCESS) {
6059 return status;
6060 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006061 hkdf->state = HKDF_STATE_STARTED;
6062 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 if (hkdf->state != HKDF_STATE_STARTED) {
6064 return PSA_ERROR_BAD_STATE;
6065 }
6066 status = psa_mac_update(&hkdf->hmac,
6067 data, data_length);
6068 if (status != PSA_SUCCESS) {
6069 return status;
6070 }
6071 status = psa_mac_sign_finish(&hkdf->hmac,
6072 hkdf->prk,
6073 sizeof(hkdf->prk),
6074 &data_length);
6075 if (status != PSA_SUCCESS) {
6076 return status;
6077 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006078 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006079
Gilles Peskine2b522db2019-04-12 15:11:49 +02006080 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006081 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006082#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006084 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006086 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006088#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006089 {
6090 /* Block 0 is empty, and the next block will be
6091 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006093 }
6094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006096 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006097#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6099 return PSA_ERROR_INVALID_ARGUMENT;
6100 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006101#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006102#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6104 hkdf->state == HKDF_STATE_INIT) {
6105 return PSA_ERROR_BAD_STATE;
6106 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006107#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (hkdf->state == HKDF_STATE_OUTPUT) {
6109 return PSA_ERROR_BAD_STATE;
6110 }
6111 if (hkdf->info_set) {
6112 return PSA_ERROR_BAD_STATE;
6113 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006114 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (data_length != 0) {
6116 hkdf->info = mbedtls_calloc(1, data_length);
6117 if (hkdf->info == NULL) {
6118 return PSA_ERROR_INSUFFICIENT_MEMORY;
6119 }
6120 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006121 }
6122 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006124 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006126 }
6127}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006128#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006129
John Durkop07cc04a2020-11-16 22:08:34 -08006130#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6131 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006132static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6133 const uint8_t *data,
6134 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006135{
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6137 return PSA_ERROR_BAD_STATE;
6138 }
Janos Follathf08e2652019-06-13 09:05:41 +01006139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 if (data_length != 0) {
6141 prf->seed = mbedtls_calloc(1, data_length);
6142 if (prf->seed == NULL) {
6143 return PSA_ERROR_INSUFFICIENT_MEMORY;
6144 }
Janos Follathf08e2652019-06-13 09:05:41 +01006145
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006147 prf->seed_length = data_length;
6148 }
Janos Follathf08e2652019-06-13 09:05:41 +01006149
Gilles Peskine43f958b2020-12-13 14:55:14 +01006150 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006151
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006153}
6154
Gilles Peskine449bd832023-01-11 14:50:10 +01006155static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6156 const uint8_t *data,
6157 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006158{
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6160 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6161 return PSA_ERROR_BAD_STATE;
6162 }
Janos Follath81550542019-06-13 14:26:34 +01006163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 if (data_length != 0) {
6165 prf->secret = mbedtls_calloc(1, data_length);
6166 if (prf->secret == NULL) {
6167 return PSA_ERROR_INSUFFICIENT_MEMORY;
6168 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006171 prf->secret_length = data_length;
6172 }
Janos Follath81550542019-06-13 14:26:34 +01006173
Gilles Peskine43f958b2020-12-13 14:55:14 +01006174 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006177}
6178
Gilles Peskine449bd832023-01-11 14:50:10 +01006179static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6180 const uint8_t *data,
6181 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006182{
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6184 return PSA_ERROR_BAD_STATE;
6185 }
Janos Follath63028dd2019-06-13 09:15:47 +01006186
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 if (data_length != 0) {
6188 prf->label = mbedtls_calloc(1, data_length);
6189 if (prf->label == NULL) {
6190 return PSA_ERROR_INSUFFICIENT_MEMORY;
6191 }
Janos Follath63028dd2019-06-13 09:15:47 +01006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006194 prf->label_length = data_length;
6195 }
Janos Follath63028dd2019-06-13 09:15:47 +01006196
Gilles Peskine43f958b2020-12-13 14:55:14 +01006197 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006198
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006200}
6201
Gilles Peskine449bd832023-01-11 14:50:10 +01006202static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6203 psa_key_derivation_step_t step,
6204 const uint8_t *data,
6205 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006206{
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006208 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006210 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006212 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006214 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006216 }
6217}
John Durkop07cc04a2020-11-16 22:08:34 -08006218#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6219 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6220
6221#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6222static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6223 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006224 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006226{
6227 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6229 4 + data_length + prf->other_secret_length :
6230 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6233 return PSA_ERROR_INVALID_ARGUMENT;
6234 }
John Durkop07cc04a2020-11-16 22:08:34 -08006235
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 uint8_t *pms = mbedtls_calloc(1, pms_len);
6237 if (pms == NULL) {
6238 return PSA_ERROR_INSUFFICIENT_MEMORY;
6239 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006240 uint8_t *cur = pms;
6241
6242 /* pure-PSK:
6243 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006244 *
6245 * The premaster secret is formed as follows: if the PSK is N octets
6246 * long, concatenate a uint16 with the value N, N zero octets, a second
6247 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006248 *
6249 * mixed-PSK:
6250 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6251 * follows: concatenate a uint16 with the length of the other secret,
6252 * the other secret itself, uint16 with the length of PSK, and the
6253 * PSK itself.
6254 * For details please check:
6255 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6256 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6257 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006258 */
6259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6261 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6262 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6263 if (prf->other_secret_length != 0) {
6264 memcpy(cur, prf->other_secret, prf->other_secret_length);
6265 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006266 cur += prf->other_secret_length;
6267 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 } else {
6269 *cur++ = MBEDTLS_BYTE_1(data_length);
6270 *cur++ = MBEDTLS_BYTE_0(data_length);
6271 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006272 cur += data_length;
6273 }
6274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 *cur++ = MBEDTLS_BYTE_1(data_length);
6276 *cur++ = MBEDTLS_BYTE_0(data_length);
6277 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006278 cur += data_length;
6279
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 mbedtls_platform_zeroize(pms, pms_len);
6283 mbedtls_free(pms);
6284 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006285}
Janos Follath6660f0e2019-06-17 08:44:03 +01006286
Przemek Stekiele47201b2022-04-19 13:53:28 +02006287static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6288 psa_tls12_prf_key_derivation_t *prf,
6289 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006291{
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6293 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006294 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006295
6296 if (data_length != 0) {
6297 prf->other_secret = mbedtls_calloc(1, data_length);
6298 if (prf->other_secret == NULL) {
6299 return PSA_ERROR_INSUFFICIENT_MEMORY;
6300 }
6301
6302 memcpy(prf->other_secret, data, data_length);
6303 prf->other_secret_length = data_length;
6304 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006305 prf->other_secret_length = 0;
6306 }
6307
6308 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006311}
6312
Janos Follath6660f0e2019-06-17 08:44:03 +01006313static psa_status_t psa_tls12_prf_psk_to_ms_input(
6314 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006315 psa_key_derivation_step_t step,
6316 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006318{
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006320 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 return psa_tls12_prf_psk_to_ms_set_key(prf,
6322 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006323 break;
6324 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6326 data,
6327 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006328 break;
6329 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006331 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006332
Przemek Stekiele47201b2022-04-19 13:53:28 +02006333 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006334}
John Durkop07cc04a2020-11-16 22:08:34 -08006335#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006336
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006337#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6338static psa_status_t psa_tls12_ecjpake_to_pms_input(
6339 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006340 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006341 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006343{
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6345 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6346 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006347 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006348
6349 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 if (data[0] != 0x04) {
6351 return PSA_ERROR_INVALID_ARGUMENT;
6352 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006353
6354 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006358}
6359#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02006360/** Check whether the given key type is acceptable for the given
6361 * input step of a key derivation.
6362 *
6363 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6364 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6365 * Both secret and non-secret inputs can alternatively have the type
6366 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6367 * that the input was passed as a buffer rather than via a key object.
6368 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006369static int psa_key_derivation_check_input_type(
6370 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006372{
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006374 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 if (key_type == PSA_KEY_TYPE_DERIVE) {
6376 return PSA_SUCCESS;
6377 }
6378 if (key_type == PSA_KEY_TYPE_NONE) {
6379 return PSA_SUCCESS;
6380 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006381 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006382 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 if (key_type == PSA_KEY_TYPE_DERIVE) {
6384 return PSA_SUCCESS;
6385 }
6386 if (key_type == PSA_KEY_TYPE_NONE) {
6387 return PSA_SUCCESS;
6388 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006389 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006390 case PSA_KEY_DERIVATION_INPUT_LABEL:
6391 case PSA_KEY_DERIVATION_INPUT_SALT:
6392 case PSA_KEY_DERIVATION_INPUT_INFO:
6393 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6395 return PSA_SUCCESS;
6396 }
6397 if (key_type == PSA_KEY_TYPE_NONE) {
6398 return PSA_SUCCESS;
6399 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006400 break;
6401 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006403}
6404
Janos Follathb80a94e2019-06-12 15:54:46 +01006405static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006406 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006407 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006408 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006409 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006411{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006412 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 status = psa_key_derivation_check_input_type(step, key_type);
6416 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006417 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006419
Przemek Stekiel69c46792022-06-10 12:59:51 +02006420#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6422 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6423 step, data, data_length);
6424 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006425#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006426#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6428 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6429 step, data, data_length);
6430 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006431#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6432#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6434 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6435 step, data, data_length);
6436 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006437#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006438#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006440 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6442 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006443#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006444 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006445 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006446 (void) data;
6447 (void) data_length;
6448 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006450 }
6451
Gilles Peskine224b0d62019-09-23 18:13:17 +02006452exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 if (status != PSA_SUCCESS) {
6454 psa_key_derivation_abort(operation);
6455 }
6456 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006457}
6458
Janos Follath51f4a0f2019-06-14 11:35:55 +01006459psa_status_t psa_key_derivation_input_bytes(
6460 psa_key_derivation_operation_t *operation,
6461 psa_key_derivation_step_t step,
6462 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006464{
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 return psa_key_derivation_input_internal(operation, step,
6466 PSA_KEY_TYPE_NONE,
6467 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006468}
6469
Janos Follath51f4a0f2019-06-14 11:35:55 +01006470psa_status_t psa_key_derivation_input_key(
6471 psa_key_derivation_operation_t *operation,
6472 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006474{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006475 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006476 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006477 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006478
Ronald Cron5c522922020-11-14 16:35:34 +01006479 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6481 if (status != PSA_SUCCESS) {
6482 psa_key_derivation_abort(operation);
6483 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006484 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006485
6486 /* Passing a key object as a SECRET input unlocks the permission
6487 * to output to a key object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006489 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 status = psa_key_derivation_input_internal(operation,
6493 step, slot->attr.type,
6494 slot->key.data,
6495 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006498
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006500}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006501
6502
6503
6504/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006505/* Key agreement */
6506/****************************************************************/
6507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6509 const uint8_t *key_buffer,
6510 size_t key_buffer_size,
6511 psa_algorithm_t alg,
6512 const uint8_t *peer_key,
6513 size_t peer_key_length,
6514 uint8_t *shared_secret,
6515 size_t shared_secret_size,
6516 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006517{
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006519#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006520 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6522 key_buffer_size, alg,
6523 peer_key, peer_key_length,
6524 shared_secret,
6525 shared_secret_size,
6526 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006527#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006528 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006529 (void) attributes;
6530 (void) key_buffer;
6531 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006532 (void) peer_key;
6533 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006534 (void) shared_secret;
6535 (void) shared_secret_size;
6536 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006538 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006539}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006540
Aditya Deshpande17845b82022-10-13 17:21:01 +01006541/** Internal function for raw key agreement
6542 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006543 * to the driver's implementation if a driver is present.
6544 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006545 * (psa_key_agreement_raw_builtin).
6546 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006547static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6548 psa_key_slot_t *private_key,
6549 const uint8_t *peer_key,
6550 size_t peer_key_length,
6551 uint8_t *shared_secret,
6552 size_t shared_secret_size,
6553 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006554{
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6556 return PSA_ERROR_NOT_SUPPORTED;
6557 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006558
6559 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01006561 };
6562
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 return psa_driver_wrapper_key_agreement(&attributes,
6564 private_key->key.data,
6565 private_key->key.bytes, alg,
6566 peer_key, peer_key_length,
6567 shared_secret,
6568 shared_secret_size,
6569 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006570}
6571
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006572/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006573 * to potentially free embedded data structures and wipe confidential data.
6574 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006575static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6576 psa_key_derivation_step_t step,
6577 psa_key_slot_t *private_key,
6578 const uint8_t *peer_key,
6579 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006580{
6581 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00006582 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02006583 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006585
6586 /* Step 1: run the secret agreement algorithm to generate the shared
6587 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 status = psa_key_agreement_raw_internal(ka_alg,
6589 private_key,
6590 peer_key, peer_key_length,
6591 shared_secret,
6592 sizeof(shared_secret),
6593 &shared_secret_length);
6594 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006595 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006597
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006598 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006599 * the shared secret. A shared secret is permitted wherever a key
6600 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 status = psa_key_derivation_input_internal(operation, step,
6602 PSA_KEY_TYPE_DERIVE,
6603 shared_secret,
6604 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006605exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6607 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006608}
6609
Gilles Peskine449bd832023-01-11 14:50:10 +01006610psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6611 psa_key_derivation_step_t step,
6612 mbedtls_svc_key_id_t private_key,
6613 const uint8_t *peer_key,
6614 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006615{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006616 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006617 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006618 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006619
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6621 return PSA_ERROR_INVALID_ARGUMENT;
6622 }
Ronald Cron5c522922020-11-14 16:35:34 +01006623 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6625 if (status != PSA_SUCCESS) {
6626 return status;
6627 }
6628 status = psa_key_agreement_internal(operation, step,
6629 slot,
6630 peer_key, peer_key_length);
6631 if (status != PSA_SUCCESS) {
6632 psa_key_derivation_abort(operation);
6633 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006634 /* If a private key has been added as SECRET, we allow the derived
6635 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006637 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006639 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006640
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006642
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006644}
6645
Gilles Peskine449bd832023-01-11 14:50:10 +01006646psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
6647 mbedtls_svc_key_id_t private_key,
6648 const uint8_t *peer_key,
6649 size_t peer_key_length,
6650 uint8_t *output,
6651 size_t output_size,
6652 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006653{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006654 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006655 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006656 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006657
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006659 status = PSA_ERROR_INVALID_ARGUMENT;
6660 goto exit;
6661 }
Ronald Cron5c522922020-11-14 16:35:34 +01006662 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
6664 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006665 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006667
Gilles Peskine3e561302022-04-14 00:17:15 +02006668 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
6669 * for the output size. The PSA specification only guarantees that this
6670 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
6671 * but it might be nice to allow smaller buffers if the output fits.
6672 * At the time of writing this comment, with only ECDH implemented,
6673 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
6674 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
6675 * be exact for it as well. */
6676 size_t expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
6678 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02006679 status = PSA_ERROR_BUFFER_TOO_SMALL;
6680 goto exit;
6681 }
6682
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 status = psa_key_agreement_raw_internal(alg, slot,
6684 peer_key, peer_key_length,
6685 output, output_size,
6686 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006687
6688exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02006690 /* If an error happens and is not handled properly, the output
6691 * may be used as a key to protect sensitive data. Arrange for such
6692 * a key to be random, which is likely to result in decryption or
6693 * verification errors. This is better than filling the buffer with
6694 * some constant data such as zeros, which would result in the data
6695 * being protected with a reproducible, easily knowable key.
6696 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006698 *output_length = output_size;
6699 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006700
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006702
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006704}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006705
6706
Gilles Peskine30524eb2020-11-13 17:02:26 +01006707
Gilles Peskine86a440b2018-11-12 18:39:40 +01006708/****************************************************************/
6709/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006710/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006711
Gilles Peskine30524eb2020-11-13 17:02:26 +01006712/** Initialize the PSA random generator.
6713 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006714static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006715{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006716#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006718#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6719
Gilles Peskine30524eb2020-11-13 17:02:26 +01006720 /* Set default configuration if
6721 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006723 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 }
6725 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006726 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01006727 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01006728
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006730#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6731 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6732 /* The PSA entropy injection feature depends on using NV seed as an entropy
6733 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 mbedtls_entropy_add_source(&rng->entropy,
6735 mbedtls_nv_seed_poll, NULL,
6736 MBEDTLS_ENTROPY_BLOCK_SIZE,
6737 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01006738#endif
6739
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006741#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006742}
6743
6744/** Deinitialize the PSA random generator.
6745 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006746static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006747{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006748#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006750#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
6752 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006753#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006754}
6755
6756/** Seed the PSA random generator.
6757 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006758static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01006759{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006760#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6761 /* Do nothing: the external RNG seeds itself. */
6762 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006764#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006765 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
6767 drbg_seed, sizeof(drbg_seed) - 1);
6768 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006769#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006770}
6771
Gilles Peskine449bd832023-01-11 14:50:10 +01006772psa_status_t psa_generate_random(uint8_t *output,
6773 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006774{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006775 GUARD_MODULE_INITIALIZED;
6776
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006777#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6778
6779 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006780 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
6781 output, output_size,
6782 &output_length);
6783 if (status != PSA_SUCCESS) {
6784 return status;
6785 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006786 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00006787 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 if (output_length != output_size) {
6789 return PSA_ERROR_INSUFFICIENT_ENTROPY;
6790 }
6791 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006792
6793#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6794
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006796 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6798 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6799 output_size);
6800 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
6801 output, request_size);
6802 if (ret != 0) {
6803 return mbedtls_to_psa_error(ret);
6804 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01006805 output_size -= request_size;
6806 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006807 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006808 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006809#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006810}
6811
Gilles Peskine8814fc42020-12-14 15:33:44 +01006812/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006813 *
6814 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6815 * `psa_generate_random(...)`. The state parameter is ignored since the
6816 * PSA API doesn't support passing an explicit state.
6817 *
6818 * In the non-external case, psa_generate_random() calls an
6819 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6820 * and semantics as mbedtls_psa_get_random(). As an optimization,
6821 * instead of doing this back-and-forth between the PSA API and the
6822 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6823 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006824 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006825#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6826int mbedtls_psa_get_random(void *p_rng,
6827 unsigned char *output,
6828 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01006829{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006830 /* This function takes a pointer to the RNG state because that's what
6831 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6832 * its own state internally and doesn't let the caller access that state.
6833 * So we just ignore the state parameter, and in practice we'll pass
6834 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006835 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 psa_status_t status = psa_generate_random(output, output_size);
6837 if (status == PSA_SUCCESS) {
6838 return 0;
6839 } else {
6840 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
6841 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01006842}
6843#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6844
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006845#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006846#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006847
Gilles Peskine449bd832023-01-11 14:50:10 +01006848psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
6849 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006850{
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 if (global_data.initialized) {
6852 return PSA_ERROR_NOT_PERMITTED;
6853 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006854
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
6856 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
6857 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
6858 return PSA_ERROR_INVALID_ARGUMENT;
6859 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006860
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006862}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006863#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006864
Ronald Cron3772afe2021-02-08 16:10:05 +01006865/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006866 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006867 * \param type The key type
6868 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006869 *
6870 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006871 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006872 * \retval #PSA_ERROR_INVALID_ARGUMENT
6873 * The size in bits of the key is not valid.
6874 * \retval #PSA_ERROR_NOT_SUPPORTED
6875 * The type and/or the size in bits of the key or the combination of
6876 * the two is not supported.
6877 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006878static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006880{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006882
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 if (key_type_is_raw_bytes(type)) {
6884 status = psa_validate_unstructured_key_bit_size(type, bits);
6885 if (status != PSA_SUCCESS) {
6886 return status;
6887 }
6888 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006889#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6891 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
6892 return PSA_ERROR_NOT_SUPPORTED;
6893 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006894
Ronald Cron01b2aba2020-10-05 09:42:02 +02006895 /* Accept only byte-aligned keys, for the same reasons as
6896 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 if (bits % 8 != 0) {
6898 return PSA_ERROR_NOT_SUPPORTED;
6899 }
6900 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006901#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006902
Ronald Cron5c4d3862020-12-07 11:07:24 +01006903#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01006905 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 return PSA_SUCCESS;
6907 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006908#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006909 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006911 }
6912
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006914}
6915
Ronald Cron55ed0592020-10-05 10:30:40 +02006916psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006917 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006919{
6920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006921 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006922
Gilles Peskine449bd832023-01-11 14:50:10 +01006923 if ((attributes->domain_parameters == NULL) &&
6924 (attributes->domain_parameters_size != 0)) {
6925 return PSA_ERROR_INVALID_ARGUMENT;
6926 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02006927
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 if (key_type_is_raw_bytes(type)) {
6929 status = psa_generate_random(key_buffer, key_buffer_size);
6930 if (status != PSA_SUCCESS) {
6931 return status;
6932 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006933
David Brown12ca5032021-02-11 11:02:00 -07006934#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 if (type == PSA_KEY_TYPE_DES) {
6936 psa_des_set_key_parity(key_buffer, key_buffer_size);
6937 }
David Brown8107e312021-02-12 08:08:46 -07006938#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01006940
Jaeden Amero424fa932021-05-14 08:34:32 +01006941#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6942 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
6944 return mbedtls_psa_rsa_generate_key(attributes,
6945 key_buffer,
6946 key_buffer_size,
6947 key_buffer_length);
6948 } else
Jaeden Amero424fa932021-05-14 08:34:32 +01006949#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6950 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006951
John Durkop9814fa22020-11-04 12:28:15 -08006952#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
6954 return mbedtls_psa_ecp_generate_key(attributes,
6955 key_buffer,
6956 key_buffer_size,
6957 key_buffer_length);
6958 } else
John Durkop9814fa22020-11-04 12:28:15 -08006959#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006960 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 (void) key_buffer_length;
6962 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02006963 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006964
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006966}
Darryl Green0c6575a2018-11-07 16:05:30 +00006967
Gilles Peskine449bd832023-01-11 14:50:10 +01006968psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
6969 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006970{
6971 psa_status_t status;
6972 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006973 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006974 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006975
Ronald Cron81709fc2020-11-14 12:10:32 +01006976 *key = MBEDTLS_SVC_KEY_ID_INIT;
6977
Gilles Peskine0f84d622019-09-12 19:03:13 +02006978 /* Reject any attempt to create a zero-length key so that we don't
6979 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 if (psa_get_key_bits(attributes) == 0) {
6981 return PSA_ERROR_INVALID_ARGUMENT;
6982 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006983
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006984 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
6986 return PSA_ERROR_INVALID_ARGUMENT;
6987 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006988
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
6990 &slot, &driver);
6991 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02006992 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 }
Gilles Peskine11792082019-08-06 18:36:36 +02006994
Ronald Cron977c2472020-10-13 08:32:21 +02006995 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05306996 * storage ( thus not in the case of generating a key in a secure element
6997 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
6998 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 if (slot->key.data == NULL) {
7000 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7001 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007002 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 attributes->core.type, attributes->core.bits);
7004 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007005 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007006 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007007
7008 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 attributes->core.type,
7010 attributes->core.bits);
7011 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007012 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 attributes, &key_buffer_size);
7014 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007015 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 }
Ronald Cron977c2472020-10-13 08:32:21 +02007017 }
Ronald Cron977c2472020-10-13 08:32:21 +02007018
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7020 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007021 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 }
Ronald Cron977c2472020-10-13 08:32:21 +02007023 }
7024
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 status = psa_driver_wrapper_generate_key(attributes,
7026 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007027
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 if (status != PSA_SUCCESS) {
7029 psa_remove_key_data_from_memory(slot);
7030 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007031
Gilles Peskine11792082019-08-06 18:36:36 +02007032exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 if (status == PSA_SUCCESS) {
7034 status = psa_finish_key_creation(slot, driver, key);
7035 }
7036 if (status != PSA_SUCCESS) {
7037 psa_fail_key_creation(slot, driver);
7038 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007039
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007041}
7042
Gilles Peskinee59236f2018-01-27 23:32:46 +01007043/****************************************************************/
7044/* Module setup */
7045/****************************************************************/
7046
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007047#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007048psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007049 void (* entropy_init)(mbedtls_entropy_context *ctx),
7050 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007051{
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7053 return PSA_ERROR_BAD_STATE;
7054 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007055 global_data.rng.entropy_init = entropy_init;
7056 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007058}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007059#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007060
Gilles Peskine449bd832023-01-11 14:50:10 +01007061void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007062{
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 psa_wipe_all_key_slots();
7064 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7065 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007066 }
7067 /* Wipe all remaining data, including configuration.
7068 * In particular, this sets all state indicator to the value
7069 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007071
7072 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007074}
7075
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007076#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7077/** Recover a transaction that was interrupted by a power failure.
7078 *
7079 * This function is called during initialization, before psa_crypto_init()
7080 * returns. If this function returns a failure status, the initialization
7081 * fails.
7082 */
7083static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007085{
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007087 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7088 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 /* TODO - fall through to the failure case until this
7090 * is implemented.
7091 * https://github.com/ARMmbed/mbed-crypto/issues/218
7092 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007093 default:
7094 /* We found an unsupported transaction in the storage.
7095 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007096 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007097 }
7098}
7099#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7100
Gilles Peskine449bd832023-01-11 14:50:10 +01007101psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007102{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007103 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007104
Gilles Peskinec6b69072018-11-20 21:42:52 +01007105 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 if (global_data.initialized != 0) {
7107 return PSA_SUCCESS;
7108 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007109
Gilles Peskine30524eb2020-11-13 17:02:26 +01007110 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007112 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 status = mbedtls_psa_random_seed(&global_data.rng);
7114 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007117 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007118
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 status = psa_initialize_key_slots();
7120 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007122 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007123
Ronald Cron9ba76912021-04-10 16:57:30 +02007124 /* Init drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 status = psa_driver_wrapper_init();
7126 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02007127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 }
Gilles Peskined9348f22019-10-01 15:22:29 +02007129
Gilles Peskine4b734222019-07-24 15:56:31 +02007130#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 status = psa_crypto_load_transaction();
7132 if (status == PSA_SUCCESS) {
7133 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7134 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007136 }
7137 status = psa_crypto_stop_transaction();
7138 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007139 /* There's no transaction to complete. It's all good. */
7140 status = PSA_SUCCESS;
7141 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007142#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007143
Gilles Peskinec6b69072018-11-20 21:42:52 +01007144 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007145 global_data.initialized = 1;
7146
7147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 if (status != PSA_SUCCESS) {
7149 mbedtls_psa_crypto_free();
7150 }
7151 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007152}
7153
7154#endif /* MBEDTLS_PSA_CRYPTO_C */