blob: 10c7d1e668a5bd02fcd820d59b0394a97104cb69 [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 Peskine2f9c4dc2018-01-28 13:16:24 +010054
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010055#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020057#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000058#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020059#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/blowfish.h"
61#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020062#include "mbedtls/chacha20.h"
63#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020068#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/error.h"
72#include "mbedtls/gcm.h"
73#include "mbedtls/md2.h"
74#include "mbedtls/md4.h"
75#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010076#include "mbedtls/md.h"
77#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010078#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010080#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000081#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010083#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/sha1.h"
85#include "mbedtls/sha256.h"
86#include "mbedtls/sha512.h"
87#include "mbedtls/xtea.h"
88
Gilles Peskine996deb12018-08-01 15:45:45 +020089#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
90
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010091/****************************************************************/
92/* Global data, support functions and library management */
93/****************************************************************/
94
Gilles Peskine48c0ea12018-06-21 14:15:31 +020095static int key_type_is_raw_bytes( psa_key_type_t type )
96{
Gilles Peskine78b3bb62018-08-10 16:03:41 +020097 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +020098}
99
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100100/* Values for psa_global_data_t::rng_state */
101#define RNG_NOT_INITIALIZED 0
102#define RNG_INITIALIZED 1
103#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100104
Gilles Peskine2d277862018-06-18 15:41:12 +0200105typedef struct
106{
Gilles Peskinec6b69072018-11-20 21:42:52 +0100107 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100108 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +0100109 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100110} psa_global_data_t;
111
112static psa_global_data_t global_data;
113
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100114#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
115mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
116 &global_data.rng.drbg;
117#endif
118
itayzafrir0adf0fc2018-09-06 16:24:41 +0300119#define GUARD_MODULE_INITIALIZED \
120 if( global_data.initialized == 0 ) \
121 return( PSA_ERROR_BAD_STATE );
122
Steven Cooreman01164162020-07-20 15:31:37 +0200123psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100124{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100125 /* Mbed TLS error codes can combine a high-level error code and a
126 * low-level error code. The low-level error usually reflects the
127 * root cause better, so dispatch on that preferably. */
128 int low_level_ret = - ( -ret & 0x007f );
129 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130 {
131 case 0:
132 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100133
134 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
135 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
136 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
137 return( PSA_ERROR_NOT_SUPPORTED );
138 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
139 return( PSA_ERROR_HARDWARE_FAILURE );
140
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200141 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
142 return( PSA_ERROR_HARDWARE_FAILURE );
143
Gilles Peskine9a944802018-06-21 09:35:35 +0200144 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
145 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
146 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
147 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
148 case MBEDTLS_ERR_ASN1_INVALID_DATA:
149 return( PSA_ERROR_INVALID_ARGUMENT );
150 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
151 return( PSA_ERROR_INSUFFICIENT_MEMORY );
152 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
153 return( PSA_ERROR_BUFFER_TOO_SMALL );
154
Jaeden Amero93e21112019-02-20 13:57:28 +0000155#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000156 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000157#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
158 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
159#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100160 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
161 return( PSA_ERROR_NOT_SUPPORTED );
162 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
163 return( PSA_ERROR_HARDWARE_FAILURE );
164
Jaeden Amero93e21112019-02-20 13:57:28 +0000165#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000166 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000167#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
168 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
169#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100170 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
171 return( PSA_ERROR_NOT_SUPPORTED );
172 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
173 return( PSA_ERROR_HARDWARE_FAILURE );
174
175 case MBEDTLS_ERR_CCM_BAD_INPUT:
176 return( PSA_ERROR_INVALID_ARGUMENT );
177 case MBEDTLS_ERR_CCM_AUTH_FAILED:
178 return( PSA_ERROR_INVALID_SIGNATURE );
179 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
Gilles Peskine26869f22019-05-06 15:25:00 +0200182 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
183 return( PSA_ERROR_INVALID_ARGUMENT );
184
185 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
186 return( PSA_ERROR_BAD_STATE );
187 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
188 return( PSA_ERROR_INVALID_SIGNATURE );
189
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
191 return( PSA_ERROR_NOT_SUPPORTED );
192 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
193 return( PSA_ERROR_INVALID_ARGUMENT );
194 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
195 return( PSA_ERROR_INSUFFICIENT_MEMORY );
196 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
197 return( PSA_ERROR_INVALID_PADDING );
198 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200199 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100200 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
201 return( PSA_ERROR_INVALID_SIGNATURE );
202 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200203 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
205 return( PSA_ERROR_HARDWARE_FAILURE );
206
207 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
Gilles Peskine82e57d12020-11-13 21:31:17 +0100210#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
211 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100212 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
213 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100214 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
215 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
216 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
218 return( PSA_ERROR_NOT_SUPPORTED );
219 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100221#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100222
223 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
Gilles Peskinee59236f2018-01-27 23:32:46 +0100228 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
229 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
230 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
233 case MBEDTLS_ERR_GCM_AUTH_FAILED:
234 return( PSA_ERROR_INVALID_SIGNATURE );
235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
238 return( PSA_ERROR_HARDWARE_FAILURE );
239
Gilles Peskine82e57d12020-11-13 21:31:17 +0100240#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
241 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100242 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
243 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100244 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
245 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
246 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
247 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
248 return( PSA_ERROR_NOT_SUPPORTED );
249 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
250 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
251#endif
252
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200253 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
254 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
255 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
256 return( PSA_ERROR_HARDWARE_FAILURE );
257
Gilles Peskinea5905292018-02-07 20:59:33 +0100258 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
259 return( PSA_ERROR_NOT_SUPPORTED );
260 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MD_ALLOC_FAILED:
263 return( PSA_ERROR_INSUFFICIENT_MEMORY );
264 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
265 return( PSA_ERROR_STORAGE_FAILURE );
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200266 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
267 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100268
Gilles Peskinef76aa772018-10-29 19:24:33 +0100269 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
270 return( PSA_ERROR_STORAGE_FAILURE );
271 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
272 return( PSA_ERROR_INVALID_ARGUMENT );
273 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
276 return( PSA_ERROR_BUFFER_TOO_SMALL );
277 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
284 return( PSA_ERROR_INSUFFICIENT_MEMORY );
285
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100286 case MBEDTLS_ERR_PK_ALLOC_FAILED:
287 return( PSA_ERROR_INSUFFICIENT_MEMORY );
288 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
289 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100293 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
294 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
297 return( PSA_ERROR_NOT_SUPPORTED );
298 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
299 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
300 return( PSA_ERROR_NOT_PERMITTED );
301 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
302 return( PSA_ERROR_INVALID_ARGUMENT );
303 case MBEDTLS_ERR_PK_INVALID_ALG:
304 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
305 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
306 return( PSA_ERROR_NOT_SUPPORTED );
307 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
308 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100309 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311
Gilles Peskineff2d2002019-05-06 15:26:23 +0200312 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
315 return( PSA_ERROR_NOT_SUPPORTED );
316
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200317 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
318 return( PSA_ERROR_HARDWARE_FAILURE );
319
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
321 return( PSA_ERROR_INVALID_ARGUMENT );
322 case MBEDTLS_ERR_RSA_INVALID_PADDING:
323 return( PSA_ERROR_INVALID_PADDING );
324 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
327 return( PSA_ERROR_INVALID_ARGUMENT );
328 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
329 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200330 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100331 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
332 return( PSA_ERROR_INVALID_SIGNATURE );
333 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
334 return( PSA_ERROR_BUFFER_TOO_SMALL );
335 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100336 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100337 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
338 return( PSA_ERROR_NOT_SUPPORTED );
339 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
340 return( PSA_ERROR_HARDWARE_FAILURE );
341
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200342 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
343 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
344 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
345 return( PSA_ERROR_HARDWARE_FAILURE );
346
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
348 return( PSA_ERROR_INVALID_ARGUMENT );
349 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
350 return( PSA_ERROR_HARDWARE_FAILURE );
351
itayzafrir5c753392018-05-08 11:18:38 +0300352 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300353 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300354 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300355 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
356 return( PSA_ERROR_BUFFER_TOO_SMALL );
357 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
358 return( PSA_ERROR_NOT_SUPPORTED );
359 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
360 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
361 return( PSA_ERROR_INVALID_SIGNATURE );
362 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
363 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100364 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
365 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300366 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
367 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100368
Janos Follatha13b9052019-11-22 12:48:59 +0000369 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
370 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300371
Gilles Peskinee59236f2018-01-27 23:32:46 +0100372 default:
David Saadab4ecc272019-02-14 13:48:10 +0200373 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100374 }
375}
376
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200377
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200378
379
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100380/****************************************************************/
381/* Key management */
382/****************************************************************/
383
John Durkop9814fa22020-11-04 12:28:15 -0800384#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800385 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100386 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
387 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
388 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100389mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 size_t bits,
391 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200392{
393 switch( curve )
394 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100395 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100396 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100397 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100398#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100399 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100400 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100401#endif
402#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100403 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100404 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#endif
406#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100408 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100409#endif
410#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100411 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100413#endif
414#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100415 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100416 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 case 528:
418 if( bits_is_sloppy )
419 return( MBEDTLS_ECP_DP_SECP521R1 );
420 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100421#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100422 }
423 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100424
Paul Elliott8ff510a2020-06-02 17:19:28 +0100425 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100426 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100427 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100428#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100430 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100431#endif
432#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100434 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100435#endif
436#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100437 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100438 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100439#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100440 }
441 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100442
Paul Elliott8ff510a2020-06-02 17:19:28 +0100443 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100444 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100445 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100446#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100448 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 256:
450 if( bits_is_sloppy )
451 return( MBEDTLS_ECP_DP_CURVE25519 );
452 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#endif
454#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100456 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 }
459 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100460
Paul Elliott8ff510a2020-06-02 17:19:28 +0100461 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100462 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100463 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100464#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100466 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100467#endif
468#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100470 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100471#endif
472#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100474 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100475#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100476 }
477 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200478 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100479
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100480 (void) bits_is_sloppy;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100481 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200482}
John Durkop9814fa22020-11-04 12:28:15 -0800483#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100484 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
485 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
486 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
487 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200488
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200489static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
490 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491{
492 /* Check that the bit size is acceptable for the key type */
493 switch( type )
494 {
495 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200497 case PSA_KEY_TYPE_DERIVE:
498 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100499#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500 case PSA_KEY_TYPE_AES:
501 if( bits != 128 && bits != 192 && bits != 256 )
502 return( PSA_ERROR_INVALID_ARGUMENT );
503 break;
504#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200505#if defined(PSA_WANT_KEY_TYPE_ARIA)
506 case PSA_KEY_TYPE_ARIA:
507 if( bits != 128 && bits != 192 && bits != 256 )
508 return( PSA_ERROR_INVALID_ARGUMENT );
509 break;
510#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100511#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200512 case PSA_KEY_TYPE_CAMELLIA:
513 if( bits != 128 && bits != 192 && bits != 256 )
514 return( PSA_ERROR_INVALID_ARGUMENT );
515 break;
516#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100517#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200518 case PSA_KEY_TYPE_DES:
519 if( bits != 64 && bits != 128 && bits != 192 )
520 return( PSA_ERROR_INVALID_ARGUMENT );
521 break;
522#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100523#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200524 case PSA_KEY_TYPE_ARC4:
525 if( bits < 8 || bits > 2048 )
526 return( PSA_ERROR_INVALID_ARGUMENT );
527 break;
528#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100529#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200530 case PSA_KEY_TYPE_CHACHA20:
531 if( bits != 256 )
532 return( PSA_ERROR_INVALID_ARGUMENT );
533 break;
534#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200535 default:
536 return( PSA_ERROR_NOT_SUPPORTED );
537 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200538 if( bits % 8 != 0 )
539 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200541 return( PSA_SUCCESS );
542}
543
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100544/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100545 *
Steven Cooremancd640932021-03-08 12:00:27 +0100546 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
547 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548 *
549 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100550 * \param[in] key_type The key type of the key to be used with the
551 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100552 *
553 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100554 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100555 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100556 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100557 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100558MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100559 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100560 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100561{
Steven Cooremancd640932021-03-08 12:00:27 +0100562 if( PSA_ALG_IS_HMAC( algorithm ) )
563 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564 if( key_type == PSA_KEY_TYPE_HMAC )
565 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100566 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100567
568 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100569 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100570 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
571 * key. */
572 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
573 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
574 {
575 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
576 * the block length (larger than 1) for block ciphers. */
577 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
578 return( PSA_SUCCESS );
579 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100580 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100581
582 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100583}
584
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200585psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
586 size_t buffer_length )
Steven Cooreman75b74362020-07-28 14:30:13 +0200587{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100588 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200589 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200590
Ronald Cronea0f8a62020-11-25 17:52:23 +0100591 slot->key.data = mbedtls_calloc( 1, buffer_length );
592 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200593 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200594
Ronald Cronea0f8a62020-11-25 17:52:23 +0100595 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200596 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200597}
598
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200599psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
600 const uint8_t* data,
601 size_t data_length )
602{
603 psa_status_t status = psa_allocate_buffer_to_slot( slot,
604 data_length );
605 if( status != PSA_SUCCESS )
606 return( status );
607
Ronald Cronea0f8a62020-11-25 17:52:23 +0100608 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200609 return( PSA_SUCCESS );
610}
611
Ronald Crona0fe59f2020-11-29 11:14:53 +0100612psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100613 const psa_key_attributes_t *attributes,
614 const uint8_t *data, size_t data_length,
615 uint8_t *key_buffer, size_t key_buffer_size,
616 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100617{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100618 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
619 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100620
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200621 /* zero-length keys are never supported. */
622 if( data_length == 0 )
623 return( PSA_ERROR_NOT_SUPPORTED );
624
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100625 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100626 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100627 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200628
Steven Cooreman75b74362020-07-28 14:30:13 +0200629 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
630 if( data_length > SIZE_MAX / 8 )
631 return( PSA_ERROR_NOT_SUPPORTED );
632
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200633 /* Enforce a size limit, and in particular ensure that the bit
634 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100635 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200636 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200637
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100638 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200639 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200640 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200641
Ronald Crondd04d422020-11-28 15:14:42 +0100642 /* Copy the key material. */
643 memcpy( key_buffer, data, data_length );
644 *key_buffer_length = data_length;
645 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200646
Steven Cooreman40120f62020-10-29 11:42:22 +0100647 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100649 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200650 {
John Durkop9814fa22020-11-04 12:28:15 -0800651#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
652 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100653 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200654 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100655 return( mbedtls_psa_ecp_import_key( attributes,
656 data, data_length,
657 key_buffer, key_buffer_size,
658 key_buffer_length,
659 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100660 }
John Durkop9814fa22020-11-04 12:28:15 -0800661#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
662 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700663#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
664 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100665 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200666 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100667 return( mbedtls_psa_rsa_import_key( attributes,
668 data, data_length,
669 key_buffer, key_buffer_size,
670 key_buffer_length,
671 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200672 }
John Durkop9814fa22020-11-04 12:28:15 -0800673#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
674 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100675 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100676
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100677 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100678}
679
Gilles Peskinef603c712019-01-19 13:40:11 +0100680/** Calculate the intersection of two algorithm usage policies.
681 *
682 * Return 0 (which allows no operation) on incompatibility.
683 */
684static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100685 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100686 psa_algorithm_t alg1,
687 psa_algorithm_t alg2 )
688{
Gilles Peskine549ea862019-05-22 11:45:59 +0200689 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100690 if( alg1 == alg2 )
691 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100692 /* If the policies are from the same hash-and-sign family, check
693 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine8cb22c82021-09-22 16:15:05 +0200694 if( PSA_ALG_IS_SIGN_HASH( alg1 ) &&
695 PSA_ALG_IS_SIGN_HASH( alg2 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100696 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100697 {
Steven Cooreman03488022021-02-18 12:07:20 +0100698 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
699 return( alg2 );
700 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
701 return( alg1 );
702 }
703 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100704 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100705 * restrictive tag length. */
706 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
707 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
708 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
709 {
710 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
711 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100712 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100713
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100714 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100715 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
716 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100717 {
Steven Cooremancd640932021-03-08 12:00:27 +0100718 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
719 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100720 }
721 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100722 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100723 ( alg1_len <= alg2_len ) )
724 {
725 return( alg2 );
726 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100727 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100728 ( alg2_len <= alg1_len ) )
729 {
730 return( alg1 );
731 }
732 }
733 /* If the policies are from the same MAC family, check whether one
734 * of them is a minimum-MAC-length policy. Calculate the most
735 * restrictive tag length. */
736 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
737 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
738 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
739 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100740 /* Validate the combination of key type and algorithm. Since the base
741 * algorithm of alg1 and alg2 are the same, we only need this once. */
742 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100743 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100744
Steven Cooreman31a876d2021-03-03 20:47:40 +0100745 /* Get the (exact or at-least) output lengths for both sides of the
746 * requested intersection. None of the currently supported algorithms
747 * have an output length dependent on the actual key size, so setting it
748 * to a bogus value of 0 is currently OK.
749 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100750 * Note that for at-least-this-length wildcard algorithms, the output
751 * length is set to the shortest allowed length, which allows us to
752 * calculate the most restrictive tag length for the intersection. */
753 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
754 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100755 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100756
Steven Cooremana1d83222021-02-25 10:20:29 +0100757 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100758 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
759 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100760 {
Steven Cooremancd640932021-03-08 12:00:27 +0100761 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100762 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100763
764 /* If only one is an at-least-this-length policy, the intersection would
765 * be the other (fixed-length) policy as long as said fixed length is
766 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100767 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100768 {
Steven Cooremancd640932021-03-08 12:00:27 +0100769 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100770 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100771 if( ( alg2 & 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( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100774 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100775
Steven Cooremancd640932021-03-08 12:00:27 +0100776 /* If none of them are wildcards, check whether they define the same tag
777 * length. This is still possible here when one is default-length and
778 * the other specific-length. Ensure to always return the
779 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100780 if( alg1_len == alg2_len )
781 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100782 }
783 /* If the policies are incompatible, allow nothing. */
784 return( 0 );
785}
786
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100787static int psa_key_algorithm_permits( psa_key_type_t key_type,
788 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200789 psa_algorithm_t requested_alg )
790{
Gilles Peskine549ea862019-05-22 11:45:59 +0200791 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200792 if( requested_alg == policy_alg )
793 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100794 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
795 * and requested_alg is the same hash-and-sign family with any hash,
796 * then requested_alg is compliant with policy_alg. */
Gilles Peskine8cb22c82021-09-22 16:15:05 +0200797 if( PSA_ALG_IS_SIGN_HASH( requested_alg ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100798 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200799 {
Steven Cooreman03488022021-02-18 12:07:20 +0100800 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
801 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
802 }
803 /* If policy_alg is a wildcard AEAD algorithm of the same base as
804 * the requested algorithm, check the requested tag length to be
805 * equal-length or longer than the wildcard-specified length. */
806 if( PSA_ALG_IS_AEAD( policy_alg ) &&
807 PSA_ALG_IS_AEAD( requested_alg ) &&
808 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
809 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100810 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100811 {
812 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
813 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
814 }
Steven Cooremancd640932021-03-08 12:00:27 +0100815 /* If policy_alg is a MAC algorithm of the same base as the requested
816 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100817 if( PSA_ALG_IS_MAC( policy_alg ) &&
818 PSA_ALG_IS_MAC( requested_alg ) &&
819 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100820 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100821 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100822 /* Validate the combination of key type and algorithm. Since the policy
823 * and requested algorithms are the same, we only need this once. */
824 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100825 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100826
Steven Cooreman31a876d2021-03-03 20:47:40 +0100827 /* Get both the requested output length for the algorithm which is to be
828 * verified, and the default output length for the base algorithm.
829 * Note that none of the currently supported algorithms have an output
830 * length dependent on actual key size, so setting it to a bogus value
831 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100832 size_t requested_output_length = PSA_MAC_LENGTH(
833 key_type, 0, requested_alg );
834 size_t default_output_length = PSA_MAC_LENGTH(
835 key_type, 0,
836 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100837
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100838 /* If the policy is default-length, only allow an algorithm with
839 * a declared exact-length matching the default. */
840 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100841 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100842
843 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100844 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100845 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
846 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
847 {
848 return( 1 );
849 }
850
Steven Cooremancd640932021-03-08 12:00:27 +0100851 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
852 * check for the requested MAC length to be equal to or longer than the
853 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100854 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
855 {
856 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100857 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100858 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200859 }
Steven Cooremance48e852020-10-05 16:02:45 +0200860 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200861 * a key derivation with that key agreement should also be allowed. This
862 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200863 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
864 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
865 {
866 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
867 policy_alg );
868 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100869 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200870 return( 0 );
871}
872
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100873/** Test whether a policy permits an algorithm.
874 *
875 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100876 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100877 * \note This function requires providing the key type for which the policy is
878 * being validated, since some algorithm policy definitions (e.g. MAC)
879 * have different properties depending on what kind of cipher it is
880 * combined with.
881 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100882 * \retval PSA_SUCCESS When \p alg is a specific algorithm
883 * allowed by the \p policy.
884 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
885 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
886 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100887 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100888static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100889 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100890 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100891{
Steven Cooremand788fab2021-02-25 11:29:17 +0100892 /* '0' is not a valid algorithm */
893 if( alg == 0 )
894 return( PSA_ERROR_INVALID_ARGUMENT );
895
Steven Cooreman7e39f052021-02-23 14:18:32 +0100896 /* A requested algorithm cannot be a wildcard. */
897 if( PSA_ALG_IS_WILDCARD( alg ) )
898 return( PSA_ERROR_INVALID_ARGUMENT );
899
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100900 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
901 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100902 return( PSA_SUCCESS );
903 else
904 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100905}
906
Gilles Peskinef603c712019-01-19 13:40:11 +0100907/** Restrict a key policy based on a constraint.
908 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100909 * \note This function requires providing the key type for which the policy is
910 * being restricted, since some algorithm policy definitions (e.g. MAC)
911 * have different properties depending on what kind of cipher it is
912 * combined with.
913 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100914 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100915 * \param[in,out] policy The policy to restrict.
916 * \param[in] constraint The policy constraint to apply.
917 *
918 * \retval #PSA_SUCCESS
919 * \c *policy contains the intersection of the original value of
920 * \c *policy and \c *constraint.
921 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100922 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100923 * \c *policy is unchanged.
924 */
925static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100926 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100927 psa_key_policy_t *policy,
928 const psa_key_policy_t *constraint )
929{
930 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100931 psa_key_policy_algorithm_intersection( key_type, policy->alg,
932 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200933 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100934 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
935 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100936 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
937 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200938 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
939 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100940 policy->usage &= constraint->usage;
941 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200942 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100943 return( PSA_SUCCESS );
944}
945
Ronald Cron5c522922020-11-14 16:35:34 +0100946/** Get the description of a key given its identifier and policy constraints
947 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200948 *
Ronald Cron5c522922020-11-14 16:35:34 +0100949 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100950 * nonzero, the key must allow operations with this algorithm. If \p alg is
951 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100952 *
Ronald Cron5c522922020-11-14 16:35:34 +0100953 * In case of a persistent key, the function loads the description of the key
954 * into a key slot if not already done.
955 *
956 * On success, the returned key slot is locked. It is the responsibility of
957 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200958 */
Ronald Cron5c522922020-11-14 16:35:34 +0100959static psa_status_t psa_get_and_lock_key_slot_with_policy(
960 mbedtls_svc_key_id_t key,
961 psa_key_slot_t **p_slot,
962 psa_key_usage_t usage,
963 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100964{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200965 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
966 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100967
Ronald Cron5c522922020-11-14 16:35:34 +0100968 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100969 if( status != PSA_SUCCESS )
970 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200971 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100972
973 /* Enforce that usage policy for the key slot contains all the flags
974 * required by the usage parameter. There is one exception: public
975 * keys can always be exported, so we treat public key objects as
976 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200977 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100978 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200979
Gilles Peskine8e338702019-07-30 20:06:31 +0200980 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100981 {
982 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200983 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100984 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100985
Shaun Case0e7791f2021-12-20 21:14:10 -0800986 /* Enforce that the usage policy permits the requested algorithm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100987 if( alg != 0 )
988 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100989 status = psa_key_policy_permits( &slot->attr.policy,
990 slot->attr.type,
991 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +0100992 if( status != PSA_SUCCESS )
993 goto error;
994 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100995
Darryl Green06fd18d2018-07-16 11:21:11 +0100996 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200997
998error:
999 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001000 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001
1002 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001003}
Darryl Green940d72c2018-07-13 13:18:51 +01001004
Ronald Cron5c522922020-11-14 16:35:34 +01001005/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001006 *
1007 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001008 * available, as opposed to a key in a secure element and/or to be used
1009 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001010 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001011 * This is a temporary function that may be used instead of
1012 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1013 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001014 *
Ronald Cron5c522922020-11-14 16:35:34 +01001015 * On success, the returned key slot is locked. It is the responsibility of the
1016 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001017 */
Ronald Cron5c522922020-11-14 16:35:34 +01001018static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1019 mbedtls_svc_key_id_t key,
1020 psa_key_slot_t **p_slot,
1021 psa_key_usage_t usage,
1022 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001023{
Ronald Cron5c522922020-11-14 16:35:34 +01001024 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1025 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001026 if( status != PSA_SUCCESS )
1027 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001028
Ronald Cron8a0466a2021-08-24 15:39:44 +02001029 if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001030 {
Ronald Cron5c522922020-11-14 16:35:34 +01001031 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001032 *p_slot = NULL;
1033 return( PSA_ERROR_NOT_SUPPORTED );
1034 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001035
Gilles Peskine28f8f302019-07-24 13:30:31 +02001036 return( PSA_SUCCESS );
1037}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001038
Steven Cooreman7ddee7f2021-04-07 18:08:30 +02001039psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001040{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001041 /* Data pointer will always be either a valid pointer or NULL in an
1042 * initialized slot, so we can just free it. */
1043 if( slot->key.data != NULL )
1044 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1045
1046 mbedtls_free( slot->key.data );
1047 slot->key.data = NULL;
1048 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001049
1050 return( PSA_SUCCESS );
1051}
1052
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001053/** Completely wipe a slot in memory, including its policy.
1054 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001055psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001056{
1057 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001058
1059 /*
1060 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001061 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001062 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1063 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001064 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001065 */
Ronald Cron5c522922020-11-14 16:35:34 +01001066 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001067 {
1068#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001069 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001070#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001071 status = PSA_ERROR_CORRUPTION_DETECTED;
1072 }
1073
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001074 /* Multipart operations may still be using the key. This is safe
1075 * because all multipart operation objects are independent from
1076 * the key slot: if they need to access the key after the setup
1077 * phase, they have a copy of the key. Note that this means that
1078 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001079 /* At this point, key material and other type-specific content has
1080 * been wiped. Clear remaining metadata. We can call memset and not
1081 * zeroize because the metadata is not particularly sensitive. */
1082 memset( slot, 0, sizeof( *slot ) );
1083 return( status );
1084}
1085
Ronald Croncf56a0a2020-08-04 09:51:30 +02001086psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001088 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001089 psa_status_t status; /* status of the last operation */
1090 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001091#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1092 psa_se_drv_table_entry_t *driver;
1093#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001094
Ronald Croncf56a0a2020-08-04 09:51:30 +02001095 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001096 return( PSA_SUCCESS );
1097
Ronald Cronf2911112020-10-29 17:51:10 +01001098 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001099 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001100 * key, this will load the key description from persistent memory if not
1101 * done yet. We cannot avoid this loading as without it we don't know if
1102 * the key is operated by an SE or not and this information is needed by
1103 * the current implementation.
1104 */
Ronald Cron5c522922020-11-14 16:35:34 +01001105 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001106 if( status != PSA_SUCCESS )
1107 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001108
Ronald Cronf2911112020-10-29 17:51:10 +01001109 /*
1110 * If the key slot containing the key description is under access by the
1111 * library (apart from the present access), the key cannot be destroyed
1112 * yet. For the time being, just return in error. Eventually (to be
1113 * implemented), the key should be destroyed when all accesses have
1114 * stopped.
1115 */
Ronald Cron5c522922020-11-14 16:35:34 +01001116 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001117 {
Ronald Cron5c522922020-11-14 16:35:34 +01001118 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001119 return( PSA_ERROR_GENERIC_ERROR );
1120 }
1121
Gilles Peskine251c7742021-04-21 22:32:05 +02001122 if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) )
1123 {
1124 /* Refuse the destruction of a read-only key (which may or may not work
1125 * if we attempt it, depending on whether the key is merely read-only
1126 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001127 * Just do the best we can, which is to wipe the copy in memory
1128 * (done in this function's cleanup code). */
1129 overall_status = PSA_ERROR_NOT_PERMITTED;
1130 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001131 }
1132
Gilles Peskine354f7672019-07-12 23:46:38 +02001133#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001134 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001135 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001136 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001137 /* For a key in a secure element, we need to do three things:
1138 * remove the key file in internal storage, destroy the
1139 * key inside the secure element, and update the driver's
1140 * persistent data. Start a transaction that will encompass these
1141 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001142 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001143 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001144 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001145 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001146 status = psa_crypto_save_transaction( );
1147 if( status != PSA_SUCCESS )
1148 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001149 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001150 /* We should still try to destroy the key in the secure
1151 * element and the key metadata in storage. This is especially
1152 * important if the error is that the storage is full.
1153 * But how to do it exactly without risking an inconsistent
1154 * state after a reset?
1155 * https://github.com/ARMmbed/mbed-crypto/issues/215
1156 */
1157 overall_status = status;
1158 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001159 }
1160
Ronald Cronea0f8a62020-11-25 17:52:23 +01001161 status = psa_destroy_se_key( driver,
1162 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001163 if( overall_status == PSA_SUCCESS )
1164 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001165 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001166#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1167
Darryl Greend49a4992018-06-18 17:27:26 +01001168#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001169 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001170 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001171 status = psa_destroy_persistent_key( slot->attr.id );
1172 if( overall_status == PSA_SUCCESS )
1173 overall_status = status;
1174
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001175 /* TODO: other slots may have a copy of the same key. We should
1176 * invalidate them.
1177 * https://github.com/ARMmbed/mbed-crypto/issues/214
1178 */
Darryl Greend49a4992018-06-18 17:27:26 +01001179 }
1180#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001181
Gilles Peskinefc762652019-07-22 19:30:34 +02001182#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1183 if( driver != NULL )
1184 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001185 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001186 if( overall_status == PSA_SUCCESS )
1187 overall_status = status;
1188 status = psa_crypto_stop_transaction( );
1189 if( overall_status == PSA_SUCCESS )
1190 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001191 }
1192#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1193
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001194exit:
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001195 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001196 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001197 if( status != PSA_SUCCESS )
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001198 overall_status = status;
1199 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001200}
1201
John Durkop07cc04a2020-11-16 22:08:34 -08001202#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001203 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001204static psa_status_t psa_get_rsa_public_exponent(
1205 const mbedtls_rsa_context *rsa,
1206 psa_key_attributes_t *attributes )
1207{
1208 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001209 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001210 uint8_t *buffer = NULL;
1211 size_t buflen;
1212 mbedtls_mpi_init( &mpi );
1213
1214 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1215 if( ret != 0 )
1216 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001217 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1218 {
1219 /* It's the default value, which is reported as an empty string,
1220 * so there's nothing to do. */
1221 goto exit;
1222 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001223
1224 buflen = mbedtls_mpi_size( &mpi );
1225 buffer = mbedtls_calloc( 1, buflen );
1226 if( buffer == NULL )
1227 {
1228 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1229 goto exit;
1230 }
1231 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1232 if( ret != 0 )
1233 goto exit;
1234 attributes->domain_parameters = buffer;
1235 attributes->domain_parameters_size = buflen;
1236
1237exit:
1238 mbedtls_mpi_free( &mpi );
1239 if( ret != 0 )
1240 mbedtls_free( buffer );
1241 return( mbedtls_to_psa_error( ret ) );
1242}
John Durkop07cc04a2020-11-16 22:08:34 -08001243#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001244 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001245
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001246/** Retrieve all the publicly-accessible attributes of a key.
1247 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001248psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001249 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001250{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001251 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001252 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001253 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001255 psa_reset_key_attributes( attributes );
1256
Ronald Cron5c522922020-11-14 16:35:34 +01001257 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001258 if( status != PSA_SUCCESS )
1259 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001260
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001261 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001262 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1263 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1264
1265#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron0518f612021-08-24 15:50:05 +02001266 if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001267 psa_set_key_slot_number( attributes,
1268 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001269#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001270
Gilles Peskine8e338702019-07-30 20:06:31 +02001271 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001272 {
John Durkop07cc04a2020-11-16 22:08:34 -08001273#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001274 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001275 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001276 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001277 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001278 * is not yet implemented.
1279 * https://github.com/ARMmbed/mbed-crypto/issues/216
1280 */
Ronald Cron41e80062021-08-24 16:00:51 +02001281 if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02001282 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001283 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001284
Ronald Cron90857082020-11-25 14:58:33 +01001285 status = mbedtls_psa_rsa_load_representation(
1286 slot->attr.type,
1287 slot->key.data,
1288 slot->key.bytes,
1289 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001290 if( status != PSA_SUCCESS )
1291 break;
1292
Steven Cooremana2371e52020-07-28 14:30:39 +02001293 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001294 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001295 mbedtls_rsa_free( rsa );
1296 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001297 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001298 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001299#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001300 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001301 default:
1302 /* Nothing else to do. */
1303 break;
1304 }
1305
1306 if( status != PSA_SUCCESS )
1307 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001308
Ronald Cron5c522922020-11-14 16:35:34 +01001309 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001310
Ronald Cron5c522922020-11-14 16:35:34 +01001311 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312}
1313
Gilles Peskinec8000c02019-08-02 20:15:51 +02001314#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1315psa_status_t psa_get_key_slot_number(
1316 const psa_key_attributes_t *attributes,
1317 psa_key_slot_number_t *slot_number )
1318{
1319 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1320 {
1321 *slot_number = attributes->slot_number;
1322 return( PSA_SUCCESS );
1323 }
1324 else
1325 return( PSA_ERROR_INVALID_ARGUMENT );
1326}
1327#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1328
Ronald Crond18b5f82020-11-25 16:46:05 +01001329static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1330 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001331 uint8_t *data,
1332 size_t data_size,
1333 size_t *data_length )
1334{
Ronald Crond18b5f82020-11-25 16:46:05 +01001335 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001336 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001337 memcpy( data, key_buffer, key_buffer_size );
1338 memset( data + key_buffer_size, 0,
1339 data_size - key_buffer_size );
1340 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001341 return( PSA_SUCCESS );
1342}
1343
Ronald Cron7285cda2020-11-26 14:46:37 +01001344psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001345 const psa_key_attributes_t *attributes,
1346 const uint8_t *key_buffer, size_t key_buffer_size,
1347 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001348{
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 psa_key_type_t type = attributes->core.type;
1350
Ronald Crond18b5f82020-11-25 16:46:05 +01001351 if( key_type_is_raw_bytes( type ) ||
1352 PSA_KEY_TYPE_IS_RSA( type ) ||
1353 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001354 {
1355 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001356 key_buffer, key_buffer_size,
1357 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001358 }
1359 else
1360 {
1361 /* This shouldn't happen in the reference implementation, but
1362 it is valid for a special-purpose implementation to omit
1363 support for exporting certain key types. */
1364 return( PSA_ERROR_NOT_SUPPORTED );
1365 }
1366}
1367
1368psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1369 uint8_t *data,
1370 size_t data_size,
1371 size_t *data_length )
1372{
1373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1374 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1375 psa_key_slot_t *slot;
1376
Ronald Crone907e552021-01-18 13:22:38 +01001377 /* Reject a zero-length output buffer now, since this can never be a
1378 * valid key representation. This way we know that data must be a valid
1379 * pointer and we can do things like memset(data, ..., data_size). */
1380 if( data_size == 0 )
1381 return( PSA_ERROR_BUFFER_TOO_SMALL );
1382
Ronald Cron9486f9d2020-11-26 13:36:23 +01001383 /* Set the key to empty now, so that even when there are errors, we always
1384 * set data_length to a value between 0 and data_size. On error, setting
1385 * the key to empty is a good choice because an empty key representation is
1386 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001387 *data_length = 0;
1388
Ronald Cron9486f9d2020-11-26 13:36:23 +01001389 /* Export requires the EXPORT flag. There is an exception for public keys,
1390 * which don't require any flag, but
1391 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1392 */
1393 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1394 PSA_KEY_USAGE_EXPORT, 0 );
1395 if( status != PSA_SUCCESS )
1396 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001397
Ronald Crond18b5f82020-11-25 16:46:05 +01001398 psa_key_attributes_t attributes = {
1399 .core = slot->attr
1400 };
Ronald Cron67227982020-11-26 15:16:05 +01001401 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001402 slot->key.data, slot->key.bytes,
1403 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001404
Ronald Cron9486f9d2020-11-26 13:36:23 +01001405 unlock_status = psa_unlock_key_slot( slot );
1406
1407 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1408}
1409
Ronald Cron7285cda2020-11-26 14:46:37 +01001410psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001411 const psa_key_attributes_t *attributes,
1412 const uint8_t *key_buffer,
1413 size_t key_buffer_size,
1414 uint8_t *data,
1415 size_t data_size,
1416 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001417{
Ronald Crond18b5f82020-11-25 16:46:05 +01001418 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001419
Ronald Crond18b5f82020-11-25 16:46:05 +01001420 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001421 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001422 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001423 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001424 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001425 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001426 key_buffer, key_buffer_size,
1427 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001428 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001429
Ronald Crond18b5f82020-11-25 16:46:05 +01001430 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001431 {
John Durkop0e005192020-10-31 22:06:54 -07001432#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1433 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001434 return( mbedtls_psa_rsa_export_public_key( attributes,
1435 key_buffer,
1436 key_buffer_size,
1437 data,
1438 data_size,
1439 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001440#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001441 /* We don't know how to convert a private RSA key to public. */
1442 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001443#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1444 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001445 }
1446 else
Moran Pekera998bc62018-04-16 18:16:20 +03001447 {
John Durkop6ba40d12020-11-10 08:50:04 -08001448#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1449 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001450 return( mbedtls_psa_ecp_export_public_key( attributes,
1451 key_buffer,
1452 key_buffer_size,
1453 data,
1454 data_size,
1455 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001456#else
1457 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001458 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001459#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1460 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001461 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001462 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001463 else
1464 {
1465 /* This shouldn't happen in the reference implementation, but
1466 it is valid for a special-purpose implementation to omit
1467 support for exporting certain key types. */
1468 return( PSA_ERROR_NOT_SUPPORTED );
1469 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001470}
Ronald Crond18b5f82020-11-25 16:46:05 +01001471
Ronald Croncf56a0a2020-08-04 09:51:30 +02001472psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001473 uint8_t *data,
1474 size_t data_size,
1475 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001476{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001478 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001479 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001480
Ronald Crone907e552021-01-18 13:22:38 +01001481 /* Reject a zero-length output buffer now, since this can never be a
1482 * valid key representation. This way we know that data must be a valid
1483 * pointer and we can do things like memset(data, ..., data_size). */
1484 if( data_size == 0 )
1485 return( PSA_ERROR_BUFFER_TOO_SMALL );
1486
Darryl Greendd8fb772018-11-07 16:00:44 +00001487 /* Set the key to empty now, so that even when there are errors, we always
1488 * set data_length to a value between 0 and data_size. On error, setting
1489 * the key to empty is a good choice because an empty key representation is
1490 * unlikely to be accepted anywhere. */
1491 *data_length = 0;
1492
1493 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001494 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001495 if( status != PSA_SUCCESS )
1496 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001497
Ronald Cron9486f9d2020-11-26 13:36:23 +01001498 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1499 {
1500 status = PSA_ERROR_INVALID_ARGUMENT;
1501 goto exit;
1502 }
1503
Ronald Crond18b5f82020-11-25 16:46:05 +01001504 psa_key_attributes_t attributes = {
1505 .core = slot->attr
1506 };
Ronald Cron67227982020-11-26 15:16:05 +01001507 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001508 &attributes, slot->key.data, slot->key.bytes,
1509 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001510
1511exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001512 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001513
Ronald Cron5c522922020-11-14 16:35:34 +01001514 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001515}
1516
Gilles Peskine91e8c332019-08-02 19:19:39 +02001517#if defined(static_assert)
1518static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1519 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001520static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001521 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001522static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001523 "One or more key attribute flag is listed as both internal-only and external-only" );
1524#endif
1525
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001526/** Validate that a key policy is internally well-formed.
1527 *
1528 * This function only rejects invalid policies. It does not validate the
1529 * consistency of the policy with respect to other attributes of the key
1530 * such as the key type.
1531 */
1532static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001533{
1534 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001535 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001536 PSA_KEY_USAGE_ENCRYPT |
1537 PSA_KEY_USAGE_DECRYPT |
gabor-mezei-arme8efa392021-04-14 21:14:28 +02001538 PSA_KEY_USAGE_SIGN_MESSAGE |
1539 PSA_KEY_USAGE_VERIFY_MESSAGE |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001540 PSA_KEY_USAGE_SIGN_HASH |
1541 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001542 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1543 return( PSA_ERROR_INVALID_ARGUMENT );
1544
Gilles Peskine4747d192019-04-17 15:05:45 +02001545 return( PSA_SUCCESS );
1546}
1547
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001548/** Validate the internal consistency of key attributes.
1549 *
1550 * This function only rejects invalid attribute values. If does not
1551 * validate the consistency of the attributes with any key data that may
1552 * be involved in the creation of the key.
1553 *
1554 * Call this function early in the key creation process.
1555 *
1556 * \param[in] attributes Key attributes for the new key.
1557 * \param[out] p_drv On any return, the driver for the key, if any.
1558 * NULL for a transparent key.
1559 *
1560 */
1561static psa_status_t psa_validate_key_attributes(
1562 const psa_key_attributes_t *attributes,
1563 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001564{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001565 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001566 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001567 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001568
Ronald Cron54b90082020-10-29 15:26:43 +01001569 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001570 if( status != PSA_SUCCESS )
1571 return( status );
1572
Ronald Crond2ed4812020-07-17 16:11:30 +02001573 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001574 if( status != PSA_SUCCESS )
1575 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001576
Ronald Cron65f38a32020-10-23 17:11:13 +02001577 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1578 {
1579 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1580 return( PSA_ERROR_INVALID_ARGUMENT );
1581 }
1582 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001583 {
Ronald Cron77e412c2021-03-31 17:36:31 +02001584 if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) )
1585 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Crond2ed4812020-07-17 16:11:30 +02001586 }
1587
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001588 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001589 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001590 return( status );
1591
1592 /* Refuse to create overly large keys.
1593 * Note that this doesn't trigger on import if the attributes don't
1594 * explicitly specify a size (so psa_get_key_bits returns 0), so
1595 * psa_import_key() needs its own checks. */
1596 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1597 return( PSA_ERROR_NOT_SUPPORTED );
1598
Gilles Peskine91e8c332019-08-02 19:19:39 +02001599 /* Reject invalid flags. These should not be reachable through the API. */
1600 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1601 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1602 return( PSA_ERROR_INVALID_ARGUMENT );
1603
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001604 return( PSA_SUCCESS );
1605}
1606
Gilles Peskine4747d192019-04-17 15:05:45 +02001607/** Prepare a key slot to receive key material.
1608 *
1609 * This function allocates a key slot and sets its metadata.
1610 *
1611 * If this function fails, call psa_fail_key_creation().
1612 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001613 * This function is intended to be used as follows:
1614 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001615 * it with the specified attributes, and in case of a volatile key assign it
1616 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001617 * -# Populate the slot with the key material.
1618 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1619 * In case of failure at any step, stop the sequence and call
1620 * psa_fail_key_creation().
1621 *
Ronald Cron5c522922020-11-14 16:35:34 +01001622 * On success, the key slot is locked. It is the responsibility of the caller
1623 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001624 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001625 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001626 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001627 * \param[out] p_slot On success, a pointer to the prepared slot.
1628 * \param[out] p_drv On any return, the driver for the key, if any.
1629 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001630 *
1631 * \retval #PSA_SUCCESS
1632 * The key slot is ready to receive key material.
1633 * \return If this function fails, the key slot is an invalid state.
1634 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001635 */
1636static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001637 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001638 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001639 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001640 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001641{
1642 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001643 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001644 psa_key_slot_t *slot;
1645
Gilles Peskinedf179142019-07-15 22:02:14 +02001646 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001647 *p_drv = NULL;
1648
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001649 status = psa_validate_key_attributes( attributes, p_drv );
1650 if( status != PSA_SUCCESS )
1651 return( status );
1652
Ronald Cronc4d1b512020-07-31 11:26:37 +02001653 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001654 if( status != PSA_SUCCESS )
1655 return( status );
1656 slot = *p_slot;
1657
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001658 /* We're storing the declared bit-size of the key. It's up to each
1659 * creation mechanism to verify that this information is correct.
1660 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001661 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001662 * is optional (import, copy). In case of a volatile key, assign it the
1663 * volatile key identifier associated to the slot returned to contain its
1664 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001665
1666 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001667 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1668 {
1669#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1670 slot->attr.id = volatile_key_id;
1671#else
1672 slot->attr.id.key_id = volatile_key_id;
1673#endif
1674 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001675
Gilles Peskine91e8c332019-08-02 19:19:39 +02001676 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001677 * external-only flags, query `attributes`. Thanks to the check
1678 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001679 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001680 * may have set. */
1681 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001682
Gilles Peskinecbaff462019-07-12 23:46:04 +02001683#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001684 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001685 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001686 * create the key file in internal storage, create the
1687 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001688 * persistent data. This is done by starting a transaction that will
1689 * encompass these three actions.
1690 * For registering a volatile key, we just need to find an appropriate
1691 * slot number inside the SE. Since the key is designated volatile, creating
1692 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001693 /* The first thing to do is to find a slot number for the new key.
1694 * We save the slot number in persistent storage as part of the
1695 * transaction data. It will be needed to recover if the power
1696 * fails during the key creation process, to clean up on the secure
1697 * element side after restarting. Obtaining a slot number from the
1698 * secure element driver updates its persistent state, but we do not yet
1699 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001700 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001701 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001702 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001703 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001704 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001705 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001706 if( status != PSA_SUCCESS )
1707 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001708
1709 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001710 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001711 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1712 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001713 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001714 psa_crypto_transaction.key.id = slot->attr.id;
1715 status = psa_crypto_save_transaction( );
1716 if( status != PSA_SUCCESS )
1717 {
1718 (void) psa_crypto_stop_transaction( );
1719 return( status );
1720 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001721 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001722
1723 status = psa_copy_key_material_into_slot(
1724 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001725 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001726
1727 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1728 {
1729 /* Key registration only makes sense with a secure element. */
1730 return( PSA_ERROR_INVALID_ARGUMENT );
1731 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001732#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1733
Ronald Cronc4d1b512020-07-31 11:26:37 +02001734 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001735}
Gilles Peskine4747d192019-04-17 15:05:45 +02001736
1737/** Finalize the creation of a key once its key material has been set.
1738 *
1739 * This entails writing the key to persistent storage.
1740 *
1741 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001742 * See the documentation of psa_start_key_creation() for the intended use
1743 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 *
Ronald Cron5c522922020-11-14 16:35:34 +01001745 * If the finalization succeeds, the function unlocks the key slot (it was
1746 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1747 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001748 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001749 * \param[in,out] slot Pointer to the slot with key material.
1750 * \param[in] driver The secure element driver for the key,
1751 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001752 * \param[out] key On success, identifier of the key. Note that the
1753 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001754 *
1755 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001756 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001757 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1758 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1759 * \retval #PSA_ERROR_ALREADY_EXISTS
1760 * \retval #PSA_ERROR_DATA_INVALID
1761 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001762 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001763 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001764 * \return If this function fails, the key slot is an invalid state.
1765 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001766 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001767static psa_status_t psa_finish_key_creation(
1768 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001769 psa_se_drv_table_entry_t *driver,
1770 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001771{
1772 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001773 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001774 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001775
1776#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001777 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001779#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1780 if( driver != NULL )
1781 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001782 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001783 psa_key_slot_number_t slot_number =
1784 psa_key_slot_get_slot_number( slot ) ;
1785
Gilles Peskineb46bef22019-07-30 21:32:04 +02001786#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001787 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001788 sizeof( data.slot_number ),
1789 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001790#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001791 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001792 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001793 (uint8_t*) &data,
1794 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001795 }
1796 else
1797#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1798 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001799 /* Key material is saved in export representation in the slot, so
1800 * just pass the slot buffer for storage. */
1801 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001802 slot->key.data,
1803 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001804 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001805 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001806#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1807
Gilles Peskinecbaff462019-07-12 23:46:04 +02001808#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001809 /* Finish the transaction for a key creation. This does not
1810 * happen when registering an existing key. Detect this case
1811 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001812 * creation of a persistent key in a secure element requires a transaction,
1813 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001814 if( driver != NULL &&
1815 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001816 {
1817 status = psa_save_se_persistent_data( driver );
1818 if( status != PSA_SUCCESS )
1819 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001820 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001821 return( status );
1822 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001823 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001824 }
1825#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1826
Ronald Cron50972942020-11-14 11:28:25 +01001827 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001828 {
1829 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001830 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001831 if( status != PSA_SUCCESS )
1832 *key = MBEDTLS_SVC_KEY_ID_INIT;
1833 }
Ronald Cron50972942020-11-14 11:28:25 +01001834
Gilles Peskine4747d192019-04-17 15:05:45 +02001835 return( status );
1836}
1837
1838/** Abort the creation of a key.
1839 *
1840 * You may call this function after calling psa_start_key_creation(),
1841 * or after psa_finish_key_creation() fails. In other circumstances, this
1842 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001843 * See the documentation of psa_start_key_creation() for the intended use
1844 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001845 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001846 * \param[in,out] slot Pointer to the slot with key material.
1847 * \param[in] driver The secure element driver for the key,
1848 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001849 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001850static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001851 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001852{
Gilles Peskine011e4282019-06-26 18:34:38 +02001853 (void) driver;
1854
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 if( slot == NULL )
1856 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001857
1858#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001859 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001860 * element, and the failure happened later (when saving metadata
1861 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001862 * element.
1863 * https://github.com/ARMmbed/mbed-crypto/issues/217
1864 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001865
Gilles Peskined7729582019-08-05 15:55:54 +02001866 /* Abort the ongoing transaction if any (there may not be one if
1867 * the creation process failed before starting one, or if the
1868 * key creation is a registration of a key in a secure element).
1869 * Earlier functions must already have done what it takes to undo any
1870 * partial creation. All that's left is to update the transaction data
1871 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001872 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001873#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1874
Gilles Peskine4747d192019-04-17 15:05:45 +02001875 psa_wipe_key_slot( slot );
1876}
1877
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001878/** Validate optional attributes during key creation.
1879 *
1880 * Some key attributes are optional during key creation. If they are
1881 * specified in the attributes structure, check that they are consistent
1882 * with the data in the slot.
1883 *
1884 * This function should be called near the end of key creation, after
1885 * the slot in memory is fully populated but before saving persistent data.
1886 */
1887static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001888 const psa_key_slot_t *slot,
1889 const psa_key_attributes_t *attributes )
1890{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001891 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001892 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001893 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001894 return( PSA_ERROR_INVALID_ARGUMENT );
1895 }
1896
1897 if( attributes->domain_parameters_size != 0 )
1898 {
John Durkop0e005192020-10-31 22:06:54 -07001899#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1900 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001901 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001902 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001903 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001904 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001906
Ronald Cron90857082020-11-25 14:58:33 +01001907 psa_status_t status = mbedtls_psa_rsa_load_representation(
1908 slot->attr.type,
1909 slot->key.data,
1910 slot->key.bytes,
1911 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001912 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001913 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001914
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001915 mbedtls_mpi_init( &actual );
1916 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001917 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001918 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001919 mbedtls_rsa_free( rsa );
1920 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001921 if( ret != 0 )
1922 goto rsa_exit;
1923 ret = mbedtls_mpi_read_binary( &required,
1924 attributes->domain_parameters,
1925 attributes->domain_parameters_size );
1926 if( ret != 0 )
1927 goto rsa_exit;
1928 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1929 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1930 rsa_exit:
1931 mbedtls_mpi_free( &actual );
1932 mbedtls_mpi_free( &required );
1933 if( ret != 0)
1934 return( mbedtls_to_psa_error( ret ) );
1935 }
1936 else
John Durkop9814fa22020-11-04 12:28:15 -08001937#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1938 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001939 {
1940 return( PSA_ERROR_INVALID_ARGUMENT );
1941 }
1942 }
1943
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001944 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001945 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001946 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947 return( PSA_ERROR_INVALID_ARGUMENT );
1948 }
1949
1950 return( PSA_SUCCESS );
1951}
1952
Gilles Peskine4747d192019-04-17 15:05:45 +02001953psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001954 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001955 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001956 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001957{
1958 psa_status_t status;
1959 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001960 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001961 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001962
Ronald Cron81709fc2020-11-14 12:10:32 +01001963 *key = MBEDTLS_SVC_KEY_ID_INIT;
1964
Gilles Peskine0f84d622019-09-12 19:03:13 +02001965 /* Reject zero-length symmetric keys (including raw data key objects).
1966 * This also rejects any key which might be encoded as an empty string,
1967 * which is never valid. */
1968 if( data_length == 0 )
1969 return( PSA_ERROR_INVALID_ARGUMENT );
1970
Gilles Peskinedf179142019-07-15 22:02:14 +02001971 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001972 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001973 if( status != PSA_SUCCESS )
1974 goto exit;
1975
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001976 /* In the case of a transparent key or an opaque key stored in local
1977 * storage (thus not in the case of generating a key in a secure element
1978 * or cryptoprocessor with storage), we have to allocate a buffer to
1979 * hold the generated key material. */
1980 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001981 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001982 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001983 if( status != PSA_SUCCESS )
1984 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001985 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001986
1987 bits = slot->attr.bits;
1988 status = psa_driver_wrapper_import_key( attributes,
1989 data, data_length,
1990 slot->key.data,
1991 slot->key.bytes,
1992 &slot->key.bytes, &bits );
1993 if( status != PSA_SUCCESS )
1994 goto exit;
1995
1996 if( slot->attr.bits == 0 )
1997 slot->attr.bits = (psa_key_bits_t) bits;
1998 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001999 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002000 status = PSA_ERROR_INVALID_ARGUMENT;
2001 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002002 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002003
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002004 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002005 if( status != PSA_SUCCESS )
2006 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007
Ronald Cron81709fc2020-11-14 12:10:32 +01002008 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002010 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002011 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002012
Gilles Peskine4747d192019-04-17 15:05:45 +02002013 return( status );
2014}
2015
Gilles Peskined7729582019-08-05 15:55:54 +02002016#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2017psa_status_t mbedtls_psa_register_se_key(
2018 const psa_key_attributes_t *attributes )
2019{
2020 psa_status_t status;
2021 psa_key_slot_t *slot = NULL;
2022 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002024
2025 /* Leaving attributes unspecified is not currently supported.
2026 * It could make sense to query the key type and size from the
2027 * secure element, but not all secure elements support this
2028 * and the driver HAL doesn't currently support it. */
2029 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2030 return( PSA_ERROR_NOT_SUPPORTED );
2031 if( psa_get_key_bits( attributes ) == 0 )
2032 return( PSA_ERROR_NOT_SUPPORTED );
2033
2034 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002035 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002036 if( status != PSA_SUCCESS )
2037 goto exit;
2038
Ronald Cron81709fc2020-11-14 12:10:32 +01002039 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002040
2041exit:
2042 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002043 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002044
Gilles Peskined7729582019-08-05 15:55:54 +02002045 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002046 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002047 return( status );
2048}
2049#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2050
Gilles Peskinef603c712019-01-19 13:40:11 +01002051static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002052 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002053{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002054 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002055 source->key.data,
2056 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002057 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002058 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002059
Steven Cooreman398aee52020-10-13 14:35:45 +02002060 target->attr.type = source->attr.type;
2061 target->attr.bits = source->attr.bits;
2062
2063 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002064}
2065
Ronald Croncf56a0a2020-08-04 09:51:30 +02002066psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002067 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002068 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002069{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002070 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002071 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002072 psa_key_slot_t *source_slot = NULL;
2073 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002074 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002075 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002076
Ronald Cron81709fc2020-11-14 12:10:32 +01002077 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2078
Ronald Cron5c522922020-11-14 16:35:34 +01002079 status = psa_get_and_lock_transparent_key_slot_with_policy(
2080 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002081 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002082 goto exit;
2083
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002084 status = psa_validate_optional_attributes( source_slot,
2085 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002086 if( status != PSA_SUCCESS )
2087 goto exit;
2088
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002089 status = psa_restrict_key_policy( source_slot->attr.type,
2090 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002091 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002092 if( status != PSA_SUCCESS )
2093 goto exit;
2094
Ronald Cron81709fc2020-11-14 12:10:32 +01002095 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2096 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002097 if( status != PSA_SUCCESS )
2098 goto exit;
2099
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002100#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2101 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002102 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002103 /* Copying to a secure element is not implemented yet. */
2104 status = PSA_ERROR_NOT_SUPPORTED;
2105 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002106 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002107#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002108
Ronald Cron6cc66312021-04-02 12:27:47 +02002109 if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) )
2110 {
2111 /*
2112 * Copying through an opaque driver is not implemented yet, consider
2113 * a lifetime with an external location as an invalid parameter for
2114 * now.
2115 */
2116 status = PSA_ERROR_INVALID_ARGUMENT;
2117 goto exit;
2118 }
2119
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002120 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002121 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002122 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002123
Ronald Cron81709fc2020-11-14 12:10:32 +01002124 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002125exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002126 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002127 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002128
Ronald Cron5c522922020-11-14 16:35:34 +01002129 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002130
Ronald Cron5c522922020-11-14 16:35:34 +01002131 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002132}
2133
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002134
2135
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002136/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002137/* Message digests */
2138/****************************************************************/
2139
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002140psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2141{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002142 /* Aborting a non-active operation is allowed */
2143 if( operation->id == 0 )
2144 return( PSA_SUCCESS );
2145
2146 psa_status_t status = psa_driver_wrapper_hash_abort( operation );
2147 operation->id = 0;
2148
2149 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002150}
2151
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002152psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002153 psa_algorithm_t alg )
2154{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002155 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002156
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002157 /* A context must be freshly initialized before it can be set up. */
2158 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002159 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002160 status = PSA_ERROR_BAD_STATE;
2161 goto exit;
2162 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002163
2164 if( !PSA_ALG_IS_HASH( alg ) )
Dave Rodgmandb861792021-06-24 16:17:43 +01002165 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002166 status = PSA_ERROR_INVALID_ARGUMENT;
2167 goto exit;
2168 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002169
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002170 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2171 * directly zeroes the int-sized dummy member of the context union. */
Steven Cooreman893232f2021-03-15 12:23:37 +01002172 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
2173
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002174 status = psa_driver_wrapper_hash_setup( operation, alg );
2175
2176exit:
2177 if( status != PSA_SUCCESS )
Dave Rodgmandb861792021-06-24 16:17:43 +01002178 psa_hash_abort( operation );
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002179
2180 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002181}
2182
2183psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2184 const uint8_t *input,
2185 size_t input_length )
2186{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2188
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002189 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002190 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002191 status = PSA_ERROR_BAD_STATE;
2192 goto exit;
2193 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002194
Steven Cooremanc8288352021-03-04 14:02:19 +01002195 /* Don't require hash implementations to behave correctly on a
2196 * zero-length input, which may have an invalid pointer. */
2197 if( input_length == 0 )
2198 return( PSA_SUCCESS );
2199
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002200 status = psa_driver_wrapper_hash_update( operation, input, input_length );
2201
2202exit:
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002203 if( status != PSA_SUCCESS )
2204 psa_hash_abort( operation );
2205
2206 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002207}
2208
2209psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2210 uint8_t *hash,
2211 size_t hash_size,
2212 size_t *hash_length )
2213{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002214 *hash_length = 0;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002215 if( operation->id == 0 )
2216 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002217
Steven Cooreman1e582352021-02-18 17:24:37 +01002218 psa_status_t status = psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002219 operation, hash, hash_size, hash_length );
Steven Cooreman0e307642021-02-18 16:18:32 +01002220 psa_hash_abort( operation );
2221 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002222}
2223
Gilles Peskine2d277862018-06-18 15:41:12 +02002224psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2225 const uint8_t *hash,
2226 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002227{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002228 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002229 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002230 psa_status_t status = psa_hash_finish(
2231 operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01002232 actual_hash, sizeof( actual_hash ),
2233 &actual_hash_length );
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002234
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002235 if( status != PSA_SUCCESS )
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002236 goto exit;
2237
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238 if( actual_hash_length != hash_length )
Dave Rodgmandb861792021-06-24 16:17:43 +01002239 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002240 status = PSA_ERROR_INVALID_SIGNATURE;
2241 goto exit;
2242 }
2243
Steven Cooremana2a1b802021-04-29 16:37:59 +02002244 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002245 status = PSA_ERROR_INVALID_SIGNATURE;
2246
2247exit:
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002248 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002249 if( status != PSA_SUCCESS )
2250 psa_hash_abort(operation);
2251
2252 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002253}
2254
Gilles Peskine0a749c82019-11-28 19:33:58 +01002255psa_status_t psa_hash_compute( psa_algorithm_t alg,
2256 const uint8_t *input, size_t input_length,
2257 uint8_t *hash, size_t hash_size,
2258 size_t *hash_length )
2259{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002260 *hash_length = 0;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002261 if( !PSA_ALG_IS_HASH( alg ) )
2262 return( PSA_ERROR_INVALID_ARGUMENT );
2263
Steven Cooreman1e582352021-02-18 17:24:37 +01002264 return( psa_driver_wrapper_hash_compute( alg, input, input_length,
2265 hash, hash_size, hash_length ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002266}
2267
2268psa_status_t psa_hash_compare( psa_algorithm_t alg,
2269 const uint8_t *input, size_t input_length,
2270 const uint8_t *hash, size_t hash_length )
2271{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002272 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002273 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002274
2275 if( !PSA_ALG_IS_HASH( alg ) )
2276 return( PSA_ERROR_INVALID_ARGUMENT );
2277
Steven Cooreman1e582352021-02-18 17:24:37 +01002278 psa_status_t status = psa_driver_wrapper_hash_compute(
2279 alg, input, input_length,
2280 actual_hash, sizeof(actual_hash),
2281 &actual_hash_length );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002282 if( status != PSA_SUCCESS )
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002283 goto exit;
Steven Cooreman84d670d2021-02-18 16:22:53 +01002284 if( actual_hash_length != hash_length )
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002285 {
2286 status = PSA_ERROR_INVALID_SIGNATURE;
2287 goto exit;
2288 }
Steven Cooremana2a1b802021-04-29 16:37:59 +02002289 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002290 status = PSA_ERROR_INVALID_SIGNATURE;
2291
2292exit:
2293 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
2294 return( status );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002295}
2296
Gilles Peskineeb35d782019-01-22 17:56:16 +01002297psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2298 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002299{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002300 if( source_operation->id == 0 ||
2301 target_operation->id != 0 )
2302 {
2303 return( PSA_ERROR_BAD_STATE );
2304 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002305
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002306 psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
2307 target_operation );
2308 if( status != PSA_SUCCESS )
2309 psa_hash_abort( target_operation );
2310
2311 return( status );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002312}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002313
2314
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002315/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002316/* MAC */
2317/****************************************************************/
2318
Steven Cooreman07897832021-03-19 18:28:56 +01002319psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002320{
Steven Cooreman07897832021-03-19 18:28:56 +01002321 /* Aborting a non-active operation is allowed */
2322 if( operation->id == 0 )
2323 return( PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002324
Steven Cooreman07897832021-03-19 18:28:56 +01002325 psa_status_t status = psa_driver_wrapper_mac_abort( operation );
Steven Cooreman15f0d922021-05-07 14:14:37 +02002326 operation->mac_size = 0;
2327 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002328 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002329
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002330 return( status );
2331}
2332
Ronald Cron1c650a12021-06-17 16:33:22 +02002333static psa_status_t psa_mac_finalize_alg_and_key_validation(
2334 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002335 const psa_key_attributes_t *attributes,
2336 uint8_t *mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002337{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002338 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondef68e72021-06-17 16:46:44 +02002339 psa_key_type_t key_type = psa_get_key_type( attributes );
2340 size_t key_bits = psa_get_key_bits( attributes );
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002341
Ronald Cron48f875e2021-06-17 16:10:24 +02002342 if( ! PSA_ALG_IS_MAC( alg ) )
Ronald Crondef68e72021-06-17 16:46:44 +02002343 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman07897832021-03-19 18:28:56 +01002344
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002345 /* Validate the combination of key type and algorithm */
Ronald Crondef68e72021-06-17 16:46:44 +02002346 status = psa_mac_key_can_do( alg, key_type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002347 if( status != PSA_SUCCESS )
Ronald Crondef68e72021-06-17 16:46:44 +02002348 return( status );
Steven Cooreman15472f82021-03-02 16:16:22 +01002349
Steven Cooremana6474de2021-05-10 11:13:41 +02002350 /* Get the output length for the algorithm and key combination */
Ronald Crondef68e72021-06-17 16:46:44 +02002351 *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002352
Ronald Crondef68e72021-06-17 16:46:44 +02002353 if( *mac_size < 4 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002354 {
2355 /* A very short MAC is too short for security since it can be
2356 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2357 * so we make this our minimum, even though 32 bits is still
2358 * too small for security. */
Ronald Crondef68e72021-06-17 16:46:44 +02002359 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman15472f82021-03-02 16:16:22 +01002360 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002361
Ronald Crondef68e72021-06-17 16:46:44 +02002362 if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits,
2363 PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002364 {
2365 /* It's impossible to "truncate" to a larger length than the full length
2366 * of the algorithm. */
Ronald Crondef68e72021-06-17 16:46:44 +02002367 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002368 }
2369
Gilles Peskinebc795822022-03-16 13:54:49 +01002370 if( *mac_size > PSA_MAC_MAX_SIZE )
2371 {
2372 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2373 * that is disabled in the compile-time configuration. The result can
2374 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2375 * configuration into account. In this case, force a return of
2376 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2377 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2378 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2379 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2380 * systematically generated tests. */
2381 return( PSA_ERROR_NOT_SUPPORTED );
2382 }
2383
Ronald Crondef68e72021-06-17 16:46:44 +02002384 return( PSA_SUCCESS );
Ronald Cron1c650a12021-06-17 16:33:22 +02002385}
2386
2387static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
2388 mbedtls_svc_key_id_t key,
2389 psa_algorithm_t alg,
2390 int is_sign )
2391{
2392 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2393 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002394 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002395
2396 /* A context must be freshly initialized before it can be set up. */
2397 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002398 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002399 status = PSA_ERROR_BAD_STATE;
2400 goto exit;
2401 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002402
2403 status = psa_get_and_lock_key_slot_with_policy(
2404 key,
2405 &slot,
Mateusz Starzykdd55b252021-08-17 15:24:32 +02002406 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Cron1c650a12021-06-17 16:33:22 +02002407 alg );
2408 if( status != PSA_SUCCESS )
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002409 goto exit;
Ronald Cron1c650a12021-06-17 16:33:22 +02002410
2411 psa_key_attributes_t attributes = {
2412 .core = slot->attr
2413 };
2414
Ronald Crondef68e72021-06-17 16:46:44 +02002415 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2416 &operation->mac_size );
Ronald Cron1c650a12021-06-17 16:33:22 +02002417 if( status != PSA_SUCCESS )
2418 goto exit;
2419
2420 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002421 /* Dispatch the MAC setup call with validated input */
2422 if( is_sign )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002423 {
Steven Cooreman07897832021-03-19 18:28:56 +01002424 status = psa_driver_wrapper_mac_sign_setup( operation,
Ronald Cron1c650a12021-06-17 16:33:22 +02002425 &attributes,
Steven Cooreman07897832021-03-19 18:28:56 +01002426 slot->key.data,
2427 slot->key.bytes,
2428 alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002429 }
2430 else
Gilles Peskinefbfac682018-07-08 20:51:54 +02002431 {
Steven Cooreman07897832021-03-19 18:28:56 +01002432 status = psa_driver_wrapper_mac_verify_setup( operation,
Ronald Cron1c650a12021-06-17 16:33:22 +02002433 &attributes,
Steven Cooreman07897832021-03-19 18:28:56 +01002434 slot->key.data,
2435 slot->key.bytes,
2436 alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002437 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002438
Gilles Peskinefbfac682018-07-08 20:51:54 +02002439exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002440 if( status != PSA_SUCCESS )
Steven Cooreman07897832021-03-19 18:28:56 +01002441 psa_mac_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002442
Ronald Cron5c522922020-11-14 16:35:34 +01002443 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002444
Ronald Cron5c522922020-11-14 16:35:34 +01002445 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002446}
2447
Gilles Peskine89167cb2018-07-08 20:12:23 +02002448psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002449 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002450 psa_algorithm_t alg )
2451{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002452 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002453}
2454
2455psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002456 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002457 psa_algorithm_t alg )
2458{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002459 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002460}
2461
Steven Cooreman11743f92021-03-19 18:38:46 +01002462psa_status_t psa_mac_update( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002463 const uint8_t *input,
2464 size_t input_length )
2465{
Steven Cooreman11743f92021-03-19 18:38:46 +01002466 if( operation->id == 0 )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002467 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002468
Steven Cooreman11743f92021-03-19 18:38:46 +01002469 /* Don't require hash implementations to behave correctly on a
2470 * zero-length input, which may have an invalid pointer. */
2471 if( input_length == 0 )
2472 return( PSA_SUCCESS );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002473
Steven Cooreman11743f92021-03-19 18:38:46 +01002474 psa_status_t status = psa_driver_wrapper_mac_update( operation,
2475 input, input_length );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002476 if( status != PSA_SUCCESS )
Steven Cooreman11743f92021-03-19 18:38:46 +01002477 psa_mac_abort( operation );
2478
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002479 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480}
2481
Steven Cooreman87885df2021-03-19 19:04:39 +01002482psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002483 uint8_t *mac,
2484 size_t mac_size,
2485 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002486{
Steven Cooreman87885df2021-03-19 19:04:39 +01002487 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2488 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002489
Steven Cooreman87885df2021-03-19 19:04:39 +01002490 if( operation->id == 0 )
Dave Rodgmand73e1b02021-06-24 16:19:08 +01002491 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002492 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002493 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002494 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002495
Steven Cooreman15f0d922021-05-07 14:14:37 +02002496 if( ! operation->is_sign )
Dave Rodgmandb861792021-06-24 16:17:43 +01002497 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002498 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002499 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002500 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002501
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002502 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2503 * once all the error checks are done. */
2504 if( operation->mac_size == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002505 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002506 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002507 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002508 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002509
2510 if( mac_size < operation->mac_size )
Dave Rodgmandb861792021-06-24 16:17:43 +01002511 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002512 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002513 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002514 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002515
Steven Cooreman87885df2021-03-19 19:04:39 +01002516 status = psa_driver_wrapper_mac_sign_finish( operation,
Steven Cooreman15f0d922021-05-07 14:14:37 +02002517 mac, operation->mac_size,
2518 mac_length );
2519
Dave Rodgman478ab542021-06-25 09:09:02 +01002520exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002521 /* In case of success, set the potential excess room in the output buffer
2522 * to an invalid value, to avoid potentially leaking a longer MAC.
2523 * In case of error, set the output length and content to a safe default,
2524 * such that in case the caller misses an error check, the output would be
2525 * an unachievable MAC.
2526 */
2527 if( status != PSA_SUCCESS )
Steven Cooreman15f0d922021-05-07 14:14:37 +02002528 {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002529 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002530 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002531 }
mohammad16036df908f2018-04-02 08:34:15 -07002532
Ronald Cron882eb782021-06-18 13:05:48 +02002533 if( mac_size > operation->mac_size )
2534 memset( &mac[operation->mac_size], '!',
2535 mac_size - operation->mac_size );
2536
Steven Cooreman87885df2021-03-19 19:04:39 +01002537 abort_status = psa_mac_abort( operation );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002538
Steven Cooreman87885df2021-03-19 19:04:39 +01002539 return( status == PSA_SUCCESS ? abort_status : status );
mohammad16036df908f2018-04-02 08:34:15 -07002540}
2541
Steven Cooreman87885df2021-03-19 19:04:39 +01002542psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002543 const uint8_t *mac,
2544 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545{
Steven Cooreman87885df2021-03-19 19:04:39 +01002546 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2547 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002548
Steven Cooreman87885df2021-03-19 19:04:39 +01002549 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002550 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002551 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002552 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002553 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002554
Steven Cooreman15f0d922021-05-07 14:14:37 +02002555 if( operation->is_sign )
Dave Rodgmandb861792021-06-24 16:17:43 +01002556 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002557 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002558 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002559 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002560
2561 if( operation->mac_size != mac_length )
2562 {
2563 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002564 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002565 }
2566
Steven Cooreman87885df2021-03-19 19:04:39 +01002567 status = psa_driver_wrapper_mac_verify_finish( operation,
2568 mac, mac_length );
Steven Cooreman15f0d922021-05-07 14:14:37 +02002569
Dave Rodgman478ab542021-06-25 09:09:02 +01002570exit:
Steven Cooreman87885df2021-03-19 19:04:39 +01002571 abort_status = psa_mac_abort( operation );
mohammad16036df908f2018-04-02 08:34:15 -07002572
Steven Cooreman87885df2021-03-19 19:04:39 +01002573 return( status == PSA_SUCCESS ? abort_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002574}
2575
Ronald Cronef0d8f12021-06-18 14:23:33 +02002576static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key,
2577 psa_algorithm_t alg,
2578 const uint8_t *input,
2579 size_t input_length,
2580 uint8_t *mac,
2581 size_t mac_size,
2582 size_t *mac_length,
2583 int is_sign )
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002584{
2585 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002586 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2587 psa_key_slot_t *slot;
2588 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589
Ronald Crondbb86462021-06-17 17:17:20 +02002590 status = psa_get_and_lock_key_slot_with_policy(
Mateusz Starzykdd55b252021-08-17 15:24:32 +02002591 key,
2592 &slot,
2593 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Cronef0d8f12021-06-18 14:23:33 +02002594 alg );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002595 if( status != PSA_SUCCESS )
2596 goto exit;
2597
Ronald Crondbb86462021-06-17 17:17:20 +02002598 psa_key_attributes_t attributes = {
2599 .core = slot->attr
2600 };
2601
2602 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2603 &operation_mac_size );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002604 if( status != PSA_SUCCESS )
2605 goto exit;
2606
Ronald Crondbb86462021-06-17 17:17:20 +02002607 if( mac_size < operation_mac_size )
2608 {
2609 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002610 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002611 }
2612
2613 status = psa_driver_wrapper_mac_compute(
2614 &attributes,
2615 slot->key.data, slot->key.bytes,
2616 alg,
2617 input, input_length,
2618 mac, operation_mac_size, mac_length );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002619
2620exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002621 /* In case of success, set the potential excess room in the output buffer
2622 * to an invalid value, to avoid potentially leaking a longer MAC.
2623 * In case of error, set the output length and content to a safe default,
2624 * such that in case the caller misses an error check, the output would be
2625 * an unachievable MAC.
2626 */
2627 if( status != PSA_SUCCESS )
Ronald Crondbb86462021-06-17 17:17:20 +02002628 {
Ronald Crondbb86462021-06-17 17:17:20 +02002629 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002630 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002631 }
Ronald Cron882eb782021-06-18 13:05:48 +02002632 if( mac_size > operation_mac_size )
2633 memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002634
Ronald Crondbb86462021-06-17 17:17:20 +02002635 unlock_status = psa_unlock_key_slot( slot );
2636
2637 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002638}
2639
Ronald Cronef0d8f12021-06-18 14:23:33 +02002640psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key,
2641 psa_algorithm_t alg,
2642 const uint8_t *input,
2643 size_t input_length,
2644 uint8_t *mac,
2645 size_t mac_size,
2646 size_t *mac_length)
2647{
2648 return( psa_mac_compute_internal( key, alg,
2649 input, input_length,
2650 mac, mac_size, mac_length, 1 ) );
2651}
2652
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002653psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key,
2654 psa_algorithm_t alg,
2655 const uint8_t *input,
2656 size_t input_length,
2657 const uint8_t *mac,
2658 size_t mac_length)
2659{
2660 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002661 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2662 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002663
Ronald Cron4ed83682021-06-18 14:51:29 +02002664 status = psa_mac_compute_internal( key, alg,
2665 input, input_length,
2666 actual_mac, sizeof( actual_mac ),
2667 &actual_mac_length, 0 );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002668 if( status != PSA_SUCCESS )
2669 goto exit;
2670
Ronald Cron4ed83682021-06-18 14:51:29 +02002671 if( mac_length != actual_mac_length )
2672 {
2673 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002674 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002675 }
2676 if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
2677 {
2678 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002679 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002680 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002681
2682exit:
Ronald Cron4ed83682021-06-18 14:51:29 +02002683 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002684
2685 return ( status );
2686}
Gilles Peskine20035e32018-02-03 22:44:14 +01002687
Gilles Peskine20035e32018-02-03 22:44:14 +01002688/****************************************************************/
2689/* Asymmetric cryptography */
2690/****************************************************************/
2691
gabor-mezei-armc9795782021-05-12 11:03:09 +02002692static psa_status_t psa_sign_verify_check_alg( int input_is_message,
gabor-mezei-arm56980482021-05-05 13:56:27 +02002693 psa_algorithm_t alg )
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002694{
gabor-mezei-armc9795782021-05-12 11:03:09 +02002695 if( input_is_message )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002696 {
2697 if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) )
2698 return( PSA_ERROR_INVALID_ARGUMENT );
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002699
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002700 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002701 {
2702 if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) )
2703 return( PSA_ERROR_INVALID_ARGUMENT );
2704 }
2705 }
2706 else
2707 {
Mateusz Starzykd22362c2021-08-26 11:46:14 +02002708 if( ! PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002709 return( PSA_ERROR_INVALID_ARGUMENT );
2710 }
2711
2712 return( PSA_SUCCESS );
2713}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002714
2715static psa_status_t psa_sign_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002716 int input_is_message,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002717 psa_algorithm_t alg,
2718 const uint8_t * input,
2719 size_t input_length,
2720 uint8_t * signature,
2721 size_t signature_size,
2722 size_t * signature_length )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002723{
2724 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2725 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2726 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002727
2728 *signature_length = 0;
2729
gabor-mezei-armc9795782021-05-12 11:03:09 +02002730 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm56980482021-05-05 13:56:27 +02002731 if( status != PSA_SUCCESS )
2732 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002733
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002734 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002735 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002736 * buffer can in principle be empty since it doesn't actually have
2737 * to be a hash.) */
2738 if( signature_size == 0 )
2739 return( PSA_ERROR_BUFFER_TOO_SMALL );
2740
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002741 status = psa_get_and_lock_key_slot_with_policy(
2742 key, &slot,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002743 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2744 PSA_KEY_USAGE_SIGN_HASH,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002745 alg );
2746
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002747 if( status != PSA_SUCCESS )
2748 goto exit;
2749
2750 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
2751 {
2752 status = PSA_ERROR_INVALID_ARGUMENT;
2753 goto exit;
2754 }
2755
2756 psa_key_attributes_t attributes = {
2757 .core = slot->attr
2758 };
2759
gabor-mezei-armc9795782021-05-12 11:03:09 +02002760 if( input_is_message )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002761 {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002762 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002763 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002764 alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002765 signature, signature_size, signature_length );
2766 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002767 else
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002768 {
2769
2770 status = psa_driver_wrapper_sign_hash(
2771 &attributes, slot->key.data, slot->key.bytes,
2772 alg, input, input_length,
2773 signature, signature_size, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002774 }
2775
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002776
2777exit:
2778 /* Fill the unused part of the output buffer (the whole buffer on error,
2779 * the trailing part on success) with something that isn't a valid signature
2780 * (barring an attack on the signature and deliberately-crafted input),
2781 * in case the caller doesn't check the return status properly. */
2782 if( status == PSA_SUCCESS )
2783 memset( signature + *signature_length, '!',
2784 signature_size - *signature_length );
2785 else
2786 memset( signature, '!', signature_size );
2787 /* If signature_size is 0 then we have nothing to do. We must not call
2788 * memset because signature may be NULL in this case. */
2789
2790 unlock_status = psa_unlock_key_slot( slot );
2791
2792 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
2793}
2794
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002795static psa_status_t psa_verify_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002796 int input_is_message,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002797 psa_algorithm_t alg,
2798 const uint8_t * input,
2799 size_t input_length,
2800 const uint8_t * signature,
2801 size_t signature_length )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002802{
2803 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2804 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2805 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002806
gabor-mezei-armc9795782021-05-12 11:03:09 +02002807 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm56980482021-05-05 13:56:27 +02002808 if( status != PSA_SUCCESS )
2809 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002810
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002811 status = psa_get_and_lock_key_slot_with_policy(
2812 key, &slot,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002813 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2814 PSA_KEY_USAGE_VERIFY_HASH,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002815 alg );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002816
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002817 if( status != PSA_SUCCESS )
2818 return( status );
2819
2820 psa_key_attributes_t attributes = {
2821 .core = slot->attr
2822 };
2823
gabor-mezei-armc9795782021-05-12 11:03:09 +02002824 if( input_is_message )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002825 {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002826 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002827 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002828 alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002829 signature, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002830 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002831 else
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002832 {
2833 status = psa_driver_wrapper_verify_hash(
2834 &attributes, slot->key.data, slot->key.bytes,
2835 alg, input, input_length,
2836 signature, signature_length );
2837 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002838
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002839 unlock_status = psa_unlock_key_slot( slot );
2840
2841 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002842
2843}
2844
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002845psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002846 const psa_key_attributes_t *attributes,
2847 const uint8_t *key_buffer,
2848 size_t key_buffer_size,
2849 psa_algorithm_t alg,
2850 const uint8_t *input,
2851 size_t input_length,
2852 uint8_t *signature,
2853 size_t signature_size,
2854 size_t *signature_length )
2855{
2856 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002857
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002858 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002859 {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002860 size_t hash_length;
2861 uint8_t hash[PSA_HASH_MAX_SIZE];
2862
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002863 status = psa_driver_wrapper_hash_compute(
2864 PSA_ALG_SIGN_GET_HASH( alg ),
2865 input, input_length,
2866 hash, sizeof( hash ), &hash_length );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002867
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002868 if( status != PSA_SUCCESS )
2869 return status;
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002870
2871 return psa_driver_wrapper_sign_hash(
2872 attributes, key_buffer, key_buffer_size,
2873 alg, hash, hash_length,
2874 signature, signature_size, signature_length );
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002875 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002876
gabor-mezei-arm56980482021-05-05 13:56:27 +02002877 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002878}
2879
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002880psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
2881 psa_algorithm_t alg,
2882 const uint8_t * input,
2883 size_t input_length,
2884 uint8_t * signature,
2885 size_t signature_size,
2886 size_t * signature_length )
2887{
2888 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002889 key, 1, alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002890 signature, signature_size, signature_length );
2891}
2892
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002893psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002894 const psa_key_attributes_t *attributes,
2895 const uint8_t *key_buffer,
2896 size_t key_buffer_size,
2897 psa_algorithm_t alg,
2898 const uint8_t *input,
2899 size_t input_length,
2900 const uint8_t *signature,
2901 size_t signature_length )
2902{
2903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002904
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002905 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002906 {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002907 size_t hash_length;
2908 uint8_t hash[PSA_HASH_MAX_SIZE];
2909
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002910 status = psa_driver_wrapper_hash_compute(
2911 PSA_ALG_SIGN_GET_HASH( alg ),
2912 input, input_length,
2913 hash, sizeof( hash ), &hash_length );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002914
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002915 if( status != PSA_SUCCESS )
2916 return status;
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002917
2918 return psa_driver_wrapper_verify_hash(
2919 attributes, key_buffer, key_buffer_size,
2920 alg, hash, hash_length,
2921 signature, signature_length );
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002922 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002923
gabor-mezei-armef6f2aa2021-05-05 13:59:01 +02002924 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002925}
2926
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002927psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
2928 psa_algorithm_t alg,
2929 const uint8_t * input,
2930 size_t input_length,
2931 const uint8_t * signature,
2932 size_t signature_length )
2933{
2934 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002935 key, 1, alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002936 signature, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002937}
2938
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002939psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002940 const psa_key_attributes_t *attributes,
2941 const uint8_t *key_buffer, size_t key_buffer_size,
2942 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2943 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002944{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002945 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002946 {
Ronald Cron4501c982021-03-19 20:09:32 +01002947 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2948 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002949 {
Ronald Cron4501c982021-03-19 20:09:32 +01002950#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2951 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2952 return( mbedtls_psa_rsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002953 attributes,
2954 key_buffer, key_buffer_size,
2955 alg, hash, hash_length,
2956 signature, signature_size, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002957#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2958 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002959 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002960 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002961 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002962 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002963 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002964 }
Ronald Cron4bcccc62021-04-09 15:32:03 +02002965 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Ronald Cron4501c982021-03-19 20:09:32 +01002966 {
2967 if( PSA_ALG_IS_ECDSA( alg ) )
2968 {
2969#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2970 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2971 return( mbedtls_psa_ecdsa_sign_hash(
2972 attributes,
2973 key_buffer, key_buffer_size,
2974 alg, hash, hash_length,
2975 signature, signature_size, signature_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002976#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2977 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron4501c982021-03-19 20:09:32 +01002978 }
2979 else
2980 {
2981 return( PSA_ERROR_INVALID_ARGUMENT );
2982 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002983 }
Ronald Cron4501c982021-03-19 20:09:32 +01002984
2985 (void)key_buffer;
2986 (void)key_buffer_size;
2987 (void)hash;
2988 (void)hash_length;
2989 (void)signature;
2990 (void)signature_size;
2991 (void)signature_length;
2992
2993 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01002994}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002995
2996psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
2997 psa_algorithm_t alg,
2998 const uint8_t *hash,
2999 size_t hash_length,
3000 uint8_t *signature,
3001 size_t signature_size,
3002 size_t *signature_length )
3003{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003004 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003005 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01003006 signature, signature_size, signature_length );
itayzafrir5c753392018-05-08 11:18:38 +03003007}
3008
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003009psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003010 const psa_key_attributes_t *attributes,
3011 const uint8_t *key_buffer, size_t key_buffer_size,
3012 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
3013 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003014{
Ronald Cron99b8ed72021-02-17 10:33:32 +01003015 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003016 {
Ronald Cron4501c982021-03-19 20:09:32 +01003017 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
3018 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003019 {
Ronald Cron4501c982021-03-19 20:09:32 +01003020#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3021 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3022 return( mbedtls_psa_rsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01003023 attributes,
3024 key_buffer, key_buffer_size,
3025 alg, hash, hash_length,
3026 signature, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01003027#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3028 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02003029 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003030 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003031 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003032 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003033 }
itayzafrir5c753392018-05-08 11:18:38 +03003034 }
Ronald Cron4bcccc62021-04-09 15:32:03 +02003035 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003036 {
Ronald Cron4501c982021-03-19 20:09:32 +01003037 if( PSA_ALG_IS_ECDSA( alg ) )
3038 {
3039#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3040 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3041 return( mbedtls_psa_ecdsa_verify_hash(
3042 attributes,
3043 key_buffer, key_buffer_size,
3044 alg, hash, hash_length,
3045 signature, signature_length ) );
3046#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3047 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3048 }
3049 else
3050 {
3051 return( PSA_ERROR_INVALID_ARGUMENT );
3052 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003053 }
Ronald Cron4501c982021-03-19 20:09:32 +01003054
3055 (void)key_buffer;
3056 (void)key_buffer_size;
3057 (void)hash;
3058 (void)hash_length;
3059 (void)signature;
3060 (void)signature_length;
3061
3062 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01003063}
3064
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003065psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3066 psa_algorithm_t alg,
3067 const uint8_t *hash,
3068 size_t hash_length,
3069 const uint8_t *signature,
3070 size_t signature_length )
3071{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003072 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003073 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01003074 signature, signature_length );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003075}
3076
John Durkop0e005192020-10-31 22:06:54 -07003077#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003078static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3079 mbedtls_rsa_context *rsa )
3080{
3081 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3082 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3083 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3084 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3085}
John Durkop0e005192020-10-31 22:06:54 -07003086#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003087
Ronald Croncf56a0a2020-08-04 09:51:30 +02003088psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003089 psa_algorithm_t alg,
3090 const uint8_t *input,
3091 size_t input_length,
3092 const uint8_t *salt,
3093 size_t salt_length,
3094 uint8_t *output,
3095 size_t output_size,
3096 size_t *output_length )
3097{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003098 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003099 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003100 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003101
Darryl Green5cc689a2018-07-24 15:34:10 +01003102 (void) input;
3103 (void) input_length;
3104 (void) salt;
3105 (void) output;
3106 (void) output_size;
3107
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003108 *output_length = 0;
3109
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003110 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3111 return( PSA_ERROR_INVALID_ARGUMENT );
3112
Ronald Cron5c522922020-11-14 16:35:34 +01003113 status = psa_get_and_lock_transparent_key_slot_with_policy(
3114 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003115 if( status != PSA_SUCCESS )
3116 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003117 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3118 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003119 {
3120 status = PSA_ERROR_INVALID_ARGUMENT;
3121 goto exit;
3122 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003123
Gilles Peskine8e338702019-07-30 20:06:31 +02003124 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125 {
Ronald Cron7207d572021-09-08 14:28:35 +02003126#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3127 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003128 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003129 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3130 slot->key.data,
3131 slot->key.bytes,
3132 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003133 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003134 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003135
3136 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003137 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003138 status = PSA_ERROR_BUFFER_TOO_SMALL;
3139 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003140 }
Ronald Cron7207d572021-09-08 14:28:35 +02003141#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3142 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003143 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
3144 {
Ronald Cron7207d572021-09-08 14:28:35 +02003145#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003146 status = mbedtls_to_psa_error(
3147 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003148 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003149 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003150 MBEDTLS_RSA_PUBLIC,
3151 input_length,
3152 input,
3153 output ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003154#else
3155 status = PSA_ERROR_NOT_SUPPORTED;
3156#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003157 }
3158 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003159 if( PSA_ALG_IS_RSA_OAEP( alg ) )
3160 {
Ronald Cron7207d572021-09-08 14:28:35 +02003161#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003162 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003163 status = mbedtls_to_psa_error(
3164 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003165 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003166 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003167 MBEDTLS_RSA_PUBLIC,
3168 salt, salt_length,
3169 input_length,
3170 input,
3171 output ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003172#else
3173 status = PSA_ERROR_NOT_SUPPORTED;
3174#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003175 }
3176 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003178 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179 }
Ronald Cron7207d572021-09-08 14:28:35 +02003180#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3181 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003182rsa_exit:
3183 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003184 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003185
Steven Cooremana2371e52020-07-28 14:30:39 +02003186 mbedtls_rsa_free( rsa );
3187 mbedtls_free( rsa );
John Durkop9814fa22020-11-04 12:28:15 -08003188#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003189 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Ronald Cron7207d572021-09-08 14:28:35 +02003190 }
3191 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003193 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003195
3196exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003197 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003198
Ronald Cron5c522922020-11-14 16:35:34 +01003199 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003200}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201
Ronald Croncf56a0a2020-08-04 09:51:30 +02003202psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003203 psa_algorithm_t alg,
3204 const uint8_t *input,
3205 size_t input_length,
3206 const uint8_t *salt,
3207 size_t salt_length,
3208 uint8_t *output,
3209 size_t output_size,
3210 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003212 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003213 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003214 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003215
Darryl Green5cc689a2018-07-24 15:34:10 +01003216 (void) input;
3217 (void) input_length;
3218 (void) salt;
3219 (void) output;
3220 (void) output_size;
3221
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003222 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003223
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003224 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3225 return( PSA_ERROR_INVALID_ARGUMENT );
3226
Ronald Cron5c522922020-11-14 16:35:34 +01003227 status = psa_get_and_lock_transparent_key_slot_with_policy(
3228 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003229 if( status != PSA_SUCCESS )
3230 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003231 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003232 {
3233 status = PSA_ERROR_INVALID_ARGUMENT;
3234 goto exit;
3235 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003236
Gilles Peskine8e338702019-07-30 20:06:31 +02003237 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238 {
Ronald Cron7207d572021-09-08 14:28:35 +02003239#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3240 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003241 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003242 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3243 slot->key.data,
3244 slot->key.bytes,
3245 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003246 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003247 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003248
Steven Cooremana2371e52020-07-28 14:30:39 +02003249 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003250 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003251 status = PSA_ERROR_INVALID_ARGUMENT;
3252 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003253 }
Ronald Cron7207d572021-09-08 14:28:35 +02003254#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3255 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003256
Gilles Peskine6afe7892018-05-31 13:16:08 +02003257 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003258 {
Ronald Cron7207d572021-09-08 14:28:35 +02003259#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003260 status = mbedtls_to_psa_error(
3261 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003262 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003263 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003264 MBEDTLS_RSA_PRIVATE,
3265 output_length,
3266 input,
3267 output,
3268 output_size ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003269#else
3270 status = PSA_ERROR_NOT_SUPPORTED;
3271#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003272 }
3273 else
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003274 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003275 {
Ronald Cron7207d572021-09-08 14:28:35 +02003276#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003277 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003278 status = mbedtls_to_psa_error(
3279 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003280 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003281 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003282 MBEDTLS_RSA_PRIVATE,
3283 salt, salt_length,
3284 output_length,
3285 input,
3286 output,
3287 output_size ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003288#else
3289 status = PSA_ERROR_NOT_SUPPORTED;
3290#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003291 }
3292 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003293 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003294 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003295 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003296
Ronald Cron7207d572021-09-08 14:28:35 +02003297#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3298 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003299rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003300 mbedtls_rsa_free( rsa );
3301 mbedtls_free( rsa );
John Durkop6ba40d12020-11-10 08:50:04 -08003302#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3303 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Ronald Cron7207d572021-09-08 14:28:35 +02003304 }
3305 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003307 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003309
3310exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003311 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003312
Ronald Cron5c522922020-11-14 16:35:34 +01003313 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003314}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003315
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003316
3317
mohammad1603503973b2018-03-12 15:59:30 +02003318/****************************************************************/
3319/* Symmetric cryptography */
3320/****************************************************************/
3321
Ronald Cronab99ac22020-10-01 16:38:28 +02003322static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
3323 mbedtls_svc_key_id_t key,
3324 psa_algorithm_t alg,
3325 mbedtls_operation_t cipher_operation )
3326{
3327 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3328 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003329 psa_key_slot_t *slot = NULL;
Ronald Cronab99ac22020-10-01 16:38:28 +02003330 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3331 PSA_KEY_USAGE_ENCRYPT :
3332 PSA_KEY_USAGE_DECRYPT );
3333
3334 /* A context must be freshly initialized before it can be set up. */
Ronald Cron49fafa92021-03-10 08:34:23 +01003335 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01003336 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003337 status = PSA_ERROR_BAD_STATE;
3338 goto exit;
3339 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003340
Ronald Cronab99ac22020-10-01 16:38:28 +02003341 if( ! PSA_ALG_IS_CIPHER( alg ) )
Dave Rodgmandb861792021-06-24 16:17:43 +01003342 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003343 status = PSA_ERROR_INVALID_ARGUMENT;
3344 goto exit;
3345 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003346
Ronald Cronab99ac22020-10-01 16:38:28 +02003347 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
3348 if( status != PSA_SUCCESS )
3349 goto exit;
3350
Ronald Cron49fafa92021-03-10 08:34:23 +01003351 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003352 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003353 * so we only set it (in the driver wrapper) after resources have been
3354 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003355 operation->iv_set = 0;
Ronald Cronab99ac22020-10-01 16:38:28 +02003356 if( alg == PSA_ALG_ECB_NO_PADDING )
3357 operation->iv_required = 0;
Gilles Peskine4a83c102022-04-20 17:00:22 +02003358 else if( slot->attr.type == PSA_KEY_TYPE_ARC4 )
3359 operation->iv_required = 0;
Ronald Cronab99ac22020-10-01 16:38:28 +02003360 else
3361 operation->iv_required = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003362 operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
Ronald Cronab99ac22020-10-01 16:38:28 +02003363
Ronald Crona4af55f2020-12-14 14:36:06 +01003364 psa_key_attributes_t attributes = {
3365 .core = slot->attr
3366 };
3367
Ronald Cronab99ac22020-10-01 16:38:28 +02003368 /* Try doing the operation through a driver before using software fallback. */
3369 if( cipher_operation == MBEDTLS_ENCRYPT )
Ronald Crona4af55f2020-12-14 14:36:06 +01003370 status = psa_driver_wrapper_cipher_encrypt_setup( operation,
3371 &attributes,
3372 slot->key.data,
3373 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003374 alg );
3375 else
Ronald Crona4af55f2020-12-14 14:36:06 +01003376 status = psa_driver_wrapper_cipher_decrypt_setup( operation,
3377 &attributes,
3378 slot->key.data,
3379 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003380 alg );
3381
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003382exit:
Ronald Cron06aa4422021-03-09 17:32:57 +01003383 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003384 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003385
Ronald Cron5c522922020-11-14 16:35:34 +01003386 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003387
Ronald Cron5c522922020-11-14 16:35:34 +01003388 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003389}
3390
Gilles Peskinefe119512018-07-08 21:39:34 +02003391psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003392 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003393 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003394{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003395 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003396}
3397
Gilles Peskinefe119512018-07-08 21:39:34 +02003398psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003399 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003400 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003401{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003402 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003403}
3404
Gilles Peskinefe119512018-07-08 21:39:34 +02003405psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003406 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003407 size_t iv_size,
3408 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003409{
Ronald Cron6d051732020-10-01 14:10:20 +02003410 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003411 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3412 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01003413
Ronald Cron49fafa92021-03-10 08:34:23 +01003414 if( operation->id == 0 )
Ronald Cronc45b4af2020-09-29 16:18:05 +02003415 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003416 status = PSA_ERROR_BAD_STATE;
3417 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003418 }
3419
Steven Cooreman7df02922020-09-09 15:28:49 +02003420 if( operation->iv_set || ! operation->iv_required )
3421 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003422 status = PSA_ERROR_BAD_STATE;
3423 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003424 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003425
Ronald Cronc423acb2021-07-05 12:31:44 +02003426 default_iv_length = operation->default_iv_length;
3427 if( iv_size < default_iv_length )
Ronald Cron5618a392021-03-26 09:52:26 +01003428 {
3429 status = PSA_ERROR_BUFFER_TOO_SMALL;
3430 goto exit;
3431 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003432
Ronald Cronc423acb2021-07-05 12:31:44 +02003433 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
3434 {
3435 status = PSA_ERROR_GENERIC_ERROR;
3436 goto exit;
3437 }
3438
3439 status = psa_generate_random( local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003440 if( status != PSA_SUCCESS )
3441 goto exit;
3442
3443 status = psa_driver_wrapper_cipher_set_iv( operation,
Ronald Cronc423acb2021-07-05 12:31:44 +02003444 local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003445
3446exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003447 if( status == PSA_SUCCESS )
Ronald Cron5618a392021-03-26 09:52:26 +01003448 {
Ronald Cronc423acb2021-07-05 12:31:44 +02003449 memcpy( iv, local_iv, default_iv_length );
3450 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003451 operation->iv_set = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003452 }
Steven Cooreman7df02922020-09-09 15:28:49 +02003453 else
Ronald Cronc423acb2021-07-05 12:31:44 +02003454 {
3455 *iv_length = 0;
Moran Peker395db872018-05-31 14:07:14 +03003456 psa_cipher_abort( operation );
Ronald Cronc423acb2021-07-05 12:31:44 +02003457 }
Ronald Cron6d051732020-10-01 14:10:20 +02003458
itayzafrir534bd7c2018-08-02 13:56:32 +03003459 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003460}
3461
Gilles Peskinefe119512018-07-08 21:39:34 +02003462psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003463 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003464 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003465{
Ronald Cron6d051732020-10-01 14:10:20 +02003466 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003467
Ronald Cron49fafa92021-03-10 08:34:23 +01003468 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01003469 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003470 status = PSA_ERROR_BAD_STATE;
3471 goto exit;
3472 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003473
Steven Cooreman7df02922020-09-09 15:28:49 +02003474 if( operation->iv_set || ! operation->iv_required )
Dave Rodgmandb861792021-06-24 16:17:43 +01003475 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003476 status = PSA_ERROR_BAD_STATE;
3477 goto exit;
3478 }
Ronald Crona0d68172021-03-26 10:15:08 +01003479
3480 if( iv_length > PSA_CIPHER_IV_MAX_SIZE )
Dave Rodgmandb861792021-06-24 16:17:43 +01003481 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003482 status = PSA_ERROR_INVALID_ARGUMENT;
3483 goto exit;
3484 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003485
Ronald Crondd24c9b2020-12-15 14:10:01 +01003486 status = psa_driver_wrapper_cipher_set_iv( operation,
3487 iv,
3488 iv_length );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003489
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003490exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003491 if( status == PSA_SUCCESS )
3492 operation->iv_set = 1;
3493 else
mohammad1603503973b2018-03-12 15:59:30 +02003494 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003495 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003496}
3497
Gilles Peskinee553c652018-06-04 16:22:46 +02003498psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3499 const uint8_t *input,
3500 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003501 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003502 size_t output_size,
3503 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003504{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003505 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003506
Ronald Cron49fafa92021-03-10 08:34:23 +01003507 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003508 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003509 status = PSA_ERROR_BAD_STATE;
3510 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003511 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003512
Steven Cooreman7df02922020-09-09 15:28:49 +02003513 if( operation->iv_required && ! operation->iv_set )
3514 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003515 status = PSA_ERROR_BAD_STATE;
3516 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003517 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003518
Ronald Crondd24c9b2020-12-15 14:10:01 +01003519 status = psa_driver_wrapper_cipher_update( operation,
3520 input,
3521 input_length,
3522 output,
3523 output_size,
3524 output_length );
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003525
3526exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003527 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003528 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003529
itayzafrir534bd7c2018-08-02 13:56:32 +03003530 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003531}
3532
Gilles Peskinee553c652018-06-04 16:22:46 +02003533psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3534 uint8_t *output,
3535 size_t output_size,
3536 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003537{
David Saadab4ecc272019-02-14 13:48:10 +02003538 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003539
Ronald Cron49fafa92021-03-10 08:34:23 +01003540 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003541 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003542 status = PSA_ERROR_BAD_STATE;
3543 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003544 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003545
Steven Cooreman7df02922020-09-09 15:28:49 +02003546 if( operation->iv_required && ! operation->iv_set )
3547 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003548 status = PSA_ERROR_BAD_STATE;
3549 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003550 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003551
Ronald Crondd24c9b2020-12-15 14:10:01 +01003552 status = psa_driver_wrapper_cipher_finish( operation,
3553 output,
3554 output_size,
3555 output_length );
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003556
3557exit:
Steven Cooremanef8575e2020-09-11 11:44:50 +02003558 if( status == PSA_SUCCESS )
3559 return( psa_cipher_abort( operation ) );
3560 else
3561 {
3562 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003563 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01003564
Steven Cooremanef8575e2020-09-11 11:44:50 +02003565 return( status );
3566 }
mohammad1603503973b2018-03-12 15:59:30 +02003567}
3568
Gilles Peskinee553c652018-06-04 16:22:46 +02003569psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3570{
Ronald Cron49fafa92021-03-10 08:34:23 +01003571 if( operation->id == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003572 {
Steven Cooremana07b9972020-09-10 14:54:14 +02003573 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003574 * in use. It's ok to call abort on such an object, and there's
3575 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003576 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003577 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003578
Ronald Crondd24c9b2020-12-15 14:10:01 +01003579 psa_driver_wrapper_cipher_abort( operation );
Gilles Peskinee553c652018-06-04 16:22:46 +02003580
Ronald Cron49fafa92021-03-10 08:34:23 +01003581 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003582 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003583 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003584
Moran Peker395db872018-05-31 14:07:14 +03003585 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003586}
3587
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003588psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key,
3589 psa_algorithm_t alg,
3590 const uint8_t *input,
3591 size_t input_length,
3592 uint8_t *output,
3593 size_t output_size,
3594 size_t *output_length )
3595{
3596 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003597 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron16377072021-07-08 19:00:07 +02003598 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003599 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3600 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003601
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003602 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron16377072021-07-08 19:00:07 +02003603 {
3604 status = PSA_ERROR_INVALID_ARGUMENT;
3605 goto exit;
3606 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003607
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003608 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3609 PSA_KEY_USAGE_ENCRYPT,
3610 alg );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003611 if( status != PSA_SUCCESS )
Ronald Cron16377072021-07-08 19:00:07 +02003612 goto exit;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003613
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003614 psa_key_attributes_t attributes = {
3615 .core = slot->attr
3616 };
3617
Ronald Cron4c224fe2021-07-09 18:44:58 +02003618 default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
3619 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003620 {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003621 status = PSA_ERROR_GENERIC_ERROR;
3622 goto exit;
3623 }
3624
3625 if( default_iv_length > 0 )
3626 {
3627 if( output_size < default_iv_length )
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003628 {
3629 status = PSA_ERROR_BUFFER_TOO_SMALL;
3630 goto exit;
3631 }
3632
Ronald Cron4c224fe2021-07-09 18:44:58 +02003633 status = psa_generate_random( local_iv, default_iv_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003634 if( status != PSA_SUCCESS )
3635 goto exit;
3636 }
3637
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003638 status = psa_driver_wrapper_cipher_encrypt(
3639 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003640 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine01bf6312022-11-23 14:15:57 +01003641 mbedtls_buffer_offset( output, default_iv_length ),
3642 output_size - default_iv_length, output_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003643
3644exit:
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003645 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron16377072021-07-08 19:00:07 +02003646 if( status == PSA_SUCCESS )
3647 status = unlock_status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003648
Ronald Crona8331692021-07-09 09:19:35 +02003649 if( status == PSA_SUCCESS )
Ronald Cron4c224fe2021-07-09 18:44:58 +02003650 {
3651 if( default_iv_length > 0 )
3652 memcpy( output, local_iv, default_iv_length );
3653 *output_length += default_iv_length;
3654 }
Ronald Crona8331692021-07-09 09:19:35 +02003655 else
Ronald Cron16377072021-07-08 19:00:07 +02003656 *output_length = 0;
3657
3658 return( status );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003659}
3660
3661psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key,
3662 psa_algorithm_t alg,
3663 const uint8_t *input,
3664 size_t input_length,
3665 uint8_t *output,
3666 size_t output_size,
3667 size_t *output_length )
3668{
3669 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003670 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron16377072021-07-08 19:00:07 +02003671 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003672
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003673 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron16377072021-07-08 19:00:07 +02003674 {
3675 status = PSA_ERROR_INVALID_ARGUMENT;
3676 goto exit;
3677 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003678
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003679 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3680 PSA_KEY_USAGE_DECRYPT,
3681 alg );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003682 if( status != PSA_SUCCESS )
Ronald Cron16377072021-07-08 19:00:07 +02003683 goto exit;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003684
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003685 psa_key_attributes_t attributes = {
3686 .core = slot->attr
3687 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003688
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003689 if( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) )
3690 {
3691 status = PSA_ERROR_INVALID_ARGUMENT;
3692 goto exit;
3693 }
3694
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003695 status = psa_driver_wrapper_cipher_decrypt(
3696 &attributes, slot->key.data, slot->key.bytes,
3697 alg, input, input_length,
3698 output, output_size, output_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003699
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003700exit:
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003701 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron16377072021-07-08 19:00:07 +02003702 if( status == PSA_SUCCESS )
3703 status = unlock_status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003704
Ronald Cron16377072021-07-08 19:00:07 +02003705 if( status != PSA_SUCCESS )
3706 *output_length = 0;
3707
3708 return( status );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003709}
3710
3711
mohammad16035955c982018-04-26 00:53:03 +03003712/****************************************************************/
3713/* AEAD */
3714/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003715
Ronald Croncf56a0a2020-08-04 09:51:30 +02003716psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003717 psa_algorithm_t alg,
3718 const uint8_t *nonce,
3719 size_t nonce_length,
3720 const uint8_t *additional_data,
3721 size_t additional_data_length,
3722 const uint8_t *plaintext,
3723 size_t plaintext_length,
3724 uint8_t *ciphertext,
3725 size_t ciphertext_size,
3726 size_t *ciphertext_length )
3727{
Ronald Cron215633c2021-03-16 17:15:37 +01003728 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3729 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003730
mohammad1603f08a5502018-06-03 15:05:47 +03003731 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003732
Steven Cooremanea7ab132021-03-17 16:28:00 +01003733 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3734 return( PSA_ERROR_NOT_SUPPORTED );
3735
Ronald Cron9a986162021-03-26 12:40:07 +01003736 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003737 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003738 if( status != PSA_SUCCESS )
3739 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003740
Ronald Cron215633c2021-03-16 17:15:37 +01003741 psa_key_attributes_t attributes = {
3742 .core = slot->attr
3743 };
mohammad16035955c982018-04-26 00:53:03 +03003744
Ronald Cronde822812021-03-17 16:08:20 +01003745 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003746 &attributes, slot->key.data, slot->key.bytes,
3747 alg,
3748 nonce, nonce_length,
3749 additional_data, additional_data_length,
3750 plaintext, plaintext_length,
3751 ciphertext, ciphertext_size, ciphertext_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003752
Gilles Peskineedf9a652018-08-17 18:11:56 +02003753 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3754 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003755
Ronald Cron9f310172021-03-16 16:36:37 +01003756 psa_unlock_key_slot( slot );
mohammad16035955c982018-04-26 00:53:03 +03003757
mohammad16035955c982018-04-26 00:53:03 +03003758 return( status );
Gilles Peskineee652a32018-06-01 19:23:52 +02003759}
3760
Ronald Croncf56a0a2020-08-04 09:51:30 +02003761psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003762 psa_algorithm_t alg,
3763 const uint8_t *nonce,
3764 size_t nonce_length,
3765 const uint8_t *additional_data,
3766 size_t additional_data_length,
3767 const uint8_t *ciphertext,
3768 size_t ciphertext_length,
3769 uint8_t *plaintext,
3770 size_t plaintext_size,
3771 size_t *plaintext_length )
3772{
Ronald Cron215633c2021-03-16 17:15:37 +01003773 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3774 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003775
Gilles Peskineee652a32018-06-01 19:23:52 +02003776 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003777
Steven Cooremanea7ab132021-03-17 16:28:00 +01003778 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3779 return( PSA_ERROR_NOT_SUPPORTED );
3780
Ronald Cron9a986162021-03-26 12:40:07 +01003781 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003782 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003783 if( status != PSA_SUCCESS )
3784 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003785
Ronald Cron215633c2021-03-16 17:15:37 +01003786 psa_key_attributes_t attributes = {
3787 .core = slot->attr
3788 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003789
Ronald Cronde822812021-03-17 16:08:20 +01003790 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003791 &attributes, slot->key.data, slot->key.bytes,
3792 alg,
3793 nonce, nonce_length,
3794 additional_data, additional_data_length,
3795 ciphertext, ciphertext_length,
3796 plaintext, plaintext_size, plaintext_length );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003797
Gilles Peskineedf9a652018-08-17 18:11:56 +02003798 if( status != PSA_SUCCESS && plaintext_size != 0 )
3799 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003800
Ronald Cron215633c2021-03-16 17:15:37 +01003801 psa_unlock_key_slot( slot );
3802
Gilles Peskineedf9a652018-08-17 18:11:56 +02003803 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003804}
3805
Gilles Peskinee59236f2018-01-27 23:32:46 +01003806/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003807/* Generators */
3808/****************************************************************/
3809
John Durkop07cc04a2020-11-16 22:08:34 -08003810#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3811 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3812 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3813#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003814#endif /* At least one builtin KDF */
3815
3816#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3817 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3818 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3819static psa_status_t psa_key_derivation_start_hmac(
3820 psa_mac_operation_t *operation,
3821 psa_algorithm_t hash_alg,
3822 const uint8_t *hmac_key,
3823 size_t hmac_key_length )
3824{
3825 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3826 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3827 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
3828 psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) );
3829 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
3830
Steven Cooreman15f0d922021-05-07 14:14:37 +02003831 operation->is_sign = 1;
3832 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
3833
Steven Cooreman9878a162021-05-06 17:58:36 +02003834 status = psa_driver_wrapper_mac_sign_setup( operation,
3835 &attributes,
3836 hmac_key, hmac_key_length,
3837 PSA_ALG_HMAC( hash_alg ) );
3838
3839 psa_reset_key_attributes( &attributes );
3840 return( status );
3841}
3842#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003843
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003844#define HKDF_STATE_INIT 0 /* no input yet */
3845#define HKDF_STATE_STARTED 1 /* got salt */
3846#define HKDF_STATE_KEYED 2 /* got key */
3847#define HKDF_STATE_OUTPUT 3 /* output started */
3848
Gilles Peskinecbe66502019-05-16 16:59:18 +02003849static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003850 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003851{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003852 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3853 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003854 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003855 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003856}
3857
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003858psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003859{
3860 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003861 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003862 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003863 {
3864 /* The object has (apparently) been initialized but it is not
3865 * in use. It's ok to call abort on such an object, and there's
3866 * nothing to do. */
3867 }
3868 else
John Durkopd0321952020-10-29 21:37:36 -07003869#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003870 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003871 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003872 mbedtls_free( operation->ctx.hkdf.info );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003873 status = psa_mac_abort( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003874 }
John Durkop07cc04a2020-11-16 22:08:34 -08003875 else
3876#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3877#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3878 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3879 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003880 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003881 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003882 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003883 if( operation->ctx.tls12_prf.secret != NULL )
3884 {
3885 mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret,
3886 operation->ctx.tls12_prf.secret_length );
3887 mbedtls_free( operation->ctx.tls12_prf.secret );
3888 }
3889
Janos Follath6a1d2622019-06-11 10:37:28 +01003890 if( operation->ctx.tls12_prf.seed != NULL )
3891 {
3892 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
3893 operation->ctx.tls12_prf.seed_length );
3894 mbedtls_free( operation->ctx.tls12_prf.seed );
3895 }
3896
3897 if( operation->ctx.tls12_prf.label != NULL )
3898 {
3899 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
3900 operation->ctx.tls12_prf.label_length );
3901 mbedtls_free( operation->ctx.tls12_prf.label );
3902 }
3903
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003904 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003905
3906 /* We leave the fields Ai and output_block to be erased safely by the
3907 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003908 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003909 else
John Durkop07cc04a2020-11-16 22:08:34 -08003910#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3911 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003912 {
3913 status = PSA_ERROR_BAD_STATE;
3914 }
Janos Follath083036a2019-06-11 10:22:26 +01003915 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003916 return( status );
3917}
3918
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003919psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003920 size_t *capacity)
3921{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003922 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003923 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003924 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02003925 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003926 }
3927
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003928 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003929 return( PSA_SUCCESS );
3930}
3931
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003932psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003933 size_t capacity )
3934{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003935 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003936 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003937 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003938 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003939 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003940 return( PSA_SUCCESS );
3941}
3942
John Durkopd0321952020-10-29 21:37:36 -07003943#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003944/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003945 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003946static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Steven Cooremanb27e3502021-04-29 19:11:25 +02003947 psa_algorithm_t hash_alg,
3948 uint8_t *output,
3949 size_t output_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003950{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003951 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003952 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003953 psa_status_t status;
3954
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003955 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3956 return( PSA_ERROR_BAD_STATE );
3957 hkdf->state = HKDF_STATE_OUTPUT;
3958
Gilles Peskinebef7f142018-07-12 17:22:21 +02003959 while( output_length != 0 )
3960 {
3961 /* Copy what remains of the current block */
3962 uint8_t n = hash_length - hkdf->offset_in_block;
3963 if( n > output_length )
3964 n = (uint8_t) output_length;
3965 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3966 output += n;
3967 output_length -= n;
3968 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003969 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003970 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003971 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003972 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003973 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003974 * object was corrupted or if this function is called directly
3975 * inside the library. */
3976 if( hkdf->block_number == 0xff )
3977 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003978
3979 /* We need a new block */
3980 ++hkdf->block_number;
3981 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02003982
Steven Cooreman9878a162021-05-06 17:58:36 +02003983 status = psa_key_derivation_start_hmac( &hkdf->hmac,
3984 hash_alg,
3985 hkdf->prk,
3986 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003987 if( status != PSA_SUCCESS )
3988 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003989
Gilles Peskinebef7f142018-07-12 17:22:21 +02003990 if( hkdf->block_number != 1 )
3991 {
Steven Cooremanb27e3502021-04-29 19:11:25 +02003992 status = psa_mac_update( &hkdf->hmac,
3993 hkdf->output_block,
3994 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003995 if( status != PSA_SUCCESS )
3996 return( status );
3997 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02003998 status = psa_mac_update( &hkdf->hmac,
3999 hkdf->info,
4000 hkdf->info_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004001 if( status != PSA_SUCCESS )
4002 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004003 status = psa_mac_update( &hkdf->hmac,
4004 &hkdf->block_number, 1 );
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_sign_finish( &hkdf->hmac,
4008 hkdf->output_block,
4009 sizeof( hkdf->output_block ),
4010 &hmac_output_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004011 if( status != PSA_SUCCESS )
4012 return( status );
4013 }
4014
4015 return( PSA_SUCCESS );
4016}
John Durkop07cc04a2020-11-16 22:08:34 -08004017#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004018
John Durkop07cc04a2020-11-16 22:08:34 -08004019#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4020 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004021static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4022 psa_tls12_prf_key_derivation_t *tls12_prf,
4023 psa_algorithm_t alg )
4024{
4025 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004026 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004027 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4028 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004029 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004030
Janos Follath7742fee2019-06-17 12:58:10 +01004031 /* We can't be wanting more output after block 0xff, otherwise
4032 * the capacity check in psa_key_derivation_output_bytes() would have
4033 * prevented this call. It could happen only if the operation
4034 * object was corrupted or if this function is called directly
4035 * inside the library. */
4036 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004037 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004038
4039 /* We need a new block */
4040 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004041 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004042
4043 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4044 *
4045 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4046 *
4047 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4048 * HMAC_hash(secret, A(2) + seed) +
4049 * HMAC_hash(secret, A(3) + seed) + ...
4050 *
4051 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004052 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004053 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004054 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004055 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004056 * is currently extracted as `output_block` and where i is
4057 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004058 */
4059
Steven Cooreman9878a162021-05-06 17:58:36 +02004060 status = psa_key_derivation_start_hmac( &hmac,
4061 hash_alg,
4062 tls12_prf->secret,
4063 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004064 if( status != PSA_SUCCESS )
4065 goto cleanup;
4066
4067 /* Calculate A(i) where i = tls12_prf->block_number. */
4068 if( tls12_prf->block_number == 1 )
4069 {
4070 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4071 * the variable seed and in this instance means it in the context of the
4072 * P_hash function, where seed = label + seed.) */
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004073 status = psa_mac_update( &hmac,
4074 tls12_prf->label,
4075 tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004076 if( status != PSA_SUCCESS )
4077 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004078 status = psa_mac_update( &hmac,
4079 tls12_prf->seed,
4080 tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004081 if( status != PSA_SUCCESS )
4082 goto cleanup;
4083 }
4084 else
4085 {
4086 /* A(i) = HMAC_hash(secret, A(i-1)) */
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004087 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004088 if( status != PSA_SUCCESS )
4089 goto cleanup;
4090 }
4091
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004092 status = psa_mac_sign_finish( &hmac,
4093 tls12_prf->Ai, hash_length,
4094 &hmac_output_length );
4095 if( hmac_output_length != hash_length )
4096 status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follathea29bfb2019-06-19 12:21:20 +01004097 if( status != PSA_SUCCESS )
4098 goto cleanup;
4099
4100 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Steven Cooreman9878a162021-05-06 17:58:36 +02004101 status = psa_key_derivation_start_hmac( &hmac,
4102 hash_alg,
4103 tls12_prf->secret,
4104 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004105 if( status != PSA_SUCCESS )
4106 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004107 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004108 if( status != PSA_SUCCESS )
4109 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004110 status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004111 if( status != PSA_SUCCESS )
4112 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004113 status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004114 if( status != PSA_SUCCESS )
4115 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004116 status = psa_mac_sign_finish( &hmac,
4117 tls12_prf->output_block, hash_length,
4118 &hmac_output_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004119 if( status != PSA_SUCCESS )
4120 goto cleanup;
4121
Janos Follath7742fee2019-06-17 12:58:10 +01004122
4123cleanup:
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004124 cleanup_status = psa_mac_abort( &hmac );
Janos Follathea29bfb2019-06-19 12:21:20 +01004125 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4126 status = cleanup_status;
4127
4128 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004129}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004130
Janos Follath844eb0e2019-06-19 12:10:49 +01004131static psa_status_t psa_key_derivation_tls12_prf_read(
4132 psa_tls12_prf_key_derivation_t *tls12_prf,
4133 psa_algorithm_t alg,
4134 uint8_t *output,
4135 size_t output_length )
4136{
4137 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004138 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004139 psa_status_t status;
4140 uint8_t offset, length;
4141
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004142 switch( tls12_prf->state )
4143 {
4144 case PSA_TLS12_PRF_STATE_LABEL_SET:
4145 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4146 break;
4147 case PSA_TLS12_PRF_STATE_OUTPUT:
4148 break;
4149 default:
4150 return( PSA_ERROR_BAD_STATE );
4151 }
4152
Janos Follath844eb0e2019-06-19 12:10:49 +01004153 while( output_length != 0 )
4154 {
4155 /* Check if we have fully processed the current block. */
4156 if( tls12_prf->left_in_block == 0 )
4157 {
4158 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4159 alg );
4160 if( status != PSA_SUCCESS )
4161 return( status );
4162
4163 continue;
4164 }
4165
4166 if( tls12_prf->left_in_block > output_length )
4167 length = (uint8_t) output_length;
4168 else
4169 length = tls12_prf->left_in_block;
4170
4171 offset = hash_length - tls12_prf->left_in_block;
4172 memcpy( output, tls12_prf->output_block + offset, length );
4173 output += length;
4174 output_length -= length;
4175 tls12_prf->left_in_block -= length;
4176 }
4177
4178 return( PSA_SUCCESS );
4179}
John Durkop07cc04a2020-11-16 22:08:34 -08004180#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4181 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004182
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004183psa_status_t psa_key_derivation_output_bytes(
4184 psa_key_derivation_operation_t *operation,
4185 uint8_t *output,
4186 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004187{
4188 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004189 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004190
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004192 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004194 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004195 }
4196
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004197 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004198 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004199 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004200 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004201 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004202 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004203 goto exit;
4204 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004205 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004206 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004207 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004208 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004209 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4210 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004211 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004212 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004213 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004214 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004215 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004216
John Durkopd0321952020-10-29 21:37:36 -07004217#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004218 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004219 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004220 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004221 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004222 output, output_length );
4223 }
Janos Follathadbec812019-06-14 11:05:39 +01004224 else
John Durkop07cc04a2020-11-16 22:08:34 -08004225#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4226#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4227 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004228 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004229 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004230 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004231 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004232 kdf_alg, output,
4233 output_length );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004234 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004235 else
John Durkop07cc04a2020-11-16 22:08:34 -08004236#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4237 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004238 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004239 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004240 return( PSA_ERROR_BAD_STATE );
4241 }
4242
4243exit:
4244 if( status != PSA_SUCCESS )
4245 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004246 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004247 * This allows us to differentiate between exhausted operations and
4248 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4249 * operations. */
4250 psa_algorithm_t alg = operation->alg;
4251 psa_key_derivation_abort( operation );
4252 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004253 memset( output, '!', output_length );
4254 }
4255 return( status );
4256}
4257
David Brown7807bf72021-02-09 16:28:23 -07004258#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004259static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4260{
4261 if( data_size >= 8 )
4262 mbedtls_des_key_set_parity( data );
4263 if( data_size >= 16 )
4264 mbedtls_des_key_set_parity( data + 8 );
4265 if( data_size >= 24 )
4266 mbedtls_des_key_set_parity( data + 16 );
4267}
David Brown7807bf72021-02-09 16:28:23 -07004268#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004269
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004270static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004271 psa_key_slot_t *slot,
4272 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004273 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004274{
4275 uint8_t *data = NULL;
4276 size_t bytes = PSA_BITS_TO_BYTES( bits );
4277 psa_status_t status;
4278
Gilles Peskine8e338702019-07-30 20:06:31 +02004279 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004280 return( PSA_ERROR_INVALID_ARGUMENT );
4281 if( bits % 8 != 0 )
4282 return( PSA_ERROR_INVALID_ARGUMENT );
4283 data = mbedtls_calloc( 1, bytes );
4284 if( data == NULL )
4285 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4286
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004287 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004288 if( status != PSA_SUCCESS )
4289 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07004290#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004291 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004292 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07004293#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004294
4295 status = psa_allocate_buffer_to_slot( slot, bytes );
4296 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00004297 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01004298
Ronald Cronbf33c932020-11-28 18:06:53 +01004299 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004300 psa_key_attributes_t attributes = {
4301 .core = slot->attr
4302 };
4303
Ronald Cronbf33c932020-11-28 18:06:53 +01004304 status = psa_driver_wrapper_import_key( &attributes,
4305 data, bytes,
4306 slot->key.data,
4307 slot->key.bytes,
4308 &slot->key.bytes, &bits );
4309 if( bits != slot->attr.bits )
4310 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004311
4312exit:
4313 mbedtls_free( data );
4314 return( status );
4315}
4316
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004317psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004318 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004319 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004320{
4321 psa_status_t status;
4322 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004323 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004324
Ronald Cron81709fc2020-11-14 12:10:32 +01004325 *key = MBEDTLS_SVC_KEY_ID_INIT;
4326
Gilles Peskine0f84d622019-09-12 19:03:13 +02004327 /* Reject any attempt to create a zero-length key so that we don't
4328 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4329 if( psa_get_key_bits( attributes ) == 0 )
4330 return( PSA_ERROR_INVALID_ARGUMENT );
4331
Dave Rodgman021e7242021-11-16 10:32:48 +00004332 if( operation->alg == PSA_ALG_NONE )
4333 return( PSA_ERROR_BAD_STATE );
4334
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004335 if( ! operation->can_output_key )
4336 return( PSA_ERROR_NOT_PERMITTED );
4337
Ronald Cron81709fc2020-11-14 12:10:32 +01004338 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
4339 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004340#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4341 if( driver != NULL )
4342 {
4343 /* Deriving a key in a secure element is not implemented yet. */
4344 status = PSA_ERROR_NOT_SUPPORTED;
4345 }
4346#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004347 if( status == PSA_SUCCESS )
4348 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004349 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004350 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004351 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004352 }
4353 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01004354 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004355 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004356 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004357
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004358 return( status );
4359}
4360
Gilles Peskineeab56e42018-07-12 17:12:33 +02004361
4362
4363/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004364/* Key derivation */
4365/****************************************************************/
4366
Steven Cooreman932ffb72021-02-15 12:14:32 +01004367#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004368static int is_kdf_alg_supported( psa_algorithm_t kdf_alg )
4369{
4370#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
4371 if( PSA_ALG_IS_HKDF( kdf_alg ) )
4372 return( 1 );
4373#endif
4374#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4375 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
4376 return( 1 );
4377#endif
4378#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4379 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
4380 return( 1 );
4381#endif
4382 return( 0 );
4383}
4384
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004385static psa_status_t psa_hash_try_support( psa_algorithm_t alg )
4386{
4387 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
4388 psa_status_t status = psa_hash_setup( &operation, alg );
4389 psa_hash_abort( &operation );
4390 return( status );
4391}
4392
Gilles Peskine969c5d62019-01-16 15:53:06 +01004393static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004394 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004395 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004396{
Janos Follath5fe19732019-06-20 15:09:30 +01004397 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4398 * initialisers for this union leave some bytes unspecified.) */
4399 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4400
Gilles Peskine969c5d62019-01-16 15:53:06 +01004401 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskinecdacf042021-04-29 21:10:00 +02004402 if( ! is_kdf_alg_supported( kdf_alg ) )
4403 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop07cc04a2020-11-16 22:08:34 -08004404
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004405 /* All currently supported key derivation algorithms are based on a
4406 * hash algorithm. */
Gilles Peskinecdacf042021-04-29 21:10:00 +02004407 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
4408 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
4409 if( hash_size == 0 )
4410 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004411
4412 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4413 * we might fail later, which is somewhat unfriendly and potentially
4414 * risk-prone. */
4415 psa_status_t status = psa_hash_try_support( hash_alg );
4416 if( status != PSA_SUCCESS )
4417 return( status );
4418
Gilles Peskinecdacf042021-04-29 21:10:00 +02004419 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4420 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
4421 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004422 {
Gilles Peskinecdacf042021-04-29 21:10:00 +02004423 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004424 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004425
Gilles Peskinecdacf042021-04-29 21:10:00 +02004426 operation->capacity = 255 * hash_size;
4427 return( PSA_SUCCESS );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004428}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004429
4430static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg )
4431{
4432#if defined(PSA_WANT_ALG_ECDH)
4433 if( alg == PSA_ALG_ECDH )
4434 return( PSA_SUCCESS );
4435#endif
4436 (void) alg;
4437 return( PSA_ERROR_NOT_SUPPORTED );
4438}
John Durkop07cc04a2020-11-16 22:08:34 -08004439#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004441psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004442 psa_algorithm_t alg )
4443{
4444 psa_status_t status;
4445
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004446 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004447 return( PSA_ERROR_BAD_STATE );
4448
Gilles Peskine6843c292019-01-18 16:44:49 +01004449 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4450 return( PSA_ERROR_INVALID_ARGUMENT );
4451 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004452 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004453#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004454 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine739c98c2021-04-29 21:34:33 +02004455 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg );
4456 status = psa_key_agreement_try_support( ka_alg );
4457 if( status != PSA_SUCCESS )
4458 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004459 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004460#else
4461 return( PSA_ERROR_NOT_SUPPORTED );
4462#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004463 }
4464 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4465 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004466#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004467 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004468#else
4469 return( PSA_ERROR_NOT_SUPPORTED );
4470#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004471 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004472 else
4473 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004474
4475 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004476 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004477 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004478}
4479
John Durkopd0321952020-10-29 21:37:36 -07004480#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004481static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004482 psa_algorithm_t hash_alg,
4483 psa_key_derivation_step_t step,
4484 const uint8_t *data,
4485 size_t data_length )
4486{
4487 psa_status_t status;
4488 switch( step )
4489 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004490 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004491 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004492 return( PSA_ERROR_BAD_STATE );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004493 else
4494 {
Steven Cooreman9878a162021-05-06 17:58:36 +02004495 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4496 hash_alg,
4497 data, data_length );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004498 if( status != PSA_SUCCESS )
4499 return( status );
4500 hkdf->state = HKDF_STATE_STARTED;
4501 return( PSA_SUCCESS );
4502 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004503 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004504 /* If no salt was provided, use an empty salt. */
4505 if( hkdf->state == HKDF_STATE_INIT )
4506 {
Steven Cooreman9878a162021-05-06 17:58:36 +02004507 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4508 hash_alg,
4509 NULL, 0 );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004510 if( status != PSA_SUCCESS )
4511 return( status );
4512 hkdf->state = HKDF_STATE_STARTED;
4513 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004514 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004515 return( PSA_ERROR_BAD_STATE );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004516 status = psa_mac_update( &hkdf->hmac,
4517 data, data_length );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004518 if( status != PSA_SUCCESS )
4519 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004520 status = psa_mac_sign_finish( &hkdf->hmac,
4521 hkdf->prk,
4522 sizeof( hkdf->prk ),
4523 &data_length );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004524 if( status != PSA_SUCCESS )
4525 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004526 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004527 hkdf->block_number = 0;
4528 hkdf->state = HKDF_STATE_KEYED;
4529 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004530 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004531 if( hkdf->state == HKDF_STATE_OUTPUT )
4532 return( PSA_ERROR_BAD_STATE );
4533 if( hkdf->info_set )
4534 return( PSA_ERROR_BAD_STATE );
4535 hkdf->info_length = data_length;
4536 if( data_length != 0 )
4537 {
4538 hkdf->info = mbedtls_calloc( 1, data_length );
4539 if( hkdf->info == NULL )
4540 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4541 memcpy( hkdf->info, data, data_length );
4542 }
4543 hkdf->info_set = 1;
4544 return( PSA_SUCCESS );
4545 default:
4546 return( PSA_ERROR_INVALID_ARGUMENT );
4547 }
4548}
John Durkop07cc04a2020-11-16 22:08:34 -08004549#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004550
John Durkop07cc04a2020-11-16 22:08:34 -08004551#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4552 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01004553static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4554 const uint8_t *data,
4555 size_t data_length )
4556{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004557 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01004558 return( PSA_ERROR_BAD_STATE );
4559
Janos Follathd6dce9f2019-07-04 09:11:38 +01004560 if( data_length != 0 )
4561 {
4562 prf->seed = mbedtls_calloc( 1, data_length );
4563 if( prf->seed == NULL )
4564 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004565
Janos Follathd6dce9f2019-07-04 09:11:38 +01004566 memcpy( prf->seed, data, data_length );
4567 prf->seed_length = data_length;
4568 }
Janos Follathf08e2652019-06-13 09:05:41 +01004569
Gilles Peskine43f958b2020-12-13 14:55:14 +01004570 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004571
4572 return( PSA_SUCCESS );
4573}
4574
Janos Follath81550542019-06-13 14:26:34 +01004575static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
Janos Follath81550542019-06-13 14:26:34 +01004576 const uint8_t *data,
4577 size_t data_length )
4578{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004579 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01004580 return( PSA_ERROR_BAD_STATE );
4581
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004582 if( data_length != 0 )
4583 {
4584 prf->secret = mbedtls_calloc( 1, data_length );
4585 if( prf->secret == NULL )
4586 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4587
4588 memcpy( prf->secret, data, data_length );
4589 prf->secret_length = data_length;
4590 }
Janos Follath81550542019-06-13 14:26:34 +01004591
Gilles Peskine43f958b2020-12-13 14:55:14 +01004592 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004593
4594 return( PSA_SUCCESS );
4595}
4596
Janos Follath63028dd2019-06-13 09:15:47 +01004597static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
4598 const uint8_t *data,
4599 size_t data_length )
4600{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004601 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01004602 return( PSA_ERROR_BAD_STATE );
4603
Janos Follathd6dce9f2019-07-04 09:11:38 +01004604 if( data_length != 0 )
4605 {
4606 prf->label = mbedtls_calloc( 1, data_length );
4607 if( prf->label == NULL )
4608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01004609
Janos Follathd6dce9f2019-07-04 09:11:38 +01004610 memcpy( prf->label, data, data_length );
4611 prf->label_length = data_length;
4612 }
Janos Follath63028dd2019-06-13 09:15:47 +01004613
Gilles Peskine43f958b2020-12-13 14:55:14 +01004614 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004615
4616 return( PSA_SUCCESS );
4617}
4618
Janos Follathb03233e2019-06-11 15:30:30 +01004619static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
Janos Follathb03233e2019-06-11 15:30:30 +01004620 psa_key_derivation_step_t step,
4621 const uint8_t *data,
4622 size_t data_length )
4623{
Janos Follathb03233e2019-06-11 15:30:30 +01004624 switch( step )
4625 {
Janos Follathf08e2652019-06-13 09:05:41 +01004626 case PSA_KEY_DERIVATION_INPUT_SEED:
4627 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01004628 case PSA_KEY_DERIVATION_INPUT_SECRET:
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004629 return( psa_tls12_prf_set_key( prf, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01004630 case PSA_KEY_DERIVATION_INPUT_LABEL:
4631 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01004632 default:
4633 return( PSA_ERROR_INVALID_ARGUMENT );
4634 }
4635}
John Durkop07cc04a2020-11-16 22:08:34 -08004636#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4637 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4638
4639#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4640static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4641 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004642 const uint8_t *data,
4643 size_t data_length )
4644{
4645 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004646 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08004647 uint8_t *cur = pms;
4648
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004649 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08004650 return( PSA_ERROR_INVALID_ARGUMENT );
4651
4652 /* Quoting RFC 4279, Section 2:
4653 *
4654 * The premaster secret is formed as follows: if the PSK is N octets
4655 * long, concatenate a uint16 with the value N, N zero octets, a second
4656 * uint16 with the value N, and the PSK itself.
4657 */
4658
Joe Subbiani2bbafda2021-06-24 13:00:03 +01004659 *cur++ = MBEDTLS_BYTE_1( data_length );
4660 *cur++ = MBEDTLS_BYTE_0( data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08004661 memset( cur, 0, data_length );
4662 cur += data_length;
4663 *cur++ = pms[0];
4664 *cur++ = pms[1];
4665 memcpy( cur, data, data_length );
4666 cur += data_length;
4667
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004668 status = psa_tls12_prf_set_key( prf, pms, cur - pms );
John Durkop07cc04a2020-11-16 22:08:34 -08004669
4670 mbedtls_platform_zeroize( pms, sizeof( pms ) );
4671 return( status );
4672}
Janos Follath6660f0e2019-06-17 08:44:03 +01004673
4674static psa_status_t psa_tls12_prf_psk_to_ms_input(
4675 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004676 psa_key_derivation_step_t step,
4677 const uint8_t *data,
4678 size_t data_length )
4679{
4680 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01004681 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004682 return( psa_tls12_prf_psk_to_ms_set_key( prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004683 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01004684 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004685
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004686 return( psa_tls12_prf_input( prf, step, data, data_length ) );
Janos Follath6660f0e2019-06-17 08:44:03 +01004687}
John Durkop07cc04a2020-11-16 22:08:34 -08004688#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004689
Gilles Peskineb8965192019-09-24 16:21:10 +02004690/** Check whether the given key type is acceptable for the given
4691 * input step of a key derivation.
4692 *
4693 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4694 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4695 * Both secret and non-secret inputs can alternatively have the type
4696 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4697 * that the input was passed as a buffer rather than via a key object.
4698 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004699static int psa_key_derivation_check_input_type(
4700 psa_key_derivation_step_t step,
4701 psa_key_type_t key_type )
4702{
4703 switch( step )
4704 {
4705 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02004706 if( key_type == PSA_KEY_TYPE_DERIVE )
4707 return( PSA_SUCCESS );
4708 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004709 return( PSA_SUCCESS );
4710 break;
4711 case PSA_KEY_DERIVATION_INPUT_LABEL:
4712 case PSA_KEY_DERIVATION_INPUT_SALT:
4713 case PSA_KEY_DERIVATION_INPUT_INFO:
4714 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02004715 if( key_type == PSA_KEY_TYPE_RAW_DATA )
4716 return( PSA_SUCCESS );
4717 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004718 return( PSA_SUCCESS );
4719 break;
4720 }
4721 return( PSA_ERROR_INVALID_ARGUMENT );
4722}
4723
Janos Follathb80a94e2019-06-12 15:54:46 +01004724static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004726 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004727 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004728 const uint8_t *data,
4729 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004730{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004731 psa_status_t status;
4732 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
4733
4734 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02004735 if( status != PSA_SUCCESS )
4736 goto exit;
4737
John Durkopd0321952020-10-29 21:37:36 -07004738#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004739 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004740 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004742 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004743 step, data, data_length );
4744 }
John Durkop07cc04a2020-11-16 22:08:34 -08004745 else
4746#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4747#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4748 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004749 {
Janos Follathb03233e2019-06-11 15:30:30 +01004750 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
Janos Follathb03233e2019-06-11 15:30:30 +01004751 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01004752 }
John Durkop07cc04a2020-11-16 22:08:34 -08004753 else
4754#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4755#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4756 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01004757 {
4758 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004759 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004760 }
4761 else
John Durkop07cc04a2020-11-16 22:08:34 -08004762#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004763 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004764 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004765 (void) data;
4766 (void) data_length;
4767 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004768 return( PSA_ERROR_BAD_STATE );
4769 }
4770
Gilles Peskine224b0d62019-09-23 18:13:17 +02004771exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004772 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004774 return( status );
4775}
4776
Janos Follath51f4a0f2019-06-14 11:35:55 +01004777psa_status_t psa_key_derivation_input_bytes(
4778 psa_key_derivation_operation_t *operation,
4779 psa_key_derivation_step_t step,
4780 const uint8_t *data,
4781 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004782{
Gilles Peskineb8965192019-09-24 16:21:10 +02004783 return( psa_key_derivation_input_internal( operation, step,
4784 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01004785 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004786}
4787
Janos Follath51f4a0f2019-06-14 11:35:55 +01004788psa_status_t psa_key_derivation_input_key(
4789 psa_key_derivation_operation_t *operation,
4790 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004791 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004792{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004793 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004794 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004795 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004796
Ronald Cron5c522922020-11-14 16:35:34 +01004797 status = psa_get_and_lock_transparent_key_slot_with_policy(
4798 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004799 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02004800 {
4801 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004802 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02004803 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004804
4805 /* Passing a key object as a SECRET input unlocks the permission
4806 * to output to a key object. */
4807 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4808 operation->can_output_key = 1;
4809
Ronald Cronf95a2b12020-10-22 15:24:49 +02004810 status = psa_key_derivation_input_internal( operation,
4811 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004812 slot->key.data,
4813 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004814
Ronald Cron5c522922020-11-14 16:35:34 +01004815 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004816
Ronald Cron5c522922020-11-14 16:35:34 +01004817 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004818}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004819
4820
4821
4822/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004823/* Key agreement */
4824/****************************************************************/
4825
John Durkop6ba40d12020-11-10 08:50:04 -08004826#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004827static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4828 size_t peer_key_length,
4829 const mbedtls_ecp_keypair *our_key,
4830 uint8_t *shared_secret,
4831 size_t shared_secret_size,
4832 size_t *shared_secret_length )
4833{
Steven Cooremand4867872020-08-05 16:31:39 +02004834 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004835 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004836 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004837 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01004838 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004839 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004840
Ronald Cron90857082020-11-25 14:58:33 +01004841 status = mbedtls_psa_ecp_load_representation(
4842 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004843 bits,
Ronald Cron90857082020-11-25 14:58:33 +01004844 peer_key,
4845 peer_key_length,
4846 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004847 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004848 goto exit;
4849
Jaeden Amero97271b32019-01-10 19:38:51 +00004850 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02004851 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00004852 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004853 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004854 status = mbedtls_to_psa_error(
4855 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4856 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004857 goto exit;
4858
Jaeden Amero97271b32019-01-10 19:38:51 +00004859 status = mbedtls_to_psa_error(
4860 mbedtls_ecdh_calc_secret( &ecdh,
4861 shared_secret_length,
4862 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004863 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004864 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004865 if( status != PSA_SUCCESS )
4866 goto exit;
4867 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
4868 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004869
4870exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01004871 if( status != PSA_SUCCESS )
4872 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004873 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02004874 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02004875 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02004876
Jaeden Amero97271b32019-01-10 19:38:51 +00004877 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004878}
John Durkop6ba40d12020-11-10 08:50:04 -08004879#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004880
Gilles Peskine01d718c2018-09-18 12:01:02 +02004881#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4882
Gilles Peskine0216fe12019-04-11 21:23:21 +02004883static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4884 psa_key_slot_t *private_key,
4885 const uint8_t *peer_key,
4886 size_t peer_key_length,
4887 uint8_t *shared_secret,
4888 size_t shared_secret_size,
4889 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004890{
Gilles Peskine0216fe12019-04-11 21:23:21 +02004891 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004892 {
John Durkop6ba40d12020-11-10 08:50:04 -08004893#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004894 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02004895 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004896 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02004897 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004898 psa_status_t status = mbedtls_psa_ecp_load_representation(
4899 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004900 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01004901 private_key->key.data,
4902 private_key->key.bytes,
4903 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004904 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004905 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02004906 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02004907 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004908 shared_secret, shared_secret_size,
4909 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004910 mbedtls_ecp_keypair_free( ecp );
4911 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02004912 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08004913#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004914 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004915 (void) private_key;
4916 (void) peer_key;
4917 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004918 (void) shared_secret;
4919 (void) shared_secret_size;
4920 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004921 return( PSA_ERROR_NOT_SUPPORTED );
4922 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004923}
4924
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004925/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004926 * to potentially free embedded data structures and wipe confidential data.
4927 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004929 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004930 psa_key_slot_t *private_key,
4931 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004932 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004933{
4934 psa_status_t status;
4935 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4936 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004937 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004938
4939 /* Step 1: run the secret agreement algorithm to generate the shared
4940 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004941 status = psa_key_agreement_raw_internal( ka_alg,
4942 private_key,
4943 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03004944 shared_secret,
4945 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02004946 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004947 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004948 goto exit;
4949
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004950 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02004951 * the shared secret. A shared secret is permitted wherever a key
4952 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01004953 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004954 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01004955 shared_secret,
4956 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004957exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004958 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004959 return( status );
4960}
4961
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004963 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004964 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004965 const uint8_t *peer_key,
4966 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004967{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004969 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004970 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004971
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004973 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01004974 status = psa_get_and_lock_transparent_key_slot_with_policy(
4975 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004976 if( status != PSA_SUCCESS )
4977 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004978 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004979 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004980 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004981 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004983 else
4984 {
4985 /* If a private key has been added as SECRET, we allow the derived
4986 * key material to be used as a key in PSA Crypto. */
4987 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4988 operation->can_output_key = 1;
4989 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004990
Ronald Cron5c522922020-11-14 16:35:34 +01004991 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004992
Ronald Cron5c522922020-11-14 16:35:34 +01004993 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004994}
4995
Gilles Peskinebe697d82019-05-16 18:00:41 +02004996psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004997 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02004998 const uint8_t *peer_key,
4999 size_t peer_key_length,
5000 uint8_t *output,
5001 size_t output_size,
5002 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005003{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005004 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005005 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005006 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005007
5008 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5009 {
5010 status = PSA_ERROR_INVALID_ARGUMENT;
5011 goto exit;
5012 }
Ronald Cron5c522922020-11-14 16:35:34 +01005013 status = psa_get_and_lock_transparent_key_slot_with_policy(
5014 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005015 if( status != PSA_SUCCESS )
5016 goto exit;
5017
Gilles Peskine42313fb2022-04-14 00:17:15 +02005018 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5019 * for the output size. The PSA specification only guarantees that this
5020 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5021 * but it might be nice to allow smaller buffers if the output fits.
5022 * At the time of writing this comment, with only ECDH implemented,
5023 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5024 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5025 * be exact for it as well. */
5026 size_t expected_length =
5027 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits );
5028 if( output_size < expected_length )
5029 {
5030 status = PSA_ERROR_BUFFER_TOO_SMALL;
5031 goto exit;
5032 }
5033
Gilles Peskine0216fe12019-04-11 21:23:21 +02005034 status = psa_key_agreement_raw_internal( alg, slot,
5035 peer_key, peer_key_length,
5036 output, output_size,
5037 output_length );
5038
5039exit:
5040 if( status != PSA_SUCCESS )
5041 {
5042 /* If an error happens and is not handled properly, the output
5043 * may be used as a key to protect sensitive data. Arrange for such
5044 * a key to be random, which is likely to result in decryption or
5045 * verification errors. This is better than filling the buffer with
5046 * some constant data such as zeros, which would result in the data
5047 * being protected with a reproducible, easily knowable key.
5048 */
5049 psa_generate_random( output, output_size );
5050 *output_length = output_size;
5051 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005052
Ronald Cron5c522922020-11-14 16:35:34 +01005053 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005054
Ronald Cron5c522922020-11-14 16:35:34 +01005055 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005056}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005057
5058
Gilles Peskine30524eb2020-11-13 17:02:26 +01005059
Gilles Peskine86a440b2018-11-12 18:39:40 +01005060/****************************************************************/
5061/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005062/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005063
Gilles Peskine30524eb2020-11-13 17:02:26 +01005064/** Initialize the PSA random generator.
5065 */
5066static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5067{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005068#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5069 memset( rng, 0, sizeof( *rng ) );
5070#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5071
Gilles Peskine30524eb2020-11-13 17:02:26 +01005072 /* Set default configuration if
5073 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5074 if( rng->entropy_init == NULL )
5075 rng->entropy_init = mbedtls_entropy_init;
5076 if( rng->entropy_free == NULL )
5077 rng->entropy_free = mbedtls_entropy_free;
5078
5079 rng->entropy_init( &rng->entropy );
5080#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5081 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5082 /* The PSA entropy injection feature depends on using NV seed as an entropy
5083 * source. Add NV seed as an entropy source for PSA entropy injection. */
5084 mbedtls_entropy_add_source( &rng->entropy,
5085 mbedtls_nv_seed_poll, NULL,
5086 MBEDTLS_ENTROPY_BLOCK_SIZE,
5087 MBEDTLS_ENTROPY_SOURCE_STRONG );
5088#endif
5089
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005090 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005091#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005092}
5093
5094/** Deinitialize the PSA random generator.
5095 */
5096static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5097{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005098#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5099 memset( rng, 0, sizeof( *rng ) );
5100#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005101 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005102 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005103#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005104}
5105
5106/** Seed the PSA random generator.
5107 */
5108static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5109{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005110#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5111 /* Do nothing: the external RNG seeds itself. */
5112 (void) rng;
5113 return( PSA_SUCCESS );
5114#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005115 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005116 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5117 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005118 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005119#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005120}
5121
Gilles Peskinee59236f2018-01-27 23:32:46 +01005122psa_status_t psa_generate_random( uint8_t *output,
5123 size_t output_size )
5124{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005125 GUARD_MODULE_INITIALIZED;
5126
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005127#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5128
5129 size_t output_length = 0;
5130 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5131 output, output_size,
5132 &output_length );
5133 if( status != PSA_SUCCESS )
5134 return( status );
5135 /* Breaking up a request into smaller chunks is currently not supported
5136 * for the extrernal RNG interface. */
5137 if( output_length != output_size )
5138 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5139 return( PSA_SUCCESS );
5140
5141#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5142
Gilles Peskine71ddab92021-01-04 21:01:07 +01005143 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005144 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005145 size_t request_size =
5146 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5147 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5148 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005149 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
5150 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02005151 if( ret != 0 )
5152 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01005153 output_size -= request_size;
5154 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005155 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005156 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005157#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005158}
5159
Gilles Peskine8814fc42020-12-14 15:33:44 +01005160/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005161 *
5162 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5163 * `psa_generate_random(...)`. The state parameter is ignored since the
5164 * PSA API doesn't support passing an explicit state.
5165 *
5166 * In the non-external case, psa_generate_random() calls an
5167 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5168 * and semantics as mbedtls_psa_get_random(). As an optimization,
5169 * instead of doing this back-and-forth between the PSA API and the
5170 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5171 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005172 */
5173#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5174int mbedtls_psa_get_random( void *p_rng,
5175 unsigned char *output,
5176 size_t output_size )
5177{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005178 /* This function takes a pointer to the RNG state because that's what
5179 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5180 * its own state internally and doesn't let the caller access that state.
5181 * So we just ignore the state parameter, and in practice we'll pass
5182 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005183 (void) p_rng;
5184 psa_status_t status = psa_generate_random( output, output_size );
5185 if( status == PSA_SUCCESS )
5186 return( 0 );
5187 else
5188 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
5189}
5190#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5191
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005192#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5193#include "mbedtls/entropy_poll.h"
5194
Gilles Peskine7228da22019-07-15 11:06:15 +02005195psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005196 size_t seed_size )
5197{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005198 if( global_data.initialized )
5199 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005200
5201 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5202 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5203 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5204 return( PSA_ERROR_INVALID_ARGUMENT );
5205
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005206 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005207}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005208#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005209
Ronald Cron3772afe2021-02-08 16:10:05 +01005210/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005211 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005212 * \param type The key type
5213 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005214 *
5215 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005216 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005217 * \retval #PSA_ERROR_INVALID_ARGUMENT
5218 * The size in bits of the key is not valid.
5219 * \retval #PSA_ERROR_NOT_SUPPORTED
5220 * The type and/or the size in bits of the key or the combination of
5221 * the two is not supported.
5222 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005223static psa_status_t psa_validate_key_type_and_size_for_key_generation(
5224 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005225{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005227
Gilles Peskinee59236f2018-01-27 23:32:46 +01005228 if( key_type_is_raw_bytes( type ) )
5229 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02005230 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005231 if( status != PSA_SUCCESS )
5232 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005233 }
5234 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005235#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005236 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5237 {
5238 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5239 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005240
Ronald Cron01b2aba2020-10-05 09:42:02 +02005241 /* Accept only byte-aligned keys, for the same reasons as
5242 * in psa_import_rsa_key(). */
5243 if( bits % 8 != 0 )
5244 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005245 }
5246 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005247#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005248
Ronald Cron5c4d3862020-12-07 11:07:24 +01005249#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005250 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5251 {
Ronald Crond81ab562021-02-16 09:01:16 +01005252 /* To avoid empty block, return successfully here. */
5253 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005254 }
5255 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005256#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005257 {
5258 return( PSA_ERROR_NOT_SUPPORTED );
5259 }
5260
5261 return( PSA_SUCCESS );
5262}
5263
Ronald Cron55ed0592020-10-05 10:30:40 +02005264psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005265 const psa_key_attributes_t *attributes,
5266 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005267{
5268 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005269 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005270
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005271 if( ( attributes->domain_parameters == NULL ) &&
5272 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005273 return( PSA_ERROR_INVALID_ARGUMENT );
5274
Ronald Cron01b2aba2020-10-05 09:42:02 +02005275 if( key_type_is_raw_bytes( type ) )
5276 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005277 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005278 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005279 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005280
David Brown12ca5032021-02-11 11:02:00 -07005281#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005282 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005283 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07005284#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005285 }
5286 else
5287
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005288#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5289 defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005290 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005291 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01005292 return( mbedtls_psa_rsa_generate_key( attributes,
5293 key_buffer,
5294 key_buffer_size,
5295 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005296 }
5297 else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005298#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5299 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005300
John Durkop9814fa22020-11-04 12:28:15 -08005301#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005302 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005303 {
Ronald Cron7023db52020-11-20 18:17:42 +01005304 return( mbedtls_psa_ecp_generate_key( attributes,
5305 key_buffer,
5306 key_buffer_size,
5307 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005308 }
5309 else
John Durkop9814fa22020-11-04 12:28:15 -08005310#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005311 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005312 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005313 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02005314 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005315
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005316 return( PSA_SUCCESS );
5317}
Darryl Green0c6575a2018-11-07 16:05:30 +00005318
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005319psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005320 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005321{
5322 psa_status_t status;
5323 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005324 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005325 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005326
Ronald Cron81709fc2020-11-14 12:10:32 +01005327 *key = MBEDTLS_SVC_KEY_ID_INIT;
5328
Gilles Peskine0f84d622019-09-12 19:03:13 +02005329 /* Reject any attempt to create a zero-length key so that we don't
5330 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5331 if( psa_get_key_bits( attributes ) == 0 )
5332 return( PSA_ERROR_INVALID_ARGUMENT );
5333
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005334 /* Reject any attempt to create a public key. */
5335 if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) )
5336 return( PSA_ERROR_INVALID_ARGUMENT );
5337
Ronald Cron81709fc2020-11-14 12:10:32 +01005338 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
5339 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005340 if( status != PSA_SUCCESS )
5341 goto exit;
5342
Ronald Cron977c2472020-10-13 08:32:21 +02005343 /* In the case of a transparent key or an opaque key stored in local
5344 * storage (thus not in the case of generating a key in a secure element
5345 * or cryptoprocessor with storage), we have to allocate a buffer to
5346 * hold the generated key material. */
5347 if( slot->key.data == NULL )
5348 {
5349 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
5350 PSA_KEY_LOCATION_LOCAL_STORAGE )
5351 {
Ronald Cron3772afe2021-02-08 16:10:05 +01005352 status = psa_validate_key_type_and_size_for_key_generation(
5353 attributes->core.type, attributes->core.bits );
5354 if( status != PSA_SUCCESS )
5355 goto exit;
5356
5357 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
5358 attributes->core.type,
5359 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02005360 }
5361 else
5362 {
5363 status = psa_driver_wrapper_get_key_buffer_size(
5364 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01005365 if( status != PSA_SUCCESS )
5366 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02005367 }
Ronald Cron977c2472020-10-13 08:32:21 +02005368
5369 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
5370 if( status != PSA_SUCCESS )
5371 goto exit;
5372 }
5373
Steven Cooreman2a1664c2020-07-20 15:33:08 +02005374 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02005375 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02005376
Ronald Cron2b56bc82020-10-05 10:02:26 +02005377 if( status != PSA_SUCCESS )
5378 psa_remove_key_data_from_memory( slot );
5379
Gilles Peskine11792082019-08-06 18:36:36 +02005380exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005381 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005382 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005383 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005384 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005385
Darryl Green0c6575a2018-11-07 16:05:30 +00005386 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005387}
5388
Gilles Peskinee59236f2018-01-27 23:32:46 +01005389/****************************************************************/
5390/* Module setup */
5391/****************************************************************/
5392
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005393#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005394psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5395 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5396 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5397{
5398 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5399 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005400 global_data.rng.entropy_init = entropy_init;
5401 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01005402 return( PSA_SUCCESS );
5403}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005404#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005405
Gilles Peskinee59236f2018-01-27 23:32:46 +01005406void mbedtls_psa_crypto_free( void )
5407{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005408 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005409 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5410 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005411 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005412 }
5413 /* Wipe all remaining data, including configuration.
5414 * In particular, this sets all state indicator to the value
5415 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005416 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Ronald Cron088d5d02021-04-10 16:57:30 +02005417
5418 /* Terminate drivers */
5419 psa_driver_wrapper_free( );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005420}
5421
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005422#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5423/** Recover a transaction that was interrupted by a power failure.
5424 *
5425 * This function is called during initialization, before psa_crypto_init()
5426 * returns. If this function returns a failure status, the initialization
5427 * fails.
5428 */
5429static psa_status_t psa_crypto_recover_transaction(
5430 const psa_crypto_transaction_t *transaction )
5431{
5432 switch( transaction->unknown.type )
5433 {
5434 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5435 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005436 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005437 * is implemented.
5438 * https://github.com/ARMmbed/mbed-crypto/issues/218
5439 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005440 default:
5441 /* We found an unsupported transaction in the storage.
5442 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01005443 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005444 }
5445}
5446#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5447
Gilles Peskinee59236f2018-01-27 23:32:46 +01005448psa_status_t psa_crypto_init( void )
5449{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005450 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005451
Gilles Peskinec6b69072018-11-20 21:42:52 +01005452 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005453 if( global_data.initialized != 0 )
5454 return( PSA_SUCCESS );
5455
Gilles Peskine30524eb2020-11-13 17:02:26 +01005456 /* Initialize and seed the random generator. */
5457 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005458 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01005459 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005460 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005461 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005462 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005463
Gilles Peskine66fb1262018-12-10 16:29:04 +01005464 status = psa_initialize_key_slots( );
5465 if( status != PSA_SUCCESS )
5466 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005467
Ronald Cron088d5d02021-04-10 16:57:30 +02005468 /* Init drivers */
5469 status = psa_driver_wrapper_init( );
Gilles Peskined9348f22019-10-01 15:22:29 +02005470 if( status != PSA_SUCCESS )
5471 goto exit;
Gilles Peskined9348f22019-10-01 15:22:29 +02005472
Gilles Peskine4b734222019-07-24 15:56:31 +02005473#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005474 status = psa_crypto_load_transaction( );
5475 if( status == PSA_SUCCESS )
5476 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005477 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5478 if( status != PSA_SUCCESS )
5479 goto exit;
5480 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005481 }
5482 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5483 {
5484 /* There's no transaction to complete. It's all good. */
5485 status = PSA_SUCCESS;
5486 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005487#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005488
Gilles Peskinec6b69072018-11-20 21:42:52 +01005489 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005490 global_data.initialized = 1;
5491
5492exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005493 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005494 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005495 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005496}
5497
5498#endif /* MBEDTLS_PSA_CRYPTO_C */