blob: 8df26f0dd1dd34a6d59f42d9108134e6ba3db306 [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"
30
Ronald Crond6d28882020-12-14 14:56:02 +010031#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010036#include "psa_crypto_hash.h"
Steven Cooreman32d56942021-03-19 17:39:17 +010037#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010038#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010039#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020040#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020041#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020042#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010043#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010044/* Include internal declarations that are useful for implementing persistently
45 * stored keys. */
46#include "psa_crypto_storage.h"
47
Gilles Peskineb2b64d32020-12-14 16:43:58 +010048#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010049
Gilles Peskineb46bef22019-07-30 21:32:04 +020050#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include <stdlib.h>
52#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020054#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#define mbedtls_calloc calloc
56#define mbedtls_free free
57#endif
58
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010059#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020061#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000062#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020063#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/blowfish.h"
65#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020066#include "mbedtls/chacha20.h"
67#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/cipher.h"
69#include "mbedtls/ccm.h"
70#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020072#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010074#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/error.h"
76#include "mbedtls/gcm.h"
77#include "mbedtls/md2.h"
78#include "mbedtls/md4.h"
79#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010080#include "mbedtls/md.h"
81#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010082#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010083#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010084#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000085#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010087#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088#include "mbedtls/sha1.h"
89#include "mbedtls/sha256.h"
90#include "mbedtls/sha512.h"
91#include "mbedtls/xtea.h"
92
Gilles Peskine996deb12018-08-01 15:45:45 +020093#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
94
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010095/****************************************************************/
96/* Global data, support functions and library management */
97/****************************************************************/
98
Gilles Peskine48c0ea12018-06-21 14:15:31 +020099static int key_type_is_raw_bytes( psa_key_type_t type )
100{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200101 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102}
103
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100104/* Values for psa_global_data_t::rng_state */
105#define RNG_NOT_INITIALIZED 0
106#define RNG_INITIALIZED 1
107#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108
Gilles Peskine2d277862018-06-18 15:41:12 +0200109typedef struct
110{
Gilles Peskinec6b69072018-11-20 21:42:52 +0100111 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100112 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +0100113 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114} psa_global_data_t;
115
116static psa_global_data_t global_data;
117
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100118#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
119mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
120 &global_data.rng.drbg;
121#endif
122
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123#define GUARD_MODULE_INITIALIZED \
124 if( global_data.initialized == 0 ) \
125 return( PSA_ERROR_BAD_STATE );
126
Steven Cooreman01164162020-07-20 15:31:37 +0200127psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100128{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100129 /* Mbed TLS error codes can combine a high-level error code and a
130 * low-level error code. The low-level error usually reflects the
131 * root cause better, so dispatch on that preferably. */
132 int low_level_ret = - ( -ret & 0x007f );
133 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100134 {
135 case 0:
136 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100137
138 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
139 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
140 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
141 return( PSA_ERROR_NOT_SUPPORTED );
142 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200145 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
Gilles Peskine9a944802018-06-21 09:35:35 +0200148 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
149 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
150 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
151 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
152 case MBEDTLS_ERR_ASN1_INVALID_DATA:
153 return( PSA_ERROR_INVALID_ARGUMENT );
154 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
155 return( PSA_ERROR_INSUFFICIENT_MEMORY );
156 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
157 return( PSA_ERROR_BUFFER_TOO_SMALL );
158
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000160 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
162 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
163#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
165 return( PSA_ERROR_NOT_SUPPORTED );
166 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
167 return( PSA_ERROR_HARDWARE_FAILURE );
168
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000170 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
172 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
173#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
175 return( PSA_ERROR_NOT_SUPPORTED );
176 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_CCM_BAD_INPUT:
180 return( PSA_ERROR_INVALID_ARGUMENT );
181 case MBEDTLS_ERR_CCM_AUTH_FAILED:
182 return( PSA_ERROR_INVALID_SIGNATURE );
183 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188
189 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
190 return( PSA_ERROR_BAD_STATE );
191 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
192 return( PSA_ERROR_INVALID_SIGNATURE );
193
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
197 return( PSA_ERROR_INVALID_ARGUMENT );
198 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
200 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
201 return( PSA_ERROR_INVALID_PADDING );
202 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200203 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200207 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
Gilles Peskine82e57d12020-11-13 21:31:17 +0100214#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
215 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100216 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
217 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100218 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
221 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
224 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100225#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100226
227 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
228 return( PSA_ERROR_NOT_SUPPORTED );
229 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
230 return( PSA_ERROR_HARDWARE_FAILURE );
231
Gilles Peskinee59236f2018-01-27 23:32:46 +0100232 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
233 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
234 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
235 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
237 case MBEDTLS_ERR_GCM_AUTH_FAILED:
238 return( PSA_ERROR_INVALID_SIGNATURE );
239 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200240 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100241 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskine82e57d12020-11-13 21:31:17 +0100244#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
245 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100246 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
247 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100248 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
250 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
251 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
252 return( PSA_ERROR_NOT_SUPPORTED );
253 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
254 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
255#endif
256
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200257 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
258 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
259 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
260 return( PSA_ERROR_HARDWARE_FAILURE );
261
Gilles Peskinea5905292018-02-07 20:59:33 +0100262 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
263 return( PSA_ERROR_NOT_SUPPORTED );
264 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MD_ALLOC_FAILED:
267 return( PSA_ERROR_INSUFFICIENT_MEMORY );
268 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
269 return( PSA_ERROR_STORAGE_FAILURE );
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200270 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
271 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100272
Gilles Peskinef76aa772018-10-29 19:24:33 +0100273 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
274 return( PSA_ERROR_STORAGE_FAILURE );
275 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
280 return( PSA_ERROR_BUFFER_TOO_SMALL );
281 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
288 return( PSA_ERROR_INSUFFICIENT_MEMORY );
289
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100290 case MBEDTLS_ERR_PK_ALLOC_FAILED:
291 return( PSA_ERROR_INSUFFICIENT_MEMORY );
292 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
293 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
294 return( PSA_ERROR_INVALID_ARGUMENT );
295 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100297 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
298 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
299 return( PSA_ERROR_INVALID_ARGUMENT );
300 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
301 return( PSA_ERROR_NOT_SUPPORTED );
302 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
303 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
304 return( PSA_ERROR_NOT_PERMITTED );
305 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_PK_INVALID_ALG:
308 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
309 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
312 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315
Gilles Peskineff2d2002019-05-06 15:26:23 +0200316 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
317 return( PSA_ERROR_HARDWARE_FAILURE );
318 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
319 return( PSA_ERROR_NOT_SUPPORTED );
320
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200321 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
322 return( PSA_ERROR_HARDWARE_FAILURE );
323
Gilles Peskinea5905292018-02-07 20:59:33 +0100324 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
325 return( PSA_ERROR_INVALID_ARGUMENT );
326 case MBEDTLS_ERR_RSA_INVALID_PADDING:
327 return( PSA_ERROR_INVALID_PADDING );
328 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
331 return( PSA_ERROR_INVALID_ARGUMENT );
332 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
333 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200334 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100335 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
336 return( PSA_ERROR_INVALID_SIGNATURE );
337 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
338 return( PSA_ERROR_BUFFER_TOO_SMALL );
339 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100340 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100341 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
342 return( PSA_ERROR_NOT_SUPPORTED );
343 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
344 return( PSA_ERROR_HARDWARE_FAILURE );
345
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200346 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
347 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
348 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
349 return( PSA_ERROR_HARDWARE_FAILURE );
350
Gilles Peskinea5905292018-02-07 20:59:33 +0100351 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
352 return( PSA_ERROR_INVALID_ARGUMENT );
353 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
354 return( PSA_ERROR_HARDWARE_FAILURE );
355
itayzafrir5c753392018-05-08 11:18:38 +0300356 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300357 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300358 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300359 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
360 return( PSA_ERROR_BUFFER_TOO_SMALL );
361 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
362 return( PSA_ERROR_NOT_SUPPORTED );
363 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
364 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
365 return( PSA_ERROR_INVALID_SIGNATURE );
366 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
367 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100368 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
369 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300370 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
371 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100372
Janos Follatha13b9052019-11-22 12:48:59 +0000373 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
374 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300375
Gilles Peskinee59236f2018-01-27 23:32:46 +0100376 default:
David Saadab4ecc272019-02-14 13:48:10 +0200377 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100378 }
379}
380
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200381
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200382
383
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100384/****************************************************************/
385/* Key management */
386/****************************************************************/
387
John Durkop9814fa22020-11-04 12:28:15 -0800388#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800389 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100390 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
391 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
392 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100393mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 size_t bits,
395 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200396{
397 switch( curve )
398 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100399 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100400 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100401 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100402#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100403 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100404 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#endif
406#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100408 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100409#endif
410#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100411 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100413#endif
414#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100415 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100416 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100417#endif
418#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100419 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100420 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100421 case 528:
422 if( bits_is_sloppy )
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100425#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 }
427 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100428
Paul Elliott8ff510a2020-06-02 17:19:28 +0100429 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100430 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100431 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100432#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100434 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100435#endif
436#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100437 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100438 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100439#endif
440#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100441 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100442 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100443#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100444 }
445 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100446
Paul Elliott8ff510a2020-06-02 17:19:28 +0100447 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100448 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100449 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100452 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100453 case 256:
454 if( bits_is_sloppy )
455 return( MBEDTLS_ECP_DP_CURVE25519 );
456 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#endif
458#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100460 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100461#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100462 }
463 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100464
Paul Elliott8ff510a2020-06-02 17:19:28 +0100465 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100466 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100467 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100468#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100470 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100471#endif
472#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100474 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100475#endif
476#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100477 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100478 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100479#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100480 }
481 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200482 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100483
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100484 (void) bits_is_sloppy;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100485 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200486}
John Durkop9814fa22020-11-04 12:28:15 -0800487#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100488 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
489 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
490 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
491 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200492
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200493static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
494 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495{
496 /* Check that the bit size is acceptable for the key type */
497 switch( type )
498 {
499 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200501 case PSA_KEY_TYPE_DERIVE:
502 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100503#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504 case PSA_KEY_TYPE_AES:
505 if( bits != 128 && bits != 192 && bits != 256 )
506 return( PSA_ERROR_INVALID_ARGUMENT );
507 break;
508#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200509#if defined(PSA_WANT_KEY_TYPE_ARIA)
510 case PSA_KEY_TYPE_ARIA:
511 if( bits != 128 && bits != 192 && bits != 256 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100515#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200516 case PSA_KEY_TYPE_CAMELLIA:
517 if( bits != 128 && bits != 192 && bits != 256 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100521#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 case PSA_KEY_TYPE_DES:
523 if( bits != 64 && bits != 128 && bits != 192 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100527#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528 case PSA_KEY_TYPE_ARC4:
529 if( bits < 8 || bits > 2048 )
530 return( PSA_ERROR_INVALID_ARGUMENT );
531 break;
532#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100533#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200534 case PSA_KEY_TYPE_CHACHA20:
535 if( bits != 256 )
536 return( PSA_ERROR_INVALID_ARGUMENT );
537 break;
538#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539 default:
540 return( PSA_ERROR_NOT_SUPPORTED );
541 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200542 if( bits % 8 != 0 )
543 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200545 return( PSA_SUCCESS );
546}
547
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 *
Steven Cooremancd640932021-03-08 12:00:27 +0100550 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
551 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100552 *
553 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 * \param[in] key_type The key type of the key to be used with the
555 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100556 *
557 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100558 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100559 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100560 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100561 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100562MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100563 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100565{
Steven Cooremancd640932021-03-08 12:00:27 +0100566 if( PSA_ALG_IS_HMAC( algorithm ) )
567 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100568 if( key_type == PSA_KEY_TYPE_HMAC )
569 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100570 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100571
572 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100573 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100574 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
575 * key. */
576 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
577 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
578 {
579 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
580 * the block length (larger than 1) for block ciphers. */
581 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
582 return( PSA_SUCCESS );
583 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100584 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100585
586 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100587}
588
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200589psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
590 size_t buffer_length )
Steven Cooreman75b74362020-07-28 14:30:13 +0200591{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100592 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200593 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200594
Ronald Cronea0f8a62020-11-25 17:52:23 +0100595 slot->key.data = mbedtls_calloc( 1, buffer_length );
596 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200597 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200598
Ronald Cronea0f8a62020-11-25 17:52:23 +0100599 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200600 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200601}
602
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200603psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
604 const uint8_t* data,
605 size_t data_length )
606{
607 psa_status_t status = psa_allocate_buffer_to_slot( slot,
608 data_length );
609 if( status != PSA_SUCCESS )
610 return( status );
611
Ronald Cronea0f8a62020-11-25 17:52:23 +0100612 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200613 return( PSA_SUCCESS );
614}
615
Ronald Crona0fe59f2020-11-29 11:14:53 +0100616psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100617 const psa_key_attributes_t *attributes,
618 const uint8_t *data, size_t data_length,
619 uint8_t *key_buffer, size_t key_buffer_size,
620 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100621{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100622 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
623 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200625 /* zero-length keys are never supported. */
626 if( data_length == 0 )
627 return( PSA_ERROR_NOT_SUPPORTED );
628
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100629 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100631 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200632
Steven Cooreman75b74362020-07-28 14:30:13 +0200633 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
634 if( data_length > SIZE_MAX / 8 )
635 return( PSA_ERROR_NOT_SUPPORTED );
636
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200637 /* Enforce a size limit, and in particular ensure that the bit
638 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100639 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200640 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200641
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100642 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200643 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200644 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200645
Ronald Crondd04d422020-11-28 15:14:42 +0100646 /* Copy the key material. */
647 memcpy( key_buffer, data, data_length );
648 *key_buffer_length = data_length;
649 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200650
Steven Cooreman40120f62020-10-29 11:42:22 +0100651 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100653 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200654 {
John Durkop9814fa22020-11-04 12:28:15 -0800655#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
656 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100657 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200658 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100659 return( mbedtls_psa_ecp_import_key( attributes,
660 data, data_length,
661 key_buffer, key_buffer_size,
662 key_buffer_length,
663 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100664 }
John Durkop9814fa22020-11-04 12:28:15 -0800665#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
666 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700667#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
668 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100669 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200670 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100671 return( mbedtls_psa_rsa_import_key( attributes,
672 data, data_length,
673 key_buffer, key_buffer_size,
674 key_buffer_length,
675 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200676 }
John Durkop9814fa22020-11-04 12:28:15 -0800677#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
678 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100679 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100680
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100681 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100682}
683
Gilles Peskinef603c712019-01-19 13:40:11 +0100684/** Calculate the intersection of two algorithm usage policies.
685 *
686 * Return 0 (which allows no operation) on incompatibility.
687 */
688static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100689 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100690 psa_algorithm_t alg1,
691 psa_algorithm_t alg2 )
692{
Gilles Peskine549ea862019-05-22 11:45:59 +0200693 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100694 if( alg1 == alg2 )
695 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100696 /* If the policies are from the same hash-and-sign family, check
697 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine8cb22c82021-09-22 16:15:05 +0200698 if( PSA_ALG_IS_SIGN_HASH( alg1 ) &&
699 PSA_ALG_IS_SIGN_HASH( alg2 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100700 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100701 {
Steven Cooreman03488022021-02-18 12:07:20 +0100702 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
703 return( alg2 );
704 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
705 return( alg1 );
706 }
707 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100708 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100709 * restrictive tag length. */
710 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
711 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
712 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
713 {
714 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
715 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100716 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100717
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100718 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100719 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
720 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100721 {
Steven Cooremancd640932021-03-08 12:00:27 +0100722 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
723 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100724 }
725 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100726 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100727 ( alg1_len <= alg2_len ) )
728 {
729 return( alg2 );
730 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100731 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100732 ( alg2_len <= alg1_len ) )
733 {
734 return( alg1 );
735 }
736 }
737 /* If the policies are from the same MAC family, check whether one
738 * of them is a minimum-MAC-length policy. Calculate the most
739 * restrictive tag length. */
740 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
741 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
742 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
743 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100744 /* Validate the combination of key type and algorithm. Since the base
745 * algorithm of alg1 and alg2 are the same, we only need this once. */
746 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100747 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100748
Steven Cooreman31a876d2021-03-03 20:47:40 +0100749 /* Get the (exact or at-least) output lengths for both sides of the
750 * requested intersection. None of the currently supported algorithms
751 * have an output length dependent on the actual key size, so setting it
752 * to a bogus value of 0 is currently OK.
753 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100754 * Note that for at-least-this-length wildcard algorithms, the output
755 * length is set to the shortest allowed length, which allows us to
756 * calculate the most restrictive tag length for the intersection. */
757 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
758 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100759 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100760
Steven Cooremana1d83222021-02-25 10:20:29 +0100761 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100762 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
763 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100764 {
Steven Cooremancd640932021-03-08 12:00:27 +0100765 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100766 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100767
768 /* If only one is an at-least-this-length policy, the intersection would
769 * be the other (fixed-length) policy as long as said fixed length is
770 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100771 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100772 {
Steven Cooremancd640932021-03-08 12:00:27 +0100773 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100774 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100775 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100776 {
Steven Cooremancd640932021-03-08 12:00:27 +0100777 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100778 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100779
Steven Cooremancd640932021-03-08 12:00:27 +0100780 /* If none of them are wildcards, check whether they define the same tag
781 * length. This is still possible here when one is default-length and
782 * the other specific-length. Ensure to always return the
783 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100784 if( alg1_len == alg2_len )
785 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100786 }
787 /* If the policies are incompatible, allow nothing. */
788 return( 0 );
789}
790
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100791static int psa_key_algorithm_permits( psa_key_type_t key_type,
792 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200793 psa_algorithm_t requested_alg )
794{
Gilles Peskine549ea862019-05-22 11:45:59 +0200795 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200796 if( requested_alg == policy_alg )
797 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100798 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
799 * and requested_alg is the same hash-and-sign family with any hash,
800 * then requested_alg is compliant with policy_alg. */
Gilles Peskine8cb22c82021-09-22 16:15:05 +0200801 if( PSA_ALG_IS_SIGN_HASH( requested_alg ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100802 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200803 {
Steven Cooreman03488022021-02-18 12:07:20 +0100804 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
805 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
806 }
807 /* If policy_alg is a wildcard AEAD algorithm of the same base as
808 * the requested algorithm, check the requested tag length to be
809 * equal-length or longer than the wildcard-specified length. */
810 if( PSA_ALG_IS_AEAD( policy_alg ) &&
811 PSA_ALG_IS_AEAD( requested_alg ) &&
812 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
813 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100814 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100815 {
816 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
817 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
818 }
Steven Cooremancd640932021-03-08 12:00:27 +0100819 /* If policy_alg is a MAC algorithm of the same base as the requested
820 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100821 if( PSA_ALG_IS_MAC( policy_alg ) &&
822 PSA_ALG_IS_MAC( requested_alg ) &&
823 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100824 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100825 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100826 /* Validate the combination of key type and algorithm. Since the policy
827 * and requested algorithms are the same, we only need this once. */
828 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100829 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100830
Steven Cooreman31a876d2021-03-03 20:47:40 +0100831 /* Get both the requested output length for the algorithm which is to be
832 * verified, and the default output length for the base algorithm.
833 * Note that none of the currently supported algorithms have an output
834 * length dependent on actual key size, so setting it to a bogus value
835 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100836 size_t requested_output_length = PSA_MAC_LENGTH(
837 key_type, 0, requested_alg );
838 size_t default_output_length = PSA_MAC_LENGTH(
839 key_type, 0,
840 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100841
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100842 /* If the policy is default-length, only allow an algorithm with
843 * a declared exact-length matching the default. */
844 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100845 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100846
847 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100848 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100849 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
850 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
851 {
852 return( 1 );
853 }
854
Steven Cooremancd640932021-03-08 12:00:27 +0100855 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
856 * check for the requested MAC length to be equal to or longer than the
857 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100858 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
859 {
860 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100861 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100862 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200863 }
Steven Cooremance48e852020-10-05 16:02:45 +0200864 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200865 * a key derivation with that key agreement should also be allowed. This
866 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200867 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
868 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
869 {
870 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
871 policy_alg );
872 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100873 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200874 return( 0 );
875}
876
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100877/** Test whether a policy permits an algorithm.
878 *
879 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100880 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100881 * \note This function requires providing the key type for which the policy is
882 * being validated, since some algorithm policy definitions (e.g. MAC)
883 * have different properties depending on what kind of cipher it is
884 * combined with.
885 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100886 * \retval PSA_SUCCESS When \p alg is a specific algorithm
887 * allowed by the \p policy.
888 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
889 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
890 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100891 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100892static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100893 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100894 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100895{
Steven Cooremand788fab2021-02-25 11:29:17 +0100896 /* '0' is not a valid algorithm */
897 if( alg == 0 )
898 return( PSA_ERROR_INVALID_ARGUMENT );
899
Steven Cooreman7e39f052021-02-23 14:18:32 +0100900 /* A requested algorithm cannot be a wildcard. */
901 if( PSA_ALG_IS_WILDCARD( alg ) )
902 return( PSA_ERROR_INVALID_ARGUMENT );
903
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100904 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
905 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100906 return( PSA_SUCCESS );
907 else
908 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100909}
910
Gilles Peskinef603c712019-01-19 13:40:11 +0100911/** Restrict a key policy based on a constraint.
912 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100913 * \note This function requires providing the key type for which the policy is
914 * being restricted, since some algorithm policy definitions (e.g. MAC)
915 * have different properties depending on what kind of cipher it is
916 * combined with.
917 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100918 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100919 * \param[in,out] policy The policy to restrict.
920 * \param[in] constraint The policy constraint to apply.
921 *
922 * \retval #PSA_SUCCESS
923 * \c *policy contains the intersection of the original value of
924 * \c *policy and \c *constraint.
925 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100926 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100927 * \c *policy is unchanged.
928 */
929static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100930 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100931 psa_key_policy_t *policy,
932 const psa_key_policy_t *constraint )
933{
934 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100935 psa_key_policy_algorithm_intersection( key_type, policy->alg,
936 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200937 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100938 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
939 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100940 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
941 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200942 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
943 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100944 policy->usage &= constraint->usage;
945 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200946 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100947 return( PSA_SUCCESS );
948}
949
Ronald Cron5c522922020-11-14 16:35:34 +0100950/** Get the description of a key given its identifier and policy constraints
951 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200952 *
Ronald Cron5c522922020-11-14 16:35:34 +0100953 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100954 * nonzero, the key must allow operations with this algorithm. If \p alg is
955 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100956 *
Ronald Cron5c522922020-11-14 16:35:34 +0100957 * In case of a persistent key, the function loads the description of the key
958 * into a key slot if not already done.
959 *
960 * On success, the returned key slot is locked. It is the responsibility of
961 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200962 */
Ronald Cron5c522922020-11-14 16:35:34 +0100963static psa_status_t psa_get_and_lock_key_slot_with_policy(
964 mbedtls_svc_key_id_t key,
965 psa_key_slot_t **p_slot,
966 psa_key_usage_t usage,
967 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100968{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
970 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100971
Ronald Cron5c522922020-11-14 16:35:34 +0100972 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100973 if( status != PSA_SUCCESS )
974 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200975 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100976
977 /* Enforce that usage policy for the key slot contains all the flags
978 * required by the usage parameter. There is one exception: public
979 * keys can always be exported, so we treat public key objects as
980 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100982 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200983
Gilles Peskine8e338702019-07-30 20:06:31 +0200984 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100985 {
986 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200987 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100988 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100989
Shaun Case0e7791f2021-12-20 21:14:10 -0800990 /* Enforce that the usage policy permits the requested algorithm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100991 if( alg != 0 )
992 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100993 status = psa_key_policy_permits( &slot->attr.policy,
994 slot->attr.type,
995 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +0100996 if( status != PSA_SUCCESS )
997 goto error;
998 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100999
Darryl Green06fd18d2018-07-16 11:21:11 +01001000 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001
1002error:
1003 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001004 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001005
1006 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001007}
Darryl Green940d72c2018-07-13 13:18:51 +01001008
Ronald Cron5c522922020-11-14 16:35:34 +01001009/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001010 *
1011 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001012 * available, as opposed to a key in a secure element and/or to be used
1013 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001015 * This is a temporary function that may be used instead of
1016 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1017 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001018 *
Ronald Cron5c522922020-11-14 16:35:34 +01001019 * On success, the returned key slot is locked. It is the responsibility of the
1020 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001021 */
Ronald Cron5c522922020-11-14 16:35:34 +01001022static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1023 mbedtls_svc_key_id_t key,
1024 psa_key_slot_t **p_slot,
1025 psa_key_usage_t usage,
1026 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001027{
Ronald Cron5c522922020-11-14 16:35:34 +01001028 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1029 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001030 if( status != PSA_SUCCESS )
1031 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001032
Ronald Cron8a0466a2021-08-24 15:39:44 +02001033 if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001034 {
Ronald Cron5c522922020-11-14 16:35:34 +01001035 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001036 *p_slot = NULL;
1037 return( PSA_ERROR_NOT_SUPPORTED );
1038 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001039
Gilles Peskine28f8f302019-07-24 13:30:31 +02001040 return( PSA_SUCCESS );
1041}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001042
Steven Cooreman7ddee7f2021-04-07 18:08:30 +02001043psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001044{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001045 /* Data pointer will always be either a valid pointer or NULL in an
1046 * initialized slot, so we can just free it. */
1047 if( slot->key.data != NULL )
1048 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1049
1050 mbedtls_free( slot->key.data );
1051 slot->key.data = NULL;
1052 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001053
1054 return( PSA_SUCCESS );
1055}
1056
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001057/** Completely wipe a slot in memory, including its policy.
1058 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001059psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001060{
1061 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001062
1063 /*
1064 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001065 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001066 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1067 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001068 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001069 */
Ronald Cron5c522922020-11-14 16:35:34 +01001070 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001071 {
1072#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001073 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001074#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001075 status = PSA_ERROR_CORRUPTION_DETECTED;
1076 }
1077
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001078 /* Multipart operations may still be using the key. This is safe
1079 * because all multipart operation objects are independent from
1080 * the key slot: if they need to access the key after the setup
1081 * phase, they have a copy of the key. Note that this means that
1082 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001083 /* At this point, key material and other type-specific content has
1084 * been wiped. Clear remaining metadata. We can call memset and not
1085 * zeroize because the metadata is not particularly sensitive. */
1086 memset( slot, 0, sizeof( *slot ) );
1087 return( status );
1088}
1089
Ronald Croncf56a0a2020-08-04 09:51:30 +02001090psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001092 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001093 psa_status_t status; /* status of the last operation */
1094 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001095#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1096 psa_se_drv_table_entry_t *driver;
1097#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098
Ronald Croncf56a0a2020-08-04 09:51:30 +02001099 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001100 return( PSA_SUCCESS );
1101
Ronald Cronf2911112020-10-29 17:51:10 +01001102 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001103 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001104 * key, this will load the key description from persistent memory if not
1105 * done yet. We cannot avoid this loading as without it we don't know if
1106 * the key is operated by an SE or not and this information is needed by
1107 * the current implementation.
1108 */
Ronald Cron5c522922020-11-14 16:35:34 +01001109 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001110 if( status != PSA_SUCCESS )
1111 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001112
Ronald Cronf2911112020-10-29 17:51:10 +01001113 /*
1114 * If the key slot containing the key description is under access by the
1115 * library (apart from the present access), the key cannot be destroyed
1116 * yet. For the time being, just return in error. Eventually (to be
1117 * implemented), the key should be destroyed when all accesses have
1118 * stopped.
1119 */
Ronald Cron5c522922020-11-14 16:35:34 +01001120 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001121 {
Ronald Cron5c522922020-11-14 16:35:34 +01001122 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001123 return( PSA_ERROR_GENERIC_ERROR );
1124 }
1125
Gilles Peskine251c7742021-04-21 22:32:05 +02001126 if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) )
1127 {
1128 /* Refuse the destruction of a read-only key (which may or may not work
1129 * if we attempt it, depending on whether the key is merely read-only
1130 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001131 * Just do the best we can, which is to wipe the copy in memory
1132 * (done in this function's cleanup code). */
1133 overall_status = PSA_ERROR_NOT_PERMITTED;
1134 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001135 }
1136
Gilles Peskine354f7672019-07-12 23:46:38 +02001137#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001138 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001139 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001140 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001141 /* For a key in a secure element, we need to do three things:
1142 * remove the key file in internal storage, destroy the
1143 * key inside the secure element, and update the driver's
1144 * persistent data. Start a transaction that will encompass these
1145 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001146 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001147 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001148 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001149 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001150 status = psa_crypto_save_transaction( );
1151 if( status != PSA_SUCCESS )
1152 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001153 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001154 /* We should still try to destroy the key in the secure
1155 * element and the key metadata in storage. This is especially
1156 * important if the error is that the storage is full.
1157 * But how to do it exactly without risking an inconsistent
1158 * state after a reset?
1159 * https://github.com/ARMmbed/mbed-crypto/issues/215
1160 */
1161 overall_status = status;
1162 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001163 }
1164
Ronald Cronea0f8a62020-11-25 17:52:23 +01001165 status = psa_destroy_se_key( driver,
1166 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001167 if( overall_status == PSA_SUCCESS )
1168 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001169 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001170#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1171
Darryl Greend49a4992018-06-18 17:27:26 +01001172#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001173 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001174 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001175 status = psa_destroy_persistent_key( slot->attr.id );
1176 if( overall_status == PSA_SUCCESS )
1177 overall_status = status;
1178
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001179 /* TODO: other slots may have a copy of the same key. We should
1180 * invalidate them.
1181 * https://github.com/ARMmbed/mbed-crypto/issues/214
1182 */
Darryl Greend49a4992018-06-18 17:27:26 +01001183 }
1184#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001185
Gilles Peskinefc762652019-07-22 19:30:34 +02001186#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1187 if( driver != NULL )
1188 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001189 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001190 if( overall_status == PSA_SUCCESS )
1191 overall_status = status;
1192 status = psa_crypto_stop_transaction( );
1193 if( overall_status == PSA_SUCCESS )
1194 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001195 }
1196#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1197
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001198exit:
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001199 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001200 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001201 if( status != PSA_SUCCESS )
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001202 overall_status = status;
1203 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001204}
1205
John Durkop07cc04a2020-11-16 22:08:34 -08001206#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001207 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001208static psa_status_t psa_get_rsa_public_exponent(
1209 const mbedtls_rsa_context *rsa,
1210 psa_key_attributes_t *attributes )
1211{
1212 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001214 uint8_t *buffer = NULL;
1215 size_t buflen;
1216 mbedtls_mpi_init( &mpi );
1217
1218 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1219 if( ret != 0 )
1220 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001221 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1222 {
1223 /* It's the default value, which is reported as an empty string,
1224 * so there's nothing to do. */
1225 goto exit;
1226 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001227
1228 buflen = mbedtls_mpi_size( &mpi );
1229 buffer = mbedtls_calloc( 1, buflen );
1230 if( buffer == NULL )
1231 {
1232 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1233 goto exit;
1234 }
1235 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1236 if( ret != 0 )
1237 goto exit;
1238 attributes->domain_parameters = buffer;
1239 attributes->domain_parameters_size = buflen;
1240
1241exit:
1242 mbedtls_mpi_free( &mpi );
1243 if( ret != 0 )
1244 mbedtls_free( buffer );
1245 return( mbedtls_to_psa_error( ret ) );
1246}
John Durkop07cc04a2020-11-16 22:08:34 -08001247#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001248 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001249
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001250/** Retrieve all the publicly-accessible attributes of a key.
1251 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001252psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001253 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001255 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001256 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001257 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001258
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001259 psa_reset_key_attributes( attributes );
1260
Ronald Cron5c522922020-11-14 16:35:34 +01001261 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001262 if( status != PSA_SUCCESS )
1263 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001264
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001265 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001266 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1267 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1268
1269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron0518f612021-08-24 15:50:05 +02001270 if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001271 psa_set_key_slot_number( attributes,
1272 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001273#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001274
Gilles Peskine8e338702019-07-30 20:06:31 +02001275 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001276 {
John Durkop07cc04a2020-11-16 22:08:34 -08001277#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001278 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001279 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001280 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001281 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001282 * is not yet implemented.
1283 * https://github.com/ARMmbed/mbed-crypto/issues/216
1284 */
Ronald Cron41e80062021-08-24 16:00:51 +02001285 if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02001286 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001287 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001288
Ronald Cron90857082020-11-25 14:58:33 +01001289 status = mbedtls_psa_rsa_load_representation(
1290 slot->attr.type,
1291 slot->key.data,
1292 slot->key.bytes,
1293 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001294 if( status != PSA_SUCCESS )
1295 break;
1296
Steven Cooremana2371e52020-07-28 14:30:39 +02001297 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001298 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001299 mbedtls_rsa_free( rsa );
1300 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001301 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001302 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001303#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001304 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001305 default:
1306 /* Nothing else to do. */
1307 break;
1308 }
1309
1310 if( status != PSA_SUCCESS )
1311 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001312
Ronald Cron5c522922020-11-14 16:35:34 +01001313 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001314
Ronald Cron5c522922020-11-14 16:35:34 +01001315 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001316}
1317
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1319psa_status_t psa_get_key_slot_number(
1320 const psa_key_attributes_t *attributes,
1321 psa_key_slot_number_t *slot_number )
1322{
1323 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1324 {
1325 *slot_number = attributes->slot_number;
1326 return( PSA_SUCCESS );
1327 }
1328 else
1329 return( PSA_ERROR_INVALID_ARGUMENT );
1330}
1331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1332
Ronald Crond18b5f82020-11-25 16:46:05 +01001333static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1334 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001335 uint8_t *data,
1336 size_t data_size,
1337 size_t *data_length )
1338{
Ronald Crond18b5f82020-11-25 16:46:05 +01001339 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001340 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001341 memcpy( data, key_buffer, key_buffer_size );
1342 memset( data + key_buffer_size, 0,
1343 data_size - key_buffer_size );
1344 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001345 return( PSA_SUCCESS );
1346}
1347
Ronald Cron7285cda2020-11-26 14:46:37 +01001348psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 const psa_key_attributes_t *attributes,
1350 const uint8_t *key_buffer, size_t key_buffer_size,
1351 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352{
Ronald Crond18b5f82020-11-25 16:46:05 +01001353 psa_key_type_t type = attributes->core.type;
1354
Ronald Crond18b5f82020-11-25 16:46:05 +01001355 if( key_type_is_raw_bytes( type ) ||
1356 PSA_KEY_TYPE_IS_RSA( type ) ||
1357 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001358 {
1359 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001360 key_buffer, key_buffer_size,
1361 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001362 }
1363 else
1364 {
1365 /* This shouldn't happen in the reference implementation, but
1366 it is valid for a special-purpose implementation to omit
1367 support for exporting certain key types. */
1368 return( PSA_ERROR_NOT_SUPPORTED );
1369 }
1370}
1371
1372psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1373 uint8_t *data,
1374 size_t data_size,
1375 size_t *data_length )
1376{
1377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1378 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1379 psa_key_slot_t *slot;
1380
Ronald Crone907e552021-01-18 13:22:38 +01001381 /* Reject a zero-length output buffer now, since this can never be a
1382 * valid key representation. This way we know that data must be a valid
1383 * pointer and we can do things like memset(data, ..., data_size). */
1384 if( data_size == 0 )
1385 return( PSA_ERROR_BUFFER_TOO_SMALL );
1386
Ronald Cron9486f9d2020-11-26 13:36:23 +01001387 /* Set the key to empty now, so that even when there are errors, we always
1388 * set data_length to a value between 0 and data_size. On error, setting
1389 * the key to empty is a good choice because an empty key representation is
1390 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001391 *data_length = 0;
1392
Ronald Cron9486f9d2020-11-26 13:36:23 +01001393 /* Export requires the EXPORT flag. There is an exception for public keys,
1394 * which don't require any flag, but
1395 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1396 */
1397 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1398 PSA_KEY_USAGE_EXPORT, 0 );
1399 if( status != PSA_SUCCESS )
1400 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001401
Ronald Crond18b5f82020-11-25 16:46:05 +01001402 psa_key_attributes_t attributes = {
1403 .core = slot->attr
1404 };
Ronald Cron67227982020-11-26 15:16:05 +01001405 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001406 slot->key.data, slot->key.bytes,
1407 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408
Ronald Cron9486f9d2020-11-26 13:36:23 +01001409 unlock_status = psa_unlock_key_slot( slot );
1410
1411 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1412}
1413
Ronald Cron7285cda2020-11-26 14:46:37 +01001414psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001415 const psa_key_attributes_t *attributes,
1416 const uint8_t *key_buffer,
1417 size_t key_buffer_size,
1418 uint8_t *data,
1419 size_t data_size,
1420 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001421{
Ronald Crond18b5f82020-11-25 16:46:05 +01001422 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001423
Ronald Crond18b5f82020-11-25 16:46:05 +01001424 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001425 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001426 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001427 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001428 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001429 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001430 key_buffer, key_buffer_size,
1431 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001432 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001433
Ronald Crond18b5f82020-11-25 16:46:05 +01001434 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001435 {
John Durkop0e005192020-10-31 22:06:54 -07001436#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1437 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001438 return( mbedtls_psa_rsa_export_public_key( attributes,
1439 key_buffer,
1440 key_buffer_size,
1441 data,
1442 data_size,
1443 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001444#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001445 /* We don't know how to convert a private RSA key to public. */
1446 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001447#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1448 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001449 }
1450 else
Moran Pekera998bc62018-04-16 18:16:20 +03001451 {
John Durkop6ba40d12020-11-10 08:50:04 -08001452#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1453 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001454 return( mbedtls_psa_ecp_export_public_key( attributes,
1455 key_buffer,
1456 key_buffer_size,
1457 data,
1458 data_size,
1459 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001460#else
1461 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001462 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001463#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1464 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001465 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001466 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001467 else
1468 {
1469 /* This shouldn't happen in the reference implementation, but
1470 it is valid for a special-purpose implementation to omit
1471 support for exporting certain key types. */
1472 return( PSA_ERROR_NOT_SUPPORTED );
1473 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001474}
Ronald Crond18b5f82020-11-25 16:46:05 +01001475
Ronald Croncf56a0a2020-08-04 09:51:30 +02001476psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001477 uint8_t *data,
1478 size_t data_size,
1479 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001480{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001481 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001482 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001483 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001484
Ronald Crone907e552021-01-18 13:22:38 +01001485 /* Reject a zero-length output buffer now, since this can never be a
1486 * valid key representation. This way we know that data must be a valid
1487 * pointer and we can do things like memset(data, ..., data_size). */
1488 if( data_size == 0 )
1489 return( PSA_ERROR_BUFFER_TOO_SMALL );
1490
Darryl Greendd8fb772018-11-07 16:00:44 +00001491 /* Set the key to empty now, so that even when there are errors, we always
1492 * set data_length to a value between 0 and data_size. On error, setting
1493 * the key to empty is a good choice because an empty key representation is
1494 * unlikely to be accepted anywhere. */
1495 *data_length = 0;
1496
1497 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001498 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001499 if( status != PSA_SUCCESS )
1500 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001501
Ronald Cron9486f9d2020-11-26 13:36:23 +01001502 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1503 {
1504 status = PSA_ERROR_INVALID_ARGUMENT;
1505 goto exit;
1506 }
1507
Ronald Crond18b5f82020-11-25 16:46:05 +01001508 psa_key_attributes_t attributes = {
1509 .core = slot->attr
1510 };
Ronald Cron67227982020-11-26 15:16:05 +01001511 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001512 &attributes, slot->key.data, slot->key.bytes,
1513 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001514
1515exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001516 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001517
Ronald Cron5c522922020-11-14 16:35:34 +01001518 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001519}
1520
Gilles Peskine91e8c332019-08-02 19:19:39 +02001521#if defined(static_assert)
1522static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1523 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001524static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001525 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001526static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001527 "One or more key attribute flag is listed as both internal-only and external-only" );
1528#endif
1529
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001530/** Validate that a key policy is internally well-formed.
1531 *
1532 * This function only rejects invalid policies. It does not validate the
1533 * consistency of the policy with respect to other attributes of the key
1534 * such as the key type.
1535 */
1536static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001537{
1538 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001539 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001540 PSA_KEY_USAGE_ENCRYPT |
1541 PSA_KEY_USAGE_DECRYPT |
gabor-mezei-arme8efa392021-04-14 21:14:28 +02001542 PSA_KEY_USAGE_SIGN_MESSAGE |
1543 PSA_KEY_USAGE_VERIFY_MESSAGE |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001544 PSA_KEY_USAGE_SIGN_HASH |
1545 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001546 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1547 return( PSA_ERROR_INVALID_ARGUMENT );
1548
Gilles Peskine4747d192019-04-17 15:05:45 +02001549 return( PSA_SUCCESS );
1550}
1551
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001552/** Validate the internal consistency of key attributes.
1553 *
1554 * This function only rejects invalid attribute values. If does not
1555 * validate the consistency of the attributes with any key data that may
1556 * be involved in the creation of the key.
1557 *
1558 * Call this function early in the key creation process.
1559 *
1560 * \param[in] attributes Key attributes for the new key.
1561 * \param[out] p_drv On any return, the driver for the key, if any.
1562 * NULL for a transparent key.
1563 *
1564 */
1565static psa_status_t psa_validate_key_attributes(
1566 const psa_key_attributes_t *attributes,
1567 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001568{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001569 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001570 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001571 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001572
Ronald Cron54b90082020-10-29 15:26:43 +01001573 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001574 if( status != PSA_SUCCESS )
1575 return( status );
1576
Ronald Crond2ed4812020-07-17 16:11:30 +02001577 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001578 if( status != PSA_SUCCESS )
1579 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001580
Ronald Cron65f38a32020-10-23 17:11:13 +02001581 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1582 {
1583 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1584 return( PSA_ERROR_INVALID_ARGUMENT );
1585 }
1586 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001587 {
Ronald Cron77e412c2021-03-31 17:36:31 +02001588 if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) )
1589 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Crond2ed4812020-07-17 16:11:30 +02001590 }
1591
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001592 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001593 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001594 return( status );
1595
1596 /* Refuse to create overly large keys.
1597 * Note that this doesn't trigger on import if the attributes don't
1598 * explicitly specify a size (so psa_get_key_bits returns 0), so
1599 * psa_import_key() needs its own checks. */
1600 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1601 return( PSA_ERROR_NOT_SUPPORTED );
1602
Gilles Peskine91e8c332019-08-02 19:19:39 +02001603 /* Reject invalid flags. These should not be reachable through the API. */
1604 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1605 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1606 return( PSA_ERROR_INVALID_ARGUMENT );
1607
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001608 return( PSA_SUCCESS );
1609}
1610
Gilles Peskine4747d192019-04-17 15:05:45 +02001611/** Prepare a key slot to receive key material.
1612 *
1613 * This function allocates a key slot and sets its metadata.
1614 *
1615 * If this function fails, call psa_fail_key_creation().
1616 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001617 * This function is intended to be used as follows:
1618 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001619 * it with the specified attributes, and in case of a volatile key assign it
1620 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001621 * -# Populate the slot with the key material.
1622 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1623 * In case of failure at any step, stop the sequence and call
1624 * psa_fail_key_creation().
1625 *
Ronald Cron5c522922020-11-14 16:35:34 +01001626 * On success, the key slot is locked. It is the responsibility of the caller
1627 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001628 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001629 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001630 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001631 * \param[out] p_slot On success, a pointer to the prepared slot.
1632 * \param[out] p_drv On any return, the driver for the key, if any.
1633 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001634 *
1635 * \retval #PSA_SUCCESS
1636 * The key slot is ready to receive key material.
1637 * \return If this function fails, the key slot is an invalid state.
1638 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001639 */
1640static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001641 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001642 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001643 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001644 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001645{
1646 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001647 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001648 psa_key_slot_t *slot;
1649
Gilles Peskinedf179142019-07-15 22:02:14 +02001650 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001651 *p_drv = NULL;
1652
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001653 status = psa_validate_key_attributes( attributes, p_drv );
1654 if( status != PSA_SUCCESS )
1655 return( status );
1656
Ronald Cronc4d1b512020-07-31 11:26:37 +02001657 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001658 if( status != PSA_SUCCESS )
1659 return( status );
1660 slot = *p_slot;
1661
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001662 /* We're storing the declared bit-size of the key. It's up to each
1663 * creation mechanism to verify that this information is correct.
1664 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001665 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001666 * is optional (import, copy). In case of a volatile key, assign it the
1667 * volatile key identifier associated to the slot returned to contain its
1668 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001669
1670 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001671 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1672 {
1673#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1674 slot->attr.id = volatile_key_id;
1675#else
1676 slot->attr.id.key_id = volatile_key_id;
1677#endif
1678 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001679
Gilles Peskine91e8c332019-08-02 19:19:39 +02001680 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001681 * external-only flags, query `attributes`. Thanks to the check
1682 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001683 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001684 * may have set. */
1685 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001686
Gilles Peskinecbaff462019-07-12 23:46:04 +02001687#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001688 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001689 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001690 * create the key file in internal storage, create the
1691 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001692 * persistent data. This is done by starting a transaction that will
1693 * encompass these three actions.
1694 * For registering a volatile key, we just need to find an appropriate
1695 * slot number inside the SE. Since the key is designated volatile, creating
1696 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001697 /* The first thing to do is to find a slot number for the new key.
1698 * We save the slot number in persistent storage as part of the
1699 * transaction data. It will be needed to recover if the power
1700 * fails during the key creation process, to clean up on the secure
1701 * element side after restarting. Obtaining a slot number from the
1702 * secure element driver updates its persistent state, but we do not yet
1703 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001704 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001705 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001706 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001707 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001708 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001709 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001710 if( status != PSA_SUCCESS )
1711 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001712
1713 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001714 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001715 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1716 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001717 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001718 psa_crypto_transaction.key.id = slot->attr.id;
1719 status = psa_crypto_save_transaction( );
1720 if( status != PSA_SUCCESS )
1721 {
1722 (void) psa_crypto_stop_transaction( );
1723 return( status );
1724 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001725 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001726
1727 status = psa_copy_key_material_into_slot(
1728 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001729 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001730
1731 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1732 {
1733 /* Key registration only makes sense with a secure element. */
1734 return( PSA_ERROR_INVALID_ARGUMENT );
1735 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001736#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1737
Ronald Cronc4d1b512020-07-31 11:26:37 +02001738 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001739}
Gilles Peskine4747d192019-04-17 15:05:45 +02001740
1741/** Finalize the creation of a key once its key material has been set.
1742 *
1743 * This entails writing the key to persistent storage.
1744 *
1745 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001746 * See the documentation of psa_start_key_creation() for the intended use
1747 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 *
Ronald Cron5c522922020-11-14 16:35:34 +01001749 * If the finalization succeeds, the function unlocks the key slot (it was
1750 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1751 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001752 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001753 * \param[in,out] slot Pointer to the slot with key material.
1754 * \param[in] driver The secure element driver for the key,
1755 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001756 * \param[out] key On success, identifier of the key. Note that the
1757 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001758 *
1759 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001760 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001761 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1762 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1763 * \retval #PSA_ERROR_ALREADY_EXISTS
1764 * \retval #PSA_ERROR_DATA_INVALID
1765 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001766 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001767 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001768 * \return If this function fails, the key slot is an invalid state.
1769 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001771static psa_status_t psa_finish_key_creation(
1772 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001773 psa_se_drv_table_entry_t *driver,
1774 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001775{
1776 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001777 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001778 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001779
1780#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001781 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001783#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1784 if( driver != NULL )
1785 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001786 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001787 psa_key_slot_number_t slot_number =
1788 psa_key_slot_get_slot_number( slot ) ;
1789
Gilles Peskineb46bef22019-07-30 21:32:04 +02001790#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001791 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001792 sizeof( data.slot_number ),
1793 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001794#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001795 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001796 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001797 (uint8_t*) &data,
1798 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001799 }
1800 else
1801#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1802 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001803 /* Key material is saved in export representation in the slot, so
1804 * just pass the slot buffer for storage. */
1805 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001806 slot->key.data,
1807 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001808 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001809 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001810#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1811
Gilles Peskinecbaff462019-07-12 23:46:04 +02001812#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001813 /* Finish the transaction for a key creation. This does not
1814 * happen when registering an existing key. Detect this case
1815 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001816 * creation of a persistent key in a secure element requires a transaction,
1817 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001818 if( driver != NULL &&
1819 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001820 {
1821 status = psa_save_se_persistent_data( driver );
1822 if( status != PSA_SUCCESS )
1823 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001824 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001825 return( status );
1826 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001827 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001828 }
1829#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1830
Ronald Cron50972942020-11-14 11:28:25 +01001831 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001832 {
1833 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001834 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001835 if( status != PSA_SUCCESS )
1836 *key = MBEDTLS_SVC_KEY_ID_INIT;
1837 }
Ronald Cron50972942020-11-14 11:28:25 +01001838
Gilles Peskine4747d192019-04-17 15:05:45 +02001839 return( status );
1840}
1841
1842/** Abort the creation of a key.
1843 *
1844 * You may call this function after calling psa_start_key_creation(),
1845 * or after psa_finish_key_creation() fails. In other circumstances, this
1846 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001847 * See the documentation of psa_start_key_creation() for the intended use
1848 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001849 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001850 * \param[in,out] slot Pointer to the slot with key material.
1851 * \param[in] driver The secure element driver for the key,
1852 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001853 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001854static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001855 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001856{
Gilles Peskine011e4282019-06-26 18:34:38 +02001857 (void) driver;
1858
Gilles Peskine4747d192019-04-17 15:05:45 +02001859 if( slot == NULL )
1860 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001861
1862#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001863 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001864 * element, and the failure happened later (when saving metadata
1865 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001866 * element.
1867 * https://github.com/ARMmbed/mbed-crypto/issues/217
1868 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001869
Gilles Peskined7729582019-08-05 15:55:54 +02001870 /* Abort the ongoing transaction if any (there may not be one if
1871 * the creation process failed before starting one, or if the
1872 * key creation is a registration of a key in a secure element).
1873 * Earlier functions must already have done what it takes to undo any
1874 * partial creation. All that's left is to update the transaction data
1875 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001876 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001877#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1878
Gilles Peskine4747d192019-04-17 15:05:45 +02001879 psa_wipe_key_slot( slot );
1880}
1881
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001882/** Validate optional attributes during key creation.
1883 *
1884 * Some key attributes are optional during key creation. If they are
1885 * specified in the attributes structure, check that they are consistent
1886 * with the data in the slot.
1887 *
1888 * This function should be called near the end of key creation, after
1889 * the slot in memory is fully populated but before saving persistent data.
1890 */
1891static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001892 const psa_key_slot_t *slot,
1893 const psa_key_attributes_t *attributes )
1894{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001895 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001897 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 return( PSA_ERROR_INVALID_ARGUMENT );
1899 }
1900
1901 if( attributes->domain_parameters_size != 0 )
1902 {
John Durkop0e005192020-10-31 22:06:54 -07001903#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1904 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001905 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001906 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001907 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001908 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001910
Ronald Cron90857082020-11-25 14:58:33 +01001911 psa_status_t status = mbedtls_psa_rsa_load_representation(
1912 slot->attr.type,
1913 slot->key.data,
1914 slot->key.bytes,
1915 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001916 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001917 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001918
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001919 mbedtls_mpi_init( &actual );
1920 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001921 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001922 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001923 mbedtls_rsa_free( rsa );
1924 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001925 if( ret != 0 )
1926 goto rsa_exit;
1927 ret = mbedtls_mpi_read_binary( &required,
1928 attributes->domain_parameters,
1929 attributes->domain_parameters_size );
1930 if( ret != 0 )
1931 goto rsa_exit;
1932 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1933 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1934 rsa_exit:
1935 mbedtls_mpi_free( &actual );
1936 mbedtls_mpi_free( &required );
1937 if( ret != 0)
1938 return( mbedtls_to_psa_error( ret ) );
1939 }
1940 else
John Durkop9814fa22020-11-04 12:28:15 -08001941#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1942 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001943 {
1944 return( PSA_ERROR_INVALID_ARGUMENT );
1945 }
1946 }
1947
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001948 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001950 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 return( PSA_ERROR_INVALID_ARGUMENT );
1952 }
1953
1954 return( PSA_SUCCESS );
1955}
1956
Gilles Peskine4747d192019-04-17 15:05:45 +02001957psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001958 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001959 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001960 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001961{
1962 psa_status_t status;
1963 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001964 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001965 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001966
Ronald Cron81709fc2020-11-14 12:10:32 +01001967 *key = MBEDTLS_SVC_KEY_ID_INIT;
1968
Gilles Peskine0f84d622019-09-12 19:03:13 +02001969 /* Reject zero-length symmetric keys (including raw data key objects).
1970 * This also rejects any key which might be encoded as an empty string,
1971 * which is never valid. */
1972 if( data_length == 0 )
1973 return( PSA_ERROR_INVALID_ARGUMENT );
1974
Gilles Peskinedf179142019-07-15 22:02:14 +02001975 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001976 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001977 if( status != PSA_SUCCESS )
1978 goto exit;
1979
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001980 /* In the case of a transparent key or an opaque key stored in local
1981 * storage (thus not in the case of generating a key in a secure element
1982 * or cryptoprocessor with storage), we have to allocate a buffer to
1983 * hold the generated key material. */
1984 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001985 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001986 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001987 if( status != PSA_SUCCESS )
1988 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001989 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001990
1991 bits = slot->attr.bits;
1992 status = psa_driver_wrapper_import_key( attributes,
1993 data, data_length,
1994 slot->key.data,
1995 slot->key.bytes,
1996 &slot->key.bytes, &bits );
1997 if( status != PSA_SUCCESS )
1998 goto exit;
1999
2000 if( slot->attr.bits == 0 )
2001 slot->attr.bits = (psa_key_bits_t) bits;
2002 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01002003 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002004 status = PSA_ERROR_INVALID_ARGUMENT;
2005 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002006 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002007
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002008 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002009 if( status != PSA_SUCCESS )
2010 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011
Ronald Cron81709fc2020-11-14 12:10:32 +01002012 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002013exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002014 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002015 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002016
Gilles Peskine4747d192019-04-17 15:05:45 +02002017 return( status );
2018}
2019
Gilles Peskined7729582019-08-05 15:55:54 +02002020#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2021psa_status_t mbedtls_psa_register_se_key(
2022 const psa_key_attributes_t *attributes )
2023{
2024 psa_status_t status;
2025 psa_key_slot_t *slot = NULL;
2026 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002027 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002028
2029 /* Leaving attributes unspecified is not currently supported.
2030 * It could make sense to query the key type and size from the
2031 * secure element, but not all secure elements support this
2032 * and the driver HAL doesn't currently support it. */
2033 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2034 return( PSA_ERROR_NOT_SUPPORTED );
2035 if( psa_get_key_bits( attributes ) == 0 )
2036 return( PSA_ERROR_NOT_SUPPORTED );
2037
2038 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002039 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002040 if( status != PSA_SUCCESS )
2041 goto exit;
2042
Ronald Cron81709fc2020-11-14 12:10:32 +01002043 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002044
2045exit:
2046 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002047 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002048
Gilles Peskined7729582019-08-05 15:55:54 +02002049 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002050 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002051 return( status );
2052}
2053#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2054
Gilles Peskinef603c712019-01-19 13:40:11 +01002055static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002056 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002057{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002058 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002059 source->key.data,
2060 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002061 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002062 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002063
Steven Cooreman398aee52020-10-13 14:35:45 +02002064 target->attr.type = source->attr.type;
2065 target->attr.bits = source->attr.bits;
2066
2067 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002068}
2069
Ronald Croncf56a0a2020-08-04 09:51:30 +02002070psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002071 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002072 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002073{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002074 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002075 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002076 psa_key_slot_t *source_slot = NULL;
2077 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002078 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002079 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002080
Ronald Cron81709fc2020-11-14 12:10:32 +01002081 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2082
Ronald Cron5c522922020-11-14 16:35:34 +01002083 status = psa_get_and_lock_transparent_key_slot_with_policy(
2084 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002085 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002086 goto exit;
2087
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002088 status = psa_validate_optional_attributes( source_slot,
2089 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002090 if( status != PSA_SUCCESS )
2091 goto exit;
2092
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002093 status = psa_restrict_key_policy( source_slot->attr.type,
2094 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002095 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002096 if( status != PSA_SUCCESS )
2097 goto exit;
2098
Ronald Cron81709fc2020-11-14 12:10:32 +01002099 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2100 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002101 if( status != PSA_SUCCESS )
2102 goto exit;
2103
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002104#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2105 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002106 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002107 /* Copying to a secure element is not implemented yet. */
2108 status = PSA_ERROR_NOT_SUPPORTED;
2109 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002110 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002111#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002112
Ronald Cron6cc66312021-04-02 12:27:47 +02002113 if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) )
2114 {
2115 /*
2116 * Copying through an opaque driver is not implemented yet, consider
2117 * a lifetime with an external location as an invalid parameter for
2118 * now.
2119 */
2120 status = PSA_ERROR_INVALID_ARGUMENT;
2121 goto exit;
2122 }
2123
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002124 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002125 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002126 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002127
Ronald Cron81709fc2020-11-14 12:10:32 +01002128 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002129exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002130 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002131 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002132
Ronald Cron5c522922020-11-14 16:35:34 +01002133 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002134
Ronald Cron5c522922020-11-14 16:35:34 +01002135 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002136}
2137
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002138
2139
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002140/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002141/* Message digests */
2142/****************************************************************/
2143
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002144psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2145{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002146 /* Aborting a non-active operation is allowed */
2147 if( operation->id == 0 )
2148 return( PSA_SUCCESS );
2149
2150 psa_status_t status = psa_driver_wrapper_hash_abort( operation );
2151 operation->id = 0;
2152
2153 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002154}
2155
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002156psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002157 psa_algorithm_t alg )
2158{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002159 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002160
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002161 /* A context must be freshly initialized before it can be set up. */
2162 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002163 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002164 status = PSA_ERROR_BAD_STATE;
2165 goto exit;
2166 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002167
2168 if( !PSA_ALG_IS_HASH( alg ) )
Dave Rodgmandb861792021-06-24 16:17:43 +01002169 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002170 status = PSA_ERROR_INVALID_ARGUMENT;
2171 goto exit;
2172 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002173
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002174 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2175 * directly zeroes the int-sized dummy member of the context union. */
Steven Cooreman893232f2021-03-15 12:23:37 +01002176 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
2177
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002178 status = psa_driver_wrapper_hash_setup( operation, alg );
2179
2180exit:
2181 if( status != PSA_SUCCESS )
Dave Rodgmandb861792021-06-24 16:17:43 +01002182 psa_hash_abort( operation );
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002183
2184 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002185}
2186
2187psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2188 const uint8_t *input,
2189 size_t input_length )
2190{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2192
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002193 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002194 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002195 status = PSA_ERROR_BAD_STATE;
2196 goto exit;
2197 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002198
Steven Cooremanc8288352021-03-04 14:02:19 +01002199 /* Don't require hash implementations to behave correctly on a
2200 * zero-length input, which may have an invalid pointer. */
2201 if( input_length == 0 )
2202 return( PSA_SUCCESS );
2203
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002204 status = psa_driver_wrapper_hash_update( operation, input, input_length );
2205
2206exit:
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002207 if( status != PSA_SUCCESS )
2208 psa_hash_abort( operation );
2209
2210 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211}
2212
2213psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2214 uint8_t *hash,
2215 size_t hash_size,
2216 size_t *hash_length )
2217{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002218 *hash_length = 0;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002219 if( operation->id == 0 )
2220 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221
Steven Cooreman1e582352021-02-18 17:24:37 +01002222 psa_status_t status = psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002223 operation, hash, hash_size, hash_length );
Steven Cooreman0e307642021-02-18 16:18:32 +01002224 psa_hash_abort( operation );
2225 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002226}
2227
Gilles Peskine2d277862018-06-18 15:41:12 +02002228psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2229 const uint8_t *hash,
2230 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002231{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002232 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002233 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002234 psa_status_t status = psa_hash_finish(
2235 operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01002236 actual_hash, sizeof( actual_hash ),
2237 &actual_hash_length );
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002238
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 if( status != PSA_SUCCESS )
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002240 goto exit;
2241
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242 if( actual_hash_length != hash_length )
Dave Rodgmandb861792021-06-24 16:17:43 +01002243 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002244 status = PSA_ERROR_INVALID_SIGNATURE;
2245 goto exit;
2246 }
2247
Steven Cooremana2a1b802021-04-29 16:37:59 +02002248 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002249 status = PSA_ERROR_INVALID_SIGNATURE;
2250
2251exit:
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002252 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002253 if( status != PSA_SUCCESS )
2254 psa_hash_abort(operation);
2255
2256 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257}
2258
Gilles Peskine0a749c82019-11-28 19:33:58 +01002259psa_status_t psa_hash_compute( psa_algorithm_t alg,
2260 const uint8_t *input, size_t input_length,
2261 uint8_t *hash, size_t hash_size,
2262 size_t *hash_length )
2263{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002264 *hash_length = 0;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002265 if( !PSA_ALG_IS_HASH( alg ) )
2266 return( PSA_ERROR_INVALID_ARGUMENT );
2267
Steven Cooreman1e582352021-02-18 17:24:37 +01002268 return( psa_driver_wrapper_hash_compute( alg, input, input_length,
2269 hash, hash_size, hash_length ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002270}
2271
2272psa_status_t psa_hash_compare( psa_algorithm_t alg,
2273 const uint8_t *input, size_t input_length,
2274 const uint8_t *hash, size_t hash_length )
2275{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002276 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002277 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002278
2279 if( !PSA_ALG_IS_HASH( alg ) )
2280 return( PSA_ERROR_INVALID_ARGUMENT );
2281
Steven Cooreman1e582352021-02-18 17:24:37 +01002282 psa_status_t status = psa_driver_wrapper_hash_compute(
2283 alg, input, input_length,
2284 actual_hash, sizeof(actual_hash),
2285 &actual_hash_length );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002286 if( status != PSA_SUCCESS )
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002287 goto exit;
Steven Cooreman84d670d2021-02-18 16:22:53 +01002288 if( actual_hash_length != hash_length )
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002289 {
2290 status = PSA_ERROR_INVALID_SIGNATURE;
2291 goto exit;
2292 }
Steven Cooremana2a1b802021-04-29 16:37:59 +02002293 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002294 status = PSA_ERROR_INVALID_SIGNATURE;
2295
2296exit:
2297 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
2298 return( status );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002299}
2300
Gilles Peskineeb35d782019-01-22 17:56:16 +01002301psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2302 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002303{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002304 if( source_operation->id == 0 ||
2305 target_operation->id != 0 )
2306 {
2307 return( PSA_ERROR_BAD_STATE );
2308 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002309
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002310 psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
2311 target_operation );
2312 if( status != PSA_SUCCESS )
2313 psa_hash_abort( target_operation );
2314
2315 return( status );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002316}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002317
2318
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002320/* MAC */
2321/****************************************************************/
2322
Steven Cooreman07897832021-03-19 18:28:56 +01002323psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002324{
Steven Cooreman07897832021-03-19 18:28:56 +01002325 /* Aborting a non-active operation is allowed */
2326 if( operation->id == 0 )
2327 return( PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002328
Steven Cooreman07897832021-03-19 18:28:56 +01002329 psa_status_t status = psa_driver_wrapper_mac_abort( operation );
Steven Cooreman15f0d922021-05-07 14:14:37 +02002330 operation->mac_size = 0;
2331 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002332 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002333
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002334 return( status );
2335}
2336
Ronald Cron1c650a12021-06-17 16:33:22 +02002337static psa_status_t psa_mac_finalize_alg_and_key_validation(
2338 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002339 const psa_key_attributes_t *attributes,
2340 uint8_t *mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002341{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondef68e72021-06-17 16:46:44 +02002343 psa_key_type_t key_type = psa_get_key_type( attributes );
2344 size_t key_bits = psa_get_key_bits( attributes );
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002345
Ronald Cron48f875e2021-06-17 16:10:24 +02002346 if( ! PSA_ALG_IS_MAC( alg ) )
Ronald Crondef68e72021-06-17 16:46:44 +02002347 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman07897832021-03-19 18:28:56 +01002348
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002349 /* Validate the combination of key type and algorithm */
Ronald Crondef68e72021-06-17 16:46:44 +02002350 status = psa_mac_key_can_do( alg, key_type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002351 if( status != PSA_SUCCESS )
Ronald Crondef68e72021-06-17 16:46:44 +02002352 return( status );
Steven Cooreman15472f82021-03-02 16:16:22 +01002353
Steven Cooremana6474de2021-05-10 11:13:41 +02002354 /* Get the output length for the algorithm and key combination */
Ronald Crondef68e72021-06-17 16:46:44 +02002355 *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002356
Ronald Crondef68e72021-06-17 16:46:44 +02002357 if( *mac_size < 4 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002358 {
2359 /* A very short MAC is too short for security since it can be
2360 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2361 * so we make this our minimum, even though 32 bits is still
2362 * too small for security. */
Ronald Crondef68e72021-06-17 16:46:44 +02002363 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman15472f82021-03-02 16:16:22 +01002364 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002365
Ronald Crondef68e72021-06-17 16:46:44 +02002366 if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits,
2367 PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002368 {
2369 /* It's impossible to "truncate" to a larger length than the full length
2370 * of the algorithm. */
Ronald Crondef68e72021-06-17 16:46:44 +02002371 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002372 }
2373
Gilles Peskinebc795822022-03-16 13:54:49 +01002374 if( *mac_size > PSA_MAC_MAX_SIZE )
2375 {
2376 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2377 * that is disabled in the compile-time configuration. The result can
2378 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2379 * configuration into account. In this case, force a return of
2380 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2381 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2382 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2383 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2384 * systematically generated tests. */
2385 return( PSA_ERROR_NOT_SUPPORTED );
2386 }
2387
Ronald Crondef68e72021-06-17 16:46:44 +02002388 return( PSA_SUCCESS );
Ronald Cron1c650a12021-06-17 16:33:22 +02002389}
2390
2391static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
2392 mbedtls_svc_key_id_t key,
2393 psa_algorithm_t alg,
2394 int is_sign )
2395{
2396 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2397 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002398 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002399
2400 /* A context must be freshly initialized before it can be set up. */
2401 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002402 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002403 status = PSA_ERROR_BAD_STATE;
2404 goto exit;
2405 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002406
2407 status = psa_get_and_lock_key_slot_with_policy(
2408 key,
2409 &slot,
Mateusz Starzykdd55b252021-08-17 15:24:32 +02002410 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Cron1c650a12021-06-17 16:33:22 +02002411 alg );
2412 if( status != PSA_SUCCESS )
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002413 goto exit;
Ronald Cron1c650a12021-06-17 16:33:22 +02002414
2415 psa_key_attributes_t attributes = {
2416 .core = slot->attr
2417 };
2418
Ronald Crondef68e72021-06-17 16:46:44 +02002419 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2420 &operation->mac_size );
Ronald Cron1c650a12021-06-17 16:33:22 +02002421 if( status != PSA_SUCCESS )
2422 goto exit;
2423
2424 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002425 /* Dispatch the MAC setup call with validated input */
2426 if( is_sign )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002427 {
Steven Cooreman07897832021-03-19 18:28:56 +01002428 status = psa_driver_wrapper_mac_sign_setup( operation,
Ronald Cron1c650a12021-06-17 16:33:22 +02002429 &attributes,
Steven Cooreman07897832021-03-19 18:28:56 +01002430 slot->key.data,
2431 slot->key.bytes,
2432 alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002433 }
2434 else
Gilles Peskinefbfac682018-07-08 20:51:54 +02002435 {
Steven Cooreman07897832021-03-19 18:28:56 +01002436 status = psa_driver_wrapper_mac_verify_setup( operation,
Ronald Cron1c650a12021-06-17 16:33:22 +02002437 &attributes,
Steven Cooreman07897832021-03-19 18:28:56 +01002438 slot->key.data,
2439 slot->key.bytes,
2440 alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002442
Gilles Peskinefbfac682018-07-08 20:51:54 +02002443exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002444 if( status != PSA_SUCCESS )
Steven Cooreman07897832021-03-19 18:28:56 +01002445 psa_mac_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002446
Ronald Cron5c522922020-11-14 16:35:34 +01002447 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002448
Ronald Cron5c522922020-11-14 16:35:34 +01002449 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450}
2451
Gilles Peskine89167cb2018-07-08 20:12:23 +02002452psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002453 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002454 psa_algorithm_t alg )
2455{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002456 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002457}
2458
2459psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002460 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002461 psa_algorithm_t alg )
2462{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002463 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002464}
2465
Steven Cooreman11743f92021-03-19 18:38:46 +01002466psa_status_t psa_mac_update( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002467 const uint8_t *input,
2468 size_t input_length )
2469{
Steven Cooreman11743f92021-03-19 18:38:46 +01002470 if( operation->id == 0 )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002471 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472
Steven Cooreman11743f92021-03-19 18:38:46 +01002473 /* Don't require hash implementations to behave correctly on a
2474 * zero-length input, which may have an invalid pointer. */
2475 if( input_length == 0 )
2476 return( PSA_SUCCESS );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002477
Steven Cooreman11743f92021-03-19 18:38:46 +01002478 psa_status_t status = psa_driver_wrapper_mac_update( operation,
2479 input, input_length );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002480 if( status != PSA_SUCCESS )
Steven Cooreman11743f92021-03-19 18:38:46 +01002481 psa_mac_abort( operation );
2482
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002483 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484}
2485
Steven Cooreman87885df2021-03-19 19:04:39 +01002486psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002487 uint8_t *mac,
2488 size_t mac_size,
2489 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002490{
Steven Cooreman87885df2021-03-19 19:04:39 +01002491 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2492 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002493
Steven Cooreman87885df2021-03-19 19:04:39 +01002494 if( operation->id == 0 )
Dave Rodgmand73e1b02021-06-24 16:19:08 +01002495 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002496 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002497 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002498 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002499
Steven Cooreman15f0d922021-05-07 14:14:37 +02002500 if( ! operation->is_sign )
Dave Rodgmandb861792021-06-24 16:17:43 +01002501 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002502 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002503 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002504 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002505
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002506 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2507 * once all the error checks are done. */
2508 if( operation->mac_size == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002509 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002510 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002511 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002512 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002513
2514 if( mac_size < operation->mac_size )
Dave Rodgmandb861792021-06-24 16:17:43 +01002515 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002516 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002517 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002518 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002519
Steven Cooreman87885df2021-03-19 19:04:39 +01002520 status = psa_driver_wrapper_mac_sign_finish( operation,
Steven Cooreman15f0d922021-05-07 14:14:37 +02002521 mac, operation->mac_size,
2522 mac_length );
2523
Dave Rodgman478ab542021-06-25 09:09:02 +01002524exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002525 /* In case of success, set the potential excess room in the output buffer
2526 * to an invalid value, to avoid potentially leaking a longer MAC.
2527 * In case of error, set the output length and content to a safe default,
2528 * such that in case the caller misses an error check, the output would be
2529 * an unachievable MAC.
2530 */
2531 if( status != PSA_SUCCESS )
Steven Cooreman15f0d922021-05-07 14:14:37 +02002532 {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002533 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002534 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002535 }
mohammad16036df908f2018-04-02 08:34:15 -07002536
Ronald Cron882eb782021-06-18 13:05:48 +02002537 if( mac_size > operation->mac_size )
2538 memset( &mac[operation->mac_size], '!',
2539 mac_size - operation->mac_size );
2540
Steven Cooreman87885df2021-03-19 19:04:39 +01002541 abort_status = psa_mac_abort( operation );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002542
Steven Cooreman87885df2021-03-19 19:04:39 +01002543 return( status == PSA_SUCCESS ? abort_status : status );
mohammad16036df908f2018-04-02 08:34:15 -07002544}
2545
Steven Cooreman87885df2021-03-19 19:04:39 +01002546psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002547 const uint8_t *mac,
2548 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549{
Steven Cooreman87885df2021-03-19 19:04:39 +01002550 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2551 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002552
Steven Cooreman87885df2021-03-19 19:04:39 +01002553 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002554 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002555 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002556 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002557 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002558
Steven Cooreman15f0d922021-05-07 14:14:37 +02002559 if( operation->is_sign )
Dave Rodgmandb861792021-06-24 16:17:43 +01002560 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002561 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002562 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002563 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002564
2565 if( operation->mac_size != mac_length )
2566 {
2567 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002568 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002569 }
2570
Steven Cooreman87885df2021-03-19 19:04:39 +01002571 status = psa_driver_wrapper_mac_verify_finish( operation,
2572 mac, mac_length );
Steven Cooreman15f0d922021-05-07 14:14:37 +02002573
Dave Rodgman478ab542021-06-25 09:09:02 +01002574exit:
Steven Cooreman87885df2021-03-19 19:04:39 +01002575 abort_status = psa_mac_abort( operation );
mohammad16036df908f2018-04-02 08:34:15 -07002576
Steven Cooreman87885df2021-03-19 19:04:39 +01002577 return( status == PSA_SUCCESS ? abort_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002578}
2579
Ronald Cronef0d8f12021-06-18 14:23:33 +02002580static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key,
2581 psa_algorithm_t alg,
2582 const uint8_t *input,
2583 size_t input_length,
2584 uint8_t *mac,
2585 size_t mac_size,
2586 size_t *mac_length,
2587 int is_sign )
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002588{
2589 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002590 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2591 psa_key_slot_t *slot;
2592 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593
Ronald Crondbb86462021-06-17 17:17:20 +02002594 status = psa_get_and_lock_key_slot_with_policy(
Mateusz Starzykdd55b252021-08-17 15:24:32 +02002595 key,
2596 &slot,
2597 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Cronef0d8f12021-06-18 14:23:33 +02002598 alg );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002599 if( status != PSA_SUCCESS )
2600 goto exit;
2601
Ronald Crondbb86462021-06-17 17:17:20 +02002602 psa_key_attributes_t attributes = {
2603 .core = slot->attr
2604 };
2605
2606 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2607 &operation_mac_size );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002608 if( status != PSA_SUCCESS )
2609 goto exit;
2610
Ronald Crondbb86462021-06-17 17:17:20 +02002611 if( mac_size < operation_mac_size )
2612 {
2613 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002614 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002615 }
2616
2617 status = psa_driver_wrapper_mac_compute(
2618 &attributes,
2619 slot->key.data, slot->key.bytes,
2620 alg,
2621 input, input_length,
2622 mac, operation_mac_size, mac_length );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002623
2624exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002625 /* In case of success, set the potential excess room in the output buffer
2626 * to an invalid value, to avoid potentially leaking a longer MAC.
2627 * In case of error, set the output length and content to a safe default,
2628 * such that in case the caller misses an error check, the output would be
2629 * an unachievable MAC.
2630 */
2631 if( status != PSA_SUCCESS )
Ronald Crondbb86462021-06-17 17:17:20 +02002632 {
Ronald Crondbb86462021-06-17 17:17:20 +02002633 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002634 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002635 }
Ronald Cron882eb782021-06-18 13:05:48 +02002636 if( mac_size > operation_mac_size )
2637 memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002638
Ronald Crondbb86462021-06-17 17:17:20 +02002639 unlock_status = psa_unlock_key_slot( slot );
2640
2641 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002642}
2643
Ronald Cronef0d8f12021-06-18 14:23:33 +02002644psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key,
2645 psa_algorithm_t alg,
2646 const uint8_t *input,
2647 size_t input_length,
2648 uint8_t *mac,
2649 size_t mac_size,
2650 size_t *mac_length)
2651{
2652 return( psa_mac_compute_internal( key, alg,
2653 input, input_length,
2654 mac, mac_size, mac_length, 1 ) );
2655}
2656
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002657psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key,
2658 psa_algorithm_t alg,
2659 const uint8_t *input,
2660 size_t input_length,
2661 const uint8_t *mac,
2662 size_t mac_length)
2663{
2664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002665 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2666 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002667
Ronald Cron4ed83682021-06-18 14:51:29 +02002668 status = psa_mac_compute_internal( key, alg,
2669 input, input_length,
2670 actual_mac, sizeof( actual_mac ),
2671 &actual_mac_length, 0 );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002672 if( status != PSA_SUCCESS )
2673 goto exit;
2674
Ronald Cron4ed83682021-06-18 14:51:29 +02002675 if( mac_length != actual_mac_length )
2676 {
2677 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002678 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002679 }
2680 if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
2681 {
2682 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002683 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002684 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002685
2686exit:
Ronald Cron4ed83682021-06-18 14:51:29 +02002687 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002688
2689 return ( status );
2690}
Gilles Peskine20035e32018-02-03 22:44:14 +01002691
Gilles Peskine20035e32018-02-03 22:44:14 +01002692/****************************************************************/
2693/* Asymmetric cryptography */
2694/****************************************************************/
2695
gabor-mezei-armc9795782021-05-12 11:03:09 +02002696static psa_status_t psa_sign_verify_check_alg( int input_is_message,
gabor-mezei-arm56980482021-05-05 13:56:27 +02002697 psa_algorithm_t alg )
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002698{
gabor-mezei-armc9795782021-05-12 11:03:09 +02002699 if( input_is_message )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002700 {
2701 if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) )
2702 return( PSA_ERROR_INVALID_ARGUMENT );
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002703
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002704 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002705 {
2706 if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) )
2707 return( PSA_ERROR_INVALID_ARGUMENT );
2708 }
2709 }
2710 else
2711 {
Mateusz Starzykd22362c2021-08-26 11:46:14 +02002712 if( ! PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002713 return( PSA_ERROR_INVALID_ARGUMENT );
2714 }
2715
2716 return( PSA_SUCCESS );
2717}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002718
2719static psa_status_t psa_sign_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002720 int input_is_message,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002721 psa_algorithm_t alg,
2722 const uint8_t * input,
2723 size_t input_length,
2724 uint8_t * signature,
2725 size_t signature_size,
2726 size_t * signature_length )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002727{
2728 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2729 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2730 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002731
2732 *signature_length = 0;
2733
gabor-mezei-armc9795782021-05-12 11:03:09 +02002734 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm56980482021-05-05 13:56:27 +02002735 if( status != PSA_SUCCESS )
2736 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002737
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002738 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002739 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002740 * buffer can in principle be empty since it doesn't actually have
2741 * to be a hash.) */
2742 if( signature_size == 0 )
2743 return( PSA_ERROR_BUFFER_TOO_SMALL );
2744
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002745 status = psa_get_and_lock_key_slot_with_policy(
2746 key, &slot,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002747 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2748 PSA_KEY_USAGE_SIGN_HASH,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002749 alg );
2750
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002751 if( status != PSA_SUCCESS )
2752 goto exit;
2753
2754 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
2755 {
2756 status = PSA_ERROR_INVALID_ARGUMENT;
2757 goto exit;
2758 }
2759
2760 psa_key_attributes_t attributes = {
2761 .core = slot->attr
2762 };
2763
gabor-mezei-armc9795782021-05-12 11:03:09 +02002764 if( input_is_message )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002765 {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002766 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002767 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002768 alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002769 signature, signature_size, signature_length );
2770 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002771 else
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002772 {
2773
2774 status = psa_driver_wrapper_sign_hash(
2775 &attributes, slot->key.data, slot->key.bytes,
2776 alg, input, input_length,
2777 signature, signature_size, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002778 }
2779
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002780
2781exit:
2782 /* Fill the unused part of the output buffer (the whole buffer on error,
2783 * the trailing part on success) with something that isn't a valid signature
2784 * (barring an attack on the signature and deliberately-crafted input),
2785 * in case the caller doesn't check the return status properly. */
2786 if( status == PSA_SUCCESS )
2787 memset( signature + *signature_length, '!',
2788 signature_size - *signature_length );
2789 else
2790 memset( signature, '!', signature_size );
2791 /* If signature_size is 0 then we have nothing to do. We must not call
2792 * memset because signature may be NULL in this case. */
2793
2794 unlock_status = psa_unlock_key_slot( slot );
2795
2796 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
2797}
2798
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002799static psa_status_t psa_verify_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002800 int input_is_message,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002801 psa_algorithm_t alg,
2802 const uint8_t * input,
2803 size_t input_length,
2804 const uint8_t * signature,
2805 size_t signature_length )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002806{
2807 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2808 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2809 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002810
gabor-mezei-armc9795782021-05-12 11:03:09 +02002811 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm56980482021-05-05 13:56:27 +02002812 if( status != PSA_SUCCESS )
2813 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002814
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002815 status = psa_get_and_lock_key_slot_with_policy(
2816 key, &slot,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002817 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2818 PSA_KEY_USAGE_VERIFY_HASH,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002819 alg );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002820
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002821 if( status != PSA_SUCCESS )
2822 return( status );
2823
2824 psa_key_attributes_t attributes = {
2825 .core = slot->attr
2826 };
2827
gabor-mezei-armc9795782021-05-12 11:03:09 +02002828 if( input_is_message )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002829 {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002830 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002831 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002832 alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002833 signature, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002834 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002835 else
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002836 {
2837 status = psa_driver_wrapper_verify_hash(
2838 &attributes, slot->key.data, slot->key.bytes,
2839 alg, input, input_length,
2840 signature, signature_length );
2841 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002842
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002843 unlock_status = psa_unlock_key_slot( slot );
2844
2845 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002846
2847}
2848
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002849psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002850 const psa_key_attributes_t *attributes,
2851 const uint8_t *key_buffer,
2852 size_t key_buffer_size,
2853 psa_algorithm_t alg,
2854 const uint8_t *input,
2855 size_t input_length,
2856 uint8_t *signature,
2857 size_t signature_size,
2858 size_t *signature_length )
2859{
2860 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002861
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002862 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002863 {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002864 size_t hash_length;
2865 uint8_t hash[PSA_HASH_MAX_SIZE];
2866
gabor-mezei-armbfbe4652021-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-armc53f4f62021-04-22 11:32:19 +02002871
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002872 if( status != PSA_SUCCESS )
2873 return status;
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002874
2875 return psa_driver_wrapper_sign_hash(
2876 attributes, key_buffer, key_buffer_size,
2877 alg, hash, hash_length,
2878 signature, signature_size, signature_length );
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002879 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002880
gabor-mezei-arm56980482021-05-05 13:56:27 +02002881 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002882}
2883
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002884psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
2885 psa_algorithm_t alg,
2886 const uint8_t * input,
2887 size_t input_length,
2888 uint8_t * signature,
2889 size_t signature_size,
2890 size_t * signature_length )
2891{
2892 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002893 key, 1, alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002894 signature, signature_size, signature_length );
2895}
2896
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002897psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002898 const psa_key_attributes_t *attributes,
2899 const uint8_t *key_buffer,
2900 size_t key_buffer_size,
2901 psa_algorithm_t alg,
2902 const uint8_t *input,
2903 size_t input_length,
2904 const uint8_t *signature,
2905 size_t signature_length )
2906{
2907 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002908
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002909 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002910 {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002911 size_t hash_length;
2912 uint8_t hash[PSA_HASH_MAX_SIZE];
2913
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002914 status = psa_driver_wrapper_hash_compute(
2915 PSA_ALG_SIGN_GET_HASH( alg ),
2916 input, input_length,
2917 hash, sizeof( hash ), &hash_length );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002918
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002919 if( status != PSA_SUCCESS )
2920 return status;
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002921
2922 return psa_driver_wrapper_verify_hash(
2923 attributes, key_buffer, key_buffer_size,
2924 alg, hash, hash_length,
2925 signature, signature_length );
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002926 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002927
gabor-mezei-armef6f2aa2021-05-05 13:59:01 +02002928 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002929}
2930
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002931psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
2932 psa_algorithm_t alg,
2933 const uint8_t * input,
2934 size_t input_length,
2935 const uint8_t * signature,
2936 size_t signature_length )
2937{
2938 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002939 key, 1, alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002940 signature, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002941}
2942
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002943psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002944 const psa_key_attributes_t *attributes,
2945 const uint8_t *key_buffer, size_t key_buffer_size,
2946 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2947 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002948{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002949 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002950 {
Ronald Cron4501c982021-03-19 20:09:32 +01002951 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2952 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002953 {
Ronald Cron4501c982021-03-19 20:09:32 +01002954#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2955 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2956 return( mbedtls_psa_rsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002957 attributes,
2958 key_buffer, key_buffer_size,
2959 alg, hash, hash_length,
2960 signature, signature_size, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002961#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2962 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002963 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002964 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002965 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002966 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002967 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002968 }
Ronald Cron4bcccc62021-04-09 15:32:03 +02002969 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Ronald Cron4501c982021-03-19 20:09:32 +01002970 {
2971 if( PSA_ALG_IS_ECDSA( alg ) )
2972 {
2973#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2974 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2975 return( mbedtls_psa_ecdsa_sign_hash(
2976 attributes,
2977 key_buffer, key_buffer_size,
2978 alg, hash, hash_length,
2979 signature, signature_size, signature_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002980#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2981 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron4501c982021-03-19 20:09:32 +01002982 }
2983 else
2984 {
2985 return( PSA_ERROR_INVALID_ARGUMENT );
2986 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002987 }
Ronald Cron4501c982021-03-19 20:09:32 +01002988
2989 (void)key_buffer;
2990 (void)key_buffer_size;
2991 (void)hash;
2992 (void)hash_length;
2993 (void)signature;
2994 (void)signature_size;
2995 (void)signature_length;
2996
2997 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01002998}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002999
3000psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
3001 psa_algorithm_t alg,
3002 const uint8_t *hash,
3003 size_t hash_length,
3004 uint8_t *signature,
3005 size_t signature_size,
3006 size_t *signature_length )
3007{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003008 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003009 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01003010 signature, signature_size, signature_length );
itayzafrir5c753392018-05-08 11:18:38 +03003011}
3012
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003013psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003014 const psa_key_attributes_t *attributes,
3015 const uint8_t *key_buffer, size_t key_buffer_size,
3016 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
3017 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003018{
Ronald Cron99b8ed72021-02-17 10:33:32 +01003019 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003020 {
Ronald Cron4501c982021-03-19 20:09:32 +01003021 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
3022 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003023 {
Ronald Cron4501c982021-03-19 20:09:32 +01003024#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3025 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3026 return( mbedtls_psa_rsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01003027 attributes,
3028 key_buffer, key_buffer_size,
3029 alg, hash, hash_length,
3030 signature, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01003031#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3032 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02003033 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003034 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003035 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003036 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003037 }
itayzafrir5c753392018-05-08 11:18:38 +03003038 }
Ronald Cron4bcccc62021-04-09 15:32:03 +02003039 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003040 {
Ronald Cron4501c982021-03-19 20:09:32 +01003041 if( PSA_ALG_IS_ECDSA( alg ) )
3042 {
3043#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3044 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3045 return( mbedtls_psa_ecdsa_verify_hash(
3046 attributes,
3047 key_buffer, key_buffer_size,
3048 alg, hash, hash_length,
3049 signature, signature_length ) );
3050#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3051 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3052 }
3053 else
3054 {
3055 return( PSA_ERROR_INVALID_ARGUMENT );
3056 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003057 }
Ronald Cron4501c982021-03-19 20:09:32 +01003058
3059 (void)key_buffer;
3060 (void)key_buffer_size;
3061 (void)hash;
3062 (void)hash_length;
3063 (void)signature;
3064 (void)signature_length;
3065
3066 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01003067}
3068
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003069psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3070 psa_algorithm_t alg,
3071 const uint8_t *hash,
3072 size_t hash_length,
3073 const uint8_t *signature,
3074 size_t signature_length )
3075{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003076 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003077 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01003078 signature, signature_length );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003079}
3080
John Durkop0e005192020-10-31 22:06:54 -07003081#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003082static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3083 mbedtls_rsa_context *rsa )
3084{
3085 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3086 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3087 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3088 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3089}
John Durkop0e005192020-10-31 22:06:54 -07003090#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003091
Ronald Croncf56a0a2020-08-04 09:51:30 +02003092psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093 psa_algorithm_t alg,
3094 const uint8_t *input,
3095 size_t input_length,
3096 const uint8_t *salt,
3097 size_t salt_length,
3098 uint8_t *output,
3099 size_t output_size,
3100 size_t *output_length )
3101{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003103 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003104 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003105
Darryl Green5cc689a2018-07-24 15:34:10 +01003106 (void) input;
3107 (void) input_length;
3108 (void) salt;
3109 (void) output;
3110 (void) output_size;
3111
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003112 *output_length = 0;
3113
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003114 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3115 return( PSA_ERROR_INVALID_ARGUMENT );
3116
Ronald Cron5c522922020-11-14 16:35:34 +01003117 status = psa_get_and_lock_transparent_key_slot_with_policy(
3118 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003119 if( status != PSA_SUCCESS )
3120 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003121 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3122 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003123 {
3124 status = PSA_ERROR_INVALID_ARGUMENT;
3125 goto exit;
3126 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003127
Gilles Peskine8e338702019-07-30 20:06:31 +02003128 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003129 {
Ronald Cron7207d572021-09-08 14:28:35 +02003130#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3131 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003132 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003133 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3134 slot->key.data,
3135 slot->key.bytes,
3136 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003137 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003138 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003139
3140 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003141 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003142 status = PSA_ERROR_BUFFER_TOO_SMALL;
3143 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003144 }
Ronald Cron7207d572021-09-08 14:28:35 +02003145#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3146 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
3148 {
Ronald Cron7207d572021-09-08 14:28:35 +02003149#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003150 status = mbedtls_to_psa_error(
3151 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003152 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003153 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003154 MBEDTLS_RSA_PUBLIC,
3155 input_length,
3156 input,
3157 output ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003158#else
3159 status = PSA_ERROR_NOT_SUPPORTED;
3160#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003161 }
3162 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003163 if( PSA_ALG_IS_RSA_OAEP( alg ) )
3164 {
Ronald Cron7207d572021-09-08 14:28:35 +02003165#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003166 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003167 status = mbedtls_to_psa_error(
3168 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003169 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003170 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003171 MBEDTLS_RSA_PUBLIC,
3172 salt, salt_length,
3173 input_length,
3174 input,
3175 output ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003176#else
3177 status = PSA_ERROR_NOT_SUPPORTED;
3178#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179 }
3180 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003182 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003183 }
Ronald Cron7207d572021-09-08 14:28:35 +02003184#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3185 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003186rsa_exit:
3187 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003188 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003189
Steven Cooremana2371e52020-07-28 14:30:39 +02003190 mbedtls_rsa_free( rsa );
3191 mbedtls_free( rsa );
John Durkop9814fa22020-11-04 12:28:15 -08003192#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003193 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Ronald Cron7207d572021-09-08 14:28:35 +02003194 }
3195 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003197 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003198 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003199
3200exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003201 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003202
Ronald Cron5c522922020-11-14 16:35:34 +01003203 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003204}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003205
Ronald Croncf56a0a2020-08-04 09:51:30 +02003206psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003207 psa_algorithm_t alg,
3208 const uint8_t *input,
3209 size_t input_length,
3210 const uint8_t *salt,
3211 size_t salt_length,
3212 uint8_t *output,
3213 size_t output_size,
3214 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003215{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003217 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003218 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003219
Darryl Green5cc689a2018-07-24 15:34:10 +01003220 (void) input;
3221 (void) input_length;
3222 (void) salt;
3223 (void) output;
3224 (void) output_size;
3225
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003226 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003227
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003228 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3229 return( PSA_ERROR_INVALID_ARGUMENT );
3230
Ronald Cron5c522922020-11-14 16:35:34 +01003231 status = psa_get_and_lock_transparent_key_slot_with_policy(
3232 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003233 if( status != PSA_SUCCESS )
3234 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003235 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003236 {
3237 status = PSA_ERROR_INVALID_ARGUMENT;
3238 goto exit;
3239 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003240
Gilles Peskine8e338702019-07-30 20:06:31 +02003241 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242 {
Ronald Cron7207d572021-09-08 14:28:35 +02003243#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3244 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003245 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003246 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3247 slot->key.data,
3248 slot->key.bytes,
3249 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003250 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003251 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252
Steven Cooremana2371e52020-07-28 14:30:39 +02003253 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003254 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003255 status = PSA_ERROR_INVALID_ARGUMENT;
3256 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003257 }
Ronald Cron7207d572021-09-08 14:28:35 +02003258#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3259 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003260
Gilles Peskine6afe7892018-05-31 13:16:08 +02003261 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262 {
Ronald Cron7207d572021-09-08 14:28:35 +02003263#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003264 status = mbedtls_to_psa_error(
3265 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003266 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003267 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003268 MBEDTLS_RSA_PRIVATE,
3269 output_length,
3270 input,
3271 output,
3272 output_size ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003273#else
3274 status = PSA_ERROR_NOT_SUPPORTED;
3275#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003276 }
3277 else
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003278 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003279 {
Ronald Cron7207d572021-09-08 14:28:35 +02003280#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003281 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003282 status = mbedtls_to_psa_error(
3283 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003284 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003285 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003286 MBEDTLS_RSA_PRIVATE,
3287 salt, salt_length,
3288 output_length,
3289 input,
3290 output,
3291 output_size ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003292#else
3293 status = PSA_ERROR_NOT_SUPPORTED;
3294#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003295 }
3296 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003297 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003298 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003299 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003300
Ronald Cron7207d572021-09-08 14:28:35 +02003301#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3302 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003303rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003304 mbedtls_rsa_free( rsa );
3305 mbedtls_free( rsa );
John Durkop6ba40d12020-11-10 08:50:04 -08003306#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3307 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Ronald Cron7207d572021-09-08 14:28:35 +02003308 }
3309 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003310 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003311 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003313
3314exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003315 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003316
Ronald Cron5c522922020-11-14 16:35:34 +01003317 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003318}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003319
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003320
3321
mohammad1603503973b2018-03-12 15:59:30 +02003322/****************************************************************/
3323/* Symmetric cryptography */
3324/****************************************************************/
3325
Ronald Cronab99ac22020-10-01 16:38:28 +02003326static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
3327 mbedtls_svc_key_id_t key,
3328 psa_algorithm_t alg,
3329 mbedtls_operation_t cipher_operation )
3330{
3331 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3332 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003333 psa_key_slot_t *slot = NULL;
Ronald Cronab99ac22020-10-01 16:38:28 +02003334 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3335 PSA_KEY_USAGE_ENCRYPT :
3336 PSA_KEY_USAGE_DECRYPT );
3337
3338 /* A context must be freshly initialized before it can be set up. */
Ronald Cron49fafa92021-03-10 08:34:23 +01003339 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01003340 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003341 status = PSA_ERROR_BAD_STATE;
3342 goto exit;
3343 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003344
Ronald Cronab99ac22020-10-01 16:38:28 +02003345 if( ! PSA_ALG_IS_CIPHER( alg ) )
Dave Rodgmandb861792021-06-24 16:17:43 +01003346 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003347 status = PSA_ERROR_INVALID_ARGUMENT;
3348 goto exit;
3349 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003350
Ronald Cronab99ac22020-10-01 16:38:28 +02003351 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
3352 if( status != PSA_SUCCESS )
3353 goto exit;
3354
Ronald Cron49fafa92021-03-10 08:34:23 +01003355 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003356 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003357 * so we only set it (in the driver wrapper) after resources have been
3358 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003359 operation->iv_set = 0;
Ronald Cronab99ac22020-10-01 16:38:28 +02003360 if( alg == PSA_ALG_ECB_NO_PADDING )
3361 operation->iv_required = 0;
Gilles Peskine4a83c102022-04-20 17:00:22 +02003362 else if( slot->attr.type == PSA_KEY_TYPE_ARC4 )
3363 operation->iv_required = 0;
Ronald Cronab99ac22020-10-01 16:38:28 +02003364 else
3365 operation->iv_required = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003366 operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
Ronald Cronab99ac22020-10-01 16:38:28 +02003367
Ronald Crona4af55f2020-12-14 14:36:06 +01003368 psa_key_attributes_t attributes = {
3369 .core = slot->attr
3370 };
3371
Ronald Cronab99ac22020-10-01 16:38:28 +02003372 /* Try doing the operation through a driver before using software fallback. */
3373 if( cipher_operation == MBEDTLS_ENCRYPT )
Ronald Crona4af55f2020-12-14 14:36:06 +01003374 status = psa_driver_wrapper_cipher_encrypt_setup( operation,
3375 &attributes,
3376 slot->key.data,
3377 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003378 alg );
3379 else
Ronald Crona4af55f2020-12-14 14:36:06 +01003380 status = psa_driver_wrapper_cipher_decrypt_setup( operation,
3381 &attributes,
3382 slot->key.data,
3383 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003384 alg );
3385
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003386exit:
Ronald Cron06aa4422021-03-09 17:32:57 +01003387 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003388 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003389
Ronald Cron5c522922020-11-14 16:35:34 +01003390 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003391
Ronald Cron5c522922020-11-14 16:35:34 +01003392 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003393}
3394
Gilles Peskinefe119512018-07-08 21:39:34 +02003395psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003396 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003397 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003398{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003399 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003400}
3401
Gilles Peskinefe119512018-07-08 21:39:34 +02003402psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003403 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003404 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003405{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003406 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003407}
3408
Gilles Peskinefe119512018-07-08 21:39:34 +02003409psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003410 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003411 size_t iv_size,
3412 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003413{
Ronald Cron6d051732020-10-01 14:10:20 +02003414 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003415 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3416 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01003417
Ronald Cron49fafa92021-03-10 08:34:23 +01003418 if( operation->id == 0 )
Ronald Cronc45b4af2020-09-29 16:18:05 +02003419 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003420 status = PSA_ERROR_BAD_STATE;
3421 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003422 }
3423
Steven Cooreman7df02922020-09-09 15:28:49 +02003424 if( operation->iv_set || ! operation->iv_required )
3425 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003426 status = PSA_ERROR_BAD_STATE;
3427 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003428 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003429
Ronald Cronc423acb2021-07-05 12:31:44 +02003430 default_iv_length = operation->default_iv_length;
3431 if( iv_size < default_iv_length )
Ronald Cron5618a392021-03-26 09:52:26 +01003432 {
3433 status = PSA_ERROR_BUFFER_TOO_SMALL;
3434 goto exit;
3435 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003436
Ronald Cronc423acb2021-07-05 12:31:44 +02003437 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
3438 {
3439 status = PSA_ERROR_GENERIC_ERROR;
3440 goto exit;
3441 }
3442
3443 status = psa_generate_random( local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003444 if( status != PSA_SUCCESS )
3445 goto exit;
3446
3447 status = psa_driver_wrapper_cipher_set_iv( operation,
Ronald Cronc423acb2021-07-05 12:31:44 +02003448 local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003449
3450exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003451 if( status == PSA_SUCCESS )
Ronald Cron5618a392021-03-26 09:52:26 +01003452 {
Ronald Cronc423acb2021-07-05 12:31:44 +02003453 memcpy( iv, local_iv, default_iv_length );
3454 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003455 operation->iv_set = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003456 }
Steven Cooreman7df02922020-09-09 15:28:49 +02003457 else
Ronald Cronc423acb2021-07-05 12:31:44 +02003458 {
3459 *iv_length = 0;
Moran Peker395db872018-05-31 14:07:14 +03003460 psa_cipher_abort( operation );
Ronald Cronc423acb2021-07-05 12:31:44 +02003461 }
Ronald Cron6d051732020-10-01 14:10:20 +02003462
itayzafrir534bd7c2018-08-02 13:56:32 +03003463 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003464}
3465
Gilles Peskinefe119512018-07-08 21:39:34 +02003466psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003467 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003468 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003469{
Ronald Cron6d051732020-10-01 14:10:20 +02003470 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003471
Ronald Cron49fafa92021-03-10 08:34:23 +01003472 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01003473 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003474 status = PSA_ERROR_BAD_STATE;
3475 goto exit;
3476 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003477
Steven Cooreman7df02922020-09-09 15:28:49 +02003478 if( operation->iv_set || ! operation->iv_required )
Dave Rodgmandb861792021-06-24 16:17:43 +01003479 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003480 status = PSA_ERROR_BAD_STATE;
3481 goto exit;
3482 }
Ronald Crona0d68172021-03-26 10:15:08 +01003483
3484 if( iv_length > PSA_CIPHER_IV_MAX_SIZE )
Dave Rodgmandb861792021-06-24 16:17:43 +01003485 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003486 status = PSA_ERROR_INVALID_ARGUMENT;
3487 goto exit;
3488 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003489
Ronald Crondd24c9b2020-12-15 14:10:01 +01003490 status = psa_driver_wrapper_cipher_set_iv( operation,
3491 iv,
3492 iv_length );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003493
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003494exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003495 if( status == PSA_SUCCESS )
3496 operation->iv_set = 1;
3497 else
mohammad1603503973b2018-03-12 15:59:30 +02003498 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003499 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003500}
3501
Gilles Peskinee553c652018-06-04 16:22:46 +02003502psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3503 const uint8_t *input,
3504 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003505 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003506 size_t output_size,
3507 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003508{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003509 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003510
Ronald Cron49fafa92021-03-10 08:34:23 +01003511 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003512 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003513 status = PSA_ERROR_BAD_STATE;
3514 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003515 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003516
Steven Cooreman7df02922020-09-09 15:28:49 +02003517 if( operation->iv_required && ! operation->iv_set )
3518 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003519 status = PSA_ERROR_BAD_STATE;
3520 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003521 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003522
Ronald Crondd24c9b2020-12-15 14:10:01 +01003523 status = psa_driver_wrapper_cipher_update( operation,
3524 input,
3525 input_length,
3526 output,
3527 output_size,
3528 output_length );
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003529
3530exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003531 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003532 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003533
itayzafrir534bd7c2018-08-02 13:56:32 +03003534 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003535}
3536
Gilles Peskinee553c652018-06-04 16:22:46 +02003537psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3538 uint8_t *output,
3539 size_t output_size,
3540 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003541{
David Saadab4ecc272019-02-14 13:48:10 +02003542 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003543
Ronald Cron49fafa92021-03-10 08:34:23 +01003544 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003545 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003546 status = PSA_ERROR_BAD_STATE;
3547 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003548 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003549
Steven Cooreman7df02922020-09-09 15:28:49 +02003550 if( operation->iv_required && ! operation->iv_set )
3551 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003552 status = PSA_ERROR_BAD_STATE;
3553 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003554 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003555
Ronald Crondd24c9b2020-12-15 14:10:01 +01003556 status = psa_driver_wrapper_cipher_finish( operation,
3557 output,
3558 output_size,
3559 output_length );
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003560
3561exit:
Steven Cooremanef8575e2020-09-11 11:44:50 +02003562 if( status == PSA_SUCCESS )
3563 return( psa_cipher_abort( operation ) );
3564 else
3565 {
3566 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003567 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01003568
Steven Cooremanef8575e2020-09-11 11:44:50 +02003569 return( status );
3570 }
mohammad1603503973b2018-03-12 15:59:30 +02003571}
3572
Gilles Peskinee553c652018-06-04 16:22:46 +02003573psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3574{
Ronald Cron49fafa92021-03-10 08:34:23 +01003575 if( operation->id == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003576 {
Steven Cooremana07b9972020-09-10 14:54:14 +02003577 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003578 * in use. It's ok to call abort on such an object, and there's
3579 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003580 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003581 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003582
Ronald Crondd24c9b2020-12-15 14:10:01 +01003583 psa_driver_wrapper_cipher_abort( operation );
Gilles Peskinee553c652018-06-04 16:22:46 +02003584
Ronald Cron49fafa92021-03-10 08:34:23 +01003585 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003586 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003587 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003588
Moran Peker395db872018-05-31 14:07:14 +03003589 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003590}
3591
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003592psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key,
3593 psa_algorithm_t alg,
3594 const uint8_t *input,
3595 size_t input_length,
3596 uint8_t *output,
3597 size_t output_size,
3598 size_t *output_length )
3599{
3600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003601 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron16377072021-07-08 19:00:07 +02003602 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003603 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3604 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003605
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003606 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron16377072021-07-08 19:00:07 +02003607 {
3608 status = PSA_ERROR_INVALID_ARGUMENT;
3609 goto exit;
3610 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003611
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003612 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3613 PSA_KEY_USAGE_ENCRYPT,
3614 alg );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003615 if( status != PSA_SUCCESS )
Ronald Cron16377072021-07-08 19:00:07 +02003616 goto exit;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003617
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003618 psa_key_attributes_t attributes = {
3619 .core = slot->attr
3620 };
3621
Ronald Cron4c224fe2021-07-09 18:44:58 +02003622 default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
3623 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003624 {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003625 status = PSA_ERROR_GENERIC_ERROR;
3626 goto exit;
3627 }
3628
3629 if( default_iv_length > 0 )
3630 {
3631 if( output_size < default_iv_length )
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003632 {
3633 status = PSA_ERROR_BUFFER_TOO_SMALL;
3634 goto exit;
3635 }
3636
Ronald Cron4c224fe2021-07-09 18:44:58 +02003637 status = psa_generate_random( local_iv, default_iv_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003638 if( status != PSA_SUCCESS )
3639 goto exit;
3640 }
3641
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003642 status = psa_driver_wrapper_cipher_encrypt(
3643 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003644 alg, local_iv, default_iv_length, input, input_length,
3645 output + default_iv_length, output_size - default_iv_length,
3646 output_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003647
3648exit:
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003649 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron16377072021-07-08 19:00:07 +02003650 if( status == PSA_SUCCESS )
3651 status = unlock_status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003652
Ronald Crona8331692021-07-09 09:19:35 +02003653 if( status == PSA_SUCCESS )
Ronald Cron4c224fe2021-07-09 18:44:58 +02003654 {
3655 if( default_iv_length > 0 )
3656 memcpy( output, local_iv, default_iv_length );
3657 *output_length += default_iv_length;
3658 }
Ronald Crona8331692021-07-09 09:19:35 +02003659 else
Ronald Cron16377072021-07-08 19:00:07 +02003660 *output_length = 0;
3661
3662 return( status );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003663}
3664
3665psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key,
3666 psa_algorithm_t alg,
3667 const uint8_t *input,
3668 size_t input_length,
3669 uint8_t *output,
3670 size_t output_size,
3671 size_t *output_length )
3672{
3673 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003674 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron16377072021-07-08 19:00:07 +02003675 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003676
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003677 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron16377072021-07-08 19:00:07 +02003678 {
3679 status = PSA_ERROR_INVALID_ARGUMENT;
3680 goto exit;
3681 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003682
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003683 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3684 PSA_KEY_USAGE_DECRYPT,
3685 alg );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003686 if( status != PSA_SUCCESS )
Ronald Cron16377072021-07-08 19:00:07 +02003687 goto exit;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003688
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003689 psa_key_attributes_t attributes = {
3690 .core = slot->attr
3691 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003692
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003693 if( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) )
3694 {
3695 status = PSA_ERROR_INVALID_ARGUMENT;
3696 goto exit;
3697 }
3698
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003699 status = psa_driver_wrapper_cipher_decrypt(
3700 &attributes, slot->key.data, slot->key.bytes,
3701 alg, input, input_length,
3702 output, output_size, output_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003703
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003704exit:
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003705 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron16377072021-07-08 19:00:07 +02003706 if( status == PSA_SUCCESS )
3707 status = unlock_status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003708
Ronald Cron16377072021-07-08 19:00:07 +02003709 if( status != PSA_SUCCESS )
3710 *output_length = 0;
3711
3712 return( status );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003713}
3714
3715
mohammad16035955c982018-04-26 00:53:03 +03003716/****************************************************************/
3717/* AEAD */
3718/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003719
Ronald Croncf56a0a2020-08-04 09:51:30 +02003720psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003721 psa_algorithm_t alg,
3722 const uint8_t *nonce,
3723 size_t nonce_length,
3724 const uint8_t *additional_data,
3725 size_t additional_data_length,
3726 const uint8_t *plaintext,
3727 size_t plaintext_length,
3728 uint8_t *ciphertext,
3729 size_t ciphertext_size,
3730 size_t *ciphertext_length )
3731{
Ronald Cron215633c2021-03-16 17:15:37 +01003732 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3733 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003734
mohammad1603f08a5502018-06-03 15:05:47 +03003735 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003736
Steven Cooremanea7ab132021-03-17 16:28:00 +01003737 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3738 return( PSA_ERROR_NOT_SUPPORTED );
3739
Ronald Cron9a986162021-03-26 12:40:07 +01003740 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003741 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003742 if( status != PSA_SUCCESS )
3743 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003744
Ronald Cron215633c2021-03-16 17:15:37 +01003745 psa_key_attributes_t attributes = {
3746 .core = slot->attr
3747 };
mohammad16035955c982018-04-26 00:53:03 +03003748
Ronald Cronde822812021-03-17 16:08:20 +01003749 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003750 &attributes, slot->key.data, slot->key.bytes,
3751 alg,
3752 nonce, nonce_length,
3753 additional_data, additional_data_length,
3754 plaintext, plaintext_length,
3755 ciphertext, ciphertext_size, ciphertext_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003756
Gilles Peskineedf9a652018-08-17 18:11:56 +02003757 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3758 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003759
Ronald Cron9f310172021-03-16 16:36:37 +01003760 psa_unlock_key_slot( slot );
mohammad16035955c982018-04-26 00:53:03 +03003761
mohammad16035955c982018-04-26 00:53:03 +03003762 return( status );
Gilles Peskineee652a32018-06-01 19:23:52 +02003763}
3764
Ronald Croncf56a0a2020-08-04 09:51:30 +02003765psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003766 psa_algorithm_t alg,
3767 const uint8_t *nonce,
3768 size_t nonce_length,
3769 const uint8_t *additional_data,
3770 size_t additional_data_length,
3771 const uint8_t *ciphertext,
3772 size_t ciphertext_length,
3773 uint8_t *plaintext,
3774 size_t plaintext_size,
3775 size_t *plaintext_length )
3776{
Ronald Cron215633c2021-03-16 17:15:37 +01003777 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3778 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003779
Gilles Peskineee652a32018-06-01 19:23:52 +02003780 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003781
Steven Cooremanea7ab132021-03-17 16:28:00 +01003782 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3783 return( PSA_ERROR_NOT_SUPPORTED );
3784
Ronald Cron9a986162021-03-26 12:40:07 +01003785 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003786 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003787 if( status != PSA_SUCCESS )
3788 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003789
Ronald Cron215633c2021-03-16 17:15:37 +01003790 psa_key_attributes_t attributes = {
3791 .core = slot->attr
3792 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003793
Ronald Cronde822812021-03-17 16:08:20 +01003794 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003795 &attributes, slot->key.data, slot->key.bytes,
3796 alg,
3797 nonce, nonce_length,
3798 additional_data, additional_data_length,
3799 ciphertext, ciphertext_length,
3800 plaintext, plaintext_size, plaintext_length );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003801
Gilles Peskineedf9a652018-08-17 18:11:56 +02003802 if( status != PSA_SUCCESS && plaintext_size != 0 )
3803 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003804
Ronald Cron215633c2021-03-16 17:15:37 +01003805 psa_unlock_key_slot( slot );
3806
Gilles Peskineedf9a652018-08-17 18:11:56 +02003807 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003808}
3809
Gilles Peskinee59236f2018-01-27 23:32:46 +01003810/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003811/* Generators */
3812/****************************************************************/
3813
John Durkop07cc04a2020-11-16 22:08:34 -08003814#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3815 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3816 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3817#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003818#endif /* At least one builtin KDF */
3819
3820#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3821 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3822 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3823static psa_status_t psa_key_derivation_start_hmac(
3824 psa_mac_operation_t *operation,
3825 psa_algorithm_t hash_alg,
3826 const uint8_t *hmac_key,
3827 size_t hmac_key_length )
3828{
3829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3830 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3831 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
3832 psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) );
3833 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
3834
Steven Cooreman15f0d922021-05-07 14:14:37 +02003835 operation->is_sign = 1;
3836 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
3837
Steven Cooreman9878a162021-05-06 17:58:36 +02003838 status = psa_driver_wrapper_mac_sign_setup( operation,
3839 &attributes,
3840 hmac_key, hmac_key_length,
3841 PSA_ALG_HMAC( hash_alg ) );
3842
3843 psa_reset_key_attributes( &attributes );
3844 return( status );
3845}
3846#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003847
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003848#define HKDF_STATE_INIT 0 /* no input yet */
3849#define HKDF_STATE_STARTED 1 /* got salt */
3850#define HKDF_STATE_KEYED 2 /* got key */
3851#define HKDF_STATE_OUTPUT 3 /* output started */
3852
Gilles Peskinecbe66502019-05-16 16:59:18 +02003853static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003854 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003855{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003856 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3857 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003858 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003859 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003860}
3861
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003862psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003863{
3864 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003865 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003866 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003867 {
3868 /* The object has (apparently) been initialized but it is not
3869 * in use. It's ok to call abort on such an object, and there's
3870 * nothing to do. */
3871 }
3872 else
John Durkopd0321952020-10-29 21:37:36 -07003873#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003874 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003875 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003876 mbedtls_free( operation->ctx.hkdf.info );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003877 status = psa_mac_abort( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003878 }
John Durkop07cc04a2020-11-16 22:08:34 -08003879 else
3880#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3881#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3882 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3883 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003884 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003885 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003886 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003887 if( operation->ctx.tls12_prf.secret != NULL )
3888 {
3889 mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret,
3890 operation->ctx.tls12_prf.secret_length );
3891 mbedtls_free( operation->ctx.tls12_prf.secret );
3892 }
3893
Janos Follath6a1d2622019-06-11 10:37:28 +01003894 if( operation->ctx.tls12_prf.seed != NULL )
3895 {
3896 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
3897 operation->ctx.tls12_prf.seed_length );
3898 mbedtls_free( operation->ctx.tls12_prf.seed );
3899 }
3900
3901 if( operation->ctx.tls12_prf.label != NULL )
3902 {
3903 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
3904 operation->ctx.tls12_prf.label_length );
3905 mbedtls_free( operation->ctx.tls12_prf.label );
3906 }
3907
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003908 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003909
3910 /* We leave the fields Ai and output_block to be erased safely by the
3911 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003912 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003913 else
John Durkop07cc04a2020-11-16 22:08:34 -08003914#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3915 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003916 {
3917 status = PSA_ERROR_BAD_STATE;
3918 }
Janos Follath083036a2019-06-11 10:22:26 +01003919 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003920 return( status );
3921}
3922
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003923psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003924 size_t *capacity)
3925{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003926 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003927 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003928 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02003929 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003930 }
3931
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003932 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003933 return( PSA_SUCCESS );
3934}
3935
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003936psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003937 size_t capacity )
3938{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003939 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003940 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003941 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003942 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003943 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003944 return( PSA_SUCCESS );
3945}
3946
John Durkopd0321952020-10-29 21:37:36 -07003947#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003948/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003949 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003950static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Steven Cooremanb27e3502021-04-29 19:11:25 +02003951 psa_algorithm_t hash_alg,
3952 uint8_t *output,
3953 size_t output_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003954{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003955 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003956 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003957 psa_status_t status;
3958
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003959 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3960 return( PSA_ERROR_BAD_STATE );
3961 hkdf->state = HKDF_STATE_OUTPUT;
3962
Gilles Peskinebef7f142018-07-12 17:22:21 +02003963 while( output_length != 0 )
3964 {
3965 /* Copy what remains of the current block */
3966 uint8_t n = hash_length - hkdf->offset_in_block;
3967 if( n > output_length )
3968 n = (uint8_t) output_length;
3969 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3970 output += n;
3971 output_length -= n;
3972 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003973 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003974 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003975 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003976 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003977 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003978 * object was corrupted or if this function is called directly
3979 * inside the library. */
3980 if( hkdf->block_number == 0xff )
3981 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003982
3983 /* We need a new block */
3984 ++hkdf->block_number;
3985 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02003986
Steven Cooreman9878a162021-05-06 17:58:36 +02003987 status = psa_key_derivation_start_hmac( &hkdf->hmac,
3988 hash_alg,
3989 hkdf->prk,
3990 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003991 if( status != PSA_SUCCESS )
3992 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003993
Gilles Peskinebef7f142018-07-12 17:22:21 +02003994 if( hkdf->block_number != 1 )
3995 {
Steven Cooremanb27e3502021-04-29 19:11:25 +02003996 status = psa_mac_update( &hkdf->hmac,
3997 hkdf->output_block,
3998 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003999 if( status != PSA_SUCCESS )
4000 return( status );
4001 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004002 status = psa_mac_update( &hkdf->hmac,
4003 hkdf->info,
4004 hkdf->info_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004005 if( status != PSA_SUCCESS )
4006 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004007 status = psa_mac_update( &hkdf->hmac,
4008 &hkdf->block_number, 1 );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004009 if( status != PSA_SUCCESS )
4010 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004011 status = psa_mac_sign_finish( &hkdf->hmac,
4012 hkdf->output_block,
4013 sizeof( hkdf->output_block ),
4014 &hmac_output_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004015 if( status != PSA_SUCCESS )
4016 return( status );
4017 }
4018
4019 return( PSA_SUCCESS );
4020}
John Durkop07cc04a2020-11-16 22:08:34 -08004021#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004022
John Durkop07cc04a2020-11-16 22:08:34 -08004023#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4024 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004025static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4026 psa_tls12_prf_key_derivation_t *tls12_prf,
4027 psa_algorithm_t alg )
4028{
4029 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004030 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004031 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4032 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004033 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004034
Janos Follath7742fee2019-06-17 12:58:10 +01004035 /* We can't be wanting more output after block 0xff, otherwise
4036 * the capacity check in psa_key_derivation_output_bytes() would have
4037 * prevented this call. It could happen only if the operation
4038 * object was corrupted or if this function is called directly
4039 * inside the library. */
4040 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004041 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004042
4043 /* We need a new block */
4044 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004045 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004046
4047 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4048 *
4049 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4050 *
4051 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4052 * HMAC_hash(secret, A(2) + seed) +
4053 * HMAC_hash(secret, A(3) + seed) + ...
4054 *
4055 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004056 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004057 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004058 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004059 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004060 * is currently extracted as `output_block` and where i is
4061 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004062 */
4063
Steven Cooreman9878a162021-05-06 17:58:36 +02004064 status = psa_key_derivation_start_hmac( &hmac,
4065 hash_alg,
4066 tls12_prf->secret,
4067 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004068 if( status != PSA_SUCCESS )
4069 goto cleanup;
4070
4071 /* Calculate A(i) where i = tls12_prf->block_number. */
4072 if( tls12_prf->block_number == 1 )
4073 {
4074 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4075 * the variable seed and in this instance means it in the context of the
4076 * P_hash function, where seed = label + seed.) */
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004077 status = psa_mac_update( &hmac,
4078 tls12_prf->label,
4079 tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004080 if( status != PSA_SUCCESS )
4081 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004082 status = psa_mac_update( &hmac,
4083 tls12_prf->seed,
4084 tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004085 if( status != PSA_SUCCESS )
4086 goto cleanup;
4087 }
4088 else
4089 {
4090 /* A(i) = HMAC_hash(secret, A(i-1)) */
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004091 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004092 if( status != PSA_SUCCESS )
4093 goto cleanup;
4094 }
4095
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004096 status = psa_mac_sign_finish( &hmac,
4097 tls12_prf->Ai, hash_length,
4098 &hmac_output_length );
4099 if( hmac_output_length != hash_length )
4100 status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follathea29bfb2019-06-19 12:21:20 +01004101 if( status != PSA_SUCCESS )
4102 goto cleanup;
4103
4104 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Steven Cooreman9878a162021-05-06 17:58:36 +02004105 status = psa_key_derivation_start_hmac( &hmac,
4106 hash_alg,
4107 tls12_prf->secret,
4108 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004109 if( status != PSA_SUCCESS )
4110 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004111 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004112 if( status != PSA_SUCCESS )
4113 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004114 status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004115 if( status != PSA_SUCCESS )
4116 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004117 status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004118 if( status != PSA_SUCCESS )
4119 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004120 status = psa_mac_sign_finish( &hmac,
4121 tls12_prf->output_block, hash_length,
4122 &hmac_output_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004123 if( status != PSA_SUCCESS )
4124 goto cleanup;
4125
Janos Follath7742fee2019-06-17 12:58:10 +01004126
4127cleanup:
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004128 cleanup_status = psa_mac_abort( &hmac );
Janos Follathea29bfb2019-06-19 12:21:20 +01004129 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4130 status = cleanup_status;
4131
4132 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004133}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004134
Janos Follath844eb0e2019-06-19 12:10:49 +01004135static psa_status_t psa_key_derivation_tls12_prf_read(
4136 psa_tls12_prf_key_derivation_t *tls12_prf,
4137 psa_algorithm_t alg,
4138 uint8_t *output,
4139 size_t output_length )
4140{
4141 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004142 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004143 psa_status_t status;
4144 uint8_t offset, length;
4145
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004146 switch( tls12_prf->state )
4147 {
4148 case PSA_TLS12_PRF_STATE_LABEL_SET:
4149 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4150 break;
4151 case PSA_TLS12_PRF_STATE_OUTPUT:
4152 break;
4153 default:
4154 return( PSA_ERROR_BAD_STATE );
4155 }
4156
Janos Follath844eb0e2019-06-19 12:10:49 +01004157 while( output_length != 0 )
4158 {
4159 /* Check if we have fully processed the current block. */
4160 if( tls12_prf->left_in_block == 0 )
4161 {
4162 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4163 alg );
4164 if( status != PSA_SUCCESS )
4165 return( status );
4166
4167 continue;
4168 }
4169
4170 if( tls12_prf->left_in_block > output_length )
4171 length = (uint8_t) output_length;
4172 else
4173 length = tls12_prf->left_in_block;
4174
4175 offset = hash_length - tls12_prf->left_in_block;
4176 memcpy( output, tls12_prf->output_block + offset, length );
4177 output += length;
4178 output_length -= length;
4179 tls12_prf->left_in_block -= length;
4180 }
4181
4182 return( PSA_SUCCESS );
4183}
John Durkop07cc04a2020-11-16 22:08:34 -08004184#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4185 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004186
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004187psa_status_t psa_key_derivation_output_bytes(
4188 psa_key_derivation_operation_t *operation,
4189 uint8_t *output,
4190 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004191{
4192 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004194
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004195 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004196 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004197 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004198 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004199 }
4200
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004201 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004202 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004203 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004204 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004205 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004206 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004207 goto exit;
4208 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004209 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004210 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004211 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004212 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004213 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4214 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004215 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004216 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004217 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004218 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004219 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004220
John Durkopd0321952020-10-29 21:37:36 -07004221#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004222 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004223 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004224 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004225 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004226 output, output_length );
4227 }
Janos Follathadbec812019-06-14 11:05:39 +01004228 else
John Durkop07cc04a2020-11-16 22:08:34 -08004229#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4230#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4231 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004232 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004233 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004234 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004235 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004236 kdf_alg, output,
4237 output_length );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004238 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004239 else
John Durkop07cc04a2020-11-16 22:08:34 -08004240#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4241 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004242 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004243 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004244 return( PSA_ERROR_BAD_STATE );
4245 }
4246
4247exit:
4248 if( status != PSA_SUCCESS )
4249 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004250 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004251 * This allows us to differentiate between exhausted operations and
4252 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4253 * operations. */
4254 psa_algorithm_t alg = operation->alg;
4255 psa_key_derivation_abort( operation );
4256 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004257 memset( output, '!', output_length );
4258 }
4259 return( status );
4260}
4261
David Brown7807bf72021-02-09 16:28:23 -07004262#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004263static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4264{
4265 if( data_size >= 8 )
4266 mbedtls_des_key_set_parity( data );
4267 if( data_size >= 16 )
4268 mbedtls_des_key_set_parity( data + 8 );
4269 if( data_size >= 24 )
4270 mbedtls_des_key_set_parity( data + 16 );
4271}
David Brown7807bf72021-02-09 16:28:23 -07004272#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004273
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004274static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004275 psa_key_slot_t *slot,
4276 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004277 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004278{
4279 uint8_t *data = NULL;
4280 size_t bytes = PSA_BITS_TO_BYTES( bits );
4281 psa_status_t status;
4282
Gilles Peskine8e338702019-07-30 20:06:31 +02004283 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004284 return( PSA_ERROR_INVALID_ARGUMENT );
4285 if( bits % 8 != 0 )
4286 return( PSA_ERROR_INVALID_ARGUMENT );
4287 data = mbedtls_calloc( 1, bytes );
4288 if( data == NULL )
4289 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4290
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004291 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004292 if( status != PSA_SUCCESS )
4293 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07004294#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004295 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004296 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07004297#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004298
4299 status = psa_allocate_buffer_to_slot( slot, bytes );
4300 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00004301 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01004302
Ronald Cronbf33c932020-11-28 18:06:53 +01004303 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004304 psa_key_attributes_t attributes = {
4305 .core = slot->attr
4306 };
4307
Ronald Cronbf33c932020-11-28 18:06:53 +01004308 status = psa_driver_wrapper_import_key( &attributes,
4309 data, bytes,
4310 slot->key.data,
4311 slot->key.bytes,
4312 &slot->key.bytes, &bits );
4313 if( bits != slot->attr.bits )
4314 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004315
4316exit:
4317 mbedtls_free( data );
4318 return( status );
4319}
4320
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004321psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004322 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004323 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004324{
4325 psa_status_t status;
4326 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004327 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004328
Ronald Cron81709fc2020-11-14 12:10:32 +01004329 *key = MBEDTLS_SVC_KEY_ID_INIT;
4330
Gilles Peskine0f84d622019-09-12 19:03:13 +02004331 /* Reject any attempt to create a zero-length key so that we don't
4332 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4333 if( psa_get_key_bits( attributes ) == 0 )
4334 return( PSA_ERROR_INVALID_ARGUMENT );
4335
Dave Rodgman021e7242021-11-16 10:32:48 +00004336 if( operation->alg == PSA_ALG_NONE )
4337 return( PSA_ERROR_BAD_STATE );
4338
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004339 if( ! operation->can_output_key )
4340 return( PSA_ERROR_NOT_PERMITTED );
4341
Ronald Cron81709fc2020-11-14 12:10:32 +01004342 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
4343 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004344#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4345 if( driver != NULL )
4346 {
4347 /* Deriving a key in a secure element is not implemented yet. */
4348 status = PSA_ERROR_NOT_SUPPORTED;
4349 }
4350#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004351 if( status == PSA_SUCCESS )
4352 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004353 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004354 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004355 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004356 }
4357 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01004358 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004359 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004360 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004361
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004362 return( status );
4363}
4364
Gilles Peskineeab56e42018-07-12 17:12:33 +02004365
4366
4367/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004368/* Key derivation */
4369/****************************************************************/
4370
Steven Cooreman932ffb72021-02-15 12:14:32 +01004371#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004372static int is_kdf_alg_supported( psa_algorithm_t kdf_alg )
4373{
4374#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
4375 if( PSA_ALG_IS_HKDF( kdf_alg ) )
4376 return( 1 );
4377#endif
4378#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4379 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
4380 return( 1 );
4381#endif
4382#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4383 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
4384 return( 1 );
4385#endif
4386 return( 0 );
4387}
4388
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004389static psa_status_t psa_hash_try_support( psa_algorithm_t alg )
4390{
4391 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
4392 psa_status_t status = psa_hash_setup( &operation, alg );
4393 psa_hash_abort( &operation );
4394 return( status );
4395}
4396
Gilles Peskine969c5d62019-01-16 15:53:06 +01004397static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004398 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004399 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004400{
Janos Follath5fe19732019-06-20 15:09:30 +01004401 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4402 * initialisers for this union leave some bytes unspecified.) */
4403 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4404
Gilles Peskine969c5d62019-01-16 15:53:06 +01004405 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskinecdacf042021-04-29 21:10:00 +02004406 if( ! is_kdf_alg_supported( kdf_alg ) )
4407 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop07cc04a2020-11-16 22:08:34 -08004408
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004409 /* All currently supported key derivation algorithms are based on a
4410 * hash algorithm. */
Gilles Peskinecdacf042021-04-29 21:10:00 +02004411 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
4412 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
4413 if( hash_size == 0 )
4414 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004415
4416 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4417 * we might fail later, which is somewhat unfriendly and potentially
4418 * risk-prone. */
4419 psa_status_t status = psa_hash_try_support( hash_alg );
4420 if( status != PSA_SUCCESS )
4421 return( status );
4422
Gilles Peskinecdacf042021-04-29 21:10:00 +02004423 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4424 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
4425 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004426 {
Gilles Peskinecdacf042021-04-29 21:10:00 +02004427 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004428 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004429
Gilles Peskinecdacf042021-04-29 21:10:00 +02004430 operation->capacity = 255 * hash_size;
4431 return( PSA_SUCCESS );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004433
4434static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg )
4435{
4436#if defined(PSA_WANT_ALG_ECDH)
4437 if( alg == PSA_ALG_ECDH )
4438 return( PSA_SUCCESS );
4439#endif
4440 (void) alg;
4441 return( PSA_ERROR_NOT_SUPPORTED );
4442}
John Durkop07cc04a2020-11-16 22:08:34 -08004443#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004444
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004445psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004446 psa_algorithm_t alg )
4447{
4448 psa_status_t status;
4449
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004450 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004451 return( PSA_ERROR_BAD_STATE );
4452
Gilles Peskine6843c292019-01-18 16:44:49 +01004453 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4454 return( PSA_ERROR_INVALID_ARGUMENT );
4455 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004456 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004457#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004458 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine739c98c2021-04-29 21:34:33 +02004459 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg );
4460 status = psa_key_agreement_try_support( ka_alg );
4461 if( status != PSA_SUCCESS )
4462 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004463 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004464#else
4465 return( PSA_ERROR_NOT_SUPPORTED );
4466#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004467 }
4468 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4469 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004470#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004471 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004472#else
4473 return( PSA_ERROR_NOT_SUPPORTED );
4474#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004475 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004476 else
4477 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004478
4479 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004481 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004482}
4483
John Durkopd0321952020-10-29 21:37:36 -07004484#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004485static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004486 psa_algorithm_t hash_alg,
4487 psa_key_derivation_step_t step,
4488 const uint8_t *data,
4489 size_t data_length )
4490{
4491 psa_status_t status;
4492 switch( step )
4493 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004494 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004495 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004496 return( PSA_ERROR_BAD_STATE );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004497 else
4498 {
Steven Cooreman9878a162021-05-06 17:58:36 +02004499 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4500 hash_alg,
4501 data, data_length );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004502 if( status != PSA_SUCCESS )
4503 return( status );
4504 hkdf->state = HKDF_STATE_STARTED;
4505 return( PSA_SUCCESS );
4506 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004507 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004508 /* If no salt was provided, use an empty salt. */
4509 if( hkdf->state == HKDF_STATE_INIT )
4510 {
Steven Cooreman9878a162021-05-06 17:58:36 +02004511 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4512 hash_alg,
4513 NULL, 0 );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004514 if( status != PSA_SUCCESS )
4515 return( status );
4516 hkdf->state = HKDF_STATE_STARTED;
4517 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004518 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004519 return( PSA_ERROR_BAD_STATE );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004520 status = psa_mac_update( &hkdf->hmac,
4521 data, data_length );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004522 if( status != PSA_SUCCESS )
4523 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004524 status = psa_mac_sign_finish( &hkdf->hmac,
4525 hkdf->prk,
4526 sizeof( hkdf->prk ),
4527 &data_length );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004528 if( status != PSA_SUCCESS )
4529 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004530 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004531 hkdf->block_number = 0;
4532 hkdf->state = HKDF_STATE_KEYED;
4533 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004534 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004535 if( hkdf->state == HKDF_STATE_OUTPUT )
4536 return( PSA_ERROR_BAD_STATE );
4537 if( hkdf->info_set )
4538 return( PSA_ERROR_BAD_STATE );
4539 hkdf->info_length = data_length;
4540 if( data_length != 0 )
4541 {
4542 hkdf->info = mbedtls_calloc( 1, data_length );
4543 if( hkdf->info == NULL )
4544 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4545 memcpy( hkdf->info, data, data_length );
4546 }
4547 hkdf->info_set = 1;
4548 return( PSA_SUCCESS );
4549 default:
4550 return( PSA_ERROR_INVALID_ARGUMENT );
4551 }
4552}
John Durkop07cc04a2020-11-16 22:08:34 -08004553#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004554
John Durkop07cc04a2020-11-16 22:08:34 -08004555#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4556 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01004557static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4558 const uint8_t *data,
4559 size_t data_length )
4560{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004561 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01004562 return( PSA_ERROR_BAD_STATE );
4563
Janos Follathd6dce9f2019-07-04 09:11:38 +01004564 if( data_length != 0 )
4565 {
4566 prf->seed = mbedtls_calloc( 1, data_length );
4567 if( prf->seed == NULL )
4568 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004569
Janos Follathd6dce9f2019-07-04 09:11:38 +01004570 memcpy( prf->seed, data, data_length );
4571 prf->seed_length = data_length;
4572 }
Janos Follathf08e2652019-06-13 09:05:41 +01004573
Gilles Peskine43f958b2020-12-13 14:55:14 +01004574 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004575
4576 return( PSA_SUCCESS );
4577}
4578
Janos Follath81550542019-06-13 14:26:34 +01004579static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
Janos Follath81550542019-06-13 14:26:34 +01004580 const uint8_t *data,
4581 size_t data_length )
4582{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004583 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01004584 return( PSA_ERROR_BAD_STATE );
4585
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004586 if( data_length != 0 )
4587 {
4588 prf->secret = mbedtls_calloc( 1, data_length );
4589 if( prf->secret == NULL )
4590 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4591
4592 memcpy( prf->secret, data, data_length );
4593 prf->secret_length = data_length;
4594 }
Janos Follath81550542019-06-13 14:26:34 +01004595
Gilles Peskine43f958b2020-12-13 14:55:14 +01004596 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004597
4598 return( PSA_SUCCESS );
4599}
4600
Janos Follath63028dd2019-06-13 09:15:47 +01004601static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
4602 const uint8_t *data,
4603 size_t data_length )
4604{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004605 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01004606 return( PSA_ERROR_BAD_STATE );
4607
Janos Follathd6dce9f2019-07-04 09:11:38 +01004608 if( data_length != 0 )
4609 {
4610 prf->label = mbedtls_calloc( 1, data_length );
4611 if( prf->label == NULL )
4612 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01004613
Janos Follathd6dce9f2019-07-04 09:11:38 +01004614 memcpy( prf->label, data, data_length );
4615 prf->label_length = data_length;
4616 }
Janos Follath63028dd2019-06-13 09:15:47 +01004617
Gilles Peskine43f958b2020-12-13 14:55:14 +01004618 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004619
4620 return( PSA_SUCCESS );
4621}
4622
Janos Follathb03233e2019-06-11 15:30:30 +01004623static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
Janos Follathb03233e2019-06-11 15:30:30 +01004624 psa_key_derivation_step_t step,
4625 const uint8_t *data,
4626 size_t data_length )
4627{
Janos Follathb03233e2019-06-11 15:30:30 +01004628 switch( step )
4629 {
Janos Follathf08e2652019-06-13 09:05:41 +01004630 case PSA_KEY_DERIVATION_INPUT_SEED:
4631 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01004632 case PSA_KEY_DERIVATION_INPUT_SECRET:
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004633 return( psa_tls12_prf_set_key( prf, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01004634 case PSA_KEY_DERIVATION_INPUT_LABEL:
4635 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01004636 default:
4637 return( PSA_ERROR_INVALID_ARGUMENT );
4638 }
4639}
John Durkop07cc04a2020-11-16 22:08:34 -08004640#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4641 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4642
4643#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4644static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4645 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004646 const uint8_t *data,
4647 size_t data_length )
4648{
4649 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004650 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08004651 uint8_t *cur = pms;
4652
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004653 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08004654 return( PSA_ERROR_INVALID_ARGUMENT );
4655
4656 /* Quoting RFC 4279, Section 2:
4657 *
4658 * The premaster secret is formed as follows: if the PSK is N octets
4659 * long, concatenate a uint16 with the value N, N zero octets, a second
4660 * uint16 with the value N, and the PSK itself.
4661 */
4662
Joe Subbiani2bbafda2021-06-24 13:00:03 +01004663 *cur++ = MBEDTLS_BYTE_1( data_length );
4664 *cur++ = MBEDTLS_BYTE_0( data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08004665 memset( cur, 0, data_length );
4666 cur += data_length;
4667 *cur++ = pms[0];
4668 *cur++ = pms[1];
4669 memcpy( cur, data, data_length );
4670 cur += data_length;
4671
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004672 status = psa_tls12_prf_set_key( prf, pms, cur - pms );
John Durkop07cc04a2020-11-16 22:08:34 -08004673
4674 mbedtls_platform_zeroize( pms, sizeof( pms ) );
4675 return( status );
4676}
Janos Follath6660f0e2019-06-17 08:44:03 +01004677
4678static psa_status_t psa_tls12_prf_psk_to_ms_input(
4679 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004680 psa_key_derivation_step_t step,
4681 const uint8_t *data,
4682 size_t data_length )
4683{
4684 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01004685 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004686 return( psa_tls12_prf_psk_to_ms_set_key( prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004687 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01004688 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004689
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004690 return( psa_tls12_prf_input( prf, step, data, data_length ) );
Janos Follath6660f0e2019-06-17 08:44:03 +01004691}
John Durkop07cc04a2020-11-16 22:08:34 -08004692#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004693
Gilles Peskineb8965192019-09-24 16:21:10 +02004694/** Check whether the given key type is acceptable for the given
4695 * input step of a key derivation.
4696 *
4697 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4698 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4699 * Both secret and non-secret inputs can alternatively have the type
4700 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4701 * that the input was passed as a buffer rather than via a key object.
4702 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004703static int psa_key_derivation_check_input_type(
4704 psa_key_derivation_step_t step,
4705 psa_key_type_t key_type )
4706{
4707 switch( step )
4708 {
4709 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02004710 if( key_type == PSA_KEY_TYPE_DERIVE )
4711 return( PSA_SUCCESS );
4712 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004713 return( PSA_SUCCESS );
4714 break;
4715 case PSA_KEY_DERIVATION_INPUT_LABEL:
4716 case PSA_KEY_DERIVATION_INPUT_SALT:
4717 case PSA_KEY_DERIVATION_INPUT_INFO:
4718 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02004719 if( key_type == PSA_KEY_TYPE_RAW_DATA )
4720 return( PSA_SUCCESS );
4721 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004722 return( PSA_SUCCESS );
4723 break;
4724 }
4725 return( PSA_ERROR_INVALID_ARGUMENT );
4726}
4727
Janos Follathb80a94e2019-06-12 15:54:46 +01004728static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004729 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004730 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004731 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004732 const uint8_t *data,
4733 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004734{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004735 psa_status_t status;
4736 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
4737
4738 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02004739 if( status != PSA_SUCCESS )
4740 goto exit;
4741
John Durkopd0321952020-10-29 21:37:36 -07004742#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004743 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004744 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004746 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004747 step, data, data_length );
4748 }
John Durkop07cc04a2020-11-16 22:08:34 -08004749 else
4750#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4751#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4752 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004753 {
Janos Follathb03233e2019-06-11 15:30:30 +01004754 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
Janos Follathb03233e2019-06-11 15:30:30 +01004755 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01004756 }
John Durkop07cc04a2020-11-16 22:08:34 -08004757 else
4758#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4759#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4760 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01004761 {
4762 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004763 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004764 }
4765 else
John Durkop07cc04a2020-11-16 22:08:34 -08004766#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004767 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004768 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004769 (void) data;
4770 (void) data_length;
4771 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004772 return( PSA_ERROR_BAD_STATE );
4773 }
4774
Gilles Peskine224b0d62019-09-23 18:13:17 +02004775exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004776 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004777 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004778 return( status );
4779}
4780
Janos Follath51f4a0f2019-06-14 11:35:55 +01004781psa_status_t psa_key_derivation_input_bytes(
4782 psa_key_derivation_operation_t *operation,
4783 psa_key_derivation_step_t step,
4784 const uint8_t *data,
4785 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004786{
Gilles Peskineb8965192019-09-24 16:21:10 +02004787 return( psa_key_derivation_input_internal( operation, step,
4788 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01004789 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004790}
4791
Janos Follath51f4a0f2019-06-14 11:35:55 +01004792psa_status_t psa_key_derivation_input_key(
4793 psa_key_derivation_operation_t *operation,
4794 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004795 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004796{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004797 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004798 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004799 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004800
Ronald Cron5c522922020-11-14 16:35:34 +01004801 status = psa_get_and_lock_transparent_key_slot_with_policy(
4802 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004803 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02004804 {
4805 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004806 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02004807 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004808
4809 /* Passing a key object as a SECRET input unlocks the permission
4810 * to output to a key object. */
4811 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4812 operation->can_output_key = 1;
4813
Ronald Cronf95a2b12020-10-22 15:24:49 +02004814 status = psa_key_derivation_input_internal( operation,
4815 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004816 slot->key.data,
4817 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004818
Ronald Cron5c522922020-11-14 16:35:34 +01004819 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004820
Ronald Cron5c522922020-11-14 16:35:34 +01004821 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004822}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004823
4824
4825
4826/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004827/* Key agreement */
4828/****************************************************************/
4829
John Durkop6ba40d12020-11-10 08:50:04 -08004830#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004831static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4832 size_t peer_key_length,
4833 const mbedtls_ecp_keypair *our_key,
4834 uint8_t *shared_secret,
4835 size_t shared_secret_size,
4836 size_t *shared_secret_length )
4837{
Steven Cooremand4867872020-08-05 16:31:39 +02004838 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004839 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004840 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004841 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01004842 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004843 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004844
Ronald Cron90857082020-11-25 14:58:33 +01004845 status = mbedtls_psa_ecp_load_representation(
4846 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004847 bits,
Ronald Cron90857082020-11-25 14:58:33 +01004848 peer_key,
4849 peer_key_length,
4850 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004851 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004852 goto exit;
4853
Jaeden Amero97271b32019-01-10 19:38:51 +00004854 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02004855 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00004856 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004857 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004858 status = mbedtls_to_psa_error(
4859 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4860 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004861 goto exit;
4862
Jaeden Amero97271b32019-01-10 19:38:51 +00004863 status = mbedtls_to_psa_error(
4864 mbedtls_ecdh_calc_secret( &ecdh,
4865 shared_secret_length,
4866 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004867 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004868 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004869 if( status != PSA_SUCCESS )
4870 goto exit;
4871 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
4872 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004873
4874exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01004875 if( status != PSA_SUCCESS )
4876 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004877 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02004878 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02004879 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02004880
Jaeden Amero97271b32019-01-10 19:38:51 +00004881 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004882}
John Durkop6ba40d12020-11-10 08:50:04 -08004883#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004884
Gilles Peskine01d718c2018-09-18 12:01:02 +02004885#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4886
Gilles Peskine0216fe12019-04-11 21:23:21 +02004887static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4888 psa_key_slot_t *private_key,
4889 const uint8_t *peer_key,
4890 size_t peer_key_length,
4891 uint8_t *shared_secret,
4892 size_t shared_secret_size,
4893 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004894{
Gilles Peskine0216fe12019-04-11 21:23:21 +02004895 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004896 {
John Durkop6ba40d12020-11-10 08:50:04 -08004897#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004898 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02004899 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004900 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02004901 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004902 psa_status_t status = mbedtls_psa_ecp_load_representation(
4903 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004904 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01004905 private_key->key.data,
4906 private_key->key.bytes,
4907 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004908 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004909 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02004910 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02004911 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004912 shared_secret, shared_secret_size,
4913 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004914 mbedtls_ecp_keypair_free( ecp );
4915 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02004916 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08004917#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004918 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004919 (void) private_key;
4920 (void) peer_key;
4921 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004922 (void) shared_secret;
4923 (void) shared_secret_size;
4924 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004925 return( PSA_ERROR_NOT_SUPPORTED );
4926 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004927}
4928
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004929/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004930 * to potentially free embedded data structures and wipe confidential data.
4931 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004933 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004934 psa_key_slot_t *private_key,
4935 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004936 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004937{
4938 psa_status_t status;
4939 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4940 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004941 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004942
4943 /* Step 1: run the secret agreement algorithm to generate the shared
4944 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004945 status = psa_key_agreement_raw_internal( ka_alg,
4946 private_key,
4947 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03004948 shared_secret,
4949 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02004950 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004951 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004952 goto exit;
4953
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004954 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02004955 * the shared secret. A shared secret is permitted wherever a key
4956 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01004957 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004958 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01004959 shared_secret,
4960 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004961exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004962 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004963 return( status );
4964}
4965
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004967 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004968 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004969 const uint8_t *peer_key,
4970 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004971{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004972 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004973 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004974 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004975
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004977 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01004978 status = psa_get_and_lock_transparent_key_slot_with_policy(
4979 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004980 if( status != PSA_SUCCESS )
4981 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004983 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004984 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004985 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004987 else
4988 {
4989 /* If a private key has been added as SECRET, we allow the derived
4990 * key material to be used as a key in PSA Crypto. */
4991 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4992 operation->can_output_key = 1;
4993 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004994
Ronald Cron5c522922020-11-14 16:35:34 +01004995 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004996
Ronald Cron5c522922020-11-14 16:35:34 +01004997 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004998}
4999
Gilles Peskinebe697d82019-05-16 18:00:41 +02005000psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005001 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005002 const uint8_t *peer_key,
5003 size_t peer_key_length,
5004 uint8_t *output,
5005 size_t output_size,
5006 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005007{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005008 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005009 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005010 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005011
5012 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5013 {
5014 status = PSA_ERROR_INVALID_ARGUMENT;
5015 goto exit;
5016 }
Ronald Cron5c522922020-11-14 16:35:34 +01005017 status = psa_get_and_lock_transparent_key_slot_with_policy(
5018 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005019 if( status != PSA_SUCCESS )
5020 goto exit;
5021
Gilles Peskine42313fb2022-04-14 00:17:15 +02005022 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5023 * for the output size. The PSA specification only guarantees that this
5024 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5025 * but it might be nice to allow smaller buffers if the output fits.
5026 * At the time of writing this comment, with only ECDH implemented,
5027 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5028 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5029 * be exact for it as well. */
5030 size_t expected_length =
5031 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits );
5032 if( output_size < expected_length )
5033 {
5034 status = PSA_ERROR_BUFFER_TOO_SMALL;
5035 goto exit;
5036 }
5037
Gilles Peskine0216fe12019-04-11 21:23:21 +02005038 status = psa_key_agreement_raw_internal( alg, slot,
5039 peer_key, peer_key_length,
5040 output, output_size,
5041 output_length );
5042
5043exit:
5044 if( status != PSA_SUCCESS )
5045 {
5046 /* If an error happens and is not handled properly, the output
5047 * may be used as a key to protect sensitive data. Arrange for such
5048 * a key to be random, which is likely to result in decryption or
5049 * verification errors. This is better than filling the buffer with
5050 * some constant data such as zeros, which would result in the data
5051 * being protected with a reproducible, easily knowable key.
5052 */
5053 psa_generate_random( output, output_size );
5054 *output_length = output_size;
5055 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005056
Ronald Cron5c522922020-11-14 16:35:34 +01005057 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005058
Ronald Cron5c522922020-11-14 16:35:34 +01005059 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005060}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005061
5062
Gilles Peskine30524eb2020-11-13 17:02:26 +01005063
Gilles Peskine86a440b2018-11-12 18:39:40 +01005064/****************************************************************/
5065/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005066/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005067
Gilles Peskine30524eb2020-11-13 17:02:26 +01005068/** Initialize the PSA random generator.
5069 */
5070static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5071{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005072#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5073 memset( rng, 0, sizeof( *rng ) );
5074#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5075
Gilles Peskine30524eb2020-11-13 17:02:26 +01005076 /* Set default configuration if
5077 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5078 if( rng->entropy_init == NULL )
5079 rng->entropy_init = mbedtls_entropy_init;
5080 if( rng->entropy_free == NULL )
5081 rng->entropy_free = mbedtls_entropy_free;
5082
5083 rng->entropy_init( &rng->entropy );
5084#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5085 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5086 /* The PSA entropy injection feature depends on using NV seed as an entropy
5087 * source. Add NV seed as an entropy source for PSA entropy injection. */
5088 mbedtls_entropy_add_source( &rng->entropy,
5089 mbedtls_nv_seed_poll, NULL,
5090 MBEDTLS_ENTROPY_BLOCK_SIZE,
5091 MBEDTLS_ENTROPY_SOURCE_STRONG );
5092#endif
5093
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005094 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005095#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005096}
5097
5098/** Deinitialize the PSA random generator.
5099 */
5100static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5101{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005102#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5103 memset( rng, 0, sizeof( *rng ) );
5104#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005105 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005106 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005107#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005108}
5109
5110/** Seed the PSA random generator.
5111 */
5112static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5113{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005114#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5115 /* Do nothing: the external RNG seeds itself. */
5116 (void) rng;
5117 return( PSA_SUCCESS );
5118#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005119 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005120 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5121 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005122 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005123#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005124}
5125
Gilles Peskinee59236f2018-01-27 23:32:46 +01005126psa_status_t psa_generate_random( uint8_t *output,
5127 size_t output_size )
5128{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005129 GUARD_MODULE_INITIALIZED;
5130
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005131#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5132
5133 size_t output_length = 0;
5134 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5135 output, output_size,
5136 &output_length );
5137 if( status != PSA_SUCCESS )
5138 return( status );
5139 /* Breaking up a request into smaller chunks is currently not supported
5140 * for the extrernal RNG interface. */
5141 if( output_length != output_size )
5142 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5143 return( PSA_SUCCESS );
5144
5145#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5146
Gilles Peskine71ddab92021-01-04 21:01:07 +01005147 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005148 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005149 size_t request_size =
5150 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5151 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5152 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005153 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
5154 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02005155 if( ret != 0 )
5156 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01005157 output_size -= request_size;
5158 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005159 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005160 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005161#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005162}
5163
Gilles Peskine8814fc42020-12-14 15:33:44 +01005164/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005165 *
5166 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5167 * `psa_generate_random(...)`. The state parameter is ignored since the
5168 * PSA API doesn't support passing an explicit state.
5169 *
5170 * In the non-external case, psa_generate_random() calls an
5171 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5172 * and semantics as mbedtls_psa_get_random(). As an optimization,
5173 * instead of doing this back-and-forth between the PSA API and the
5174 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5175 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005176 */
5177#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5178int mbedtls_psa_get_random( void *p_rng,
5179 unsigned char *output,
5180 size_t output_size )
5181{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005182 /* This function takes a pointer to the RNG state because that's what
5183 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5184 * its own state internally and doesn't let the caller access that state.
5185 * So we just ignore the state parameter, and in practice we'll pass
5186 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005187 (void) p_rng;
5188 psa_status_t status = psa_generate_random( output, output_size );
5189 if( status == PSA_SUCCESS )
5190 return( 0 );
5191 else
5192 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
5193}
5194#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5195
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005196#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5197#include "mbedtls/entropy_poll.h"
5198
Gilles Peskine7228da22019-07-15 11:06:15 +02005199psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005200 size_t seed_size )
5201{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005202 if( global_data.initialized )
5203 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005204
5205 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5206 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5207 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5208 return( PSA_ERROR_INVALID_ARGUMENT );
5209
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005210 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005211}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005212#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005213
Ronald Cron3772afe2021-02-08 16:10:05 +01005214/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005215 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005216 * \param type The key type
5217 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005218 *
5219 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005220 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005221 * \retval #PSA_ERROR_INVALID_ARGUMENT
5222 * The size in bits of the key is not valid.
5223 * \retval #PSA_ERROR_NOT_SUPPORTED
5224 * The type and/or the size in bits of the key or the combination of
5225 * the two is not supported.
5226 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005227static psa_status_t psa_validate_key_type_and_size_for_key_generation(
5228 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005229{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005231
Gilles Peskinee59236f2018-01-27 23:32:46 +01005232 if( key_type_is_raw_bytes( type ) )
5233 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02005234 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005235 if( status != PSA_SUCCESS )
5236 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005237 }
5238 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005239#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005240 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5241 {
5242 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5243 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005244
Ronald Cron01b2aba2020-10-05 09:42:02 +02005245 /* Accept only byte-aligned keys, for the same reasons as
5246 * in psa_import_rsa_key(). */
5247 if( bits % 8 != 0 )
5248 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005249 }
5250 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005251#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005252
Ronald Cron5c4d3862020-12-07 11:07:24 +01005253#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005254 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5255 {
Ronald Crond81ab562021-02-16 09:01:16 +01005256 /* To avoid empty block, return successfully here. */
5257 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005258 }
5259 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005260#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005261 {
5262 return( PSA_ERROR_NOT_SUPPORTED );
5263 }
5264
5265 return( PSA_SUCCESS );
5266}
5267
Ronald Cron55ed0592020-10-05 10:30:40 +02005268psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005269 const psa_key_attributes_t *attributes,
5270 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005271{
5272 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005273 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005274
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005275 if( ( attributes->domain_parameters == NULL ) &&
5276 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005277 return( PSA_ERROR_INVALID_ARGUMENT );
5278
Ronald Cron01b2aba2020-10-05 09:42:02 +02005279 if( key_type_is_raw_bytes( type ) )
5280 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005281 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005282 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005283 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005284
David Brown12ca5032021-02-11 11:02:00 -07005285#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005286 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005287 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07005288#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005289 }
5290 else
5291
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005292#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5293 defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005294 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005295 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01005296 return( mbedtls_psa_rsa_generate_key( attributes,
5297 key_buffer,
5298 key_buffer_size,
5299 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005300 }
5301 else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005302#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5303 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005304
John Durkop9814fa22020-11-04 12:28:15 -08005305#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005306 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005307 {
Ronald Cron7023db52020-11-20 18:17:42 +01005308 return( mbedtls_psa_ecp_generate_key( attributes,
5309 key_buffer,
5310 key_buffer_size,
5311 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005312 }
5313 else
John Durkop9814fa22020-11-04 12:28:15 -08005314#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005315 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005316 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005317 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02005318 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005319
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005320 return( PSA_SUCCESS );
5321}
Darryl Green0c6575a2018-11-07 16:05:30 +00005322
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005323psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005324 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005325{
5326 psa_status_t status;
5327 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005328 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005329 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005330
Ronald Cron81709fc2020-11-14 12:10:32 +01005331 *key = MBEDTLS_SVC_KEY_ID_INIT;
5332
Gilles Peskine0f84d622019-09-12 19:03:13 +02005333 /* Reject any attempt to create a zero-length key so that we don't
5334 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5335 if( psa_get_key_bits( attributes ) == 0 )
5336 return( PSA_ERROR_INVALID_ARGUMENT );
5337
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005338 /* Reject any attempt to create a public key. */
5339 if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) )
5340 return( PSA_ERROR_INVALID_ARGUMENT );
5341
Ronald Cron81709fc2020-11-14 12:10:32 +01005342 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
5343 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005344 if( status != PSA_SUCCESS )
5345 goto exit;
5346
Ronald Cron977c2472020-10-13 08:32:21 +02005347 /* In the case of a transparent key or an opaque key stored in local
5348 * storage (thus not in the case of generating a key in a secure element
5349 * or cryptoprocessor with storage), we have to allocate a buffer to
5350 * hold the generated key material. */
5351 if( slot->key.data == NULL )
5352 {
5353 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
5354 PSA_KEY_LOCATION_LOCAL_STORAGE )
5355 {
Ronald Cron3772afe2021-02-08 16:10:05 +01005356 status = psa_validate_key_type_and_size_for_key_generation(
5357 attributes->core.type, attributes->core.bits );
5358 if( status != PSA_SUCCESS )
5359 goto exit;
5360
5361 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
5362 attributes->core.type,
5363 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02005364 }
5365 else
5366 {
5367 status = psa_driver_wrapper_get_key_buffer_size(
5368 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01005369 if( status != PSA_SUCCESS )
5370 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02005371 }
Ronald Cron977c2472020-10-13 08:32:21 +02005372
5373 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
5374 if( status != PSA_SUCCESS )
5375 goto exit;
5376 }
5377
Steven Cooreman2a1664c2020-07-20 15:33:08 +02005378 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02005379 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02005380
Ronald Cron2b56bc82020-10-05 10:02:26 +02005381 if( status != PSA_SUCCESS )
5382 psa_remove_key_data_from_memory( slot );
5383
Gilles Peskine11792082019-08-06 18:36:36 +02005384exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005385 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005386 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005387 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005388 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005389
Darryl Green0c6575a2018-11-07 16:05:30 +00005390 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005391}
5392
Gilles Peskinee59236f2018-01-27 23:32:46 +01005393/****************************************************************/
5394/* Module setup */
5395/****************************************************************/
5396
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005397#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005398psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5399 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5400 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5401{
5402 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5403 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005404 global_data.rng.entropy_init = entropy_init;
5405 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01005406 return( PSA_SUCCESS );
5407}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005408#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005409
Gilles Peskinee59236f2018-01-27 23:32:46 +01005410void mbedtls_psa_crypto_free( void )
5411{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005412 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005413 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5414 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005415 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005416 }
5417 /* Wipe all remaining data, including configuration.
5418 * In particular, this sets all state indicator to the value
5419 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005420 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Ronald Cron088d5d02021-04-10 16:57:30 +02005421
5422 /* Terminate drivers */
5423 psa_driver_wrapper_free( );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005424}
5425
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005426#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5427/** Recover a transaction that was interrupted by a power failure.
5428 *
5429 * This function is called during initialization, before psa_crypto_init()
5430 * returns. If this function returns a failure status, the initialization
5431 * fails.
5432 */
5433static psa_status_t psa_crypto_recover_transaction(
5434 const psa_crypto_transaction_t *transaction )
5435{
5436 switch( transaction->unknown.type )
5437 {
5438 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5439 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005440 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005441 * is implemented.
5442 * https://github.com/ARMmbed/mbed-crypto/issues/218
5443 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005444 default:
5445 /* We found an unsupported transaction in the storage.
5446 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01005447 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005448 }
5449}
5450#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5451
Gilles Peskinee59236f2018-01-27 23:32:46 +01005452psa_status_t psa_crypto_init( void )
5453{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005454 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005455
Gilles Peskinec6b69072018-11-20 21:42:52 +01005456 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005457 if( global_data.initialized != 0 )
5458 return( PSA_SUCCESS );
5459
Gilles Peskine30524eb2020-11-13 17:02:26 +01005460 /* Initialize and seed the random generator. */
5461 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005462 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01005463 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005464 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005465 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005466 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005467
Gilles Peskine66fb1262018-12-10 16:29:04 +01005468 status = psa_initialize_key_slots( );
5469 if( status != PSA_SUCCESS )
5470 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005471
Ronald Cron088d5d02021-04-10 16:57:30 +02005472 /* Init drivers */
5473 status = psa_driver_wrapper_init( );
Gilles Peskined9348f22019-10-01 15:22:29 +02005474 if( status != PSA_SUCCESS )
5475 goto exit;
Gilles Peskined9348f22019-10-01 15:22:29 +02005476
Gilles Peskine4b734222019-07-24 15:56:31 +02005477#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005478 status = psa_crypto_load_transaction( );
5479 if( status == PSA_SUCCESS )
5480 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005481 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5482 if( status != PSA_SUCCESS )
5483 goto exit;
5484 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005485 }
5486 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5487 {
5488 /* There's no transaction to complete. It's all good. */
5489 status = PSA_SUCCESS;
5490 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005491#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005492
Gilles Peskinec6b69072018-11-20 21:42:52 +01005493 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005494 global_data.initialized = 1;
5495
5496exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005497 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005498 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005499 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005500}
5501
5502#endif /* MBEDTLS_PSA_CRYPTO_C */