blob: e881f2f3cbf592e463327dada0dd513adacc14a1 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
Gilles Peskinee59236f2018-01-27 23:32:46 +010029#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010030#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031
Ronald Crond6d28882020-12-14 14:56:02 +010032#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020035#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010037#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010038#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010039#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010040#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020042#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010044#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010045/* Include internal declarations that are useful for implementing persistently
46 * stored keys. */
47#include "psa_crypto_storage.h"
48
Gilles Peskineb2b64d32020-12-14 16:43:58 +010049#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010050
Gilles Peskineb46bef22019-07-30 21:32:04 +020051#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010056#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020057#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000058#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020059#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000074#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000076#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
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 Peskine48c0ea12018-06-21 14:15:31 +020097static int key_type_is_raw_bytes( psa_key_type_t type )
98{
Gilles Peskine78b3bb62018-08-10 16:03:41 +020099 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 Peskine2d277862018-06-18 15:41:12 +0200107typedef struct
108{
Gilles Peskinec6b69072018-11-20 21:42:52 +0100109 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110 unsigned rng_state : 2;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100111 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112} psa_global_data_t;
113
114static psa_global_data_t global_data;
115
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100116#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
117mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
118 &global_data.rng.drbg;
119#endif
120
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121#define GUARD_MODULE_INITIALIZED \
122 if( global_data.initialized == 0 ) \
123 return( PSA_ERROR_BAD_STATE );
124
Steven Cooreman01164162020-07-20 15:31:37 +0200125psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100127 /* Mbed TLS error codes can combine a high-level error code and a
128 * low-level error code. The low-level error usually reflects the
129 * root cause better, so dispatch on that preferably. */
130 int low_level_ret = - ( -ret & 0x007f );
131 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132 {
133 case 0:
134 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100135
136 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
137 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskinea5905292018-02-07 20:59:33 +0100138 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9a944802018-06-21 09:35:35 +0200139 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
140 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
141 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
142 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
143 case MBEDTLS_ERR_ASN1_INVALID_DATA:
144 return( PSA_ERROR_INVALID_ARGUMENT );
145 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
146 return( PSA_ERROR_INSUFFICIENT_MEMORY );
147 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
148 return( PSA_ERROR_BUFFER_TOO_SMALL );
149
Jaeden Amero93e21112019-02-20 13:57:28 +0000150#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000151 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000152#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100153 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
154 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100155
156 case MBEDTLS_ERR_CCM_BAD_INPUT:
157 return( PSA_ERROR_INVALID_ARGUMENT );
158 case MBEDTLS_ERR_CCM_AUTH_FAILED:
159 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100160
Gilles Peskine26869f22019-05-06 15:25:00 +0200161 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
162 return( PSA_ERROR_INVALID_ARGUMENT );
163
164 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
165 return( PSA_ERROR_BAD_STATE );
166 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
167 return( PSA_ERROR_INVALID_SIGNATURE );
168
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
170 return( PSA_ERROR_NOT_SUPPORTED );
171 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
172 return( PSA_ERROR_INVALID_ARGUMENT );
173 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
174 return( PSA_ERROR_INSUFFICIENT_MEMORY );
175 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
176 return( PSA_ERROR_INVALID_PADDING );
177 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200178 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100179 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
180 return( PSA_ERROR_INVALID_SIGNATURE );
181 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200182 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100183
Gilles Peskine82e57d12020-11-13 21:31:17 +0100184#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
185 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100186 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
187 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
189 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
190 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
191 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
192 return( PSA_ERROR_NOT_SUPPORTED );
193 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
194 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100195#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100196
197 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
198 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100199
Gilles Peskinee59236f2018-01-27 23:32:46 +0100200 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
201 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
202 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
203 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100204
205 case MBEDTLS_ERR_GCM_AUTH_FAILED:
206 return( PSA_ERROR_INVALID_SIGNATURE );
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200207 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
208 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200210 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211
Gilles Peskine82e57d12020-11-13 21:31:17 +0100212#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
213 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100214 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
215 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100216 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223#endif
224
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
228 return( PSA_ERROR_INVALID_ARGUMENT );
229 case MBEDTLS_ERR_MD_ALLOC_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_MEMORY );
231 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
232 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Gilles Peskinef76aa772018-10-29 19:24:33 +0100234 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
235 return( PSA_ERROR_STORAGE_FAILURE );
236 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
237 return( PSA_ERROR_INVALID_ARGUMENT );
238 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
239 return( PSA_ERROR_INVALID_ARGUMENT );
240 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
241 return( PSA_ERROR_BUFFER_TOO_SMALL );
242 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
243 return( PSA_ERROR_INVALID_ARGUMENT );
244 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
245 return( PSA_ERROR_INVALID_ARGUMENT );
246 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100251 case MBEDTLS_ERR_PK_ALLOC_FAILED:
252 return( PSA_ERROR_INSUFFICIENT_MEMORY );
253 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
254 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100258 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
259 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
262 return( PSA_ERROR_NOT_SUPPORTED );
263 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
264 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
265 return( PSA_ERROR_NOT_PERMITTED );
266 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_PK_INVALID_ALG:
269 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
270 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
271 return( PSA_ERROR_NOT_SUPPORTED );
272 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
273 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200274 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
275 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea5905292018-02-07 20:59:33 +0100276
Gilles Peskineff2d2002019-05-06 15:26:23 +0200277 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
278 return( PSA_ERROR_HARDWARE_FAILURE );
279 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
280 return( PSA_ERROR_NOT_SUPPORTED );
281
Gilles Peskinea5905292018-02-07 20:59:33 +0100282 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_RSA_INVALID_PADDING:
285 return( PSA_ERROR_INVALID_PADDING );
286 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
287 return( PSA_ERROR_HARDWARE_FAILURE );
288 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
291 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200292 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
294 return( PSA_ERROR_INVALID_SIGNATURE );
295 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
296 return( PSA_ERROR_BUFFER_TOO_SMALL );
297 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100298 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200299
itayzafrir5c753392018-05-08 11:18:38 +0300300 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300301 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300302 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300303 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
306 return( PSA_ERROR_NOT_SUPPORTED );
307 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
308 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
309 return( PSA_ERROR_INVALID_SIGNATURE );
310 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
311 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100312 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
313 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100314
Janos Follatha13b9052019-11-22 12:48:59 +0000315 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
316 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300317
Gilles Peskinee59236f2018-01-27 23:32:46 +0100318 default:
David Saadab4ecc272019-02-14 13:48:10 +0200319 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100320 }
321}
322
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200323
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200324
325
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100326/****************************************************************/
327/* Key management */
328/****************************************************************/
329
John Durkop9814fa22020-11-04 12:28:15 -0800330#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800331 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronf467d632021-11-19 18:02:34 +0100332 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
333 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
334 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100335mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100336 size_t bits,
337 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200338{
339 switch( curve )
340 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100341 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100342 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100343 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100344#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100345 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100346 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100347#endif
348#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100349 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100350 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100351#endif
352#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100353 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100354 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100355#endif
356#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100357 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100358 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100359#endif
360#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100361 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100362 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100363 case 528:
364 if( bits_is_sloppy )
365 return( MBEDTLS_ECP_DP_SECP521R1 );
366 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100367#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100368 }
369 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100370
Paul Elliott8ff510a2020-06-02 17:19:28 +0100371 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100372 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100373 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100374#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100375 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100376 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100377#endif
378#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100379 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100381#endif
382#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100383 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100384 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100385#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100386 }
387 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100388
Paul Elliott8ff510a2020-06-02 17:19:28 +0100389 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100391 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100392#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100393 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100394 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 256:
396 if( bits_is_sloppy )
397 return( MBEDTLS_ECP_DP_CURVE25519 );
398 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100399#endif
400#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100401 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100402 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100403#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100404 }
405 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100406
Paul Elliott8ff510a2020-06-02 17:19:28 +0100407 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100408 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100409 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100410#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100411 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100413#endif
414#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100415 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100416 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100417#endif
418#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100419 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100420 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100421#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100422 }
423 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200424 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100426 (void) bits_is_sloppy;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200428}
John Durkop9814fa22020-11-04 12:28:15 -0800429#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cronf467d632021-11-19 18:02:34 +0100430 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
431 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
432 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
433 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200434
Archana4d7ae1d2021-07-07 02:50:22 +0530435psa_status_t psa_validate_unstructured_key_bit_size( psa_key_type_t type,
Archana449608b2021-09-08 15:36:05 +0530436 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200437{
438 /* Check that the bit size is acceptable for the key type */
439 switch( type )
440 {
441 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200442 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200443 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200444 case PSA_KEY_TYPE_PASSWORD:
445 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200446 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100447#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200448 case PSA_KEY_TYPE_AES:
449 if( bits != 128 && bits != 192 && bits != 256 )
450 return( PSA_ERROR_INVALID_ARGUMENT );
451 break;
452#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200453#if defined(PSA_WANT_KEY_TYPE_ARIA)
454 case PSA_KEY_TYPE_ARIA:
455 if( bits != 128 && bits != 192 && bits != 256 )
456 return( PSA_ERROR_INVALID_ARGUMENT );
457 break;
458#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100459#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460 case PSA_KEY_TYPE_CAMELLIA:
461 if( bits != 128 && bits != 192 && bits != 256 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100465#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466 case PSA_KEY_TYPE_DES:
467 if( bits != 64 && bits != 128 && bits != 192 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100471#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200472 case PSA_KEY_TYPE_CHACHA20:
473 if( bits != 256 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 default:
478 return( PSA_ERROR_NOT_SUPPORTED );
479 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200480 if( bits % 8 != 0 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200483 return( PSA_SUCCESS );
484}
485
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100486/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100487 *
Steven Cooremancd640932021-03-08 12:00:27 +0100488 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
489 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100490 *
491 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100492 * \param[in] key_type The key type of the key to be used with the
493 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100494 *
495 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100496 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100497 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100498 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100499 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100500MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100501 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100502 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100503{
Steven Cooremancd640932021-03-08 12:00:27 +0100504 if( PSA_ALG_IS_HMAC( algorithm ) )
505 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100506 if( key_type == PSA_KEY_TYPE_HMAC )
507 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100508 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100509
510 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100511 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100512 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
513 * key. */
514 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
515 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
516 {
517 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
518 * the block length (larger than 1) for block ciphers. */
519 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
520 return( PSA_SUCCESS );
521 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100522 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100523
524 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100525}
526
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200527psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
528 size_t buffer_length )
Steven Cooreman75b74362020-07-28 14:30:13 +0200529{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100530 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200531 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200532
Ronald Cronea0f8a62020-11-25 17:52:23 +0100533 slot->key.data = mbedtls_calloc( 1, buffer_length );
534 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200535 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200536
Ronald Cronea0f8a62020-11-25 17:52:23 +0100537 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200538 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200539}
540
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200541psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
542 const uint8_t* data,
543 size_t data_length )
544{
545 psa_status_t status = psa_allocate_buffer_to_slot( slot,
546 data_length );
547 if( status != PSA_SUCCESS )
548 return( status );
549
Ronald Cronea0f8a62020-11-25 17:52:23 +0100550 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200551 return( PSA_SUCCESS );
552}
553
Ronald Crona0fe59f2020-11-29 11:14:53 +0100554psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100555 const psa_key_attributes_t *attributes,
556 const uint8_t *data, size_t data_length,
557 uint8_t *key_buffer, size_t key_buffer_size,
558 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
561 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200563 /* zero-length keys are never supported. */
564 if( data_length == 0 )
565 return( PSA_ERROR_NOT_SUPPORTED );
566
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100567 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100569 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200570
Archana449608b2021-09-08 15:36:05 +0530571 status = psa_validate_unstructured_key_bit_size( attributes->core.type,
572 *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200573 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200574 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200575
Ronald Crondd04d422020-11-28 15:14:42 +0100576 /* Copy the key material. */
577 memcpy( key_buffer, data, data_length );
578 *key_buffer_length = data_length;
579 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200580
Steven Cooreman40120f62020-10-29 11:42:22 +0100581 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100582 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100583 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200584 {
John Durkop9814fa22020-11-04 12:28:15 -0800585#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
586 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100587 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200588 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100589 return( mbedtls_psa_ecp_import_key( attributes,
590 data, data_length,
591 key_buffer, key_buffer_size,
592 key_buffer_length,
593 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100594 }
John Durkop9814fa22020-11-04 12:28:15 -0800595#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
596 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700597#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
598 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100599 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200600 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100601 return( mbedtls_psa_rsa_import_key( attributes,
602 data, data_length,
603 key_buffer, key_buffer_size,
604 key_buffer_length,
605 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200606 }
John Durkop9814fa22020-11-04 12:28:15 -0800607#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
608 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100609 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100610
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100611 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100612}
613
Gilles Peskinef603c712019-01-19 13:40:11 +0100614/** Calculate the intersection of two algorithm usage policies.
615 *
616 * Return 0 (which allows no operation) on incompatibility.
617 */
618static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100619 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100620 psa_algorithm_t alg1,
621 psa_algorithm_t alg2 )
622{
Gilles Peskine549ea862019-05-22 11:45:59 +0200623 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100624 if( alg1 == alg2 )
625 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100626 /* If the policies are from the same hash-and-sign family, check
627 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskinef7b41372021-09-22 16:15:05 +0200628 if( PSA_ALG_IS_SIGN_HASH( alg1 ) &&
629 PSA_ALG_IS_SIGN_HASH( alg2 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100630 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100631 {
Steven Cooreman03488022021-02-18 12:07:20 +0100632 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
633 return( alg2 );
634 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
635 return( alg1 );
636 }
637 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100638 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100639 * restrictive tag length. */
640 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
641 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
642 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
643 {
644 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
645 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100646 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100647
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100648 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100649 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
650 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100651 {
Steven Cooremancd640932021-03-08 12:00:27 +0100652 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
653 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100654 }
655 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100656 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100657 ( alg1_len <= alg2_len ) )
658 {
659 return( alg2 );
660 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100661 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100662 ( alg2_len <= alg1_len ) )
663 {
664 return( alg1 );
665 }
666 }
667 /* If the policies are from the same MAC family, check whether one
668 * of them is a minimum-MAC-length policy. Calculate the most
669 * restrictive tag length. */
670 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
671 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
672 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
673 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100674 /* Validate the combination of key type and algorithm. Since the base
675 * algorithm of alg1 and alg2 are the same, we only need this once. */
676 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100677 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100678
Steven Cooreman31a876d2021-03-03 20:47:40 +0100679 /* Get the (exact or at-least) output lengths for both sides of the
680 * requested intersection. None of the currently supported algorithms
681 * have an output length dependent on the actual key size, so setting it
682 * to a bogus value of 0 is currently OK.
683 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100684 * Note that for at-least-this-length wildcard algorithms, the output
685 * length is set to the shortest allowed length, which allows us to
686 * calculate the most restrictive tag length for the intersection. */
687 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
688 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100689 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100690
Steven Cooremana1d83222021-02-25 10:20:29 +0100691 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100692 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
693 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100694 {
Steven Cooremancd640932021-03-08 12:00:27 +0100695 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100696 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100697
698 /* If only one is an at-least-this-length policy, the intersection would
699 * be the other (fixed-length) policy as long as said fixed length is
700 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100701 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100702 {
Steven Cooremancd640932021-03-08 12:00:27 +0100703 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100704 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100705 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100706 {
Steven Cooremancd640932021-03-08 12:00:27 +0100707 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100708 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100709
Steven Cooremancd640932021-03-08 12:00:27 +0100710 /* If none of them are wildcards, check whether they define the same tag
711 * length. This is still possible here when one is default-length and
712 * the other specific-length. Ensure to always return the
713 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100714 if( alg1_len == alg2_len )
715 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100716 }
717 /* If the policies are incompatible, allow nothing. */
718 return( 0 );
719}
720
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100721static int psa_key_algorithm_permits( psa_key_type_t key_type,
722 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200723 psa_algorithm_t requested_alg )
724{
Gilles Peskine549ea862019-05-22 11:45:59 +0200725 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200726 if( requested_alg == policy_alg )
727 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100728 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
729 * and requested_alg is the same hash-and-sign family with any hash,
730 * then requested_alg is compliant with policy_alg. */
Gilles Peskinef7b41372021-09-22 16:15:05 +0200731 if( PSA_ALG_IS_SIGN_HASH( requested_alg ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100732 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200733 {
Steven Cooreman03488022021-02-18 12:07:20 +0100734 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
735 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
736 }
737 /* If policy_alg is a wildcard AEAD algorithm of the same base as
738 * the requested algorithm, check the requested tag length to be
739 * equal-length or longer than the wildcard-specified length. */
740 if( PSA_ALG_IS_AEAD( policy_alg ) &&
741 PSA_ALG_IS_AEAD( requested_alg ) &&
742 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
743 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100744 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100745 {
746 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
747 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
748 }
Steven Cooremancd640932021-03-08 12:00:27 +0100749 /* If policy_alg is a MAC algorithm of the same base as the requested
750 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100751 if( PSA_ALG_IS_MAC( policy_alg ) &&
752 PSA_ALG_IS_MAC( requested_alg ) &&
753 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100754 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100755 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100756 /* Validate the combination of key type and algorithm. Since the policy
757 * and requested algorithms are the same, we only need this once. */
758 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100759 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100760
Steven Cooreman31a876d2021-03-03 20:47:40 +0100761 /* Get both the requested output length for the algorithm which is to be
762 * verified, and the default output length for the base algorithm.
763 * Note that none of the currently supported algorithms have an output
764 * length dependent on actual key size, so setting it to a bogus value
765 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100766 size_t requested_output_length = PSA_MAC_LENGTH(
767 key_type, 0, requested_alg );
768 size_t default_output_length = PSA_MAC_LENGTH(
769 key_type, 0,
770 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100771
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100772 /* If the policy is default-length, only allow an algorithm with
773 * a declared exact-length matching the default. */
774 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100775 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100776
777 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100778 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100779 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
780 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
781 {
782 return( 1 );
783 }
784
Steven Cooremancd640932021-03-08 12:00:27 +0100785 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
786 * check for the requested MAC length to be equal to or longer than the
787 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100788 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
789 {
790 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100791 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100792 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200793 }
Steven Cooremance48e852020-10-05 16:02:45 +0200794 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200795 * a key derivation with that key agreement should also be allowed. This
796 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200797 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
798 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
799 {
800 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
801 policy_alg );
802 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100803 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200804 return( 0 );
805}
806
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100807/** Test whether a policy permits an algorithm.
808 *
809 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100810 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100811 * \note This function requires providing the key type for which the policy is
812 * being validated, since some algorithm policy definitions (e.g. MAC)
813 * have different properties depending on what kind of cipher it is
814 * combined with.
815 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100816 * \retval PSA_SUCCESS When \p alg is a specific algorithm
817 * allowed by the \p policy.
818 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
819 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
820 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100821 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100822static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100823 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100824 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100825{
Steven Cooremand788fab2021-02-25 11:29:17 +0100826 /* '0' is not a valid algorithm */
827 if( alg == 0 )
828 return( PSA_ERROR_INVALID_ARGUMENT );
829
Steven Cooreman7e39f052021-02-23 14:18:32 +0100830 /* A requested algorithm cannot be a wildcard. */
831 if( PSA_ALG_IS_WILDCARD( alg ) )
832 return( PSA_ERROR_INVALID_ARGUMENT );
833
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100834 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
835 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100836 return( PSA_SUCCESS );
837 else
838 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100839}
840
Gilles Peskinef603c712019-01-19 13:40:11 +0100841/** Restrict a key policy based on a constraint.
842 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100843 * \note This function requires providing the key type for which the policy is
844 * being restricted, since some algorithm policy definitions (e.g. MAC)
845 * have different properties depending on what kind of cipher it is
846 * combined with.
847 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100848 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100849 * \param[in,out] policy The policy to restrict.
850 * \param[in] constraint The policy constraint to apply.
851 *
852 * \retval #PSA_SUCCESS
853 * \c *policy contains the intersection of the original value of
854 * \c *policy and \c *constraint.
855 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100856 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100857 * \c *policy is unchanged.
858 */
859static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100860 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100861 psa_key_policy_t *policy,
862 const psa_key_policy_t *constraint )
863{
864 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100865 psa_key_policy_algorithm_intersection( key_type, policy->alg,
866 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200867 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100868 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
869 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100870 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
871 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200872 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
873 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100874 policy->usage &= constraint->usage;
875 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200876 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100877 return( PSA_SUCCESS );
878}
879
Przemek Stekiel348410f2022-11-15 22:22:07 +0100880psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +0100881 mbedtls_svc_key_id_t key,
882 psa_key_slot_t **p_slot,
883 psa_key_usage_t usage,
884 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100885{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200886 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
887 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100888
Ronald Cron5c522922020-11-14 16:35:34 +0100889 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100890 if( status != PSA_SUCCESS )
891 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200892 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100893
894 /* Enforce that usage policy for the key slot contains all the flags
895 * required by the usage parameter. There is one exception: public
896 * keys can always be exported, so we treat public key objects as
897 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200898 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100899 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200900
Gilles Peskine8e338702019-07-30 20:06:31 +0200901 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100902 {
903 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200904 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100905 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100906
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800907 /* Enforce that the usage policy permits the requested algorithm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100908 if( alg != 0 )
909 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100910 status = psa_key_policy_permits( &slot->attr.policy,
911 slot->attr.type,
912 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +0100913 if( status != PSA_SUCCESS )
914 goto error;
915 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100916
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200918
919error:
920 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100921 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200922
923 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100924}
Darryl Green940d72c2018-07-13 13:18:51 +0100925
Ronald Cron5c522922020-11-14 16:35:34 +0100926/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200927 *
928 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200929 * available, as opposed to a key in a secure element and/or to be used
930 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200931 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200932 * This is a temporary function that may be used instead of
933 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
934 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200935 *
Ronald Cron5c522922020-11-14 16:35:34 +0100936 * On success, the returned key slot is locked. It is the responsibility of the
937 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200938 */
Ronald Cron5c522922020-11-14 16:35:34 +0100939static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
940 mbedtls_svc_key_id_t key,
941 psa_key_slot_t **p_slot,
942 psa_key_usage_t usage,
943 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200944{
Ronald Cron5c522922020-11-14 16:35:34 +0100945 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
946 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200947 if( status != PSA_SUCCESS )
948 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200949
Ronald Cronddae0f52021-08-24 15:39:44 +0200950 if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200951 {
Ronald Cron5c522922020-11-14 16:35:34 +0100952 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200953 *p_slot = NULL;
954 return( PSA_ERROR_NOT_SUPPORTED );
955 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200956
Gilles Peskine28f8f302019-07-24 13:30:31 +0200957 return( PSA_SUCCESS );
958}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200959
Steven Cooreman7ddee7f2021-04-07 18:08:30 +0200960psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000961{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100962 /* Data pointer will always be either a valid pointer or NULL in an
963 * initialized slot, so we can just free it. */
964 if( slot->key.data != NULL )
965 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
966
967 mbedtls_free( slot->key.data );
968 slot->key.data = NULL;
969 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000970
971 return( PSA_SUCCESS );
972}
973
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100974/** Completely wipe a slot in memory, including its policy.
975 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100976psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100977{
978 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +0100979
TRodziewicz18cddc02021-07-13 12:19:15 +0200980 /*
981 * As the return error code may not be handled in case of multiple errors,
TRodziewiczc9890e92021-07-14 10:16:26 +0200982 * do our best to report an unexpected lock counter. Assert with
983 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
984 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
985 * function is called as part of the execution of a test suite, the
986 * execution of the test suite is stopped in error if the assertion fails.
TRodziewicz18cddc02021-07-13 12:19:15 +0200987 */
Ronald Cron5c522922020-11-14 16:35:34 +0100988 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +0100989 {
TRodziewicz7871c2e2021-07-07 17:29:43 +0200990 MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +0100991 status = PSA_ERROR_CORRUPTION_DETECTED;
992 }
993
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200994 /* Multipart operations may still be using the key. This is safe
995 * because all multipart operation objects are independent from
996 * the key slot: if they need to access the key after the setup
997 * phase, they have a copy of the key. Note that this means that
998 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100999 /* At this point, key material and other type-specific content has
1000 * been wiped. Clear remaining metadata. We can call memset and not
1001 * zeroize because the metadata is not particularly sensitive. */
1002 memset( slot, 0, sizeof( *slot ) );
1003 return( status );
1004}
1005
Ronald Croncf56a0a2020-08-04 09:51:30 +02001006psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001008 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001009 psa_status_t status; /* status of the last operation */
1010 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001011#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1012 psa_se_drv_table_entry_t *driver;
1013#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014
Ronald Croncf56a0a2020-08-04 09:51:30 +02001015 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001016 return( PSA_SUCCESS );
1017
Ronald Cronf2911112020-10-29 17:51:10 +01001018 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001019 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001020 * key, this will load the key description from persistent memory if not
1021 * done yet. We cannot avoid this loading as without it we don't know if
1022 * the key is operated by an SE or not and this information is needed by
1023 * the current implementation.
1024 */
Ronald Cron5c522922020-11-14 16:35:34 +01001025 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001026 if( status != PSA_SUCCESS )
1027 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001028
Ronald Cronf2911112020-10-29 17:51:10 +01001029 /*
1030 * If the key slot containing the key description is under access by the
1031 * library (apart from the present access), the key cannot be destroyed
1032 * yet. For the time being, just return in error. Eventually (to be
1033 * implemented), the key should be destroyed when all accesses have
1034 * stopped.
1035 */
Ronald Cron5c522922020-11-14 16:35:34 +01001036 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001037 {
Ronald Cron5c522922020-11-14 16:35:34 +01001038 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001039 return( PSA_ERROR_GENERIC_ERROR );
1040 }
1041
Gilles Peskine6687cd02021-04-21 22:32:05 +02001042 if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) )
1043 {
1044 /* Refuse the destruction of a read-only key (which may or may not work
1045 * if we attempt it, depending on whether the key is merely read-only
1046 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001047 * Just do the best we can, which is to wipe the copy in memory
1048 * (done in this function's cleanup code). */
1049 overall_status = PSA_ERROR_NOT_PERMITTED;
1050 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001051 }
1052
Gilles Peskine354f7672019-07-12 23:46:38 +02001053#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001054 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001055 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001056 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001057 /* For a key in a secure element, we need to do three things:
1058 * remove the key file in internal storage, destroy the
1059 * key inside the secure element, and update the driver's
1060 * persistent data. Start a transaction that will encompass these
1061 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001062 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001063 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001064 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001065 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001066 status = psa_crypto_save_transaction( );
1067 if( status != PSA_SUCCESS )
1068 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001069 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001070 /* We should still try to destroy the key in the secure
1071 * element and the key metadata in storage. This is especially
1072 * important if the error is that the storage is full.
1073 * But how to do it exactly without risking an inconsistent
1074 * state after a reset?
1075 * https://github.com/ARMmbed/mbed-crypto/issues/215
1076 */
1077 overall_status = status;
1078 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001079 }
1080
Ronald Cronea0f8a62020-11-25 17:52:23 +01001081 status = psa_destroy_se_key( driver,
1082 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001083 if( overall_status == PSA_SUCCESS )
1084 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001085 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001086#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1087
Darryl Greend49a4992018-06-18 17:27:26 +01001088#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001089 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001090 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001091 status = psa_destroy_persistent_key( slot->attr.id );
1092 if( overall_status == PSA_SUCCESS )
1093 overall_status = status;
1094
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001095 /* TODO: other slots may have a copy of the same key. We should
1096 * invalidate them.
1097 * https://github.com/ARMmbed/mbed-crypto/issues/214
1098 */
Darryl Greend49a4992018-06-18 17:27:26 +01001099 }
1100#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001101
Gilles Peskinefc762652019-07-22 19:30:34 +02001102#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1103 if( driver != NULL )
1104 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001105 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001106 if( overall_status == PSA_SUCCESS )
1107 overall_status = status;
1108 status = psa_crypto_stop_transaction( );
1109 if( overall_status == PSA_SUCCESS )
1110 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001111 }
1112#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1113
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001114exit:
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001115 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001116 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001117 if( status != PSA_SUCCESS )
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001118 overall_status = status;
1119 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001120}
1121
John Durkop07cc04a2020-11-16 22:08:34 -08001122#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001123 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001124static psa_status_t psa_get_rsa_public_exponent(
1125 const mbedtls_rsa_context *rsa,
1126 psa_key_attributes_t *attributes )
1127{
1128 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001130 uint8_t *buffer = NULL;
1131 size_t buflen;
1132 mbedtls_mpi_init( &mpi );
1133
1134 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1135 if( ret != 0 )
1136 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001137 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1138 {
1139 /* It's the default value, which is reported as an empty string,
1140 * so there's nothing to do. */
1141 goto exit;
1142 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001143
1144 buflen = mbedtls_mpi_size( &mpi );
1145 buffer = mbedtls_calloc( 1, buflen );
1146 if( buffer == NULL )
1147 {
1148 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1149 goto exit;
1150 }
1151 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1152 if( ret != 0 )
1153 goto exit;
1154 attributes->domain_parameters = buffer;
1155 attributes->domain_parameters_size = buflen;
1156
1157exit:
1158 mbedtls_mpi_free( &mpi );
1159 if( ret != 0 )
1160 mbedtls_free( buffer );
1161 return( mbedtls_to_psa_error( ret ) );
1162}
John Durkop07cc04a2020-11-16 22:08:34 -08001163#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001164 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001165
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001166/** Retrieve all the publicly-accessible attributes of a key.
1167 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001168psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001169 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001170{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001172 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001173 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001174
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001175 psa_reset_key_attributes( attributes );
1176
Ronald Cron5c522922020-11-14 16:35:34 +01001177 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001178 if( status != PSA_SUCCESS )
1179 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001180
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001181 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001182 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1183 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1184
1185#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron512ad812021-08-24 15:50:05 +02001186 if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001187 psa_set_key_slot_number( attributes,
1188 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001189#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001190
Gilles Peskine8e338702019-07-30 20:06:31 +02001191 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001192 {
John Durkop07cc04a2020-11-16 22:08:34 -08001193#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001194 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001195 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001196 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001197 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001198 * is not yet implemented.
1199 * https://github.com/ARMmbed/mbed-crypto/issues/216
1200 */
Ronald Cron9b8b69c2021-08-24 16:00:51 +02001201 if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02001202 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001203 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001204
Ronald Cron90857082020-11-25 14:58:33 +01001205 status = mbedtls_psa_rsa_load_representation(
1206 slot->attr.type,
1207 slot->key.data,
1208 slot->key.bytes,
1209 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001210 if( status != PSA_SUCCESS )
1211 break;
1212
Steven Cooremana2371e52020-07-28 14:30:39 +02001213 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001214 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001215 mbedtls_rsa_free( rsa );
1216 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001217 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001218 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001219#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001220 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001221 default:
1222 /* Nothing else to do. */
1223 break;
1224 }
1225
1226 if( status != PSA_SUCCESS )
1227 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001228
Ronald Cron5c522922020-11-14 16:35:34 +01001229 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001230
Ronald Cron5c522922020-11-14 16:35:34 +01001231 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001232}
1233
Gilles Peskinec8000c02019-08-02 20:15:51 +02001234#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1235psa_status_t psa_get_key_slot_number(
1236 const psa_key_attributes_t *attributes,
1237 psa_key_slot_number_t *slot_number )
1238{
1239 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1240 {
1241 *slot_number = attributes->slot_number;
1242 return( PSA_SUCCESS );
1243 }
1244 else
1245 return( PSA_ERROR_INVALID_ARGUMENT );
1246}
1247#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1248
Ronald Crond18b5f82020-11-25 16:46:05 +01001249static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1250 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001251 uint8_t *data,
1252 size_t data_size,
1253 size_t *data_length )
1254{
Ronald Crond18b5f82020-11-25 16:46:05 +01001255 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001256 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001257 memcpy( data, key_buffer, key_buffer_size );
1258 memset( data + key_buffer_size, 0,
1259 data_size - key_buffer_size );
1260 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001261 return( PSA_SUCCESS );
1262}
1263
Ronald Cron7285cda2020-11-26 14:46:37 +01001264psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001265 const psa_key_attributes_t *attributes,
1266 const uint8_t *key_buffer, size_t key_buffer_size,
1267 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001268{
Ronald Crond18b5f82020-11-25 16:46:05 +01001269 psa_key_type_t type = attributes->core.type;
1270
Ronald Crond18b5f82020-11-25 16:46:05 +01001271 if( key_type_is_raw_bytes( type ) ||
1272 PSA_KEY_TYPE_IS_RSA( type ) ||
1273 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001274 {
1275 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001276 key_buffer, key_buffer_size,
1277 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001278 }
1279 else
1280 {
1281 /* This shouldn't happen in the reference implementation, but
1282 it is valid for a special-purpose implementation to omit
1283 support for exporting certain key types. */
1284 return( PSA_ERROR_NOT_SUPPORTED );
1285 }
1286}
1287
1288psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1289 uint8_t *data,
1290 size_t data_size,
1291 size_t *data_length )
1292{
1293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1294 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1295 psa_key_slot_t *slot;
1296
Ronald Crone907e552021-01-18 13:22:38 +01001297 /* Reject a zero-length output buffer now, since this can never be a
1298 * valid key representation. This way we know that data must be a valid
1299 * pointer and we can do things like memset(data, ..., data_size). */
1300 if( data_size == 0 )
1301 return( PSA_ERROR_BUFFER_TOO_SMALL );
1302
Ronald Cron9486f9d2020-11-26 13:36:23 +01001303 /* Set the key to empty now, so that even when there are errors, we always
1304 * set data_length to a value between 0 and data_size. On error, setting
1305 * the key to empty is a good choice because an empty key representation is
1306 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001307 *data_length = 0;
1308
Ronald Cron9486f9d2020-11-26 13:36:23 +01001309 /* Export requires the EXPORT flag. There is an exception for public keys,
1310 * which don't require any flag, but
1311 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1312 */
1313 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1314 PSA_KEY_USAGE_EXPORT, 0 );
1315 if( status != PSA_SUCCESS )
1316 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001317
Ronald Crond18b5f82020-11-25 16:46:05 +01001318 psa_key_attributes_t attributes = {
1319 .core = slot->attr
1320 };
Ronald Cron67227982020-11-26 15:16:05 +01001321 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001322 slot->key.data, slot->key.bytes,
1323 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001324
Ronald Cron9486f9d2020-11-26 13:36:23 +01001325 unlock_status = psa_unlock_key_slot( slot );
1326
1327 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1328}
1329
Ronald Cron7285cda2020-11-26 14:46:37 +01001330psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001331 const psa_key_attributes_t *attributes,
1332 const uint8_t *key_buffer,
1333 size_t key_buffer_size,
1334 uint8_t *data,
1335 size_t data_size,
1336 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001337{
Ronald Crond18b5f82020-11-25 16:46:05 +01001338 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001339
Ronald Crond18b5f82020-11-25 16:46:05 +01001340 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001341 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001342 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001343 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001344 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001345 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001346 key_buffer, key_buffer_size,
1347 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001348 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001349
Ronald Crond18b5f82020-11-25 16:46:05 +01001350 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001351 {
John Durkop0e005192020-10-31 22:06:54 -07001352#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1353 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001354 return( mbedtls_psa_rsa_export_public_key( attributes,
1355 key_buffer,
1356 key_buffer_size,
1357 data,
1358 data_size,
1359 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001360#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001361 /* We don't know how to convert a private RSA key to public. */
1362 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001363#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1364 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001365 }
1366 else
Moran Pekera998bc62018-04-16 18:16:20 +03001367 {
John Durkop6ba40d12020-11-10 08:50:04 -08001368#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1369 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001370 return( mbedtls_psa_ecp_export_public_key( attributes,
1371 key_buffer,
1372 key_buffer_size,
1373 data,
1374 data_size,
1375 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001376#else
1377 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001378 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001379#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1380 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001381 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001382 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001383 else
1384 {
1385 /* This shouldn't happen in the reference implementation, but
1386 it is valid for a special-purpose implementation to omit
1387 support for exporting certain key types. */
1388 return( PSA_ERROR_NOT_SUPPORTED );
1389 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001390}
Ronald Crond18b5f82020-11-25 16:46:05 +01001391
Ronald Croncf56a0a2020-08-04 09:51:30 +02001392psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001393 uint8_t *data,
1394 size_t data_size,
1395 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001396{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001397 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001398 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001399 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001400
Ronald Crone907e552021-01-18 13:22:38 +01001401 /* Reject a zero-length output buffer now, since this can never be a
1402 * valid key representation. This way we know that data must be a valid
1403 * pointer and we can do things like memset(data, ..., data_size). */
1404 if( data_size == 0 )
1405 return( PSA_ERROR_BUFFER_TOO_SMALL );
1406
Darryl Greendd8fb772018-11-07 16:00:44 +00001407 /* Set the key to empty now, so that even when there are errors, we always
1408 * set data_length to a value between 0 and data_size. On error, setting
1409 * the key to empty is a good choice because an empty key representation is
1410 * unlikely to be accepted anywhere. */
1411 *data_length = 0;
1412
1413 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001414 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001415 if( status != PSA_SUCCESS )
1416 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001417
Ronald Cron9486f9d2020-11-26 13:36:23 +01001418 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1419 {
1420 status = PSA_ERROR_INVALID_ARGUMENT;
1421 goto exit;
1422 }
1423
Ronald Crond18b5f82020-11-25 16:46:05 +01001424 psa_key_attributes_t attributes = {
1425 .core = slot->attr
1426 };
Ronald Cron67227982020-11-26 15:16:05 +01001427 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001428 &attributes, slot->key.data, slot->key.bytes,
1429 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001430
1431exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001432 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001433
Ronald Cron5c522922020-11-14 16:35:34 +01001434 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001435}
1436
Gilles Peskine91e8c332019-08-02 19:19:39 +02001437#if defined(static_assert)
1438static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1439 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001440static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001441 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001442static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001443 "One or more key attribute flag is listed as both internal-only and external-only" );
1444#endif
1445
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001446/** Validate that a key policy is internally well-formed.
1447 *
1448 * This function only rejects invalid policies. It does not validate the
1449 * consistency of the policy with respect to other attributes of the key
1450 * such as the key type.
1451 */
1452static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001453{
1454 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001455 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001456 PSA_KEY_USAGE_ENCRYPT |
1457 PSA_KEY_USAGE_DECRYPT |
gabor-mezei-arm4a210192021-04-14 21:14:28 +02001458 PSA_KEY_USAGE_SIGN_MESSAGE |
1459 PSA_KEY_USAGE_VERIFY_MESSAGE |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001460 PSA_KEY_USAGE_SIGN_HASH |
1461 PSA_KEY_USAGE_VERIFY_HASH |
Manuel Pégourié-Gonnard805251b2021-05-04 09:49:59 +02001462 PSA_KEY_USAGE_VERIFY_DERIVATION |
Gilles Peskine4747d192019-04-17 15:05:45 +02001463 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1464 return( PSA_ERROR_INVALID_ARGUMENT );
1465
Gilles Peskine4747d192019-04-17 15:05:45 +02001466 return( PSA_SUCCESS );
1467}
1468
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001469/** Validate the internal consistency of key attributes.
1470 *
1471 * This function only rejects invalid attribute values. If does not
1472 * validate the consistency of the attributes with any key data that may
1473 * be involved in the creation of the key.
1474 *
1475 * Call this function early in the key creation process.
1476 *
1477 * \param[in] attributes Key attributes for the new key.
1478 * \param[out] p_drv On any return, the driver for the key, if any.
1479 * NULL for a transparent key.
1480 *
1481 */
1482static psa_status_t psa_validate_key_attributes(
1483 const psa_key_attributes_t *attributes,
1484 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001485{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001486 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001487 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001488 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001489
Ronald Cron54b90082020-10-29 15:26:43 +01001490 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001491 if( status != PSA_SUCCESS )
1492 return( status );
1493
Ronald Crond2ed4812020-07-17 16:11:30 +02001494 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001495 if( status != PSA_SUCCESS )
1496 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001497
Ronald Cron65f38a32020-10-23 17:11:13 +02001498 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1499 {
1500 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1501 return( PSA_ERROR_INVALID_ARGUMENT );
1502 }
1503 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001504 {
Ronald Cron77e412c2021-03-31 17:36:31 +02001505 if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) )
1506 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Crond2ed4812020-07-17 16:11:30 +02001507 }
1508
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001509 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001510 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001511 return( status );
1512
1513 /* Refuse to create overly large keys.
1514 * Note that this doesn't trigger on import if the attributes don't
1515 * explicitly specify a size (so psa_get_key_bits returns 0), so
1516 * psa_import_key() needs its own checks. */
1517 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1518 return( PSA_ERROR_NOT_SUPPORTED );
1519
Gilles Peskine91e8c332019-08-02 19:19:39 +02001520 /* Reject invalid flags. These should not be reachable through the API. */
1521 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1522 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1523 return( PSA_ERROR_INVALID_ARGUMENT );
1524
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001525 return( PSA_SUCCESS );
1526}
1527
Gilles Peskine4747d192019-04-17 15:05:45 +02001528/** Prepare a key slot to receive key material.
1529 *
1530 * This function allocates a key slot and sets its metadata.
1531 *
1532 * If this function fails, call psa_fail_key_creation().
1533 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001534 * This function is intended to be used as follows:
1535 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001536 * it with the specified attributes, and in case of a volatile key assign it
1537 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001538 * -# Populate the slot with the key material.
1539 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1540 * In case of failure at any step, stop the sequence and call
1541 * psa_fail_key_creation().
1542 *
Ronald Cron5c522922020-11-14 16:35:34 +01001543 * On success, the key slot is locked. It is the responsibility of the caller
1544 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001545 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001546 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001548 * \param[out] p_slot On success, a pointer to the prepared slot.
1549 * \param[out] p_drv On any return, the driver for the key, if any.
1550 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001551 *
1552 * \retval #PSA_SUCCESS
1553 * The key slot is ready to receive key material.
1554 * \return If this function fails, the key slot is an invalid state.
1555 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001556 */
1557static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001558 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001559 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001560 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001561 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001562{
1563 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001564 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001565 psa_key_slot_t *slot;
1566
Gilles Peskinedf179142019-07-15 22:02:14 +02001567 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001568 *p_drv = NULL;
1569
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001570 status = psa_validate_key_attributes( attributes, p_drv );
1571 if( status != PSA_SUCCESS )
1572 return( status );
1573
Ronald Cronc4d1b512020-07-31 11:26:37 +02001574 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001575 if( status != PSA_SUCCESS )
1576 return( status );
1577 slot = *p_slot;
1578
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001579 /* We're storing the declared bit-size of the key. It's up to each
1580 * creation mechanism to verify that this information is correct.
1581 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001582 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001583 * is optional (import, copy). In case of a volatile key, assign it the
1584 * volatile key identifier associated to the slot returned to contain its
1585 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001586
1587 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001588 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1589 {
1590#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1591 slot->attr.id = volatile_key_id;
1592#else
1593 slot->attr.id.key_id = volatile_key_id;
1594#endif
1595 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001596
Gilles Peskine91e8c332019-08-02 19:19:39 +02001597 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001598 * external-only flags, query `attributes`. Thanks to the check
1599 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001600 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001601 * may have set. */
1602 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001603
Gilles Peskinecbaff462019-07-12 23:46:04 +02001604#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001605 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001606 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001607 * create the key file in internal storage, create the
1608 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001609 * persistent data. This is done by starting a transaction that will
1610 * encompass these three actions.
1611 * For registering a volatile key, we just need to find an appropriate
1612 * slot number inside the SE. Since the key is designated volatile, creating
1613 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001614 /* The first thing to do is to find a slot number for the new key.
1615 * We save the slot number in persistent storage as part of the
1616 * transaction data. It will be needed to recover if the power
1617 * fails during the key creation process, to clean up on the secure
1618 * element side after restarting. Obtaining a slot number from the
1619 * secure element driver updates its persistent state, but we do not yet
1620 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001621 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001622 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001623 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001624 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001625 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001626 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001627 if( status != PSA_SUCCESS )
1628 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001629
1630 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001631 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001632 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1633 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001634 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001635 psa_crypto_transaction.key.id = slot->attr.id;
1636 status = psa_crypto_save_transaction( );
1637 if( status != PSA_SUCCESS )
1638 {
1639 (void) psa_crypto_stop_transaction( );
1640 return( status );
1641 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001642 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001643
1644 status = psa_copy_key_material_into_slot(
1645 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001646 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001647
1648 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1649 {
1650 /* Key registration only makes sense with a secure element. */
1651 return( PSA_ERROR_INVALID_ARGUMENT );
1652 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001653#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1654
Ronald Cronc4d1b512020-07-31 11:26:37 +02001655 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001656}
Gilles Peskine4747d192019-04-17 15:05:45 +02001657
1658/** Finalize the creation of a key once its key material has been set.
1659 *
1660 * This entails writing the key to persistent storage.
1661 *
1662 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001663 * See the documentation of psa_start_key_creation() for the intended use
1664 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001665 *
Ronald Cron5c522922020-11-14 16:35:34 +01001666 * If the finalization succeeds, the function unlocks the key slot (it was
1667 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1668 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001669 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001670 * \param[in,out] slot Pointer to the slot with key material.
1671 * \param[in] driver The secure element driver for the key,
1672 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001673 * \param[out] key On success, identifier of the key. Note that the
1674 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001675 *
1676 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001677 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001678 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1679 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1680 * \retval #PSA_ERROR_ALREADY_EXISTS
1681 * \retval #PSA_ERROR_DATA_INVALID
1682 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001683 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001684 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001685 * \return If this function fails, the key slot is an invalid state.
1686 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001687 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001688static psa_status_t psa_finish_key_creation(
1689 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001690 psa_se_drv_table_entry_t *driver,
1691 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001692{
1693 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001694 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001695 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001696
1697#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001698 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001699 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001700#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1701 if( driver != NULL )
1702 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001703 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001704 psa_key_slot_number_t slot_number =
1705 psa_key_slot_get_slot_number( slot ) ;
1706
Gilles Peskineb46bef22019-07-30 21:32:04 +02001707#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001708 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001709 sizeof( data.slot_number ),
1710 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001711#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001712 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001713 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001714 (uint8_t*) &data,
1715 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001716 }
1717 else
1718#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1719 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001720 /* Key material is saved in export representation in the slot, so
1721 * just pass the slot buffer for storage. */
1722 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001723 slot->key.data,
1724 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001725 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001727#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1728
Gilles Peskinecbaff462019-07-12 23:46:04 +02001729#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001730 /* Finish the transaction for a key creation. This does not
1731 * happen when registering an existing key. Detect this case
1732 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001733 * creation of a persistent key in a secure element requires a transaction,
1734 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001735 if( driver != NULL &&
1736 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001737 {
1738 status = psa_save_se_persistent_data( driver );
1739 if( status != PSA_SUCCESS )
1740 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001741 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001742 return( status );
1743 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001744 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001745 }
1746#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1747
Ronald Cron50972942020-11-14 11:28:25 +01001748 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001749 {
1750 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001751 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001752 if( status != PSA_SUCCESS )
1753 *key = MBEDTLS_SVC_KEY_ID_INIT;
1754 }
Ronald Cron50972942020-11-14 11:28:25 +01001755
Gilles Peskine4747d192019-04-17 15:05:45 +02001756 return( status );
1757}
1758
1759/** Abort the creation of a key.
1760 *
1761 * You may call this function after calling psa_start_key_creation(),
1762 * or after psa_finish_key_creation() fails. In other circumstances, this
1763 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001764 * See the documentation of psa_start_key_creation() for the intended use
1765 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001766 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001767 * \param[in,out] slot Pointer to the slot with key material.
1768 * \param[in] driver The secure element driver for the key,
1769 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001771static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001772 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001773{
Gilles Peskine011e4282019-06-26 18:34:38 +02001774 (void) driver;
1775
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 if( slot == NULL )
1777 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001778
1779#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001780 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001781 * element, and the failure happened later (when saving metadata
1782 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001783 * element.
1784 * https://github.com/ARMmbed/mbed-crypto/issues/217
1785 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001786
Gilles Peskined7729582019-08-05 15:55:54 +02001787 /* Abort the ongoing transaction if any (there may not be one if
1788 * the creation process failed before starting one, or if the
1789 * key creation is a registration of a key in a secure element).
1790 * Earlier functions must already have done what it takes to undo any
1791 * partial creation. All that's left is to update the transaction data
1792 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001793 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001794#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1795
Gilles Peskine4747d192019-04-17 15:05:45 +02001796 psa_wipe_key_slot( slot );
1797}
1798
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001799/** Validate optional attributes during key creation.
1800 *
1801 * Some key attributes are optional during key creation. If they are
1802 * specified in the attributes structure, check that they are consistent
1803 * with the data in the slot.
1804 *
1805 * This function should be called near the end of key creation, after
1806 * the slot in memory is fully populated but before saving persistent data.
1807 */
1808static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001809 const psa_key_slot_t *slot,
1810 const psa_key_attributes_t *attributes )
1811{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001812 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001813 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001814 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001815 return( PSA_ERROR_INVALID_ARGUMENT );
1816 }
1817
1818 if( attributes->domain_parameters_size != 0 )
1819 {
John Durkop0e005192020-10-31 22:06:54 -07001820#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1821 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001822 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001823 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001824 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001825 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001827
Ronald Cron90857082020-11-25 14:58:33 +01001828 psa_status_t status = mbedtls_psa_rsa_load_representation(
1829 slot->attr.type,
1830 slot->key.data,
1831 slot->key.bytes,
1832 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001833 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001834 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001835
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001836 mbedtls_mpi_init( &actual );
1837 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001838 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001839 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001840 mbedtls_rsa_free( rsa );
1841 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001842 if( ret != 0 )
1843 goto rsa_exit;
1844 ret = mbedtls_mpi_read_binary( &required,
1845 attributes->domain_parameters,
1846 attributes->domain_parameters_size );
1847 if( ret != 0 )
1848 goto rsa_exit;
1849 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1850 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1851 rsa_exit:
1852 mbedtls_mpi_free( &actual );
1853 mbedtls_mpi_free( &required );
1854 if( ret != 0)
1855 return( mbedtls_to_psa_error( ret ) );
1856 }
1857 else
John Durkop9814fa22020-11-04 12:28:15 -08001858#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1859 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001860 {
1861 return( PSA_ERROR_INVALID_ARGUMENT );
1862 }
1863 }
1864
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001865 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001866 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001867 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868 return( PSA_ERROR_INVALID_ARGUMENT );
1869 }
1870
1871 return( PSA_SUCCESS );
1872}
1873
Gilles Peskine4747d192019-04-17 15:05:45 +02001874psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001875 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001876 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001877 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001878{
1879 psa_status_t status;
1880 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001881 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001882 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301883 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001884
Ronald Cron81709fc2020-11-14 12:10:32 +01001885 *key = MBEDTLS_SVC_KEY_ID_INIT;
1886
Gilles Peskine0f84d622019-09-12 19:03:13 +02001887 /* Reject zero-length symmetric keys (including raw data key objects).
1888 * This also rejects any key which might be encoded as an empty string,
1889 * which is never valid. */
1890 if( data_length == 0 )
1891 return( PSA_ERROR_INVALID_ARGUMENT );
1892
Archana449608b2021-09-08 15:36:05 +05301893 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Archana6ed4bda2021-08-04 10:47:15 +05301894 if( data_length > SIZE_MAX / 8 )
1895 return( PSA_ERROR_NOT_SUPPORTED );
1896
Gilles Peskinedf179142019-07-15 22:02:14 +02001897 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001898 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899 if( status != PSA_SUCCESS )
1900 goto exit;
1901
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001902 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301903 * storage ( thus not in the case of importing a key in a secure element
1904 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1905 * buffer to hold the imported key material. */
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001906 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001907 {
Archanad8a83dc2021-06-14 10:04:16 +05301908 if( psa_key_lifetime_is_external( attributes->core.lifetime ) )
1909 {
Archana449608b2021-09-08 15:36:05 +05301910 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
1911 attributes, data, data_length, &storage_size );
Archanad8a83dc2021-06-14 10:04:16 +05301912 if( status != PSA_SUCCESS )
1913 goto exit;
1914 }
1915 status = psa_allocate_buffer_to_slot( slot, storage_size );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001916 if( status != PSA_SUCCESS )
1917 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001918 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001919
1920 bits = slot->attr.bits;
1921 status = psa_driver_wrapper_import_key( attributes,
1922 data, data_length,
1923 slot->key.data,
1924 slot->key.bytes,
1925 &slot->key.bytes, &bits );
1926 if( status != PSA_SUCCESS )
1927 goto exit;
1928
1929 if( slot->attr.bits == 0 )
1930 slot->attr.bits = (psa_key_bits_t) bits;
1931 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001932 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001933 status = PSA_ERROR_INVALID_ARGUMENT;
1934 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001935 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001936
Archana6ed4bda2021-08-04 10:47:15 +05301937 /* Enforce a size limit, and in particular ensure that the bit
1938 * size fits in its representation type.*/
1939 if( bits > PSA_MAX_KEY_BITS )
1940 {
1941 status = PSA_ERROR_NOT_SUPPORTED;
1942 goto exit;
1943 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001944 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001945 if( status != PSA_SUCCESS )
1946 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947
Ronald Cron81709fc2020-11-14 12:10:32 +01001948 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001950 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001951 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001952
Gilles Peskine4747d192019-04-17 15:05:45 +02001953 return( status );
1954}
1955
Gilles Peskined7729582019-08-05 15:55:54 +02001956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1957psa_status_t mbedtls_psa_register_se_key(
1958 const psa_key_attributes_t *attributes )
1959{
1960 psa_status_t status;
1961 psa_key_slot_t *slot = NULL;
1962 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001963 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001964
1965 /* Leaving attributes unspecified is not currently supported.
1966 * It could make sense to query the key type and size from the
1967 * secure element, but not all secure elements support this
1968 * and the driver HAL doesn't currently support it. */
1969 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1970 return( PSA_ERROR_NOT_SUPPORTED );
1971 if( psa_get_key_bits( attributes ) == 0 )
1972 return( PSA_ERROR_NOT_SUPPORTED );
1973
1974 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001975 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02001976 if( status != PSA_SUCCESS )
1977 goto exit;
1978
Ronald Cron81709fc2020-11-14 12:10:32 +01001979 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02001980
1981exit:
1982 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02001983 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001984
Gilles Peskined7729582019-08-05 15:55:54 +02001985 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001986 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02001987 return( status );
1988}
1989#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1990
Ronald Croncf56a0a2020-08-04 09:51:30 +02001991psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001992 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001993 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01001994{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001995 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001996 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01001997 psa_key_slot_t *source_slot = NULL;
1998 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001999 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002000 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302001 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002002
Ronald Cron81709fc2020-11-14 12:10:32 +01002003 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2004
Archana8a180362021-07-05 02:18:48 +05302005 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01002006 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002007 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002008 goto exit;
2009
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002010 status = psa_validate_optional_attributes( source_slot,
2011 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002012 if( status != PSA_SUCCESS )
2013 goto exit;
2014
Archana9d17bf42021-09-10 06:22:44 +05302015 /* The target key type and number of bits have been validated by
2016 * psa_validate_optional_attributes() to be either equal to zero or
2017 * equal to the ones of the source key. So it is safe to inherit
2018 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302019 * */
Archana9d17bf42021-09-10 06:22:44 +05302020 actual_attributes.core.bits = source_slot->attr.bits;
2021 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302022
2023
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002024 status = psa_restrict_key_policy( source_slot->attr.type,
2025 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002026 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002027 if( status != PSA_SUCCESS )
2028 goto exit;
2029
Ronald Cron81709fc2020-11-14 12:10:32 +01002030 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2031 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002032 if( status != PSA_SUCCESS )
2033 goto exit;
Archana8a180362021-07-05 02:18:48 +05302034 if( PSA_KEY_LIFETIME_GET_LOCATION( target_slot->attr.lifetime ) !=
2035 PSA_KEY_LIFETIME_GET_LOCATION( source_slot->attr.lifetime ) )
Gilles Peskinef603c712019-01-19 13:40:11 +01002036 {
Archana8a180362021-07-05 02:18:48 +05302037 /*
Archana9d17bf42021-09-10 06:22:44 +05302038 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302039 * the source key would need to be exported as plaintext and re-imported
2040 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302041 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302042 * appropriate API invocations from the application, if needed.
2043 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002044 status = PSA_ERROR_NOT_SUPPORTED;
2045 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002046 }
Archana8a180362021-07-05 02:18:48 +05302047 /*
2048 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302049 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302050 * - For opaque keys this translates to an invocation of the drivers'
2051 * copy_key entry point through the dispatch layer.
2052 * */
Ronald Cron6cc66312021-04-02 12:27:47 +02002053 if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) )
2054 {
Archana8a180362021-07-05 02:18:48 +05302055 status = psa_driver_wrapper_get_key_buffer_size( &actual_attributes,
Archana449608b2021-09-08 15:36:05 +05302056 &storage_size );
Archana8a180362021-07-05 02:18:48 +05302057 if( status != PSA_SUCCESS )
2058 goto exit;
Archana374fe5b2021-09-08 15:50:28 +05302059
Archana8a180362021-07-05 02:18:48 +05302060 status = psa_allocate_buffer_to_slot( target_slot, storage_size );
2061 if( status != PSA_SUCCESS )
2062 goto exit;
Archana374fe5b2021-09-08 15:50:28 +05302063
Archana8a180362021-07-05 02:18:48 +05302064 status = psa_driver_wrapper_copy_key( &actual_attributes,
2065 source_slot->key.data,
2066 source_slot->key.bytes,
2067 target_slot->key.data,
2068 target_slot->key.bytes,
2069 &target_slot->key.bytes );
2070 if( status != PSA_SUCCESS )
2071 goto exit;
Ronald Cron6cc66312021-04-02 12:27:47 +02002072 }
Archana8a180362021-07-05 02:18:48 +05302073 else
2074 {
Archana9d17bf42021-09-10 06:22:44 +05302075 status = psa_copy_key_material_into_slot( target_slot,
2076 source_slot->key.data,
2077 source_slot->key.bytes );
Archana8a180362021-07-05 02:18:48 +05302078 if( status != PSA_SUCCESS )
2079 goto exit;
2080 }
Ronald Cron81709fc2020-11-14 12:10:32 +01002081 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002082exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002083 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002084 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002085
Ronald Cron5c522922020-11-14 16:35:34 +01002086 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002087
Ronald Cron5c522922020-11-14 16:35:34 +01002088 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002089}
2090
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002091
2092
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002093/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002094/* Message digests */
2095/****************************************************************/
2096
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002097psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2098{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002099 /* Aborting a non-active operation is allowed */
2100 if( operation->id == 0 )
2101 return( PSA_SUCCESS );
2102
2103 psa_status_t status = psa_driver_wrapper_hash_abort( operation );
2104 operation->id = 0;
2105
2106 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002107}
2108
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002109psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002110 psa_algorithm_t alg )
2111{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002113
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002114 /* A context must be freshly initialized before it can be set up. */
2115 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002116 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002117 status = PSA_ERROR_BAD_STATE;
2118 goto exit;
2119 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002120
2121 if( !PSA_ALG_IS_HASH( alg ) )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002122 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002123 status = PSA_ERROR_INVALID_ARGUMENT;
2124 goto exit;
2125 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002126
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002127 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2128 * directly zeroes the int-sized dummy member of the context union. */
Steven Cooreman893232f2021-03-15 12:23:37 +01002129 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
2130
Dave Rodgman685b6a72021-06-24 11:49:14 +01002131 status = psa_driver_wrapper_hash_setup( operation, alg );
2132
2133exit:
2134 if( status != PSA_SUCCESS )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002135 psa_hash_abort( operation );
Dave Rodgman685b6a72021-06-24 11:49:14 +01002136
2137 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002138}
2139
2140psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2141 const uint8_t *input,
2142 size_t input_length )
2143{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002144 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2145
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002146 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002147 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002148 status = PSA_ERROR_BAD_STATE;
2149 goto exit;
2150 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002151
Steven Cooremanc8288352021-03-04 14:02:19 +01002152 /* Don't require hash implementations to behave correctly on a
2153 * zero-length input, which may have an invalid pointer. */
2154 if( input_length == 0 )
2155 return( PSA_SUCCESS );
2156
Dave Rodgman685b6a72021-06-24 11:49:14 +01002157 status = psa_driver_wrapper_hash_update( operation, input, input_length );
2158
2159exit:
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002160 if( status != PSA_SUCCESS )
2161 psa_hash_abort( operation );
2162
2163 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002164}
2165
2166psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2167 uint8_t *hash,
2168 size_t hash_size,
2169 size_t *hash_length )
2170{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002171 *hash_length = 0;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002172 if( operation->id == 0 )
2173 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002174
Steven Cooreman1e582352021-02-18 17:24:37 +01002175 psa_status_t status = psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002176 operation, hash, hash_size, hash_length );
Steven Cooreman0e307642021-02-18 16:18:32 +01002177 psa_hash_abort( operation );
2178 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002179}
2180
Gilles Peskine2d277862018-06-18 15:41:12 +02002181psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2182 const uint8_t *hash,
2183 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002184{
Ronald Cron69a63422021-10-18 09:47:58 +02002185 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002186 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002187 psa_status_t status = psa_hash_finish(
2188 operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01002189 actual_hash, sizeof( actual_hash ),
2190 &actual_hash_length );
Dave Rodgman685b6a72021-06-24 11:49:14 +01002191
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002192 if( status != PSA_SUCCESS )
Dave Rodgman685b6a72021-06-24 11:49:14 +01002193 goto exit;
2194
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195 if( actual_hash_length != hash_length )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002196 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002197 status = PSA_ERROR_INVALID_SIGNATURE;
2198 goto exit;
2199 }
2200
Steven Cooreman36876a02021-04-29 16:37:59 +02002201 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Dave Rodgman685b6a72021-06-24 11:49:14 +01002202 status = PSA_ERROR_INVALID_SIGNATURE;
2203
2204exit:
Gilles Peskine60aebec2021-12-13 12:33:18 +01002205 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
Dave Rodgman685b6a72021-06-24 11:49:14 +01002206 if( status != PSA_SUCCESS )
2207 psa_hash_abort(operation);
2208
2209 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002210}
2211
Gilles Peskine0a749c82019-11-28 19:33:58 +01002212psa_status_t psa_hash_compute( psa_algorithm_t alg,
2213 const uint8_t *input, size_t input_length,
2214 uint8_t *hash, size_t hash_size,
2215 size_t *hash_length )
2216{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002217 *hash_length = 0;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002218 if( !PSA_ALG_IS_HASH( alg ) )
2219 return( PSA_ERROR_INVALID_ARGUMENT );
2220
Steven Cooreman1e582352021-02-18 17:24:37 +01002221 return( psa_driver_wrapper_hash_compute( alg, input, input_length,
2222 hash, hash_size, hash_length ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002223}
2224
2225psa_status_t psa_hash_compare( psa_algorithm_t alg,
2226 const uint8_t *input, size_t input_length,
2227 const uint8_t *hash, size_t hash_length )
2228{
Ronald Cron69a63422021-10-18 09:47:58 +02002229 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002230 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002231
2232 if( !PSA_ALG_IS_HASH( alg ) )
2233 return( PSA_ERROR_INVALID_ARGUMENT );
2234
Steven Cooreman1e582352021-02-18 17:24:37 +01002235 psa_status_t status = psa_driver_wrapper_hash_compute(
2236 alg, input, input_length,
2237 actual_hash, sizeof(actual_hash),
2238 &actual_hash_length );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002239 if( status != PSA_SUCCESS )
Gilles Peskine60aebec2021-12-13 12:33:18 +01002240 goto exit;
Steven Cooreman84d670d2021-02-18 16:22:53 +01002241 if( actual_hash_length != hash_length )
Gilles Peskine60aebec2021-12-13 12:33:18 +01002242 {
2243 status = PSA_ERROR_INVALID_SIGNATURE;
2244 goto exit;
2245 }
Steven Cooreman36876a02021-04-29 16:37:59 +02002246 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Gilles Peskine60aebec2021-12-13 12:33:18 +01002247 status = PSA_ERROR_INVALID_SIGNATURE;
2248
2249exit:
2250 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
2251 return( status );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002252}
2253
Gilles Peskineeb35d782019-01-22 17:56:16 +01002254psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2255 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002256{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002257 if( source_operation->id == 0 ||
2258 target_operation->id != 0 )
2259 {
2260 return( PSA_ERROR_BAD_STATE );
2261 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002262
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002263 psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
2264 target_operation );
2265 if( status != PSA_SUCCESS )
2266 psa_hash_abort( target_operation );
2267
2268 return( status );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002269}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002270
2271
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002273/* MAC */
2274/****************************************************************/
2275
Steven Cooremane6804192021-03-19 18:28:56 +01002276psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002277{
Steven Cooremane6804192021-03-19 18:28:56 +01002278 /* Aborting a non-active operation is allowed */
2279 if( operation->id == 0 )
2280 return( PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002281
Steven Cooremane6804192021-03-19 18:28:56 +01002282 psa_status_t status = psa_driver_wrapper_mac_abort( operation );
Steven Cooreman72f736a2021-05-07 14:14:37 +02002283 operation->mac_size = 0;
2284 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002285 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002286
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002287 return( status );
2288}
2289
Ronald Cron2dff3b22021-06-17 16:33:22 +02002290static psa_status_t psa_mac_finalize_alg_and_key_validation(
2291 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002292 const psa_key_attributes_t *attributes,
2293 uint8_t *mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002294{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002295 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron79bdd822021-06-17 16:46:44 +02002296 psa_key_type_t key_type = psa_get_key_type( attributes );
2297 size_t key_bits = psa_get_key_bits( attributes );
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002298
Ronald Cron28ea0502021-06-17 16:10:24 +02002299 if( ! PSA_ALG_IS_MAC( alg ) )
Ronald Cron79bdd822021-06-17 16:46:44 +02002300 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremane6804192021-03-19 18:28:56 +01002301
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002302 /* Validate the combination of key type and algorithm */
Ronald Cron79bdd822021-06-17 16:46:44 +02002303 status = psa_mac_key_can_do( alg, key_type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002304 if( status != PSA_SUCCESS )
Ronald Cron79bdd822021-06-17 16:46:44 +02002305 return( status );
Steven Cooreman15472f82021-03-02 16:16:22 +01002306
Steven Cooremand1a68f12021-05-10 11:13:41 +02002307 /* Get the output length for the algorithm and key combination */
Ronald Cron79bdd822021-06-17 16:46:44 +02002308 *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002309
Ronald Cron79bdd822021-06-17 16:46:44 +02002310 if( *mac_size < 4 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002311 {
2312 /* A very short MAC is too short for security since it can be
2313 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2314 * so we make this our minimum, even though 32 bits is still
2315 * too small for security. */
Ronald Cron79bdd822021-06-17 16:46:44 +02002316 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman15472f82021-03-02 16:16:22 +01002317 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002318
Ronald Cron79bdd822021-06-17 16:46:44 +02002319 if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits,
2320 PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002321 {
2322 /* It's impossible to "truncate" to a larger length than the full length
2323 * of the algorithm. */
Ronald Cron79bdd822021-06-17 16:46:44 +02002324 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002325 }
2326
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002327 if( *mac_size > PSA_MAC_MAX_SIZE )
2328 {
2329 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2330 * that is disabled in the compile-time configuration. The result can
2331 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2332 * configuration into account. In this case, force a return of
2333 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2334 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2335 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2336 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2337 * systematically generated tests. */
2338 return( PSA_ERROR_NOT_SUPPORTED );
2339 }
2340
Ronald Cron79bdd822021-06-17 16:46:44 +02002341 return( PSA_SUCCESS );
Ronald Cron2dff3b22021-06-17 16:33:22 +02002342}
2343
Gilles Peskinee59236f2018-01-27 23:32:46 +01002344static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
2345 mbedtls_svc_key_id_t key,
2346 psa_algorithm_t alg,
2347 int is_sign )
2348{
2349 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2350 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002351 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002352
2353 /* A context must be freshly initialized before it can be set up. */
2354 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002355 {
Dave Rodgman54648242021-06-24 11:49:45 +01002356 status = PSA_ERROR_BAD_STATE;
2357 goto exit;
2358 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002359
2360 status = psa_get_and_lock_key_slot_with_policy(
2361 key,
2362 &slot,
Mateusz Starzykce0e6a92021-08-17 15:24:32 +02002363 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002364 alg );
2365 if( status != PSA_SUCCESS )
Dave Rodgman54648242021-06-24 11:49:45 +01002366 goto exit;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002367
2368 psa_key_attributes_t attributes = {
2369 .core = slot->attr
2370 };
2371
Ronald Cron79bdd822021-06-17 16:46:44 +02002372 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2373 &operation->mac_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374 if( status != PSA_SUCCESS )
2375 goto exit;
2376
2377 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002378 /* Dispatch the MAC setup call with validated input */
2379 if( is_sign )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002380 {
Steven Cooremane6804192021-03-19 18:28:56 +01002381 status = psa_driver_wrapper_mac_sign_setup( operation,
2382 &attributes,
2383 slot->key.data,
2384 slot->key.bytes,
2385 alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002386 }
2387 else
Gilles Peskinefbfac682018-07-08 20:51:54 +02002388 {
Steven Cooremane6804192021-03-19 18:28:56 +01002389 status = psa_driver_wrapper_mac_verify_setup( operation,
2390 &attributes,
2391 slot->key.data,
2392 slot->key.bytes,
2393 alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002394 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002395
Gilles Peskinefbfac682018-07-08 20:51:54 +02002396exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002397 if( status != PSA_SUCCESS )
Steven Cooremane6804192021-03-19 18:28:56 +01002398 psa_mac_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002399
Ronald Cron5c522922020-11-14 16:35:34 +01002400 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002401
Ronald Cron5c522922020-11-14 16:35:34 +01002402 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002403}
2404
Gilles Peskine89167cb2018-07-08 20:12:23 +02002405psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002406 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002407 psa_algorithm_t alg )
2408{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002409 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002410}
2411
2412psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002413 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002414 psa_algorithm_t alg )
2415{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002416 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002417}
2418
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002419psa_status_t psa_mac_update( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002420 const uint8_t *input,
2421 size_t input_length )
2422{
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002423 if( operation->id == 0 )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002424 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002425
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002426 /* Don't require hash implementations to behave correctly on a
2427 * zero-length input, which may have an invalid pointer. */
2428 if( input_length == 0 )
2429 return( PSA_SUCCESS );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002430
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002431 psa_status_t status = psa_driver_wrapper_mac_update( operation,
2432 input, input_length );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002433 if( status != PSA_SUCCESS )
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002434 psa_mac_abort( operation );
2435
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002436 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002437}
2438
Steven Cooremana5b860a2021-03-19 19:04:39 +01002439psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002440 uint8_t *mac,
2441 size_t mac_size,
2442 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002443{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002444 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2445 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002446
Steven Cooremana5b860a2021-03-19 19:04:39 +01002447 if( operation->id == 0 )
Dave Rodgman8036bdd2021-06-24 16:19:08 +01002448 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002449 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002450 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002451 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002452
Steven Cooreman72f736a2021-05-07 14:14:37 +02002453 if( ! operation->is_sign )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002454 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002455 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002456 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002457 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002458
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002459 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2460 * once all the error checks are done. */
2461 if( operation->mac_size == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002462 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002463 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002464 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002465 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002466
2467 if( mac_size < operation->mac_size )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002468 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002469 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002470 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002471 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002472
Steven Cooremana5b860a2021-03-19 19:04:39 +01002473 status = psa_driver_wrapper_mac_sign_finish( operation,
Steven Cooreman72f736a2021-05-07 14:14:37 +02002474 mac, operation->mac_size,
2475 mac_length );
2476
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002477exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002478 /* In case of success, set the potential excess room in the output buffer
2479 * to an invalid value, to avoid potentially leaking a longer MAC.
2480 * In case of error, set the output length and content to a safe default,
2481 * such that in case the caller misses an error check, the output would be
2482 * an unachievable MAC.
2483 */
2484 if( status != PSA_SUCCESS )
Steven Cooreman72f736a2021-05-07 14:14:37 +02002485 {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002486 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002487 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002488 }
mohammad16036df908f2018-04-02 08:34:15 -07002489
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002490 if( mac_size > operation->mac_size )
2491 memset( &mac[operation->mac_size], '!',
2492 mac_size - operation->mac_size );
2493
Steven Cooremana5b860a2021-03-19 19:04:39 +01002494 abort_status = psa_mac_abort( operation );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002495
Steven Cooremana5b860a2021-03-19 19:04:39 +01002496 return( status == PSA_SUCCESS ? abort_status : status );
mohammad16036df908f2018-04-02 08:34:15 -07002497}
2498
Steven Cooremana5b860a2021-03-19 19:04:39 +01002499psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002500 const uint8_t *mac,
2501 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502{
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
Steven Cooremana5b860a2021-03-19 19:04:39 +01002506 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002507 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002508 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002509 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002510 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002511
Steven Cooreman72f736a2021-05-07 14:14:37 +02002512 if( operation->is_sign )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002513 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002514 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002515 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002516 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002517
2518 if( operation->mac_size != mac_length )
2519 {
2520 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002521 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002522 }
2523
Steven Cooremana5b860a2021-03-19 19:04:39 +01002524 status = psa_driver_wrapper_mac_verify_finish( operation,
2525 mac, mac_length );
Steven Cooreman72f736a2021-05-07 14:14:37 +02002526
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002527exit:
Steven Cooremana5b860a2021-03-19 19:04:39 +01002528 abort_status = psa_mac_abort( operation );
mohammad16036df908f2018-04-02 08:34:15 -07002529
Steven Cooremana5b860a2021-03-19 19:04:39 +01002530 return( status == PSA_SUCCESS ? abort_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531}
2532
Ronald Croncd989b52021-06-18 14:23:33 +02002533static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key,
2534 psa_algorithm_t alg,
2535 const uint8_t *input,
2536 size_t input_length,
2537 uint8_t *mac,
2538 size_t mac_size,
2539 size_t *mac_length,
2540 int is_sign )
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002541{
2542 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002543 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2544 psa_key_slot_t *slot;
2545 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546
Ronald Cron51131b52021-06-17 17:17:20 +02002547 status = psa_get_and_lock_key_slot_with_policy(
Mateusz Starzykce0e6a92021-08-17 15:24:32 +02002548 key,
2549 &slot,
2550 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Croncd989b52021-06-18 14:23:33 +02002551 alg );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002552 if( status != PSA_SUCCESS )
2553 goto exit;
2554
Ronald Cron51131b52021-06-17 17:17:20 +02002555 psa_key_attributes_t attributes = {
2556 .core = slot->attr
2557 };
2558
2559 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2560 &operation_mac_size );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002561 if( status != PSA_SUCCESS )
2562 goto exit;
2563
Ronald Cron51131b52021-06-17 17:17:20 +02002564 if( mac_size < operation_mac_size )
2565 {
2566 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002567 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002568 }
2569
2570 status = psa_driver_wrapper_mac_compute(
2571 &attributes,
2572 slot->key.data, slot->key.bytes,
2573 alg,
2574 input, input_length,
2575 mac, operation_mac_size, mac_length );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002576
2577exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002578 /* In case of success, set the potential excess room in the output buffer
2579 * to an invalid value, to avoid potentially leaking a longer MAC.
2580 * In case of error, set the output length and content to a safe default,
2581 * such that in case the caller misses an error check, the output would be
2582 * an unachievable MAC.
2583 */
2584 if( status != PSA_SUCCESS )
Ronald Cron51131b52021-06-17 17:17:20 +02002585 {
Ronald Cron51131b52021-06-17 17:17:20 +02002586 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002587 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002588 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002589 if( mac_size > operation_mac_size )
2590 memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002591
Ronald Cron51131b52021-06-17 17:17:20 +02002592 unlock_status = psa_unlock_key_slot( slot );
2593
2594 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002595}
2596
Ronald Croncd989b52021-06-18 14:23:33 +02002597psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key,
2598 psa_algorithm_t alg,
2599 const uint8_t *input,
2600 size_t input_length,
2601 uint8_t *mac,
2602 size_t mac_size,
2603 size_t *mac_length)
2604{
2605 return( psa_mac_compute_internal( key, alg,
2606 input, input_length,
2607 mac, mac_size, mac_length, 1 ) );
2608}
2609
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002610psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key,
2611 psa_algorithm_t alg,
2612 const uint8_t *input,
2613 size_t input_length,
2614 const uint8_t *mac,
2615 size_t mac_length)
2616{
2617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002618 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2619 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002620
Ronald Crona587cbc2021-06-18 14:51:29 +02002621 status = psa_mac_compute_internal( key, alg,
2622 input, input_length,
2623 actual_mac, sizeof( actual_mac ),
2624 &actual_mac_length, 0 );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002625 if( status != PSA_SUCCESS )
2626 goto exit;
2627
Ronald Crona587cbc2021-06-18 14:51:29 +02002628 if( mac_length != actual_mac_length )
2629 {
2630 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002631 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002632 }
2633 if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
2634 {
2635 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002636 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002637 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002638
2639exit:
Ronald Crona587cbc2021-06-18 14:51:29 +02002640 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002641
2642 return ( status );
2643}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002644
Gilles Peskinee59236f2018-01-27 23:32:46 +01002645/****************************************************************/
2646/* Asymmetric cryptography */
2647/****************************************************************/
2648
gabor-mezei-armf0486182021-05-12 11:03:09 +02002649static psa_status_t psa_sign_verify_check_alg( int input_is_message,
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002650 psa_algorithm_t alg )
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002651{
gabor-mezei-armf0486182021-05-12 11:03:09 +02002652 if( input_is_message )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002653 {
2654 if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) )
2655 return( PSA_ERROR_INVALID_ARGUMENT );
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002656
Gilles Peskinef7b41372021-09-22 16:15:05 +02002657 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002658 {
2659 if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) )
2660 return( PSA_ERROR_INVALID_ARGUMENT );
2661 }
2662 }
2663 else
2664 {
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02002665 if( ! PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002666 return( PSA_ERROR_INVALID_ARGUMENT );
2667 }
2668
2669 return( PSA_SUCCESS );
2670}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002671
2672static psa_status_t psa_sign_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002673 int input_is_message,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002674 psa_algorithm_t alg,
2675 const uint8_t * input,
2676 size_t input_length,
2677 uint8_t * signature,
2678 size_t signature_size,
2679 size_t * signature_length )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002680{
2681 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2682 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2683 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002684
2685 *signature_length = 0;
2686
gabor-mezei-armf0486182021-05-12 11:03:09 +02002687 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002688 if( status != PSA_SUCCESS )
2689 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002690
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002691 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002692 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002693 * buffer can in principle be empty since it doesn't actually have
2694 * to be a hash.) */
2695 if( signature_size == 0 )
2696 return( PSA_ERROR_BUFFER_TOO_SMALL );
2697
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002698 status = psa_get_and_lock_key_slot_with_policy(
2699 key, &slot,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002700 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2701 PSA_KEY_USAGE_SIGN_HASH,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002702 alg );
2703
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002704 if( status != PSA_SUCCESS )
2705 goto exit;
2706
2707 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
2708 {
2709 status = PSA_ERROR_INVALID_ARGUMENT;
2710 goto exit;
2711 }
2712
2713 psa_key_attributes_t attributes = {
2714 .core = slot->attr
2715 };
2716
gabor-mezei-armf0486182021-05-12 11:03:09 +02002717 if( input_is_message )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002718 {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002719 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002720 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002721 alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002722 signature, signature_size, signature_length );
2723 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002724 else
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002725 {
2726
2727 status = psa_driver_wrapper_sign_hash(
2728 &attributes, slot->key.data, slot->key.bytes,
2729 alg, input, input_length,
2730 signature, signature_size, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002731 }
2732
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002733
2734exit:
2735 /* Fill the unused part of the output buffer (the whole buffer on error,
2736 * the trailing part on success) with something that isn't a valid signature
2737 * (barring an attack on the signature and deliberately-crafted input),
2738 * in case the caller doesn't check the return status properly. */
2739 if( status == PSA_SUCCESS )
2740 memset( signature + *signature_length, '!',
2741 signature_size - *signature_length );
2742 else
2743 memset( signature, '!', signature_size );
2744 /* If signature_size is 0 then we have nothing to do. We must not call
2745 * memset because signature may be NULL in this case. */
2746
2747 unlock_status = psa_unlock_key_slot( slot );
2748
2749 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
2750}
2751
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002752static psa_status_t psa_verify_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002753 int input_is_message,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002754 psa_algorithm_t alg,
2755 const uint8_t * input,
2756 size_t input_length,
2757 const uint8_t * signature,
2758 size_t signature_length )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002759{
2760 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2761 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2762 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002763
gabor-mezei-armf0486182021-05-12 11:03:09 +02002764 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002765 if( status != PSA_SUCCESS )
2766 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002767
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002768 status = psa_get_and_lock_key_slot_with_policy(
2769 key, &slot,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002770 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2771 PSA_KEY_USAGE_VERIFY_HASH,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002772 alg );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002773
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002774 if( status != PSA_SUCCESS )
2775 return( status );
2776
2777 psa_key_attributes_t attributes = {
2778 .core = slot->attr
2779 };
2780
gabor-mezei-armf0486182021-05-12 11:03:09 +02002781 if( input_is_message )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002782 {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002783 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002784 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002785 alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002786 signature, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002787 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002788 else
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002789 {
2790 status = psa_driver_wrapper_verify_hash(
2791 &attributes, slot->key.data, slot->key.bytes,
2792 alg, input, input_length,
2793 signature, signature_length );
2794 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002795
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002796 unlock_status = psa_unlock_key_slot( slot );
2797
2798 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002799
2800}
2801
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002802psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002803 const psa_key_attributes_t *attributes,
2804 const uint8_t *key_buffer,
2805 size_t key_buffer_size,
2806 psa_algorithm_t alg,
2807 const uint8_t *input,
2808 size_t input_length,
2809 uint8_t *signature,
2810 size_t signature_size,
2811 size_t *signature_length )
2812{
2813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002814
Gilles Peskinef7b41372021-09-22 16:15:05 +02002815 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002816 {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002817 size_t hash_length;
2818 uint8_t hash[PSA_HASH_MAX_SIZE];
2819
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002820 status = psa_driver_wrapper_hash_compute(
2821 PSA_ALG_SIGN_GET_HASH( alg ),
2822 input, input_length,
2823 hash, sizeof( hash ), &hash_length );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002824
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002825 if( status != PSA_SUCCESS )
2826 return status;
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002827
2828 return psa_driver_wrapper_sign_hash(
2829 attributes, key_buffer, key_buffer_size,
2830 alg, hash, hash_length,
2831 signature, signature_size, signature_length );
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002832 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002833
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002834 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002835}
2836
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002837psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
2838 psa_algorithm_t alg,
2839 const uint8_t * input,
2840 size_t input_length,
2841 uint8_t * signature,
2842 size_t signature_size,
2843 size_t * signature_length )
2844{
2845 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002846 key, 1, alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002847 signature, signature_size, signature_length );
2848}
2849
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002850psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002851 const psa_key_attributes_t *attributes,
2852 const uint8_t *key_buffer,
2853 size_t key_buffer_size,
2854 psa_algorithm_t alg,
2855 const uint8_t *input,
2856 size_t input_length,
2857 const uint8_t *signature,
2858 size_t signature_length )
2859{
2860 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002861
Gilles Peskinef7b41372021-09-22 16:15:05 +02002862 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002863 {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002864 size_t hash_length;
2865 uint8_t hash[PSA_HASH_MAX_SIZE];
2866
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002867 status = psa_driver_wrapper_hash_compute(
2868 PSA_ALG_SIGN_GET_HASH( alg ),
2869 input, input_length,
2870 hash, sizeof( hash ), &hash_length );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002871
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002872 if( status != PSA_SUCCESS )
2873 return status;
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002874
2875 return psa_driver_wrapper_verify_hash(
2876 attributes, key_buffer, key_buffer_size,
2877 alg, hash, hash_length,
2878 signature, signature_length );
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002879 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002880
gabor-mezei-arm474a35f2021-05-05 13:59:01 +02002881 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002882}
2883
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002884psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
2885 psa_algorithm_t alg,
2886 const uint8_t * input,
2887 size_t input_length,
2888 const uint8_t * signature,
2889 size_t signature_length )
2890{
2891 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002892 key, 1, alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002893 signature, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002894}
2895
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002896psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002897 const psa_key_attributes_t *attributes,
2898 const uint8_t *key_buffer, size_t key_buffer_size,
2899 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2900 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002901{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002902 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002903 {
Ronald Cron4501c982021-03-19 20:09:32 +01002904 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2905 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002906 {
Ronald Cron4501c982021-03-19 20:09:32 +01002907#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2908 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2909 return( mbedtls_psa_rsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002910 attributes,
2911 key_buffer, key_buffer_size,
2912 alg, hash, hash_length,
2913 signature, signature_size, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002914#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2915 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002916 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002917 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002918 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002919 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002920 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002921 }
Ronald Cron81ca97e2021-04-09 15:32:03 +02002922 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Ronald Cron4501c982021-03-19 20:09:32 +01002923 {
2924 if( PSA_ALG_IS_ECDSA( alg ) )
2925 {
2926#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2927 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2928 return( mbedtls_psa_ecdsa_sign_hash(
2929 attributes,
2930 key_buffer, key_buffer_size,
2931 alg, hash, hash_length,
2932 signature, signature_size, signature_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002933#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2934 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron4501c982021-03-19 20:09:32 +01002935 }
2936 else
2937 {
2938 return( PSA_ERROR_INVALID_ARGUMENT );
2939 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002940 }
Ronald Cron4501c982021-03-19 20:09:32 +01002941
2942 (void)key_buffer;
2943 (void)key_buffer_size;
2944 (void)hash;
2945 (void)hash_length;
2946 (void)signature;
2947 (void)signature_size;
2948 (void)signature_length;
2949
2950 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01002951}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002952
2953psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
2954 psa_algorithm_t alg,
2955 const uint8_t *hash,
2956 size_t hash_length,
2957 uint8_t *signature,
2958 size_t signature_size,
2959 size_t *signature_length )
2960{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002961 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002962 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01002963 signature, signature_size, signature_length );
itayzafrir5c753392018-05-08 11:18:38 +03002964}
2965
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002966psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002967 const psa_key_attributes_t *attributes,
2968 const uint8_t *key_buffer, size_t key_buffer_size,
2969 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2970 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002971{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002972 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002973 {
Ronald Cron4501c982021-03-19 20:09:32 +01002974 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2975 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002976 {
Ronald Cron4501c982021-03-19 20:09:32 +01002977#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2978 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2979 return( mbedtls_psa_rsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002980 attributes,
2981 key_buffer, key_buffer_size,
2982 alg, hash, hash_length,
2983 signature, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002984#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2985 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002986 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002987 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002988 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002989 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002990 }
itayzafrir5c753392018-05-08 11:18:38 +03002991 }
Ronald Cron81ca97e2021-04-09 15:32:03 +02002992 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002993 {
Ronald Cron4501c982021-03-19 20:09:32 +01002994 if( PSA_ALG_IS_ECDSA( alg ) )
2995 {
2996#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2997 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2998 return( mbedtls_psa_ecdsa_verify_hash(
2999 attributes,
3000 key_buffer, key_buffer_size,
3001 alg, hash, hash_length,
3002 signature, signature_length ) );
3003#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3004 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3005 }
3006 else
3007 {
3008 return( PSA_ERROR_INVALID_ARGUMENT );
3009 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003010 }
Ronald Cron4501c982021-03-19 20:09:32 +01003011
3012 (void)key_buffer;
3013 (void)key_buffer_size;
3014 (void)hash;
3015 (void)hash_length;
3016 (void)signature;
3017 (void)signature_length;
3018
3019 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01003020}
3021
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003022psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3023 psa_algorithm_t alg,
3024 const uint8_t *hash,
3025 size_t hash_length,
3026 const uint8_t *signature,
3027 size_t signature_length )
3028{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003029 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003030 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01003031 signature, signature_length );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003032}
3033
Ronald Croncf56a0a2020-08-04 09:51:30 +02003034psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003035 psa_algorithm_t alg,
3036 const uint8_t *input,
3037 size_t input_length,
3038 const uint8_t *salt,
3039 size_t salt_length,
3040 uint8_t *output,
3041 size_t output_size,
3042 size_t *output_length )
3043{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003045 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003046 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003047
Darryl Green5cc689a2018-07-24 15:34:10 +01003048 (void) input;
3049 (void) input_length;
3050 (void) salt;
3051 (void) output;
3052 (void) output_size;
3053
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003054 *output_length = 0;
3055
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003056 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3057 return( PSA_ERROR_INVALID_ARGUMENT );
3058
Ronald Cron5c522922020-11-14 16:35:34 +01003059 status = psa_get_and_lock_transparent_key_slot_with_policy(
3060 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003061 if( status != PSA_SUCCESS )
3062 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003063 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3064 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003065 {
3066 status = PSA_ERROR_INVALID_ARGUMENT;
3067 goto exit;
3068 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003069
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003070 psa_key_attributes_t attributes = {
3071 .core = slot->attr
3072 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003073
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003074 status = psa_driver_wrapper_asymmetric_encrypt(
3075 &attributes, slot->key.data, slot->key.bytes,
3076 alg, input, input_length, salt, salt_length,
3077 output, output_size, output_length );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003078exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003079 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003080
Ronald Cron5c522922020-11-14 16:35:34 +01003081 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003082}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003083
Ronald Croncf56a0a2020-08-04 09:51:30 +02003084psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003085 psa_algorithm_t alg,
3086 const uint8_t *input,
3087 size_t input_length,
3088 const uint8_t *salt,
3089 size_t salt_length,
3090 uint8_t *output,
3091 size_t output_size,
3092 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003095 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003096 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003097
Darryl Green5cc689a2018-07-24 15:34:10 +01003098 (void) input;
3099 (void) input_length;
3100 (void) salt;
3101 (void) output;
3102 (void) output_size;
3103
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003104 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003105
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003106 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3107 return( PSA_ERROR_INVALID_ARGUMENT );
3108
Ronald Cron5c522922020-11-14 16:35:34 +01003109 status = psa_get_and_lock_transparent_key_slot_with_policy(
3110 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003111 if( status != PSA_SUCCESS )
3112 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003113 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003114 {
3115 status = PSA_ERROR_INVALID_ARGUMENT;
3116 goto exit;
3117 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003118
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003119 psa_key_attributes_t attributes = {
3120 .core = slot->attr
3121 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003122
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003123 status = psa_driver_wrapper_asymmetric_decrypt(
3124 &attributes, slot->key.data, slot->key.bytes,
3125 alg, input, input_length, salt, salt_length,
3126 output, output_size, output_length );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003127
3128exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003129 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003130
Ronald Cron5c522922020-11-14 16:35:34 +01003131 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003132}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003133
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003134
3135
mohammad1603503973b2018-03-12 15:59:30 +02003136/****************************************************************/
3137/* Symmetric cryptography */
3138/****************************************************************/
3139
Ronald Cronab99ac22020-10-01 16:38:28 +02003140static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
3141 mbedtls_svc_key_id_t key,
3142 psa_algorithm_t alg,
3143 mbedtls_operation_t cipher_operation )
3144{
3145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3146 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003147 psa_key_slot_t *slot = NULL;
Ronald Cronab99ac22020-10-01 16:38:28 +02003148 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3149 PSA_KEY_USAGE_ENCRYPT :
3150 PSA_KEY_USAGE_DECRYPT );
3151
3152 /* A context must be freshly initialized before it can be set up. */
Ronald Cron49fafa92021-03-10 08:34:23 +01003153 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003154 {
Dave Rodgman54648242021-06-24 11:49:45 +01003155 status = PSA_ERROR_BAD_STATE;
3156 goto exit;
3157 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003158
Ronald Cronab99ac22020-10-01 16:38:28 +02003159 if( ! PSA_ALG_IS_CIPHER( alg ) )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003160 {
Dave Rodgman54648242021-06-24 11:49:45 +01003161 status = PSA_ERROR_INVALID_ARGUMENT;
3162 goto exit;
3163 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003164
Ronald Cronab99ac22020-10-01 16:38:28 +02003165 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
3166 if( status != PSA_SUCCESS )
3167 goto exit;
3168
Ronald Cron49fafa92021-03-10 08:34:23 +01003169 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003170 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003171 * so we only set it (in the driver wrapper) after resources have been
3172 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003173 operation->iv_set = 0;
Ronald Cronab99ac22020-10-01 16:38:28 +02003174 if( alg == PSA_ALG_ECB_NO_PADDING )
3175 operation->iv_required = 0;
3176 else
3177 operation->iv_required = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003178 operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
Ronald Cronab99ac22020-10-01 16:38:28 +02003179
Ronald Crona4af55f2020-12-14 14:36:06 +01003180 psa_key_attributes_t attributes = {
3181 .core = slot->attr
3182 };
3183
Ronald Cronab99ac22020-10-01 16:38:28 +02003184 /* Try doing the operation through a driver before using software fallback. */
3185 if( cipher_operation == MBEDTLS_ENCRYPT )
Ronald Crona4af55f2020-12-14 14:36:06 +01003186 status = psa_driver_wrapper_cipher_encrypt_setup( operation,
3187 &attributes,
3188 slot->key.data,
3189 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003190 alg );
3191 else
Ronald Crona4af55f2020-12-14 14:36:06 +01003192 status = psa_driver_wrapper_cipher_decrypt_setup( operation,
3193 &attributes,
3194 slot->key.data,
3195 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003196 alg );
3197
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003198exit:
Ronald Cron06aa4422021-03-09 17:32:57 +01003199 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003200 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003201
Ronald Cron5c522922020-11-14 16:35:34 +01003202 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003203
Ronald Cron5c522922020-11-14 16:35:34 +01003204 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003205}
3206
Gilles Peskinefe119512018-07-08 21:39:34 +02003207psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003208 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003209 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003210{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003211 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003212}
3213
Gilles Peskinefe119512018-07-08 21:39:34 +02003214psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003215 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003216 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003217{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003218 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003219}
3220
Gilles Peskinefe119512018-07-08 21:39:34 +02003221psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003222 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003223 size_t iv_size,
3224 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003225{
Ronald Cron6d051732020-10-01 14:10:20 +02003226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003227 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3228 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01003229
Ronald Cron49fafa92021-03-10 08:34:23 +01003230 if( operation->id == 0 )
Ronald Cronc45b4af2020-09-29 16:18:05 +02003231 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003232 status = PSA_ERROR_BAD_STATE;
3233 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003234 }
3235
Steven Cooreman7df02922020-09-09 15:28:49 +02003236 if( operation->iv_set || ! operation->iv_required )
3237 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003238 status = PSA_ERROR_BAD_STATE;
3239 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003240 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003241
Ronald Cron2fb90522021-07-05 12:31:44 +02003242 default_iv_length = operation->default_iv_length;
3243 if( iv_size < default_iv_length )
Ronald Cron5618a392021-03-26 09:52:26 +01003244 {
3245 status = PSA_ERROR_BUFFER_TOO_SMALL;
3246 goto exit;
3247 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003248
Ronald Cron2fb90522021-07-05 12:31:44 +02003249 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
3250 {
3251 status = PSA_ERROR_GENERIC_ERROR;
3252 goto exit;
3253 }
3254
3255 status = psa_generate_random( local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003256 if( status != PSA_SUCCESS )
3257 goto exit;
3258
3259 status = psa_driver_wrapper_cipher_set_iv( operation,
Ronald Cron2fb90522021-07-05 12:31:44 +02003260 local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003261
3262exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003263 if( status == PSA_SUCCESS )
Ronald Cron5618a392021-03-26 09:52:26 +01003264 {
Ronald Cron2fb90522021-07-05 12:31:44 +02003265 memcpy( iv, local_iv, default_iv_length );
3266 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003267 operation->iv_set = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003268 }
Steven Cooreman7df02922020-09-09 15:28:49 +02003269 else
Ronald Cron2fb90522021-07-05 12:31:44 +02003270 {
3271 *iv_length = 0;
Moran Peker395db872018-05-31 14:07:14 +03003272 psa_cipher_abort( operation );
Ronald Cron2fb90522021-07-05 12:31:44 +02003273 }
Ronald Cron6d051732020-10-01 14:10:20 +02003274
itayzafrir534bd7c2018-08-02 13:56:32 +03003275 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003276}
3277
Gilles Peskinefe119512018-07-08 21:39:34 +02003278psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003279 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003280 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003281{
Ronald Cron6d051732020-10-01 14:10:20 +02003282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003283
Ronald Cron49fafa92021-03-10 08:34:23 +01003284 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003285 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003286 status = PSA_ERROR_BAD_STATE;
3287 goto exit;
3288 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003289
Steven Cooreman7df02922020-09-09 15:28:49 +02003290 if( operation->iv_set || ! operation->iv_required )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003291 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003292 status = PSA_ERROR_BAD_STATE;
3293 goto exit;
3294 }
Ronald Crona0d68172021-03-26 10:15:08 +01003295
3296 if( iv_length > PSA_CIPHER_IV_MAX_SIZE )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003297 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003298 status = PSA_ERROR_INVALID_ARGUMENT;
3299 goto exit;
3300 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003301
Ronald Crondd24c9b2020-12-15 14:10:01 +01003302 status = psa_driver_wrapper_cipher_set_iv( operation,
3303 iv,
3304 iv_length );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003305
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003306exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003307 if( status == PSA_SUCCESS )
3308 operation->iv_set = 1;
3309 else
mohammad1603503973b2018-03-12 15:59:30 +02003310 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003311 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003312}
3313
Gilles Peskinee553c652018-06-04 16:22:46 +02003314psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3315 const uint8_t *input,
3316 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003317 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003318 size_t output_size,
3319 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003320{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003321 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003322
Ronald Cron49fafa92021-03-10 08:34:23 +01003323 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003324 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003325 status = PSA_ERROR_BAD_STATE;
3326 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003327 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003328
Steven Cooreman7df02922020-09-09 15:28:49 +02003329 if( operation->iv_required && ! operation->iv_set )
3330 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003331 status = PSA_ERROR_BAD_STATE;
3332 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003333 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003334
Ronald Crondd24c9b2020-12-15 14:10:01 +01003335 status = psa_driver_wrapper_cipher_update( operation,
3336 input,
3337 input_length,
3338 output,
3339 output_size,
3340 output_length );
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003341
3342exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003343 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003344 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003345
itayzafrir534bd7c2018-08-02 13:56:32 +03003346 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003347}
3348
Gilles Peskinee553c652018-06-04 16:22:46 +02003349psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3350 uint8_t *output,
3351 size_t output_size,
3352 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003353{
David Saadab4ecc272019-02-14 13:48:10 +02003354 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003355
Ronald Cron49fafa92021-03-10 08:34:23 +01003356 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003357 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003358 status = PSA_ERROR_BAD_STATE;
3359 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003360 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003361
Steven Cooreman7df02922020-09-09 15:28:49 +02003362 if( operation->iv_required && ! operation->iv_set )
3363 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003364 status = PSA_ERROR_BAD_STATE;
3365 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003366 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003367
Ronald Crondd24c9b2020-12-15 14:10:01 +01003368 status = psa_driver_wrapper_cipher_finish( operation,
3369 output,
3370 output_size,
3371 output_length );
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003372
3373exit:
Steven Cooremanef8575e2020-09-11 11:44:50 +02003374 if( status == PSA_SUCCESS )
3375 return( psa_cipher_abort( operation ) );
3376 else
3377 {
3378 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003379 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01003380
Steven Cooremanef8575e2020-09-11 11:44:50 +02003381 return( status );
3382 }
mohammad1603503973b2018-03-12 15:59:30 +02003383}
3384
Gilles Peskinee553c652018-06-04 16:22:46 +02003385psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3386{
Ronald Cron49fafa92021-03-10 08:34:23 +01003387 if( operation->id == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003388 {
Steven Cooremana07b9972020-09-10 14:54:14 +02003389 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003390 * in use. It's ok to call abort on such an object, and there's
3391 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003392 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003393 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003394
Ronald Crondd24c9b2020-12-15 14:10:01 +01003395 psa_driver_wrapper_cipher_abort( operation );
Gilles Peskinee553c652018-06-04 16:22:46 +02003396
Ronald Cron49fafa92021-03-10 08:34:23 +01003397 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003398 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003399 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003400
Moran Peker395db872018-05-31 14:07:14 +03003401 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003402}
3403
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003404psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key,
3405 psa_algorithm_t alg,
3406 const uint8_t *input,
3407 size_t input_length,
3408 uint8_t *output,
3409 size_t output_size,
3410 size_t *output_length )
3411{
3412 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003413 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02003414 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02003415 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3416 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003417
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003418 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron23919522021-07-08 19:00:07 +02003419 {
3420 status = PSA_ERROR_INVALID_ARGUMENT;
3421 goto exit;
3422 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003423
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003424 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3425 PSA_KEY_USAGE_ENCRYPT,
3426 alg );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003427 if( status != PSA_SUCCESS )
Ronald Cron23919522021-07-08 19:00:07 +02003428 goto exit;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003429
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003430 psa_key_attributes_t attributes = {
3431 .core = slot->attr
3432 };
3433
Ronald Cronc6e6f502021-07-09 18:44:58 +02003434 default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
3435 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003436 {
Ronald Cronc6e6f502021-07-09 18:44:58 +02003437 status = PSA_ERROR_GENERIC_ERROR;
3438 goto exit;
3439 }
3440
3441 if( default_iv_length > 0 )
3442 {
3443 if( output_size < default_iv_length )
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003444 {
3445 status = PSA_ERROR_BUFFER_TOO_SMALL;
3446 goto exit;
3447 }
3448
Ronald Cronc6e6f502021-07-09 18:44:58 +02003449 status = psa_generate_random( local_iv, default_iv_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003450 if( status != PSA_SUCCESS )
3451 goto exit;
3452 }
3453
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003454 status = psa_driver_wrapper_cipher_encrypt(
3455 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02003456 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine42649d92022-11-23 14:15:57 +01003457 mbedtls_buffer_offset( output, default_iv_length ),
3458 output_size - default_iv_length, output_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003459
3460exit:
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003461 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron23919522021-07-08 19:00:07 +02003462 if( status == PSA_SUCCESS )
3463 status = unlock_status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003464
Ronald Cron9b674282021-07-09 09:19:35 +02003465 if( status == PSA_SUCCESS )
Ronald Cronc6e6f502021-07-09 18:44:58 +02003466 {
3467 if( default_iv_length > 0 )
3468 memcpy( output, local_iv, default_iv_length );
3469 *output_length += default_iv_length;
3470 }
Ronald Cron9b674282021-07-09 09:19:35 +02003471 else
Ronald Cron23919522021-07-08 19:00:07 +02003472 *output_length = 0;
3473
3474 return( status );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003475}
3476
3477psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key,
3478 psa_algorithm_t alg,
3479 const uint8_t *input,
3480 size_t input_length,
3481 uint8_t *output,
3482 size_t output_size,
3483 size_t *output_length )
3484{
3485 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003486 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02003487 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003488
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003489 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron23919522021-07-08 19:00:07 +02003490 {
3491 status = PSA_ERROR_INVALID_ARGUMENT;
3492 goto exit;
3493 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003494
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003495 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3496 PSA_KEY_USAGE_DECRYPT,
3497 alg );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003498 if( status != PSA_SUCCESS )
Ronald Cron23919522021-07-08 19:00:07 +02003499 goto exit;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003500
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003501 psa_key_attributes_t attributes = {
3502 .core = slot->attr
3503 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003504
Mateusz Starzyk594215b2021-10-14 12:23:06 +02003505 if( alg == PSA_ALG_CCM_STAR_NO_TAG && input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) )
3506 {
3507 status = PSA_ERROR_INVALID_ARGUMENT;
3508 goto exit;
3509 }
3510 else if ( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) )
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003511 {
3512 status = PSA_ERROR_INVALID_ARGUMENT;
3513 goto exit;
3514 }
3515
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003516 status = psa_driver_wrapper_cipher_decrypt(
3517 &attributes, slot->key.data, slot->key.bytes,
3518 alg, input, input_length,
3519 output, output_size, output_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003520
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003521exit:
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003522 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron23919522021-07-08 19:00:07 +02003523 if( status == PSA_SUCCESS )
3524 status = unlock_status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003525
Ronald Cron23919522021-07-08 19:00:07 +02003526 if( status != PSA_SUCCESS )
3527 *output_length = 0;
3528
3529 return( status );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003530}
3531
3532
mohammad16035955c982018-04-26 00:53:03 +03003533/****************************************************************/
3534/* AEAD */
3535/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003536
Paul Elliottbaff51c2021-09-28 17:44:45 +01003537/* Helper function to get the base algorithm from its variants. */
3538static psa_algorithm_t psa_aead_get_base_algorithm( psa_algorithm_t alg )
3539{
3540 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg );
3541}
3542
3543/* Helper function to perform common nonce length checks. */
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003544static psa_status_t psa_aead_check_nonce_length( psa_algorithm_t alg,
3545 size_t nonce_length )
3546{
Paul Elliottbaff51c2021-09-28 17:44:45 +01003547 psa_algorithm_t base_alg = psa_aead_get_base_algorithm( alg );
3548
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003549 switch(base_alg)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003550 {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003551#if defined(PSA_WANT_ALG_GCM)
3552 case PSA_ALG_GCM:
3553 /* Not checking max nonce size here as GCM spec allows almost
3554 * arbitrarily large nonces. Please note that we do not generally
3555 * recommend the usage of nonces of greater length than
3556 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
3557 * size, which can then lead to collisions if you encrypt a very
3558 * large number of messages.*/
3559 if( nonce_length != 0 )
3560 return( PSA_SUCCESS );
3561 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003562#endif /* PSA_WANT_ALG_GCM */
3563#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003564 case PSA_ALG_CCM:
3565 if( nonce_length >= 7 && nonce_length <= 13 )
3566 return( PSA_SUCCESS );
3567 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003568#endif /* PSA_WANT_ALG_CCM */
3569#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003570 case PSA_ALG_CHACHA20_POLY1305:
3571 if( nonce_length == 12 )
3572 return( PSA_SUCCESS );
Bence Szépkúti6d48e202021-11-15 20:04:15 +01003573 else if( nonce_length == 8 )
3574 return( PSA_ERROR_NOT_SUPPORTED );
3575 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003576#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003577 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02003578 (void) nonce_length;
Bence Szépkúti357b78e2021-11-09 13:17:17 +01003579 return( PSA_ERROR_NOT_SUPPORTED );
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003580 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003581
Bence Szépkúti357b78e2021-11-09 13:17:17 +01003582 return( PSA_ERROR_INVALID_ARGUMENT );
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003583}
3584
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01003585static psa_status_t psa_aead_check_algorithm( psa_algorithm_t alg )
3586{
Bence Szépkúti08f34652021-12-08 21:07:13 +01003587 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3588 return( PSA_ERROR_INVALID_ARGUMENT );
3589
3590 return( PSA_SUCCESS );
3591}
3592
Ronald Croncf56a0a2020-08-04 09:51:30 +02003593psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003594 psa_algorithm_t alg,
3595 const uint8_t *nonce,
3596 size_t nonce_length,
3597 const uint8_t *additional_data,
3598 size_t additional_data_length,
3599 const uint8_t *plaintext,
3600 size_t plaintext_length,
3601 uint8_t *ciphertext,
3602 size_t ciphertext_size,
3603 size_t *ciphertext_length )
3604{
Ronald Cron215633c2021-03-16 17:15:37 +01003605 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3606 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003607
mohammad1603f08a5502018-06-03 15:05:47 +03003608 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003609
Bence Szépkúti39fb9d12022-01-13 14:33:45 +01003610 status = psa_aead_check_algorithm( alg );
Bence Szépkúti08f34652021-12-08 21:07:13 +01003611 if( status != PSA_SUCCESS )
3612 return( status );
Steven Cooremanea7ab132021-03-17 16:28:00 +01003613
Ronald Cron9a986162021-03-26 12:40:07 +01003614 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003615 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003616 if( status != PSA_SUCCESS )
3617 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003618
Ronald Cron215633c2021-03-16 17:15:37 +01003619 psa_key_attributes_t attributes = {
3620 .core = slot->attr
3621 };
mohammad16035955c982018-04-26 00:53:03 +03003622
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003623 status = psa_aead_check_nonce_length( alg, nonce_length );
3624 if( status != PSA_SUCCESS )
3625 goto exit;
3626
Ronald Cronde822812021-03-17 16:08:20 +01003627 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003628 &attributes, slot->key.data, slot->key.bytes,
3629 alg,
3630 nonce, nonce_length,
3631 additional_data, additional_data_length,
3632 plaintext, plaintext_length,
3633 ciphertext, ciphertext_size, ciphertext_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003634
Gilles Peskineedf9a652018-08-17 18:11:56 +02003635 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3636 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003637
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003638exit:
Ronald Cron9f310172021-03-16 16:36:37 +01003639 psa_unlock_key_slot( slot );
mohammad16035955c982018-04-26 00:53:03 +03003640
mohammad16035955c982018-04-26 00:53:03 +03003641 return( status );
Gilles Peskineee652a32018-06-01 19:23:52 +02003642}
3643
Ronald Croncf56a0a2020-08-04 09:51:30 +02003644psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003645 psa_algorithm_t alg,
3646 const uint8_t *nonce,
3647 size_t nonce_length,
3648 const uint8_t *additional_data,
3649 size_t additional_data_length,
3650 const uint8_t *ciphertext,
3651 size_t ciphertext_length,
3652 uint8_t *plaintext,
3653 size_t plaintext_size,
3654 size_t *plaintext_length )
3655{
Ronald Cron215633c2021-03-16 17:15:37 +01003656 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3657 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003658
Gilles Peskineee652a32018-06-01 19:23:52 +02003659 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003660
Bence Szépkúti39fb9d12022-01-13 14:33:45 +01003661 status = psa_aead_check_algorithm( alg );
Bence Szépkúti08f34652021-12-08 21:07:13 +01003662 if( status != PSA_SUCCESS )
3663 return( status );
Steven Cooremanea7ab132021-03-17 16:28:00 +01003664
Ronald Cron9a986162021-03-26 12:40:07 +01003665 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003666 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003667 if( status != PSA_SUCCESS )
3668 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003669
Ronald Cron215633c2021-03-16 17:15:37 +01003670 psa_key_attributes_t attributes = {
3671 .core = slot->attr
3672 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003673
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003674 status = psa_aead_check_nonce_length( alg, nonce_length );
3675 if( status != PSA_SUCCESS )
3676 goto exit;
3677
Ronald Cronde822812021-03-17 16:08:20 +01003678 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003679 &attributes, slot->key.data, slot->key.bytes,
3680 alg,
3681 nonce, nonce_length,
3682 additional_data, additional_data_length,
3683 ciphertext, ciphertext_length,
3684 plaintext, plaintext_size, plaintext_length );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003685
Gilles Peskineedf9a652018-08-17 18:11:56 +02003686 if( status != PSA_SUCCESS && plaintext_size != 0 )
3687 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003688
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003689exit:
Ronald Cron215633c2021-03-16 17:15:37 +01003690 psa_unlock_key_slot( slot );
3691
Gilles Peskineedf9a652018-08-17 18:11:56 +02003692 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003693}
3694
Przemek Stekiel86679c72022-10-06 17:06:56 +02003695static psa_status_t psa_validate_tag_length( psa_algorithm_t alg ) {
Przemek Stekiel8a05a642022-10-06 17:01:58 +02003696 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg );
Andrzej Kurekf8816012021-12-19 17:00:12 +01003697
3698 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
3699 {
Przemek Stekiel86679c72022-10-06 17:06:56 +02003700#if defined(PSA_WANT_ALG_CCM)
Andrzej Kurekf8816012021-12-19 17:00:12 +01003701 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
3702 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
3703 if( tag_len < 4 || tag_len > 16 || tag_len % 2 )
3704 return( PSA_ERROR_INVALID_ARGUMENT );
3705 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003706#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003707
Przemek Stekiel86679c72022-10-06 17:06:56 +02003708#if defined(PSA_WANT_ALG_GCM)
Andrzej Kurekf8816012021-12-19 17:00:12 +01003709 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
3710 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
3711 if( tag_len != 4 && tag_len != 8 && ( tag_len < 12 || tag_len > 16 ) )
3712 return( PSA_ERROR_INVALID_ARGUMENT );
3713 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003714#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003715
Przemek Stekiel86679c72022-10-06 17:06:56 +02003716#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Andrzej Kurekf8816012021-12-19 17:00:12 +01003717 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
3718 /* We only support the default tag length. */
3719 if( tag_len != 16 )
3720 return( PSA_ERROR_INVALID_ARGUMENT );
3721 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02003722#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01003723
3724 default:
3725 (void) tag_len;
3726 return( PSA_ERROR_NOT_SUPPORTED );
3727 }
3728 return( PSA_SUCCESS );
3729}
3730
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003731/* Set the key for a multipart authenticated operation. */
3732static psa_status_t psa_aead_setup( psa_aead_operation_t *operation,
Paul Elliottd9343f22021-08-23 18:59:49 +01003733 int is_encrypt,
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003734 mbedtls_svc_key_id_t key,
3735 psa_algorithm_t alg )
Paul Elliottc2b71442021-06-24 18:17:52 +01003736{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003737 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3738 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3739 psa_key_slot_t *slot = NULL;
3740 psa_key_usage_t key_usage = 0;
3741
Bence Szépkúti39fb9d12022-01-13 14:33:45 +01003742 status = psa_aead_check_algorithm( alg );
Bence Szépkúti08f34652021-12-08 21:07:13 +01003743 if( status != PSA_SUCCESS )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003744 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003745
3746 if( operation->id != 0 )
3747 {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003748 status = PSA_ERROR_BAD_STATE;
3749 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003750 }
3751
3752 if( operation->nonce_set || operation->lengths_set ||
3753 operation->ad_started || operation->body_started )
3754 {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003755 status = PSA_ERROR_BAD_STATE;
3756 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003757 }
3758
Paul Elliottd9343f22021-08-23 18:59:49 +01003759 if( is_encrypt )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003760 key_usage = PSA_KEY_USAGE_ENCRYPT;
3761 else
3762 key_usage = PSA_KEY_USAGE_DECRYPT;
3763
3764 status = psa_get_and_lock_key_slot_with_policy( key, &slot, key_usage,
3765 alg );
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003766 if( status != PSA_SUCCESS )
3767 goto exit;
3768
3769 psa_key_attributes_t attributes = {
3770 .core = slot->attr
3771 };
3772
Przemek Stekiel6ab50762022-10-08 17:54:30 +02003773 if( ( status = psa_validate_tag_length( alg ) ) != PSA_SUCCESS )
3774 goto exit;
3775
Paul Elliottd9343f22021-08-23 18:59:49 +01003776 if( is_encrypt )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003777 status = psa_driver_wrapper_aead_encrypt_setup( operation,
3778 &attributes,
3779 slot->key.data,
3780 slot->key.bytes,
3781 alg );
3782 else
3783 status = psa_driver_wrapper_aead_decrypt_setup( operation,
3784 &attributes,
3785 slot->key.data,
3786 slot->key.bytes,
3787 alg );
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003788 if( status != PSA_SUCCESS )
3789 goto exit;
3790
3791 operation->key_type = psa_get_key_type( &attributes );
3792
3793exit:
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003794 unlock_status = psa_unlock_key_slot( slot );
3795
3796 if( status == PSA_SUCCESS )
3797 {
3798 status = unlock_status;
3799 operation->alg = psa_aead_get_base_algorithm( alg );
Paul Elliottd9343f22021-08-23 18:59:49 +01003800 operation->is_encrypt = is_encrypt;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003801 }
3802 else
3803 psa_aead_abort( operation );
3804
3805 return( status );
Paul Elliottc2b71442021-06-24 18:17:52 +01003806}
3807
Paul Elliott302ff6b2021-04-20 18:10:30 +01003808/* Set the key for a multipart authenticated encryption operation. */
3809psa_status_t psa_aead_encrypt_setup( psa_aead_operation_t *operation,
3810 mbedtls_svc_key_id_t key,
3811 psa_algorithm_t alg )
3812{
Paul Elliottd9343f22021-08-23 18:59:49 +01003813 return( psa_aead_setup( operation, 1, key, alg ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003814}
3815
3816/* Set the key for a multipart authenticated decryption operation. */
3817psa_status_t psa_aead_decrypt_setup( psa_aead_operation_t *operation,
3818 mbedtls_svc_key_id_t key,
3819 psa_algorithm_t alg )
3820{
Paul Elliottd9343f22021-08-23 18:59:49 +01003821 return( psa_aead_setup( operation, 0, key, alg ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003822}
3823
3824/* Generate a random nonce / IV for multipart AEAD operation */
3825psa_status_t psa_aead_generate_nonce( psa_aead_operation_t *operation,
3826 uint8_t *nonce,
3827 size_t nonce_size,
3828 size_t *nonce_length )
3829{
3830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01003831 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01003832 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003833
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003834 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003835
Paul Elliottcee785c2021-05-20 14:29:20 +01003836 if( operation->id == 0 )
3837 {
3838 status = PSA_ERROR_BAD_STATE;
3839 goto exit;
3840 }
3841
Paul Elliottdaf5c892021-08-25 16:24:58 +01003842 if( operation->nonce_set || !operation->is_encrypt )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003843 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003844 status = PSA_ERROR_BAD_STATE;
3845 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003846 }
3847
Paul Elliotte193ea82021-10-01 13:00:16 +01003848 /* For CCM, this size may not be correct according to the PSA
3849 * specification. The PSA Crypto 1.0.1 specification states:
3850 *
3851 * CCM encodes the plaintext length pLen in L octets, with L the smallest
3852 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
3853 *
3854 * However this restriction that L has to be the smallest integer is not
3855 * applied in practice, and it is not implementable here since the
3856 * plaintext length may or may not be known at this time. */
Paul Elliottbbe90b52021-05-11 22:22:42 +01003857 required_nonce_size = PSA_AEAD_NONCE_LENGTH( operation->key_type,
Paul Elliott3242f6c2021-08-25 16:33:47 +01003858 operation->alg );
Paul Elliott39dc6b82021-05-11 19:16:09 +01003859 if( nonce_size < required_nonce_size )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003860 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003861 status = PSA_ERROR_BUFFER_TOO_SMALL;
3862 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003863 }
3864
Ronald Croncae59092021-11-26 18:53:58 +01003865 status = psa_generate_random( local_nonce, required_nonce_size );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003866 if( status != PSA_SUCCESS )
Paul Elliott39dc6b82021-05-11 19:16:09 +01003867 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003868
Ronald Croncae59092021-11-26 18:53:58 +01003869 status = psa_aead_set_nonce( operation, local_nonce, required_nonce_size );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003870
Paul Elliott39dc6b82021-05-11 19:16:09 +01003871exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01003872 if( status == PSA_SUCCESS )
Ronald Croncae59092021-11-26 18:53:58 +01003873 {
3874 memcpy( nonce, local_nonce, required_nonce_size );
Paul Elliott39dc6b82021-05-11 19:16:09 +01003875 *nonce_length = required_nonce_size;
Ronald Croncae59092021-11-26 18:53:58 +01003876 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01003877 else
3878 psa_aead_abort( operation );
3879
3880 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003881}
3882
3883/* Set the nonce for a multipart authenticated encryption or decryption
3884 operation.*/
3885psa_status_t psa_aead_set_nonce( psa_aead_operation_t *operation,
3886 const uint8_t *nonce,
3887 size_t nonce_length )
3888{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003889 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3890
Paul Elliottcee785c2021-05-20 14:29:20 +01003891 if( operation->id == 0 )
3892 {
3893 status = PSA_ERROR_BAD_STATE;
3894 goto exit;
3895 }
3896
Paul Elliott3d7d52c2021-09-01 10:33:14 +01003897 if( operation->nonce_set )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003898 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003899 status = PSA_ERROR_BAD_STATE;
3900 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003901 }
3902
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003903 status = psa_aead_check_nonce_length( operation->alg, nonce_length );
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003904 if( status != PSA_SUCCESS )
Paul Elliott4ed1ed12021-09-27 18:09:28 +01003905 {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003906 status = PSA_ERROR_INVALID_ARGUMENT;
3907 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01003908 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01003909
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003910 status = psa_driver_wrapper_aead_set_nonce( operation, nonce,
3911 nonce_length );
3912
Paul Elliott39dc6b82021-05-11 19:16:09 +01003913exit:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003914 if( status == PSA_SUCCESS )
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003915 operation->nonce_set = 1;
Paul Elliott39dc6b82021-05-11 19:16:09 +01003916 else
3917 psa_aead_abort( operation );
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003918
3919 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003920}
3921
3922/* Declare the lengths of the message and additional data for multipart AEAD. */
3923psa_status_t psa_aead_set_lengths( psa_aead_operation_t *operation,
3924 size_t ad_length,
3925 size_t plaintext_length )
3926{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003927 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3928
Paul Elliottcee785c2021-05-20 14:29:20 +01003929 if( operation->id == 0 )
3930 {
3931 status = PSA_ERROR_BAD_STATE;
3932 goto exit;
3933 }
3934
Paul Elliott6eb95982021-05-21 17:41:41 +01003935 if( operation->lengths_set || operation->ad_started ||
Paul Elliott7f429b72021-06-24 18:08:54 +01003936 operation->body_started )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003937 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003938 status = PSA_ERROR_BAD_STATE;
3939 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003940 }
3941
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003942 switch(operation->alg)
Paul Elliott325d3742021-09-27 17:56:28 +01003943 {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003944#if defined(PSA_WANT_ALG_GCM)
3945 case PSA_ALG_GCM:
3946 /* Lengths can only be too large for GCM if size_t is bigger than 32
3947 * bits. Without the guard this code will generate warnings on 32bit
3948 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01003949#if SIZE_MAX > UINT32_MAX
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003950 if( (( uint64_t ) ad_length ) >> 61 != 0 ||
3951 (( uint64_t ) plaintext_length ) > 0xFFFFFFFE0ull )
3952 {
3953 status = PSA_ERROR_INVALID_ARGUMENT;
3954 goto exit;
3955 }
Paul Elliott325d3742021-09-27 17:56:28 +01003956#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003957 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003958#endif /* PSA_WANT_ALG_GCM */
3959#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003960 case PSA_ALG_CCM:
3961 if( ad_length > 0xFF00 )
3962 {
3963 status = PSA_ERROR_INVALID_ARGUMENT;
3964 goto exit;
3965 }
3966 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003967#endif /* PSA_WANT_ALG_CCM */
3968#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003969 case PSA_ALG_CHACHA20_POLY1305:
3970 /* No length restrictions for ChaChaPoly. */
3971 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003972#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003973 default:
3974 break;
3975 }
Paul Elliott325d3742021-09-27 17:56:28 +01003976
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003977 status = psa_driver_wrapper_aead_set_lengths( operation, ad_length,
3978 plaintext_length );
3979
Paul Elliott39dc6b82021-05-11 19:16:09 +01003980exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01003981 if( status == PSA_SUCCESS )
Paul Elliottee4ffe02021-05-20 17:25:06 +01003982 {
3983 operation->ad_remaining = ad_length;
3984 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01003985 operation->lengths_set = 1;
Paul Elliottee4ffe02021-05-20 17:25:06 +01003986 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01003987 else
3988 psa_aead_abort( operation );
3989
3990 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003991}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01003992
3993/* Pass additional data to an active multipart AEAD operation. */
Paul Elliott302ff6b2021-04-20 18:10:30 +01003994psa_status_t psa_aead_update_ad( psa_aead_operation_t *operation,
3995 const uint8_t *input,
3996 size_t input_length )
3997{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003998 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3999
Paul Elliottcee785c2021-05-20 14:29:20 +01004000 if( operation->id == 0 )
4001 {
4002 status = PSA_ERROR_BAD_STATE;
4003 goto exit;
4004 }
4005
Paul Elliott6eb95982021-05-21 17:41:41 +01004006 if( !operation->nonce_set || operation->body_started )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004007 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004008 status = PSA_ERROR_BAD_STATE;
4009 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004010 }
4011
Paul Elliottee4ffe02021-05-20 17:25:06 +01004012 if( operation->lengths_set )
4013 {
Paul Elliott6eb95982021-05-21 17:41:41 +01004014 if( operation->ad_remaining < input_length )
Paul Elliottee4ffe02021-05-20 17:25:06 +01004015 {
4016 status = PSA_ERROR_INVALID_ARGUMENT;
4017 goto exit;
4018 }
4019
4020 operation->ad_remaining -= input_length;
4021 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004022#if defined(PSA_WANT_ALG_CCM)
4023 else if( operation->alg == PSA_ALG_CCM )
4024 {
4025 status = PSA_ERROR_BAD_STATE;
4026 goto exit;
4027 }
4028#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004029
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004030 status = psa_driver_wrapper_aead_update_ad( operation, input,
4031 input_length );
4032
Paul Elliott39dc6b82021-05-11 19:16:09 +01004033exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004034 if( status == PSA_SUCCESS )
4035 operation->ad_started = 1;
4036 else
4037 psa_aead_abort( operation );
4038
4039 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004040}
4041
4042/* Encrypt or decrypt a message fragment in an active multipart AEAD
4043 operation.*/
4044psa_status_t psa_aead_update( psa_aead_operation_t *operation,
4045 const uint8_t *input,
4046 size_t input_length,
4047 uint8_t *output,
4048 size_t output_size,
4049 size_t *output_length )
4050{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004052
4053 *output_length = 0;
4054
Paul Elliottcee785c2021-05-20 14:29:20 +01004055 if( operation->id == 0 )
4056 {
4057 status = PSA_ERROR_BAD_STATE;
4058 goto exit;
4059 }
4060
Paul Elliott6eb95982021-05-21 17:41:41 +01004061 if( !operation->nonce_set )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004062 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004063 status = PSA_ERROR_BAD_STATE;
4064 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004065 }
4066
Paul Elliottee4ffe02021-05-20 17:25:06 +01004067 if( operation->lengths_set )
4068 {
4069 /* Additional data length was supplied, but not all the additional
4070 data was supplied.*/
4071 if( operation->ad_remaining != 0 )
4072 {
4073 status = PSA_ERROR_INVALID_ARGUMENT;
4074 goto exit;
4075 }
4076
4077 /* Too much data provided. */
4078 if( operation->body_remaining < input_length )
4079 {
4080 status = PSA_ERROR_INVALID_ARGUMENT;
4081 goto exit;
4082 }
4083
4084 operation->body_remaining -= input_length;
4085 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004086#if defined(PSA_WANT_ALG_CCM)
4087 else if( operation->alg == PSA_ALG_CCM )
4088 {
4089 status = PSA_ERROR_BAD_STATE;
4090 goto exit;
4091 }
4092#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004093
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004094 status = psa_driver_wrapper_aead_update( operation, input, input_length,
4095 output, output_size,
4096 output_length );
4097
Paul Elliott39dc6b82021-05-11 19:16:09 +01004098exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004099 if( status == PSA_SUCCESS )
4100 operation->body_started = 1;
4101 else
4102 psa_aead_abort( operation );
4103
4104 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004105}
4106
Paul Elliott69bf5fc2021-09-19 18:26:37 +01004107static psa_status_t psa_aead_final_checks( const psa_aead_operation_t *operation )
Paul Elliottad53dcc2021-06-23 08:50:14 +01004108{
Paul Elliott7f429b72021-06-24 18:08:54 +01004109 if( operation->id == 0 || !operation->nonce_set )
Paul Elliottad53dcc2021-06-23 08:50:14 +01004110 return( PSA_ERROR_BAD_STATE );
4111
Paul Elliotta5614442021-07-14 14:54:11 +01004112 if( operation->lengths_set && ( operation->ad_remaining != 0 ||
Paul Elliottad53dcc2021-06-23 08:50:14 +01004113 operation->body_remaining != 0 ) )
4114 return( PSA_ERROR_INVALID_ARGUMENT );
4115
4116 return( PSA_SUCCESS );
4117}
4118
Paul Elliott302ff6b2021-04-20 18:10:30 +01004119/* Finish encrypting a message in a multipart AEAD operation. */
4120psa_status_t psa_aead_finish( psa_aead_operation_t *operation,
4121 uint8_t *ciphertext,
4122 size_t ciphertext_size,
4123 size_t *ciphertext_length,
4124 uint8_t *tag,
4125 size_t tag_size,
4126 size_t *tag_length )
4127{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004128 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4129
Paul Elliott302ff6b2021-04-20 18:10:30 +01004130 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004131 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004132
Paul Elliott814fffb2021-08-16 18:20:36 +01004133 status = psa_aead_final_checks( operation );
Paul Elliottad53dcc2021-06-23 08:50:14 +01004134 if( status != PSA_SUCCESS )
4135 goto exit;
4136
Paul Elliott7f429b72021-06-24 18:08:54 +01004137 if( !operation->is_encrypt )
Paul Elliottcee785c2021-05-20 14:29:20 +01004138 {
4139 status = PSA_ERROR_BAD_STATE;
4140 goto exit;
4141 }
4142
Paul Elliott39dc6b82021-05-11 19:16:09 +01004143 status = psa_driver_wrapper_aead_finish( operation, ciphertext,
4144 ciphertext_size,
4145 ciphertext_length,
4146 tag, tag_size, tag_length );
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004147
Paul Elliott39dc6b82021-05-11 19:16:09 +01004148exit:
Paul Elliottf47b0952021-05-21 18:02:33 +01004149 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004150 * the zero tag size, make sure the tag is set to something implausible.
4151 * Even if the operation succeeds, make sure we clear the rest of the
4152 * buffer to prevent potential leakage of anything previously placed in
4153 * the same buffer.*/
Paul Elliott90fdc112021-09-22 17:15:48 +01004154 if( tag != NULL )
Paul Elliottec95cc92021-09-19 22:33:09 +01004155 {
4156 if( status != PSA_SUCCESS )
4157 memset( tag, '!', tag_size );
4158 else if( *tag_length < tag_size )
4159 memset( tag + *tag_length, '!', ( tag_size - *tag_length ) );
4160 }
Paul Elliottf47b0952021-05-21 18:02:33 +01004161
Paul Elliott39dc6b82021-05-11 19:16:09 +01004162 psa_aead_abort( operation );
4163
4164 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004165}
4166
4167/* Finish authenticating and decrypting a message in a multipart AEAD
4168 operation.*/
4169psa_status_t psa_aead_verify( psa_aead_operation_t *operation,
4170 uint8_t *plaintext,
4171 size_t plaintext_size,
4172 size_t *plaintext_length,
4173 const uint8_t *tag,
4174 size_t tag_length )
4175{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004176 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4177
Paul Elliott302ff6b2021-04-20 18:10:30 +01004178 *plaintext_length = 0;
4179
Paul Elliott814fffb2021-08-16 18:20:36 +01004180 status = psa_aead_final_checks( operation );
Paul Elliottad53dcc2021-06-23 08:50:14 +01004181 if( status != PSA_SUCCESS )
4182 goto exit;
4183
Paul Elliott7f429b72021-06-24 18:08:54 +01004184 if( operation->is_encrypt )
Paul Elliottcee785c2021-05-20 14:29:20 +01004185 {
4186 status = PSA_ERROR_BAD_STATE;
4187 goto exit;
4188 }
4189
Paul Elliott39dc6b82021-05-11 19:16:09 +01004190 status = psa_driver_wrapper_aead_verify( operation, plaintext,
4191 plaintext_size,
4192 plaintext_length,
4193 tag, tag_length );
4194
4195exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004196 psa_aead_abort( operation );
4197
4198 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004199}
4200
4201/* Abort an AEAD operation. */
Paul Elliottbbe90b52021-05-11 22:22:42 +01004202psa_status_t psa_aead_abort( psa_aead_operation_t *operation )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004203{
4204 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4205
4206 if( operation->id == 0 )
4207 {
4208 /* The object has (apparently) been initialized but it is not (yet)
4209 * in use. It's ok to call abort on such an object, and there's
4210 * nothing to do. */
4211 return( PSA_SUCCESS );
4212 }
4213
4214 status = psa_driver_wrapper_aead_abort( operation );
4215
Paul Elliott70618b22021-09-22 17:12:16 +01004216 memset( operation, 0, sizeof( *operation ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004217
4218 return( status );
4219}
4220
Gilles Peskinee59236f2018-01-27 23:32:46 +01004221/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004222/* Generators */
4223/****************************************************************/
4224
Przemek Stekiel69c46792022-06-10 12:59:51 +02004225#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004226 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004227 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
4228 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
John Durkop07cc04a2020-11-16 22:08:34 -08004229#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004230#endif /* At least one builtin KDF */
4231
Przemek Stekiel69c46792022-06-10 12:59:51 +02004232#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004233 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4234 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4235static psa_status_t psa_key_derivation_start_hmac(
4236 psa_mac_operation_t *operation,
4237 psa_algorithm_t hash_alg,
4238 const uint8_t *hmac_key,
4239 size_t hmac_key_length )
4240{
4241 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4243 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
4244 psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) );
4245 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
4246
Steven Cooreman72f736a2021-05-07 14:14:37 +02004247 operation->is_sign = 1;
4248 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
4249
Steven Cooreman094a77e2021-05-06 17:58:36 +02004250 status = psa_driver_wrapper_mac_sign_setup( operation,
4251 &attributes,
4252 hmac_key, hmac_key_length,
4253 PSA_ALG_HMAC( hash_alg ) );
4254
4255 psa_reset_key_attributes( &attributes );
4256 return( status );
4257}
4258#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004259
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004260#define HKDF_STATE_INIT 0 /* no input yet */
4261#define HKDF_STATE_STARTED 1 /* got salt */
4262#define HKDF_STATE_KEYED 2 /* got key */
4263#define HKDF_STATE_OUTPUT 3 /* output started */
4264
Gilles Peskinecbe66502019-05-16 16:59:18 +02004265static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004266 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004267{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004268 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4269 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004270 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004271 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004272}
4273
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004274psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004275{
4276 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004277 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004278 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004279 {
4280 /* The object has (apparently) been initialized but it is not
4281 * in use. It's ok to call abort on such an object, and there's
4282 * nothing to do. */
4283 }
4284 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004285#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiela29b4882022-06-02 11:37:03 +02004286 if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004287 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004288 mbedtls_free( operation->ctx.hkdf.info );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004289 status = psa_mac_abort( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004290 }
John Durkop07cc04a2020-11-16 22:08:34 -08004291 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004292#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004293#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4294 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4295 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004296 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004297 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004298 {
Steven Cooremana6df6042021-04-29 19:32:25 +02004299 if( operation->ctx.tls12_prf.secret != NULL )
4300 {
4301 mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret,
4302 operation->ctx.tls12_prf.secret_length );
4303 mbedtls_free( operation->ctx.tls12_prf.secret );
4304 }
4305
Janos Follath6a1d2622019-06-11 10:37:28 +01004306 if( operation->ctx.tls12_prf.seed != NULL )
4307 {
4308 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4309 operation->ctx.tls12_prf.seed_length );
4310 mbedtls_free( operation->ctx.tls12_prf.seed );
4311 }
4312
4313 if( operation->ctx.tls12_prf.label != NULL )
4314 {
4315 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4316 operation->ctx.tls12_prf.label_length );
4317 mbedtls_free( operation->ctx.tls12_prf.label );
4318 }
4319
Przemek Stekiele3ee2212022-04-07 14:29:56 +02004320 if( operation->ctx.tls12_prf.other_secret != NULL )
4321 {
4322 mbedtls_platform_zeroize( operation->ctx.tls12_prf.other_secret,
4323 operation->ctx.tls12_prf.other_secret_length );
4324 mbedtls_free( operation->ctx.tls12_prf.other_secret );
4325 }
4326
Steven Cooremana6df6042021-04-29 19:32:25 +02004327 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004328
4329 /* We leave the fields Ai and output_block to be erased safely by the
4330 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004331 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004332 else
John Durkop07cc04a2020-11-16 22:08:34 -08004333#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4334 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004335#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04004336 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004337 {
4338 mbedtls_platform_zeroize( operation->ctx.tls12_ecjpake_to_pms.data,
Andrzej Kurek5603efd2022-09-26 10:49:16 -04004339 sizeof( operation->ctx.tls12_ecjpake_to_pms.data ) );
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004340 }
4341 else
4342#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004343 {
4344 status = PSA_ERROR_BAD_STATE;
4345 }
Janos Follath083036a2019-06-11 10:22:26 +01004346 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004347 return( status );
4348}
4349
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004351 size_t *capacity)
4352{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004353 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004354 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004355 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004356 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004357 }
4358
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004359 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004360 return( PSA_SUCCESS );
4361}
4362
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004364 size_t capacity )
4365{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004366 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004367 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004369 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004370 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004371 return( PSA_SUCCESS );
4372}
4373
Przemek Stekiel69c46792022-06-10 12:59:51 +02004374#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004375/* Read some bytes from an HKDF-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004376static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004377 psa_algorithm_t kdf_alg,
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004378 uint8_t *output,
4379 size_t output_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004380{
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004381 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004382 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004383 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004384 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004385#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004386 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004387#else
4388 const uint8_t last_block = 0xff;
4389#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004390
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004391 if( hkdf->state < HKDF_STATE_KEYED ||
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02004392 ( !hkdf->info_set
4393#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
4394 && !PSA_ALG_IS_HKDF_EXTRACT( kdf_alg )
4395#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
4396 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004397 return( PSA_ERROR_BAD_STATE );
4398 hkdf->state = HKDF_STATE_OUTPUT;
4399
Gilles Peskinebef7f142018-07-12 17:22:21 +02004400 while( output_length != 0 )
4401 {
4402 /* Copy what remains of the current block */
4403 uint8_t n = hash_length - hkdf->offset_in_block;
4404 if( n > output_length )
4405 n = (uint8_t) output_length;
4406 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4407 output += n;
4408 output_length -= n;
4409 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004410 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004411 break;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004412 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004413 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004415 * object was corrupted or if this function is called directly
4416 * inside the library. */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004417 if( hkdf->block_number == last_block )
Gilles Peskined54931c2018-07-17 21:06:59 +02004418 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004419
4420 /* We need a new block */
4421 ++hkdf->block_number;
4422 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004423
Steven Cooreman094a77e2021-05-06 17:58:36 +02004424 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4425 hash_alg,
4426 hkdf->prk,
4427 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004428 if( status != PSA_SUCCESS )
4429 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004430
Gilles Peskinebef7f142018-07-12 17:22:21 +02004431 if( hkdf->block_number != 1 )
4432 {
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004433 status = psa_mac_update( &hkdf->hmac,
4434 hkdf->output_block,
4435 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004436 if( status != PSA_SUCCESS )
4437 return( status );
4438 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004439 status = psa_mac_update( &hkdf->hmac,
4440 hkdf->info,
4441 hkdf->info_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004442 if( status != PSA_SUCCESS )
4443 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004444 status = psa_mac_update( &hkdf->hmac,
4445 &hkdf->block_number, 1 );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004446 if( status != PSA_SUCCESS )
4447 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004448 status = psa_mac_sign_finish( &hkdf->hmac,
4449 hkdf->output_block,
4450 sizeof( hkdf->output_block ),
4451 &hmac_output_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004452 if( status != PSA_SUCCESS )
4453 return( status );
4454 }
4455
4456 return( PSA_SUCCESS );
4457}
Przemek Stekiel69c46792022-06-10 12:59:51 +02004458#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004459
John Durkop07cc04a2020-11-16 22:08:34 -08004460#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4461 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004462static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4463 psa_tls12_prf_key_derivation_t *tls12_prf,
4464 psa_algorithm_t alg )
4465{
4466 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004467 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremana6df6042021-04-29 19:32:25 +02004468 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4469 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004470 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004471
Janos Follath7742fee2019-06-17 12:58:10 +01004472 /* We can't be wanting more output after block 0xff, otherwise
4473 * the capacity check in psa_key_derivation_output_bytes() would have
4474 * prevented this call. It could happen only if the operation
4475 * object was corrupted or if this function is called directly
4476 * inside the library. */
4477 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004478 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004479
4480 /* We need a new block */
4481 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004482 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004483
4484 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4485 *
4486 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4487 *
4488 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4489 * HMAC_hash(secret, A(2) + seed) +
4490 * HMAC_hash(secret, A(3) + seed) + ...
4491 *
4492 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004493 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004494 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004495 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004496 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004497 * is currently extracted as `output_block` and where i is
4498 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004499 */
4500
Steven Cooreman094a77e2021-05-06 17:58:36 +02004501 status = psa_key_derivation_start_hmac( &hmac,
4502 hash_alg,
4503 tls12_prf->secret,
4504 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004505 if( status != PSA_SUCCESS )
4506 goto cleanup;
4507
4508 /* Calculate A(i) where i = tls12_prf->block_number. */
4509 if( tls12_prf->block_number == 1 )
4510 {
4511 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4512 * the variable seed and in this instance means it in the context of the
4513 * P_hash function, where seed = label + seed.) */
Steven Cooremana6df6042021-04-29 19:32:25 +02004514 status = psa_mac_update( &hmac,
4515 tls12_prf->label,
4516 tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004517 if( status != PSA_SUCCESS )
4518 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004519 status = psa_mac_update( &hmac,
4520 tls12_prf->seed,
4521 tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004522 if( status != PSA_SUCCESS )
4523 goto cleanup;
4524 }
4525 else
4526 {
4527 /* A(i) = HMAC_hash(secret, A(i-1)) */
Steven Cooremana6df6042021-04-29 19:32:25 +02004528 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004529 if( status != PSA_SUCCESS )
4530 goto cleanup;
4531 }
4532
Steven Cooremana6df6042021-04-29 19:32:25 +02004533 status = psa_mac_sign_finish( &hmac,
4534 tls12_prf->Ai, hash_length,
4535 &hmac_output_length );
4536 if( hmac_output_length != hash_length )
4537 status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follathea29bfb2019-06-19 12:21:20 +01004538 if( status != PSA_SUCCESS )
4539 goto cleanup;
4540
4541 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Steven Cooreman094a77e2021-05-06 17:58:36 +02004542 status = psa_key_derivation_start_hmac( &hmac,
4543 hash_alg,
4544 tls12_prf->secret,
4545 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004546 if( status != PSA_SUCCESS )
4547 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004548 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004549 if( status != PSA_SUCCESS )
4550 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004551 status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004552 if( status != PSA_SUCCESS )
4553 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004554 status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004555 if( status != PSA_SUCCESS )
4556 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004557 status = psa_mac_sign_finish( &hmac,
4558 tls12_prf->output_block, hash_length,
4559 &hmac_output_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004560 if( status != PSA_SUCCESS )
4561 goto cleanup;
4562
Janos Follath7742fee2019-06-17 12:58:10 +01004563
4564cleanup:
Steven Cooremana6df6042021-04-29 19:32:25 +02004565 cleanup_status = psa_mac_abort( &hmac );
Janos Follathea29bfb2019-06-19 12:21:20 +01004566 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4567 status = cleanup_status;
4568
4569 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004570}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004571
Janos Follath844eb0e2019-06-19 12:10:49 +01004572static psa_status_t psa_key_derivation_tls12_prf_read(
4573 psa_tls12_prf_key_derivation_t *tls12_prf,
4574 psa_algorithm_t alg,
4575 uint8_t *output,
4576 size_t output_length )
4577{
4578 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004579 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004580 psa_status_t status;
4581 uint8_t offset, length;
4582
Gilles Peskineb1edaec2021-06-11 22:41:46 +02004583 switch( tls12_prf->state )
4584 {
4585 case PSA_TLS12_PRF_STATE_LABEL_SET:
4586 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4587 break;
4588 case PSA_TLS12_PRF_STATE_OUTPUT:
4589 break;
4590 default:
4591 return( PSA_ERROR_BAD_STATE );
4592 }
4593
Janos Follath844eb0e2019-06-19 12:10:49 +01004594 while( output_length != 0 )
4595 {
4596 /* Check if we have fully processed the current block. */
4597 if( tls12_prf->left_in_block == 0 )
4598 {
4599 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4600 alg );
4601 if( status != PSA_SUCCESS )
4602 return( status );
4603
4604 continue;
4605 }
4606
4607 if( tls12_prf->left_in_block > output_length )
4608 length = (uint8_t) output_length;
4609 else
4610 length = tls12_prf->left_in_block;
4611
4612 offset = hash_length - tls12_prf->left_in_block;
4613 memcpy( output, tls12_prf->output_block + offset, length );
4614 output += length;
4615 output_length -= length;
4616 tls12_prf->left_in_block -= length;
4617 }
4618
4619 return( PSA_SUCCESS );
4620}
John Durkop07cc04a2020-11-16 22:08:34 -08004621#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4622 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004623
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004624#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
4625static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
4626 psa_tls12_ecjpake_to_pms_t *ecjpake,
4627 uint8_t *output,
4628 size_t output_length )
4629{
4630 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04004631 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004632
4633 if( output_length != 32 )
4634 return ( PSA_ERROR_INVALID_ARGUMENT );
4635
4636 status = psa_hash_compute( PSA_ALG_SHA_256, ecjpake->data,
4637 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
4638 &output_size );
4639 if( status != PSA_SUCCESS )
4640 return ( status );
4641
4642 if( output_size != output_length )
4643 return ( PSA_ERROR_GENERIC_ERROR );
4644
4645 return ( PSA_SUCCESS );
4646}
4647#endif
4648
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004649psa_status_t psa_key_derivation_output_bytes(
4650 psa_key_derivation_operation_t *operation,
4651 uint8_t *output,
4652 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004653{
4654 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004655 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004656
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004657 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004658 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004660 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004661 }
4662
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004664 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004665 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004666 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004668 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004669 goto exit;
4670 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004671 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004672 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004673 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004674 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004675 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4676 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004677 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004678 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004679 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004680 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004682
Przemek Stekiel69c46792022-06-10 12:59:51 +02004683#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiela29b4882022-06-02 11:37:03 +02004684 if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004685 {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02004686 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, kdf_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004687 output, output_length );
4688 }
Janos Follathadbec812019-06-14 11:05:39 +01004689 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004690#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004691#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4692 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004693 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004694 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004695 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004696 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Steven Cooremana6df6042021-04-29 19:32:25 +02004697 kdf_alg, output,
4698 output_length );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004699 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004700 else
John Durkop07cc04a2020-11-16 22:08:34 -08004701#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4702 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004703#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04004704 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004705 {
4706 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
4707 &operation->ctx.tls12_ecjpake_to_pms, output, output_length );
4708 }
4709 else
4710#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
4711
Gilles Peskineeab56e42018-07-12 17:12:33 +02004712 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004713 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004714 return( PSA_ERROR_BAD_STATE );
4715 }
4716
4717exit:
4718 if( status != PSA_SUCCESS )
4719 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004720 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721 * This allows us to differentiate between exhausted operations and
4722 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4723 * operations. */
4724 psa_algorithm_t alg = operation->alg;
4725 psa_key_derivation_abort( operation );
4726 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004727 memset( output, '!', output_length );
4728 }
4729 return( status );
4730}
4731
David Brown7807bf72021-02-09 16:28:23 -07004732#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004733static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4734{
4735 if( data_size >= 8 )
4736 mbedtls_des_key_set_parity( data );
4737 if( data_size >= 16 )
4738 mbedtls_des_key_set_parity( data + 8 );
4739 if( data_size >= 24 )
4740 mbedtls_des_key_set_parity( data + 16 );
4741}
David Brown7807bf72021-02-09 16:28:23 -07004742#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004743
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004744/*
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +01004745* ECC keys on a Weierstrass elliptic curve require the generation
4746* of a private key which is an integer
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004747* in the range [1, N - 1], where N is the boundary of the private key domain:
4748* N is the prime p for Diffie-Hellman, or the order of the
4749* curve’s base point for ECC.
4750*
4751* Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
4752* This function generates the private key using the following process:
4753*
4754* 1. Draw a byte string of length ceiling(m/8) bytes.
4755* 2. If m is not a multiple of 8, set the most significant
4756* (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
4757* 3. Convert the string to integer k by decoding it as a big-endian byte string.
4758* 4. If k > N - 2, discard the result and return to step 1.
4759* 5. Output k + 1 as the private key.
4760*
4761* This method allows compliance to NIST standards, specifically the methods titled
4762* Key-Pair Generation by Testing Candidates in the following publications:
4763* - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
4764* Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
4765* Diffie-Hellman keys.
4766*
4767* - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
4768* Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004769*
4770* Note: Function allocates memory for *data buffer, so given *data should be
4771* always NULL.
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004772*/
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004773#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4774 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4775 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4776 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
4777 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004778static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004779 psa_key_slot_t *slot,
4780 size_t bits,
4781 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004782 uint8_t **data
4783 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004784{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004785 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004786 mbedtls_mpi k;
4787 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004788 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004789 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004790
4791 mbedtls_mpi_init( &k );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004792 mbedtls_mpi_init( &diff_N_2 );
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004793
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004794 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
4795 slot->attr.type );
4796 mbedtls_ecp_group_id grp_id =
4797 mbedtls_ecc_group_of_psa( curve, bits, 0 );
4798
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01004799 if( grp_id == MBEDTLS_ECP_DP_NONE )
4800 {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004801 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01004802 goto cleanup;
4803 }
4804
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004805 mbedtls_ecp_group ecp_group;
Przemyslaw Stekiel65348162021-11-18 11:57:07 +01004806 mbedtls_ecp_group_init( &ecp_group );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004807
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004808 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ecp_group, grp_id ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004809
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004810 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004811 /* Let m be the bit size of N. */
4812 size_t m = ecp_group.nbits;
4813
4814 size_t m_bytes = PSA_BITS_TO_BYTES( m );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004815
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004816 /* Calculate N - 2 - it will be needed later. */
4817 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &diff_N_2, &ecp_group.N, 2 ) );
4818
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004819 /* Note: This function is always called with *data == NULL and it
4820 * allocates memory for the data buffer. */
4821 *data = mbedtls_calloc( 1, m_bytes );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004822 if( *data == NULL )
4823 {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004824 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004825 goto cleanup;
4826 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004827
Przemek Stekielc85f0912022-03-08 11:37:54 +01004828 while( key_out_of_range )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004829 {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004830 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Przemek Stekielc85f0912022-03-08 11:37:54 +01004831 if( ( status = psa_key_derivation_output_bytes( operation, *data, m_bytes ) ) != 0 )
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004832 goto cleanup;
4833
4834 /* 2. If m is not a multiple of 8 */
Przemek Stekielc85f0912022-03-08 11:37:54 +01004835 if( m % 8 != 0 )
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004836 {
4837 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004838 * (8 * ceiling(m/8) - m) bits of the first byte in
4839 * the string to zero.
4840 */
4841 uint8_t clear_bit_mask = ( 1 << ( m % 8 ) ) - 1;
4842 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004843 }
4844
4845 /* 3. Convert the string to integer k by decoding it as a
4846 * big-endian byte string.
4847 */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004848 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &k, *data, m_bytes ) );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004849
4850 /* 4. If k > N - 2, discard the result and return to step 1.
4851 * Result of comparison is returned. When it indicates error
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004852 * then this function is called again.
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004853 */
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004854 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( &diff_N_2, &k, &key_out_of_range ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004855 }
4856
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004857 /* 5. Output k + 1 as the private key. */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004858 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &k, &k, 1 ) );
4859 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &k, *data, m_bytes ) );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004860cleanup:
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004861 if( ret != 0 )
Przemyslaw Stekieldc8d7d92021-12-02 09:15:20 +01004862 status = mbedtls_to_psa_error( ret );
Przemek Stekielc85f0912022-03-08 11:37:54 +01004863 if( status != PSA_SUCCESS ) {
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004864 mbedtls_free( *data );
4865 *data = NULL;
4866 }
4867 mbedtls_mpi_free( &k );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004868 mbedtls_mpi_free( &diff_N_2 );
Przemyslaw Stekieldc8d7d92021-12-02 09:15:20 +01004869 return( status );
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004870}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004871
4872/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
4873 * is determined by the curve, and sets the mandatory bits accordingly. That is:
4874 *
4875 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
4876 * draw a 32-byte string and process it as specified in
4877 * Elliptic Curves for Security [RFC7748] §5.
4878 *
4879 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
4880 * draw a 56-byte string and process it as specified in [RFC7748] §5.
4881 *
4882 * Note: Function allocates memory for *data buffer, so given *data should be
4883 * always NULL.
4884 */
4885
4886static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
4887 size_t bits,
4888 psa_key_derivation_operation_t *operation,
4889 uint8_t **data
4890 )
4891{
4892 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004893 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004894
4895 switch( bits )
4896 {
4897 case 255:
4898 output_length = 32;
4899 break;
4900 case 448:
4901 output_length = 56;
4902 break;
4903 default:
4904 return( PSA_ERROR_INVALID_ARGUMENT );
4905 break;
4906 }
4907
4908 *data = mbedtls_calloc( 1, output_length );
4909
4910 if( *data == NULL )
4911 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4912
4913 status = psa_key_derivation_output_bytes( operation, *data, output_length );
4914
4915 if( status != PSA_SUCCESS )
4916 return status;
4917
4918 switch( bits )
4919 {
4920 case 255:
4921 (*data)[0] &= 248;
4922 (*data)[31] &= 127;
4923 (*data)[31] |= 64;
4924 break;
4925 case 448:
4926 (*data)[0] &= 252;
4927 (*data)[55] |= 128;
4928 break;
4929 default:
Przemek Stekiela81aed22022-03-01 15:13:30 +01004930 return( PSA_ERROR_CORRUPTION_DETECTED );
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004931 break;
4932 }
4933
4934 return status;
4935}
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004936#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
4937 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
4938 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4939 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
4940 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01004941
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004942static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004943 psa_key_slot_t *slot,
4944 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004946{
4947 uint8_t *data = NULL;
4948 size_t bytes = PSA_BITS_TO_BYTES( bits );
Archanad8a83dc2021-06-14 10:04:16 +05304949 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01004950 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004951
Przemek Stekieldcab6cc2022-03-01 14:22:29 +01004952 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004953 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004954
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004955#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4956 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4957 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4958 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
4959 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Przemek Stekielc85f0912022-03-08 11:37:54 +01004960 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004961 {
Przemyslaw Stekiel92481592022-01-04 10:09:46 +01004962 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type );
Przemek Stekielc85f0912022-03-08 11:37:54 +01004963 if( PSA_ECC_FAMILY_IS_WEIERSTRASS( curve ) )
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004964 {
4965 /* Weierstrass elliptic curve */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004966 status = psa_generate_derived_ecc_key_weierstrass_helper( slot, bits, operation, &data );
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01004967 if( status != PSA_SUCCESS )
4968 goto exit;
Przemyslaw Stekiel50fcc532021-11-26 10:54:52 +01004969 }
4970 else
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004971 {
4972 /* Montgomery elliptic curve */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004973 status = psa_generate_derived_ecc_key_montgomery_helper( bits, operation, &data );
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004974 if( status != PSA_SUCCESS )
4975 goto exit;
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01004976 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004977 } else
Przemek Stekiel7fc07512022-03-04 14:41:11 +01004978#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
4979 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
4980 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4981 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
4982 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004983 if( key_type_is_raw_bytes( slot->attr.type ) )
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01004984 {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004985 if( bits % 8 != 0 )
4986 return( PSA_ERROR_INVALID_ARGUMENT );
4987 data = mbedtls_calloc( 1, bytes );
4988 if( data == NULL )
4989 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4990
4991 status = psa_key_derivation_output_bytes( operation, data, bytes );
4992 if( status != PSA_SUCCESS )
4993 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07004994#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004995 if( slot->attr.type == PSA_KEY_TYPE_DES )
4996 psa_des_set_key_parity( data, bytes );
Przemek Stekielf110dc02022-03-01 14:48:05 +01004997#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01004998 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01004999 else
Przemek Stekieldcab6cc2022-03-01 14:22:29 +01005000 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Crondd04d422020-11-28 15:14:42 +01005001
Ronald Cronbf33c932020-11-28 18:06:53 +01005002 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005003 psa_key_attributes_t attributes = {
5004 .core = slot->attr
5005 };
5006
Archanad8a83dc2021-06-14 10:04:16 +05305007 if( psa_key_lifetime_is_external( attributes.core.lifetime ) )
5008 {
Archana449608b2021-09-08 15:36:05 +05305009 status = psa_driver_wrapper_get_key_buffer_size( &attributes,
5010 &storage_size );
Archanad8a83dc2021-06-14 10:04:16 +05305011 if( status != PSA_SUCCESS )
5012 goto exit;
5013 }
5014 status = psa_allocate_buffer_to_slot( slot, storage_size );
5015 if( status != PSA_SUCCESS )
5016 goto exit;
5017
Ronald Cronbf33c932020-11-28 18:06:53 +01005018 status = psa_driver_wrapper_import_key( &attributes,
5019 data, bytes,
5020 slot->key.data,
5021 slot->key.bytes,
5022 &slot->key.bytes, &bits );
5023 if( bits != slot->attr.bits )
5024 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005025
5026exit:
5027 mbedtls_free( data );
5028 return( status );
5029}
5030
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005031psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005033 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005034{
5035 psa_status_t status;
5036 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005037 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005038
Ronald Cron81709fc2020-11-14 12:10:32 +01005039 *key = MBEDTLS_SVC_KEY_ID_INIT;
5040
Gilles Peskine0f84d622019-09-12 19:03:13 +02005041 /* Reject any attempt to create a zero-length key so that we don't
5042 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5043 if( psa_get_key_bits( attributes ) == 0 )
5044 return( PSA_ERROR_INVALID_ARGUMENT );
5045
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005046 if( operation->alg == PSA_ALG_NONE )
5047 return( PSA_ERROR_BAD_STATE );
5048
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005049 if( ! operation->can_output_key )
5050 return( PSA_ERROR_NOT_PERMITTED );
5051
Ronald Cron81709fc2020-11-14 12:10:32 +01005052 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5053 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005054#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5055 if( driver != NULL )
5056 {
5057 /* Deriving a key in a secure element is not implemented yet. */
5058 status = PSA_ERROR_NOT_SUPPORTED;
5059 }
5060#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005061 if( status == PSA_SUCCESS )
5062 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005063 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005064 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005065 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005066 }
5067 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005068 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005069 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005070 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005071
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005072 return( status );
5073}
5074
Gilles Peskineeab56e42018-07-12 17:12:33 +02005075
5076
5077/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005078/* Key derivation */
5079/****************************************************************/
5080
Steven Cooreman932ffb72021-02-15 12:14:32 +01005081#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005082static int is_kdf_alg_supported( psa_algorithm_t kdf_alg )
5083{
5084#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
5085 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5086 return( 1 );
5087#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005088#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
5089 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5090 return( 1 );
5091#endif
5092#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
5093 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005094 return( 1 );
5095#endif
5096#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5097 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5098 return( 1 );
5099#endif
5100#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5101 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5102 return( 1 );
5103#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005104#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005105 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005106 return( 1 );
5107#endif
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005108 return( 0 );
5109}
5110
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005111static psa_status_t psa_hash_try_support( psa_algorithm_t alg )
5112{
5113 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
5114 psa_status_t status = psa_hash_setup( &operation, alg );
5115 psa_hash_abort( &operation );
5116 return( status );
5117}
5118
Gilles Peskine969c5d62019-01-16 15:53:06 +01005119static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005120 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005121 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005122{
Janos Follath5fe19732019-06-20 15:09:30 +01005123 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5124 * initialisers for this union leave some bytes unspecified.) */
5125 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5126
Gilles Peskine969c5d62019-01-16 15:53:06 +01005127 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005128 if( ! is_kdf_alg_supported( kdf_alg ) )
5129 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop07cc04a2020-11-16 22:08:34 -08005130
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005131 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04005132 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005133 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
5134 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005135 if( kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005136 {
5137 if( hash_size == 0 )
5138 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005139
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005140 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5141 * we might fail later, which is somewhat unfriendly and potentially
5142 * risk-prone. */
5143 psa_status_t status = psa_hash_try_support( hash_alg );
5144 if( status != PSA_SUCCESS )
5145 return( status );
5146 }
5147 else
5148 {
5149 hash_size = PSA_HASH_LENGTH( PSA_ALG_SHA_256 );
5150 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005151
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005152 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5153 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
5154 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005155 {
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005156 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005157 }
Andrzej Kurek77638292022-09-16 12:24:52 -04005158#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005159 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurekb0936502022-09-16 07:13:00 -04005160 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ||
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005161 ( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) )
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005162 operation->capacity = hash_size;
5163 else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04005164#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
5165 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005166 operation->capacity = 255 * hash_size;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005167 return( PSA_SUCCESS );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005168}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005169
5170static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg )
5171{
5172#if defined(PSA_WANT_ALG_ECDH)
5173 if( alg == PSA_ALG_ECDH )
5174 return( PSA_SUCCESS );
5175#endif
5176 (void) alg;
5177 return( PSA_ERROR_NOT_SUPPORTED );
5178}
John Durkop07cc04a2020-11-16 22:08:34 -08005179#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005180
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005181psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005182 psa_algorithm_t alg )
5183{
5184 psa_status_t status;
5185
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005186 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005187 return( PSA_ERROR_BAD_STATE );
5188
Gilles Peskine6843c292019-01-18 16:44:49 +01005189 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5190 return( PSA_ERROR_INVALID_ARGUMENT );
5191 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005192 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005193#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005194 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine0c3a0712021-04-29 21:34:33 +02005195 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg );
5196 status = psa_key_agreement_try_support( ka_alg );
5197 if( status != PSA_SUCCESS )
5198 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005200#else
5201 return( PSA_ERROR_NOT_SUPPORTED );
5202#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005203 }
5204 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5205 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005206#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005208#else
5209 return( PSA_ERROR_NOT_SUPPORTED );
5210#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005211 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005212 else
5213 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005214
5215 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005217 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005218}
5219
Przemek Stekiel69c46792022-06-10 12:59:51 +02005220#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005221static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005222 psa_algorithm_t kdf_alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005223 psa_key_derivation_step_t step,
5224 const uint8_t *data,
5225 size_t data_length )
5226{
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005227 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005228 psa_status_t status;
5229 switch( step )
5230 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005231 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005232#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005233 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
5234 return( PSA_ERROR_INVALID_ARGUMENT );
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005235#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine2b522db2019-04-12 15:11:49 +02005236 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005237 return( PSA_ERROR_BAD_STATE );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005238 else
5239 {
Steven Cooreman094a77e2021-05-06 17:58:36 +02005240 status = psa_key_derivation_start_hmac( &hkdf->hmac,
5241 hash_alg,
5242 data, data_length );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005243 if( status != PSA_SUCCESS )
5244 return( status );
5245 hkdf->state = HKDF_STATE_STARTED;
5246 return( PSA_SUCCESS );
5247 }
Gilles Peskine03410b52019-05-16 16:05:19 +02005248 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005249#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005250 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005251 {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005252 /* We shouldn't be in different state as HKDF_EXPAND only allows
5253 * two inputs: SECRET (this case) and INFO which does not modify
5254 * the state. It could happen only if the hkdf
5255 * object was corrupted. */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005256 if( hkdf->state != HKDF_STATE_INIT )
5257 return( PSA_ERROR_BAD_STATE );
5258
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02005259 /* Allow only input that fits expected prk size */
5260 if( data_length != PSA_HASH_LENGTH( hash_alg ) )
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005261 return( PSA_ERROR_INVALID_ARGUMENT );
5262
5263 memcpy( hkdf->prk, data, data_length );
5264 }
5265 else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005266#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005267 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005268 /* HKDF: If no salt was provided, use an empty salt.
5269 * HKDF-EXTRACT: salt is mandatory. */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005270 if( hkdf->state == HKDF_STATE_INIT )
5271 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02005272#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
5273 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5274 return( PSA_ERROR_BAD_STATE );
5275#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005276 status = psa_key_derivation_start_hmac( &hkdf->hmac,
5277 hash_alg,
5278 NULL, 0 );
5279 if( status != PSA_SUCCESS )
5280 return( status );
5281 hkdf->state = HKDF_STATE_STARTED;
5282 }
5283 if( hkdf->state != HKDF_STATE_STARTED )
5284 return( PSA_ERROR_BAD_STATE );
5285 status = psa_mac_update( &hkdf->hmac,
5286 data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005287 if( status != PSA_SUCCESS )
5288 return( status );
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005289 status = psa_mac_sign_finish( &hkdf->hmac,
5290 hkdf->prk,
5291 sizeof( hkdf->prk ),
5292 &data_length );
5293 if( status != PSA_SUCCESS )
5294 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005295 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005296
Gilles Peskine2b522db2019-04-12 15:11:49 +02005297 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005298 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005299#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005300 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5301 {
5302 /* The only block of output is the PRK. */
5303 memcpy( hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH( hash_alg ) );
5304 hkdf->offset_in_block = 0;
5305 }
5306 else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005307#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005308 {
5309 /* Block 0 is empty, and the next block will be
5310 * generated by psa_key_derivation_hkdf_read(). */
5311 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
5312 }
5313
Gilles Peskine2b522db2019-04-12 15:11:49 +02005314 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005315 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005316#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005317 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
5318 return( PSA_ERROR_INVALID_ARGUMENT );
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005319#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02005320#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
5321 if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) &&
5322 hkdf->state == HKDF_STATE_INIT )
5323 return( PSA_ERROR_BAD_STATE );
5324#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005325 if( hkdf->state == HKDF_STATE_OUTPUT )
5326 return( PSA_ERROR_BAD_STATE );
5327 if( hkdf->info_set )
5328 return( PSA_ERROR_BAD_STATE );
5329 hkdf->info_length = data_length;
5330 if( data_length != 0 )
5331 {
5332 hkdf->info = mbedtls_calloc( 1, data_length );
5333 if( hkdf->info == NULL )
5334 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5335 memcpy( hkdf->info, data, data_length );
5336 }
5337 hkdf->info_set = 1;
5338 return( PSA_SUCCESS );
5339 default:
5340 return( PSA_ERROR_INVALID_ARGUMENT );
5341 }
5342}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005343#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005344
John Durkop07cc04a2020-11-16 22:08:34 -08005345#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5346 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005347static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5348 const uint8_t *data,
5349 size_t data_length )
5350{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005351 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005352 return( PSA_ERROR_BAD_STATE );
5353
Janos Follathd6dce9f2019-07-04 09:11:38 +01005354 if( data_length != 0 )
5355 {
5356 prf->seed = mbedtls_calloc( 1, data_length );
5357 if( prf->seed == NULL )
5358 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005359
Janos Follathd6dce9f2019-07-04 09:11:38 +01005360 memcpy( prf->seed, data, data_length );
5361 prf->seed_length = data_length;
5362 }
Janos Follathf08e2652019-06-13 09:05:41 +01005363
Gilles Peskine43f958b2020-12-13 14:55:14 +01005364 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005365
5366 return( PSA_SUCCESS );
5367}
5368
Janos Follath81550542019-06-13 14:26:34 +01005369static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
Janos Follath81550542019-06-13 14:26:34 +01005370 const uint8_t *data,
5371 size_t data_length )
5372{
Przemek Stekield7a28642022-04-07 14:58:33 +02005373 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
5374 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET )
Janos Follath81550542019-06-13 14:26:34 +01005375 return( PSA_ERROR_BAD_STATE );
5376
Steven Cooremana6df6042021-04-29 19:32:25 +02005377 if( data_length != 0 )
5378 {
5379 prf->secret = mbedtls_calloc( 1, data_length );
5380 if( prf->secret == NULL )
5381 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5382
5383 memcpy( prf->secret, data, data_length );
5384 prf->secret_length = data_length;
5385 }
Janos Follath81550542019-06-13 14:26:34 +01005386
Gilles Peskine43f958b2020-12-13 14:55:14 +01005387 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005388
5389 return( PSA_SUCCESS );
5390}
5391
Janos Follath63028dd2019-06-13 09:15:47 +01005392static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5393 const uint8_t *data,
5394 size_t data_length )
5395{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005396 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005397 return( PSA_ERROR_BAD_STATE );
5398
Janos Follathd6dce9f2019-07-04 09:11:38 +01005399 if( data_length != 0 )
5400 {
5401 prf->label = mbedtls_calloc( 1, data_length );
5402 if( prf->label == NULL )
5403 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005404
Janos Follathd6dce9f2019-07-04 09:11:38 +01005405 memcpy( prf->label, data, data_length );
5406 prf->label_length = data_length;
5407 }
Janos Follath63028dd2019-06-13 09:15:47 +01005408
Gilles Peskine43f958b2020-12-13 14:55:14 +01005409 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005410
5411 return( PSA_SUCCESS );
5412}
5413
Janos Follathb03233e2019-06-11 15:30:30 +01005414static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
Janos Follathb03233e2019-06-11 15:30:30 +01005415 psa_key_derivation_step_t step,
5416 const uint8_t *data,
5417 size_t data_length )
5418{
Janos Follathb03233e2019-06-11 15:30:30 +01005419 switch( step )
5420 {
Janos Follathf08e2652019-06-13 09:05:41 +01005421 case PSA_KEY_DERIVATION_INPUT_SEED:
5422 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005423 case PSA_KEY_DERIVATION_INPUT_SECRET:
Steven Cooremana6df6042021-04-29 19:32:25 +02005424 return( psa_tls12_prf_set_key( prf, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005425 case PSA_KEY_DERIVATION_INPUT_LABEL:
5426 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005427 default:
5428 return( PSA_ERROR_INVALID_ARGUMENT );
5429 }
5430}
John Durkop07cc04a2020-11-16 22:08:34 -08005431#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5432 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5433
5434#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5435static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5436 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005437 const uint8_t *data,
5438 size_t data_length )
5439{
5440 psa_status_t status;
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005441 const size_t pms_len = ( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
5442 4 + data_length + prf->other_secret_length :
5443 4 + 2 * data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08005444
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005445 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005446 return( PSA_ERROR_INVALID_ARGUMENT );
5447
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005448 uint8_t *pms = mbedtls_calloc( 1, pms_len );
Przemek Stekiel937b90f2022-04-20 08:33:13 +02005449 if( pms == NULL )
5450 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005451 uint8_t *cur = pms;
5452
5453 /* pure-PSK:
5454 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08005455 *
5456 * The premaster secret is formed as follows: if the PSK is N octets
5457 * long, concatenate a uint16 with the value N, N zero octets, a second
5458 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005459 *
5460 * mixed-PSK:
5461 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
5462 * follows: concatenate a uint16 with the length of the other secret,
5463 * the other secret itself, uint16 with the length of PSK, and the
5464 * PSK itself.
5465 * For details please check:
5466 * - RFC 4279, Section 4 for the definition of RSA-PSK,
5467 * - RFC 4279, Section 3 for the definition of DHE-PSK,
5468 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08005469 */
5470
Przemek Stekiel4e47a912022-04-21 11:40:18 +02005471 if( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET )
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005472 {
5473 *cur++ = MBEDTLS_BYTE_1( prf->other_secret_length );
5474 *cur++ = MBEDTLS_BYTE_0( prf->other_secret_length );
Przemek Stekiel4e47a912022-04-21 11:40:18 +02005475 if( prf->other_secret_length != 0 )
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02005476 {
5477 memcpy( cur, prf->other_secret, prf->other_secret_length );
Przemek Stekiel03faf5d22022-04-20 08:37:43 +02005478 mbedtls_platform_zeroize( prf->other_secret, prf->other_secret_length );
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02005479 cur += prf->other_secret_length;
5480 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005481 }
5482 else
5483 {
5484 *cur++ = MBEDTLS_BYTE_1( data_length );
5485 *cur++ = MBEDTLS_BYTE_0( data_length );
5486 memset( cur, 0, data_length );
5487 cur += data_length;
5488 }
5489
Joe Subbiani5ecac212021-06-24 13:00:03 +01005490 *cur++ = MBEDTLS_BYTE_1( data_length );
5491 *cur++ = MBEDTLS_BYTE_0( data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08005492 memcpy( cur, data, data_length );
5493 cur += data_length;
5494
Steven Cooremana6df6042021-04-29 19:32:25 +02005495 status = psa_tls12_prf_set_key( prf, pms, cur - pms );
John Durkop07cc04a2020-11-16 22:08:34 -08005496
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02005497 mbedtls_platform_zeroize( pms, pms_len );
5498 mbedtls_free( pms );
John Durkop07cc04a2020-11-16 22:08:34 -08005499 return( status );
5500}
Janos Follath6660f0e2019-06-17 08:44:03 +01005501
Przemek Stekiele47201b2022-04-19 13:53:28 +02005502static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
5503 psa_tls12_prf_key_derivation_t *prf,
5504 const uint8_t *data,
5505 size_t data_length )
5506{
5507 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
5508 return( PSA_ERROR_BAD_STATE );
5509
5510 if( data_length != 0 )
5511 {
5512 prf->other_secret = mbedtls_calloc( 1, data_length );
5513 if( prf->other_secret == NULL )
5514 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5515
5516 memcpy( prf->other_secret, data, data_length );
5517 prf->other_secret_length = data_length;
5518 }
5519 else
5520 {
5521 prf->other_secret_length = 0;
5522 }
5523
5524 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
5525
5526 return( PSA_SUCCESS );
5527}
5528
Janos Follath6660f0e2019-06-17 08:44:03 +01005529static psa_status_t psa_tls12_prf_psk_to_ms_input(
5530 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005531 psa_key_derivation_step_t step,
5532 const uint8_t *data,
5533 size_t data_length )
5534{
Przemek Stekiele47201b2022-04-19 13:53:28 +02005535 switch( step )
Janos Follath0c1ed842019-06-28 13:35:36 +01005536 {
Przemek Stekiele47201b2022-04-19 13:53:28 +02005537 case PSA_KEY_DERIVATION_INPUT_SECRET:
5538 return( psa_tls12_prf_psk_to_ms_set_key( prf,
5539 data, data_length ) );
5540 break;
5541 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
5542 return( psa_tls12_prf_psk_to_ms_set_other_key( prf,
5543 data,
5544 data_length ) );
5545 break;
5546 default:
5547 return( psa_tls12_prf_input( prf, step, data, data_length ) );
5548 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01005549
Przemek Stekiele47201b2022-04-19 13:53:28 +02005550 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005551}
John Durkop07cc04a2020-11-16 22:08:34 -08005552#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005553
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005554#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5555static psa_status_t psa_tls12_ecjpake_to_pms_input(
5556 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04005557 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005558 const uint8_t *data,
5559 size_t data_length )
5560{
Andrzej Kurek702776f2022-09-16 06:22:44 -04005561 if( data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
5562 step != PSA_KEY_DERIVATION_INPUT_SECRET )
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005563 {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005564 return( PSA_ERROR_INVALID_ARGUMENT );
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005565 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005566
5567 /* Check if the passed point is in an uncompressed form */
5568 if( data[0] != 0x04 )
5569 return( PSA_ERROR_INVALID_ARGUMENT );
5570
5571 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
5572 memcpy( ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
5573
5574 return( PSA_SUCCESS );
5575}
5576#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb8965192019-09-24 16:21:10 +02005577/** Check whether the given key type is acceptable for the given
5578 * input step of a key derivation.
5579 *
5580 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5581 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5582 * Both secret and non-secret inputs can alternatively have the type
5583 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5584 * that the input was passed as a buffer rather than via a key object.
5585 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005586static int psa_key_derivation_check_input_type(
5587 psa_key_derivation_step_t step,
5588 psa_key_type_t key_type )
5589{
5590 switch( step )
5591 {
5592 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005593 if( key_type == PSA_KEY_TYPE_DERIVE )
5594 return( PSA_SUCCESS );
5595 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005596 return( PSA_SUCCESS );
5597 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02005598 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
5599 if( key_type == PSA_KEY_TYPE_DERIVE )
5600 return( PSA_SUCCESS );
5601 if( key_type == PSA_KEY_TYPE_NONE )
5602 return( PSA_SUCCESS );
5603 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005604 case PSA_KEY_DERIVATION_INPUT_LABEL:
5605 case PSA_KEY_DERIVATION_INPUT_SALT:
5606 case PSA_KEY_DERIVATION_INPUT_INFO:
5607 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005608 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5609 return( PSA_SUCCESS );
5610 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005611 return( PSA_SUCCESS );
5612 break;
5613 }
5614 return( PSA_ERROR_INVALID_ARGUMENT );
5615}
5616
Janos Follathb80a94e2019-06-12 15:54:46 +01005617static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005618 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005619 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005620 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005621 const uint8_t *data,
5622 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005623{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005624 psa_status_t status;
5625 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5626
5627 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005628 if( status != PSA_SUCCESS )
5629 goto exit;
5630
Przemek Stekiel69c46792022-06-10 12:59:51 +02005631#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiela29b4882022-06-02 11:37:03 +02005632 if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005633 {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02005634 status = psa_hkdf_input( &operation->ctx.hkdf, kdf_alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005635 step, data, data_length );
5636 }
John Durkop07cc04a2020-11-16 22:08:34 -08005637 else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005638#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005639#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5640 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005641 {
Janos Follathb03233e2019-06-11 15:30:30 +01005642 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
Janos Follathb03233e2019-06-11 15:30:30 +01005643 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005644 }
John Durkop07cc04a2020-11-16 22:08:34 -08005645 else
5646#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5647#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5648 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005649 {
5650 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005651 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005652 }
5653 else
John Durkop07cc04a2020-11-16 22:08:34 -08005654#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005655#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Andrzej Kurek3c4c5142022-09-16 07:24:14 -04005656 if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS )
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005657 {
5658 status = psa_tls12_ecjpake_to_pms_input(
Andrzej Kurek702776f2022-09-16 06:22:44 -04005659 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length );
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005660 }
5661 else
5662#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005663 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005664 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005665 (void) data;
5666 (void) data_length;
5667 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005668 return( PSA_ERROR_BAD_STATE );
5669 }
5670
Gilles Peskine224b0d62019-09-23 18:13:17 +02005671exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005672 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005673 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005674 return( status );
5675}
5676
Janos Follath51f4a0f2019-06-14 11:35:55 +01005677psa_status_t psa_key_derivation_input_bytes(
5678 psa_key_derivation_operation_t *operation,
5679 psa_key_derivation_step_t step,
5680 const uint8_t *data,
5681 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005682{
Gilles Peskineb8965192019-09-24 16:21:10 +02005683 return( psa_key_derivation_input_internal( operation, step,
5684 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005685 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005686}
5687
Janos Follath51f4a0f2019-06-14 11:35:55 +01005688psa_status_t psa_key_derivation_input_key(
5689 psa_key_derivation_operation_t *operation,
5690 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005691 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005692{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005693 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005694 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005695 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005696
Ronald Cron5c522922020-11-14 16:35:34 +01005697 status = psa_get_and_lock_transparent_key_slot_with_policy(
5698 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005699 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005700 {
5701 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005702 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005703 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005704
5705 /* Passing a key object as a SECRET input unlocks the permission
5706 * to output to a key object. */
5707 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5708 operation->can_output_key = 1;
5709
Ronald Cronf95a2b12020-10-22 15:24:49 +02005710 status = psa_key_derivation_input_internal( operation,
5711 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005712 slot->key.data,
5713 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005714
Ronald Cron5c522922020-11-14 16:35:34 +01005715 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005716
Ronald Cron5c522922020-11-14 16:35:34 +01005717 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005718}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005719
5720
5721
5722/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005723/* Key agreement */
5724/****************************************************************/
5725
John Durkop6ba40d12020-11-10 08:50:04 -08005726#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005727static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5728 size_t peer_key_length,
5729 const mbedtls_ecp_keypair *our_key,
5730 uint8_t *shared_secret,
5731 size_t shared_secret_size,
5732 size_t *shared_secret_length )
5733{
Steven Cooremand4867872020-08-05 16:31:39 +02005734 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005735 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005736 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005737 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005738 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005739 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005740
Ronald Cron90857082020-11-25 14:58:33 +01005741 status = mbedtls_psa_ecp_load_representation(
5742 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005743 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005744 peer_key,
5745 peer_key_length,
5746 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005747 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005748 goto exit;
5749
Jaeden Amero97271b32019-01-10 19:38:51 +00005750 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005751 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005752 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005753 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005754 status = mbedtls_to_psa_error(
5755 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5756 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005757 goto exit;
5758
Jaeden Amero97271b32019-01-10 19:38:51 +00005759 status = mbedtls_to_psa_error(
5760 mbedtls_ecdh_calc_secret( &ecdh,
5761 shared_secret_length,
5762 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005763 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005764 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005765 if( status != PSA_SUCCESS )
5766 goto exit;
5767 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5768 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005769
5770exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005771 if( status != PSA_SUCCESS )
5772 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005773 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005774 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005775 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005776
Jaeden Amero97271b32019-01-10 19:38:51 +00005777 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005778}
John Durkop6ba40d12020-11-10 08:50:04 -08005779#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005780
Gilles Peskine01d718c2018-09-18 12:01:02 +02005781#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5782
Gilles Peskine0216fe12019-04-11 21:23:21 +02005783static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5784 psa_key_slot_t *private_key,
5785 const uint8_t *peer_key,
5786 size_t peer_key_length,
5787 uint8_t *shared_secret,
5788 size_t shared_secret_size,
5789 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005790{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005791 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005792 {
John Durkop6ba40d12020-11-10 08:50:04 -08005793#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005794 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005795 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005796 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005797 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005798 psa_status_t status = mbedtls_psa_ecp_load_representation(
5799 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005800 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005801 private_key->key.data,
5802 private_key->key.bytes,
5803 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005804 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005805 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005806 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005807 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005808 shared_secret, shared_secret_size,
5809 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005810 mbedtls_ecp_keypair_free( ecp );
5811 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005812 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005813#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005814 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005815 (void) private_key;
5816 (void) peer_key;
5817 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005818 (void) shared_secret;
5819 (void) shared_secret_size;
5820 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005821 return( PSA_ERROR_NOT_SUPPORTED );
5822 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005823}
5824
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005825/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005826 * to potentially free embedded data structures and wipe confidential data.
5827 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005828static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005829 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005830 psa_key_slot_t *private_key,
5831 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005832 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005833{
5834 psa_status_t status;
5835 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5836 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005837 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005838
5839 /* Step 1: run the secret agreement algorithm to generate the shared
5840 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005841 status = psa_key_agreement_raw_internal( ka_alg,
5842 private_key,
5843 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005844 shared_secret,
5845 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005846 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005847 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005848 goto exit;
5849
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005850 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005851 * the shared secret. A shared secret is permitted wherever a key
5852 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005853 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005854 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005855 shared_secret,
5856 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005857exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005858 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005859 return( status );
5860}
5861
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005862psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005863 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005864 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005865 const uint8_t *peer_key,
5866 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005867{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005868 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005869 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005870 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005871
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005872 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005873 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005874 status = psa_get_and_lock_transparent_key_slot_with_policy(
5875 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005876 if( status != PSA_SUCCESS )
5877 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005878 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005879 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005880 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005881 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005882 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005883 else
5884 {
5885 /* If a private key has been added as SECRET, we allow the derived
5886 * key material to be used as a key in PSA Crypto. */
5887 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5888 operation->can_output_key = 1;
5889 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005890
Ronald Cron5c522922020-11-14 16:35:34 +01005891 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005892
Ronald Cron5c522922020-11-14 16:35:34 +01005893 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005894}
5895
Gilles Peskinebe697d82019-05-16 18:00:41 +02005896psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005897 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005898 const uint8_t *peer_key,
5899 size_t peer_key_length,
5900 uint8_t *output,
5901 size_t output_size,
5902 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005903{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005904 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005905 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005906 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005907
5908 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5909 {
5910 status = PSA_ERROR_INVALID_ARGUMENT;
5911 goto exit;
5912 }
Ronald Cron5c522922020-11-14 16:35:34 +01005913 status = psa_get_and_lock_transparent_key_slot_with_policy(
5914 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005915 if( status != PSA_SUCCESS )
5916 goto exit;
5917
Gilles Peskine3e561302022-04-14 00:17:15 +02005918 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5919 * for the output size. The PSA specification only guarantees that this
5920 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5921 * but it might be nice to allow smaller buffers if the output fits.
5922 * At the time of writing this comment, with only ECDH implemented,
5923 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5924 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5925 * be exact for it as well. */
5926 size_t expected_length =
5927 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits );
5928 if( output_size < expected_length )
5929 {
5930 status = PSA_ERROR_BUFFER_TOO_SMALL;
5931 goto exit;
5932 }
5933
Gilles Peskine0216fe12019-04-11 21:23:21 +02005934 status = psa_key_agreement_raw_internal( alg, slot,
5935 peer_key, peer_key_length,
5936 output, output_size,
5937 output_length );
5938
5939exit:
5940 if( status != PSA_SUCCESS )
5941 {
5942 /* If an error happens and is not handled properly, the output
5943 * may be used as a key to protect sensitive data. Arrange for such
5944 * a key to be random, which is likely to result in decryption or
5945 * verification errors. This is better than filling the buffer with
5946 * some constant data such as zeros, which would result in the data
5947 * being protected with a reproducible, easily knowable key.
5948 */
5949 psa_generate_random( output, output_size );
5950 *output_length = output_size;
5951 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005952
Ronald Cron5c522922020-11-14 16:35:34 +01005953 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005954
Ronald Cron5c522922020-11-14 16:35:34 +01005955 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005956}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005957
5958
Gilles Peskine30524eb2020-11-13 17:02:26 +01005959
Gilles Peskine86a440b2018-11-12 18:39:40 +01005960/****************************************************************/
5961/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005962/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005963
Gilles Peskine30524eb2020-11-13 17:02:26 +01005964/** Initialize the PSA random generator.
5965 */
5966static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5967{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005968#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5969 memset( rng, 0, sizeof( *rng ) );
5970#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5971
Gilles Peskine30524eb2020-11-13 17:02:26 +01005972 /* Set default configuration if
5973 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5974 if( rng->entropy_init == NULL )
5975 rng->entropy_init = mbedtls_entropy_init;
5976 if( rng->entropy_free == NULL )
5977 rng->entropy_free = mbedtls_entropy_free;
5978
5979 rng->entropy_init( &rng->entropy );
5980#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5981 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5982 /* The PSA entropy injection feature depends on using NV seed as an entropy
5983 * source. Add NV seed as an entropy source for PSA entropy injection. */
5984 mbedtls_entropy_add_source( &rng->entropy,
5985 mbedtls_nv_seed_poll, NULL,
5986 MBEDTLS_ENTROPY_BLOCK_SIZE,
5987 MBEDTLS_ENTROPY_SOURCE_STRONG );
5988#endif
5989
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005990 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005991#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005992}
5993
5994/** Deinitialize the PSA random generator.
5995 */
5996static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5997{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005998#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5999 memset( rng, 0, sizeof( *rng ) );
6000#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006001 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006002 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006003#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006004}
6005
6006/** Seed the PSA random generator.
6007 */
6008static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6009{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006010#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6011 /* Do nothing: the external RNG seeds itself. */
6012 (void) rng;
6013 return( PSA_SUCCESS );
6014#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006015 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006016 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6017 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006018 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006019#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006020}
6021
Gilles Peskinee59236f2018-01-27 23:32:46 +01006022psa_status_t psa_generate_random( uint8_t *output,
6023 size_t output_size )
6024{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006025 GUARD_MODULE_INITIALIZED;
6026
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006027#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6028
6029 size_t output_length = 0;
6030 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6031 output, output_size,
6032 &output_length );
6033 if( status != PSA_SUCCESS )
6034 return( status );
6035 /* Breaking up a request into smaller chunks is currently not supported
6036 * for the extrernal RNG interface. */
6037 if( output_length != output_size )
6038 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6039 return( PSA_SUCCESS );
6040
6041#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6042
Gilles Peskine71ddab92021-01-04 21:01:07 +01006043 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006044 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006045 size_t request_size =
6046 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6047 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6048 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006049 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6050 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006051 if( ret != 0 )
6052 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006053 output_size -= request_size;
6054 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006055 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006056 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006057#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006058}
6059
Gilles Peskine8814fc42020-12-14 15:33:44 +01006060/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006061 *
6062 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6063 * `psa_generate_random(...)`. The state parameter is ignored since the
6064 * PSA API doesn't support passing an explicit state.
6065 *
6066 * In the non-external case, psa_generate_random() calls an
6067 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6068 * and semantics as mbedtls_psa_get_random(). As an optimization,
6069 * instead of doing this back-and-forth between the PSA API and the
6070 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6071 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006072 */
6073#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6074int mbedtls_psa_get_random( void *p_rng,
6075 unsigned char *output,
6076 size_t output_size )
6077{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006078 /* This function takes a pointer to the RNG state because that's what
6079 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6080 * its own state internally and doesn't let the caller access that state.
6081 * So we just ignore the state parameter, and in practice we'll pass
6082 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006083 (void) p_rng;
6084 psa_status_t status = psa_generate_random( output, output_size );
6085 if( status == PSA_SUCCESS )
6086 return( 0 );
6087 else
6088 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6089}
6090#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6091
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006092#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00006093#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006094
Gilles Peskine7228da22019-07-15 11:06:15 +02006095psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006096 size_t seed_size )
6097{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006098 if( global_data.initialized )
6099 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006100
6101 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6102 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6103 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6104 return( PSA_ERROR_INVALID_ARGUMENT );
6105
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006106 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006107}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006108#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006109
Ronald Cron3772afe2021-02-08 16:10:05 +01006110/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006111 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006112 * \param type The key type
6113 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006114 *
6115 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006116 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006117 * \retval #PSA_ERROR_INVALID_ARGUMENT
6118 * The size in bits of the key is not valid.
6119 * \retval #PSA_ERROR_NOT_SUPPORTED
6120 * The type and/or the size in bits of the key or the combination of
6121 * the two is not supported.
6122 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006123static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6124 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006125{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006126 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006127
Gilles Peskinee59236f2018-01-27 23:32:46 +01006128 if( key_type_is_raw_bytes( type ) )
6129 {
Archana4d7ae1d2021-07-07 02:50:22 +05306130 status = psa_validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006131 if( status != PSA_SUCCESS )
6132 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006133 }
6134 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006135#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006136 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6137 {
6138 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6139 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006140
Ronald Cron01b2aba2020-10-05 09:42:02 +02006141 /* Accept only byte-aligned keys, for the same reasons as
6142 * in psa_import_rsa_key(). */
6143 if( bits % 8 != 0 )
6144 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006145 }
6146 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006147#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006148
Ronald Cron5c4d3862020-12-07 11:07:24 +01006149#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006150 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6151 {
Ronald Crond81ab562021-02-16 09:01:16 +01006152 /* To avoid empty block, return successfully here. */
6153 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006154 }
6155 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006156#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006157 {
6158 return( PSA_ERROR_NOT_SUPPORTED );
6159 }
6160
6161 return( PSA_SUCCESS );
6162}
6163
Ronald Cron55ed0592020-10-05 10:30:40 +02006164psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006165 const psa_key_attributes_t *attributes,
6166 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006167{
6168 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006169 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006170
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006171 if( ( attributes->domain_parameters == NULL ) &&
6172 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006173 return( PSA_ERROR_INVALID_ARGUMENT );
6174
Ronald Cron01b2aba2020-10-05 09:42:02 +02006175 if( key_type_is_raw_bytes( type ) )
6176 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006177 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006178 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006179 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006180
David Brown12ca5032021-02-11 11:02:00 -07006181#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006182 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006183 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006184#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006185 }
6186 else
6187
Jaeden Amero424fa932021-05-14 08:34:32 +01006188#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6189 defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006190 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006191 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006192 return( mbedtls_psa_rsa_generate_key( attributes,
6193 key_buffer,
6194 key_buffer_size,
6195 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006196 }
6197 else
Jaeden Amero424fa932021-05-14 08:34:32 +01006198#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
6199 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006200
John Durkop9814fa22020-11-04 12:28:15 -08006201#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006202 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006203 {
Ronald Cron7023db52020-11-20 18:17:42 +01006204 return( mbedtls_psa_ecp_generate_key( attributes,
6205 key_buffer,
6206 key_buffer_size,
6207 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006208 }
6209 else
John Durkop9814fa22020-11-04 12:28:15 -08006210#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006211 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006212 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006213 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006214 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006215
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006216 return( PSA_SUCCESS );
6217}
Darryl Green0c6575a2018-11-07 16:05:30 +00006218
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006219psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006220 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006221{
6222 psa_status_t status;
6223 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006224 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006225 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006226
Ronald Cron81709fc2020-11-14 12:10:32 +01006227 *key = MBEDTLS_SVC_KEY_ID_INIT;
6228
Gilles Peskine0f84d622019-09-12 19:03:13 +02006229 /* Reject any attempt to create a zero-length key so that we don't
6230 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6231 if( psa_get_key_bits( attributes ) == 0 )
6232 return( PSA_ERROR_INVALID_ARGUMENT );
6233
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02006234 /* Reject any attempt to create a public key. */
6235 if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) )
6236 return( PSA_ERROR_INVALID_ARGUMENT );
6237
Ronald Cron81709fc2020-11-14 12:10:32 +01006238 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6239 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006240 if( status != PSA_SUCCESS )
6241 goto exit;
6242
Ronald Cron977c2472020-10-13 08:32:21 +02006243 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05306244 * storage ( thus not in the case of generating a key in a secure element
6245 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
6246 * buffer to hold the generated key material. */
Ronald Cron977c2472020-10-13 08:32:21 +02006247 if( slot->key.data == NULL )
6248 {
6249 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6250 PSA_KEY_LOCATION_LOCAL_STORAGE )
6251 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006252 status = psa_validate_key_type_and_size_for_key_generation(
6253 attributes->core.type, attributes->core.bits );
6254 if( status != PSA_SUCCESS )
6255 goto exit;
6256
6257 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6258 attributes->core.type,
6259 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006260 }
6261 else
6262 {
6263 status = psa_driver_wrapper_get_key_buffer_size(
6264 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006265 if( status != PSA_SUCCESS )
6266 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006267 }
Ronald Cron977c2472020-10-13 08:32:21 +02006268
6269 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6270 if( status != PSA_SUCCESS )
6271 goto exit;
6272 }
6273
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006274 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006275 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006276
Ronald Cron2b56bc82020-10-05 10:02:26 +02006277 if( status != PSA_SUCCESS )
6278 psa_remove_key_data_from_memory( slot );
6279
Gilles Peskine11792082019-08-06 18:36:36 +02006280exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006281 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006282 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006283 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006284 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006285
Darryl Green0c6575a2018-11-07 16:05:30 +00006286 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006287}
6288
Gilles Peskinee59236f2018-01-27 23:32:46 +01006289/****************************************************************/
6290/* Module setup */
6291/****************************************************************/
6292
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006293#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006294psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6295 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6296 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6297{
6298 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6299 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006300 global_data.rng.entropy_init = entropy_init;
6301 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006302 return( PSA_SUCCESS );
6303}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006304#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006305
Gilles Peskinee59236f2018-01-27 23:32:46 +01006306void mbedtls_psa_crypto_free( void )
6307{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006308 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006309 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6310 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006311 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006312 }
6313 /* Wipe all remaining data, including configuration.
6314 * In particular, this sets all state indicator to the value
6315 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006316 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Ronald Cron9ba76912021-04-10 16:57:30 +02006317
6318 /* Terminate drivers */
6319 psa_driver_wrapper_free( );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006320}
6321
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006322#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6323/** Recover a transaction that was interrupted by a power failure.
6324 *
6325 * This function is called during initialization, before psa_crypto_init()
6326 * returns. If this function returns a failure status, the initialization
6327 * fails.
6328 */
6329static psa_status_t psa_crypto_recover_transaction(
6330 const psa_crypto_transaction_t *transaction )
6331{
6332 switch( transaction->unknown.type )
6333 {
6334 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6335 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006336 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006337 * is implemented.
6338 * https://github.com/ARMmbed/mbed-crypto/issues/218
6339 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006340 default:
6341 /* We found an unsupported transaction in the storage.
6342 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006343 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006344 }
6345}
6346#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6347
Gilles Peskinee59236f2018-01-27 23:32:46 +01006348psa_status_t psa_crypto_init( void )
6349{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006350 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006351
Gilles Peskinec6b69072018-11-20 21:42:52 +01006352 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006353 if( global_data.initialized != 0 )
6354 return( PSA_SUCCESS );
6355
Gilles Peskine30524eb2020-11-13 17:02:26 +01006356 /* Initialize and seed the random generator. */
6357 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006358 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006359 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006360 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006361 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006362 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006363
Gilles Peskine66fb1262018-12-10 16:29:04 +01006364 status = psa_initialize_key_slots( );
6365 if( status != PSA_SUCCESS )
6366 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006367
Ronald Cron9ba76912021-04-10 16:57:30 +02006368 /* Init drivers */
6369 status = psa_driver_wrapper_init( );
Gilles Peskined9348f22019-10-01 15:22:29 +02006370 if( status != PSA_SUCCESS )
6371 goto exit;
Gilles Peskined9348f22019-10-01 15:22:29 +02006372
Gilles Peskine4b734222019-07-24 15:56:31 +02006373#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006374 status = psa_crypto_load_transaction( );
6375 if( status == PSA_SUCCESS )
6376 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006377 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6378 if( status != PSA_SUCCESS )
6379 goto exit;
6380 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006381 }
6382 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6383 {
6384 /* There's no transaction to complete. It's all good. */
6385 status = PSA_SUCCESS;
6386 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006387#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006388
Gilles Peskinec6b69072018-11-20 21:42:52 +01006389 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006390 global_data.initialized = 1;
6391
6392exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006393 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006394 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006395 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006396}
6397
6398#endif /* MBEDTLS_PSA_CRYPTO_C */