blob: 540d6594f10d8bfc5e4a516a4dbcc890e1fcd06a [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
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
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"
36#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010037#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020038#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020039#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020040#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010041#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010042/* Include internal declarations that are useful for implementing persistently
43 * stored keys. */
44#include "psa_crypto_storage.h"
45
Gilles Peskineb2b64d32020-12-14 16:43:58 +010046#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010047
Gilles Peskineb46bef22019-07-30 21:32:04 +020048#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010049#include <stdlib.h>
50#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020052#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#define mbedtls_calloc calloc
54#define mbedtls_free free
55#endif
56
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020059#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000060#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020061#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/blowfish.h"
63#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020064#include "mbedtls/chacha20.h"
65#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/cipher.h"
67#include "mbedtls/ccm.h"
68#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020070#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/error.h"
74#include "mbedtls/gcm.h"
75#include "mbedtls/md2.h"
76#include "mbedtls/md4.h"
77#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010078#include "mbedtls/md.h"
79#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010080#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010082#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000083#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010085#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/sha1.h"
87#include "mbedtls/sha256.h"
88#include "mbedtls/sha512.h"
89#include "mbedtls/xtea.h"
90
Gilles Peskine996deb12018-08-01 15:45:45 +020091#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
92
Gilles Peskine9ef733f2018-02-07 21:05:37 +010093/* constant-time buffer comparison */
94static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
95{
96 size_t i;
97 unsigned char diff = 0;
98
99 for( i = 0; i < n; i++ )
100 diff |= a[i] ^ b[i];
101
102 return( diff );
103}
104
105
106
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107/****************************************************************/
108/* Global data, support functions and library management */
109/****************************************************************/
110
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111static int key_type_is_raw_bytes( psa_key_type_t type )
112{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200113 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200114}
115
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100116/* Values for psa_global_data_t::rng_state */
117#define RNG_NOT_INITIALIZED 0
118#define RNG_INITIALIZED 1
119#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120
Gilles Peskine2d277862018-06-18 15:41:12 +0200121typedef struct
122{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100123 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100124 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100125 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126} psa_global_data_t;
127
128static psa_global_data_t global_data;
129
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100130#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
131mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
132 &global_data.rng.drbg;
133#endif
134
itayzafrir0adf0fc2018-09-06 16:24:41 +0300135#define GUARD_MODULE_INITIALIZED \
136 if( global_data.initialized == 0 ) \
137 return( PSA_ERROR_BAD_STATE );
138
Steven Cooreman01164162020-07-20 15:31:37 +0200139psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100140{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100141 /* Mbed TLS error codes can combine a high-level error code and a
142 * low-level error code. The low-level error usually reflects the
143 * root cause better, so dispatch on that preferably. */
144 int low_level_ret = - ( -ret & 0x007f );
145 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100146 {
147 case 0:
148 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100149
150 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
151 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
152 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
153 return( PSA_ERROR_NOT_SUPPORTED );
154 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
155 return( PSA_ERROR_HARDWARE_FAILURE );
156
157 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
158 return( PSA_ERROR_HARDWARE_FAILURE );
159
Gilles Peskine9a944802018-06-21 09:35:35 +0200160 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
161 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
162 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
163 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
164 case MBEDTLS_ERR_ASN1_INVALID_DATA:
165 return( PSA_ERROR_INVALID_ARGUMENT );
166 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
167 return( PSA_ERROR_INSUFFICIENT_MEMORY );
168 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
169 return( PSA_ERROR_BUFFER_TOO_SMALL );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
Jaeden Amero93e21112019-02-20 13:57:28 +0000181#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000182 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000183#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
184 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
185#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
191 case MBEDTLS_ERR_CCM_BAD_INPUT:
192 return( PSA_ERROR_INVALID_ARGUMENT );
193 case MBEDTLS_ERR_CCM_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
196 return( PSA_ERROR_HARDWARE_FAILURE );
197
Gilles Peskine26869f22019-05-06 15:25:00 +0200198 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200
201 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
202 return( PSA_ERROR_BAD_STATE );
203 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
209 return( PSA_ERROR_INVALID_ARGUMENT );
210 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
211 return( PSA_ERROR_INSUFFICIENT_MEMORY );
212 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
213 return( PSA_ERROR_INVALID_PADDING );
214 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200215 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100216 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
217 return( PSA_ERROR_INVALID_SIGNATURE );
218 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200219 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
221 return( PSA_ERROR_HARDWARE_FAILURE );
222
223 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskine82e57d12020-11-13 21:31:17 +0100226#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
227 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100228 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
229 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
232 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
233 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
236 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100237#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
239 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
240 return( PSA_ERROR_NOT_SUPPORTED );
241 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskinee59236f2018-01-27 23:32:46 +0100244 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
245 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
246 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100248
249 case MBEDTLS_ERR_GCM_AUTH_FAILED:
250 return( PSA_ERROR_INVALID_SIGNATURE );
251 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200252 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskine82e57d12020-11-13 21:31:17 +0100256#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
257 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100258 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
259 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100260 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
261 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
262 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
263 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
264 return( PSA_ERROR_NOT_SUPPORTED );
265 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
266 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
267#endif
268
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
270 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
271 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
272 return( PSA_ERROR_HARDWARE_FAILURE );
273
274 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
275 return( PSA_ERROR_NOT_SUPPORTED );
276 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_MD_ALLOC_FAILED:
279 return( PSA_ERROR_INSUFFICIENT_MEMORY );
280 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
281 return( PSA_ERROR_STORAGE_FAILURE );
282 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
283 return( PSA_ERROR_HARDWARE_FAILURE );
284
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
286 return( PSA_ERROR_STORAGE_FAILURE );
287 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
292 return( PSA_ERROR_BUFFER_TOO_SMALL );
293 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
294 return( PSA_ERROR_INVALID_ARGUMENT );
295 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
298 return( PSA_ERROR_INVALID_ARGUMENT );
299 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100302 case MBEDTLS_ERR_PK_ALLOC_FAILED:
303 return( PSA_ERROR_INSUFFICIENT_MEMORY );
304 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
305 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
310 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
311 return( PSA_ERROR_INVALID_ARGUMENT );
312 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
313 return( PSA_ERROR_NOT_SUPPORTED );
314 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
315 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
316 return( PSA_ERROR_NOT_PERMITTED );
317 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_PK_INVALID_ALG:
320 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
321 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
322 return( PSA_ERROR_NOT_SUPPORTED );
323 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
324 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
Gilles Peskineff2d2002019-05-06 15:26:23 +0200328 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
331 return( PSA_ERROR_NOT_SUPPORTED );
332
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_RSA_INVALID_PADDING:
339 return( PSA_ERROR_INVALID_PADDING );
340 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
341 return( PSA_ERROR_HARDWARE_FAILURE );
342 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
343 return( PSA_ERROR_INVALID_ARGUMENT );
344 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
345 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200346 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
350 return( PSA_ERROR_BUFFER_TOO_SMALL );
351 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100352 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100353 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
354 return( PSA_ERROR_NOT_SUPPORTED );
355 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
356 return( PSA_ERROR_HARDWARE_FAILURE );
357
358 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
359 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
360 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
361 return( PSA_ERROR_HARDWARE_FAILURE );
362
363 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
364 return( PSA_ERROR_INVALID_ARGUMENT );
365 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
366 return( PSA_ERROR_HARDWARE_FAILURE );
367
itayzafrir5c753392018-05-08 11:18:38 +0300368 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300369 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300370 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300371 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
372 return( PSA_ERROR_BUFFER_TOO_SMALL );
373 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
374 return( PSA_ERROR_NOT_SUPPORTED );
375 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
376 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
377 return( PSA_ERROR_INVALID_SIGNATURE );
378 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
379 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100380 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
381 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300382 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
383 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100384
Janos Follatha13b9052019-11-22 12:48:59 +0000385 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
386 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300387
Gilles Peskinee59236f2018-01-27 23:32:46 +0100388 default:
David Saadab4ecc272019-02-14 13:48:10 +0200389 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100390 }
391}
392
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200393
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200394
395
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100396/****************************************************************/
397/* Key management */
398/****************************************************************/
399
Gilles Peskine73167e12019-07-12 23:44:37 +0200400#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
401static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
402{
Gilles Peskine8e338702019-07-30 20:06:31 +0200403 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200404}
405#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
406
John Durkop07cc04a2020-11-16 22:08:34 -0800407/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
408 * current test driver in key_management.c is using this function
409 * when accelerators are used for ECC key pair and public key.
410 * Once that dependency is resolved these guards can be removed.
411 */
John Durkop9814fa22020-11-04 12:28:15 -0800412#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800413 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
414 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
415 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100416mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 size_t bits,
418 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200419{
420 switch( curve )
421 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100422 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100423 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100428 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100430 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100434 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 case 528:
436 if( bits_is_sloppy )
437 return( MBEDTLS_ECP_DP_SECP521R1 );
438 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100439 }
440 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100441
Paul Elliott8ff510a2020-06-02 17:19:28 +0100442 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100444 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100445 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100446 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100448 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100450 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100451 }
452 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100453
Paul Elliott8ff510a2020-06-02 17:19:28 +0100454 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100456 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 256:
460 if( bits_is_sloppy )
461 return( MBEDTLS_ECP_DP_CURVE25519 );
462 break;
463 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100464 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100465 }
466 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100467
Paul Elliott8ff510a2020-06-02 17:19:28 +0100468 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100470 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100471 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100472 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100474 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100475 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100476 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100477 }
478 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200479 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100480
481 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) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800484 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
485 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
486 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200487
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200488static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
489 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490{
491 /* Check that the bit size is acceptable for the key type */
492 switch( type )
493 {
494 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200496 case PSA_KEY_TYPE_DERIVE:
497 break;
David Browne04acc22021-01-26 11:39:16 -0700498#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 case PSA_KEY_TYPE_AES:
500 if( bits != 128 && bits != 192 && bits != 256 )
501 return( PSA_ERROR_INVALID_ARGUMENT );
502 break;
503#endif
David Browne04acc22021-01-26 11:39:16 -0700504#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505 case PSA_KEY_TYPE_CAMELLIA:
506 if( bits != 128 && bits != 192 && bits != 256 )
507 return( PSA_ERROR_INVALID_ARGUMENT );
508 break;
509#endif
David Browne04acc22021-01-26 11:39:16 -0700510#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511 case PSA_KEY_TYPE_DES:
512 if( bits != 64 && bits != 128 && bits != 192 )
513 return( PSA_ERROR_INVALID_ARGUMENT );
514 break;
515#endif
David Browne04acc22021-01-26 11:39:16 -0700516#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517 case PSA_KEY_TYPE_ARC4:
518 if( bits < 8 || bits > 2048 )
519 return( PSA_ERROR_INVALID_ARGUMENT );
520 break;
521#endif
David Brown1bfe4d72021-02-16 12:54:35 -0700522#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200523 case PSA_KEY_TYPE_CHACHA20:
524 if( bits != 256 )
525 return( PSA_ERROR_INVALID_ARGUMENT );
526 break;
527#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528 default:
529 return( PSA_ERROR_NOT_SUPPORTED );
530 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200531 if( bits % 8 != 0 )
532 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200534 return( PSA_SUCCESS );
535}
536
Gilles Peskineb46bef22019-07-30 21:32:04 +0200537/** Return the size of the key in the given slot, in bits.
538 *
539 * \param[in] slot A key slot.
540 *
541 * \return The key size in bits, read from the metadata in the slot.
542 */
543static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
544{
545 return( slot->attr.bits );
546}
547
Steven Cooreman75b74362020-07-28 14:30:13 +0200548/** Try to allocate a buffer to an empty key slot.
549 *
550 * \param[in,out] slot Key slot to attach buffer to.
551 * \param[in] buffer_length Requested size of the buffer.
552 *
553 * \retval #PSA_SUCCESS
554 * The buffer has been successfully allocated.
555 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
556 * Not enough memory was available for allocation.
557 * \retval #PSA_ERROR_ALREADY_EXISTS
558 * Trying to allocate a buffer to a non-empty key slot.
559 */
560static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
561 size_t buffer_length )
562{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100563 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200564 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200565
Ronald Cronea0f8a62020-11-25 17:52:23 +0100566 slot->key.data = mbedtls_calloc( 1, buffer_length );
567 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200568 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200569
Ronald Cronea0f8a62020-11-25 17:52:23 +0100570 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200571 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200572}
573
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200574psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
575 const uint8_t* data,
576 size_t data_length )
577{
578 psa_status_t status = psa_allocate_buffer_to_slot( slot,
579 data_length );
580 if( status != PSA_SUCCESS )
581 return( status );
582
Ronald Cronea0f8a62020-11-25 17:52:23 +0100583 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200584 return( PSA_SUCCESS );
585}
586
Ronald Crona0fe59f2020-11-29 11:14:53 +0100587psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100588 const psa_key_attributes_t *attributes,
589 const uint8_t *data, size_t data_length,
590 uint8_t *key_buffer, size_t key_buffer_size,
591 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
594 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200596 /* zero-length keys are never supported. */
597 if( data_length == 0 )
598 return( PSA_ERROR_NOT_SUPPORTED );
599
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100600 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100602 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200603
Steven Cooreman75b74362020-07-28 14:30:13 +0200604 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
605 if( data_length > SIZE_MAX / 8 )
606 return( PSA_ERROR_NOT_SUPPORTED );
607
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200608 /* Enforce a size limit, and in particular ensure that the bit
609 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100610 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200611 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200612
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100613 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200614 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200615 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200616
Ronald Crondd04d422020-11-28 15:14:42 +0100617 /* Copy the key material. */
618 memcpy( key_buffer, data, data_length );
619 *key_buffer_length = data_length;
620 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200621
Steven Cooreman40120f62020-10-29 11:42:22 +0100622 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100624 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200625 {
John Durkop9814fa22020-11-04 12:28:15 -0800626#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
627 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100628 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200629 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100630 return( mbedtls_psa_ecp_import_key( attributes,
631 data, data_length,
632 key_buffer, key_buffer_size,
633 key_buffer_length,
634 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100635 }
John Durkop9814fa22020-11-04 12:28:15 -0800636#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
637 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700638#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
639 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100640 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200641 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100642 return( mbedtls_psa_rsa_import_key( attributes,
643 data, data_length,
644 key_buffer, key_buffer_size,
645 key_buffer_length,
646 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200647 }
John Durkop9814fa22020-11-04 12:28:15 -0800648#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
649 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100650 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100651
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100652 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100653}
654
Gilles Peskinef603c712019-01-19 13:40:11 +0100655/** Calculate the intersection of two algorithm usage policies.
656 *
657 * Return 0 (which allows no operation) on incompatibility.
658 */
659static psa_algorithm_t psa_key_policy_algorithm_intersection(
660 psa_algorithm_t alg1,
661 psa_algorithm_t alg2 )
662{
Gilles Peskine549ea862019-05-22 11:45:59 +0200663 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100664 if( alg1 == alg2 )
665 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100666 /* If the policies are from the same hash-and-sign family, check
667 * if one is a wildcard. If so the other has the specific algorithm. */
668 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
669 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
670 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100671 {
Steven Cooreman03488022021-02-18 12:07:20 +0100672 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
673 return( alg2 );
674 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
675 return( alg1 );
676 }
677 /* If the policies are from the same AEAD family, check whether
678 * one f them is a minimum-tag-length wildcard. Calculate the most
679 * restrictive tag length. */
680 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
681 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
682 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
683 {
684 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
685 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
686 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100687
Steven Cooreman03488022021-02-18 12:07:20 +0100688 /* If both are wildcards, return most restricitve wildcard */
689 if( ( ( alg1 & PSA_ALG_AEAD_MINIMUM_LENGTH_FLAG ) != 0 ) &&
690 ( ( alg2 & PSA_ALG_AEAD_MINIMUM_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100691 {
Steven Cooreman03488022021-02-18 12:07:20 +0100692 return( PSA_ALG_AEAD_WITH_MINIMUM_LENGTH_TAG( alg1, max_len ) );
693 }
694 /* If only one is a wildcard, return specific algorithm if compatible. */
695 if( ( ( alg1 & PSA_ALG_AEAD_MINIMUM_LENGTH_FLAG ) != 0 ) &&
696 ( alg1_len <= alg2_len ) )
697 {
698 return( alg2 );
699 }
700 if( ( ( alg2 & PSA_ALG_AEAD_MINIMUM_LENGTH_FLAG ) != 0 ) &&
701 ( alg2_len <= alg1_len ) )
702 {
703 return( alg1 );
704 }
705 }
706 /* If the policies are from the same MAC family, check whether one
707 * of them is a minimum-MAC-length policy. Calculate the most
708 * restrictive tag length. */
709 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
710 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
711 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
712 {
713 size_t alg1_len = PSA_MAC_TRUNCATED_LENGTH( alg1 );
714 size_t alg2_len = PSA_MAC_TRUNCATED_LENGTH( alg2 );
715 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100716
Steven Cooreman03488022021-02-18 12:07:20 +0100717 /* If both are wildcards, return most restricitve wildcard */
718 if( ( ( alg1 & PSA_ALG_MAC_MINIMUM_LENGTH_FLAG ) != 0 ) &&
719 ( ( alg2 & PSA_ALG_MAC_MINIMUM_LENGTH_FLAG ) != 0 ) )
720 {
721 return( PSA_ALG_MAC_WITH_MINIMUM_LENGTH_TAG( alg1, max_len ) );
722 }
723 /* If only one is a wildcard, return specific algorithm if compatible.
724 * Special case: specific MAC algorithm with '0' as length means full-
725 * length MAC, which is always allowed by a wildcard with the same
726 * base algorithm. */
727 if( ( ( alg1 & PSA_ALG_MAC_MINIMUM_LENGTH_FLAG ) != 0 ) &&
728 ( ( alg1_len <= alg2_len ) ||
729 ( alg2 == PSA_ALG_FULL_LENGTH_MAC( alg1 ) ) ) )
730 {
731 return( alg2 );
732 }
733 if( ( ( alg2 & PSA_ALG_MAC_MINIMUM_LENGTH_FLAG ) != 0 ) &&
734 ( ( alg2_len <= alg1_len ) ||
735 ( alg1 == PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) )
736 {
737 return( alg1 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100738 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100739 }
740 /* If the policies are incompatible, allow nothing. */
741 return( 0 );
742}
743
Gilles Peskined6f371b2019-05-10 19:33:38 +0200744static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
745 psa_algorithm_t requested_alg )
746{
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100747 /* A requested algorithm cannot be a wildcard. */
748 if( PSA_ALG_IS_WILDCARD( requested_alg ) )
749 return( 0 );
Gilles Peskine549ea862019-05-22 11:45:59 +0200750 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200751 if( requested_alg == policy_alg )
752 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100753 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
754 * and requested_alg is the same hash-and-sign family with any hash,
755 * then requested_alg is compliant with policy_alg. */
756 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
757 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200758 {
Steven Cooreman03488022021-02-18 12:07:20 +0100759 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
760 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
761 }
762 /* If policy_alg is a wildcard AEAD algorithm of the same base as
763 * the requested algorithm, check the requested tag length to be
764 * equal-length or longer than the wildcard-specified length. */
765 if( PSA_ALG_IS_AEAD( policy_alg ) &&
766 PSA_ALG_IS_AEAD( requested_alg ) &&
767 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
768 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
769 ( ( policy_alg & PSA_ALG_AEAD_MINIMUM_LENGTH_FLAG ) != 0 ) )
770 {
771 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
772 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
773 }
774 /* If policy_alg is a wildcard MAC algorithm of the same base as
775 * the requested algorithm, check the requested tag length to be
776 * equal-length or longer than the wildcard-specified length. */
777 if( PSA_ALG_IS_MAC( policy_alg ) &&
778 PSA_ALG_IS_MAC( requested_alg ) &&
779 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
780 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) &&
781 ( ( policy_alg & PSA_ALG_MAC_MINIMUM_LENGTH_FLAG ) != 0 ) )
782 {
783 /* Special case: full-length MAC is encoded with 0-length.
784 * A minimum-length policy will always allow a full-length MAC. */
785 if( PSA_ALG_FULL_LENGTH_MAC( requested_alg ) == requested_alg )
786 return( 1 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100787
Steven Cooreman03488022021-02-18 12:07:20 +0100788 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
789 PSA_MAC_TRUNCATED_LENGTH( requested_alg ) );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200790 }
Steven Cooremance48e852020-10-05 16:02:45 +0200791 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200792 * a key derivation with that key agreement should also be allowed. This
793 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200794 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
795 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
796 {
797 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
798 policy_alg );
799 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100800 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200801 return( 0 );
802}
803
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100804/** Test whether a policy permits an algorithm.
805 *
806 * The caller must test usage flags separately.
807 */
808static int psa_key_policy_permits( const psa_key_policy_t *policy,
809 psa_algorithm_t alg )
810{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200811 return( psa_key_algorithm_permits( policy->alg, alg ) ||
812 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100813}
814
Gilles Peskinef603c712019-01-19 13:40:11 +0100815/** Restrict a key policy based on a constraint.
816 *
817 * \param[in,out] policy The policy to restrict.
818 * \param[in] constraint The policy constraint to apply.
819 *
820 * \retval #PSA_SUCCESS
821 * \c *policy contains the intersection of the original value of
822 * \c *policy and \c *constraint.
823 * \retval #PSA_ERROR_INVALID_ARGUMENT
824 * \c *policy and \c *constraint are incompatible.
825 * \c *policy is unchanged.
826 */
827static psa_status_t psa_restrict_key_policy(
828 psa_key_policy_t *policy,
829 const psa_key_policy_t *constraint )
830{
831 psa_algorithm_t intersection_alg =
832 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833 psa_algorithm_t intersection_alg2 =
834 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100835 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
836 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200837 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
838 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100839 policy->usage &= constraint->usage;
840 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200841 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100842 return( PSA_SUCCESS );
843}
844
Ronald Cron5c522922020-11-14 16:35:34 +0100845/** Get the description of a key given its identifier and policy constraints
846 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200847 *
Ronald Cron5c522922020-11-14 16:35:34 +0100848 * The key must have allow all the usage flags set in \p usage. If \p alg is
849 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +0100850 *
Ronald Cron5c522922020-11-14 16:35:34 +0100851 * In case of a persistent key, the function loads the description of the key
852 * into a key slot if not already done.
853 *
854 * On success, the returned key slot is locked. It is the responsibility of
855 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200856 */
Ronald Cron5c522922020-11-14 16:35:34 +0100857static psa_status_t psa_get_and_lock_key_slot_with_policy(
858 mbedtls_svc_key_id_t key,
859 psa_key_slot_t **p_slot,
860 psa_key_usage_t usage,
861 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100862{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200863 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
864 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100865
Ronald Cron5c522922020-11-14 16:35:34 +0100866 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100867 if( status != PSA_SUCCESS )
868 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200869 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100870
871 /* Enforce that usage policy for the key slot contains all the flags
872 * required by the usage parameter. There is one exception: public
873 * keys can always be exported, so we treat public key objects as
874 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200875 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100876 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200877
878 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +0200879 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200880 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100881
882 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200883 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200884 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +0100885
Darryl Green06fd18d2018-07-16 11:21:11 +0100886 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200887
888error:
889 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100890 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200891
892 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100893}
Darryl Green940d72c2018-07-13 13:18:51 +0100894
Ronald Cron5c522922020-11-14 16:35:34 +0100895/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200896 *
897 * A transparent key is a key for which the key material is directly
898 * available, as opposed to a key in a secure element.
899 *
Ronald Cron5c522922020-11-14 16:35:34 +0100900 * This is a temporary function to use instead of
901 * psa_get_and_lock_key_slot_with_policy() until secure element support is
902 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200903 *
Ronald Cron5c522922020-11-14 16:35:34 +0100904 * On success, the returned key slot is locked. It is the responsibility of the
905 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200906 */
907#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +0100908static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
909 mbedtls_svc_key_id_t key,
910 psa_key_slot_t **p_slot,
911 psa_key_usage_t usage,
912 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200913{
Ronald Cron5c522922020-11-14 16:35:34 +0100914 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
915 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200916 if( status != PSA_SUCCESS )
917 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200918
Gilles Peskineadad8132019-07-25 11:31:23 +0200919 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200920 {
Ronald Cron5c522922020-11-14 16:35:34 +0100921 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200922 *p_slot = NULL;
923 return( PSA_ERROR_NOT_SUPPORTED );
924 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200925
Gilles Peskine28f8f302019-07-24 13:30:31 +0200926 return( PSA_SUCCESS );
927}
928#else /* MBEDTLS_PSA_CRYPTO_SE_C */
929/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +0100930#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
931 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200932#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
933
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100934/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100935static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000936{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100937 /* Data pointer will always be either a valid pointer or NULL in an
938 * initialized slot, so we can just free it. */
939 if( slot->key.data != NULL )
940 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
941
942 mbedtls_free( slot->key.data );
943 slot->key.data = NULL;
944 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000945
946 return( PSA_SUCCESS );
947}
948
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100949/** Completely wipe a slot in memory, including its policy.
950 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100951psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100952{
953 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +0100954
955 /*
956 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100957 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +0100958 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
959 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +0100960 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +0100961 */
Ronald Cron5c522922020-11-14 16:35:34 +0100962 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +0100963 {
964#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +0100965 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +0100966#endif
Ronald Cronddd3d052020-10-30 14:07:07 +0100967 status = PSA_ERROR_CORRUPTION_DETECTED;
968 }
969
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200970 /* Multipart operations may still be using the key. This is safe
971 * because all multipart operation objects are independent from
972 * the key slot: if they need to access the key after the setup
973 * phase, they have a copy of the key. Note that this means that
974 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100975 /* At this point, key material and other type-specific content has
976 * been wiped. Clear remaining metadata. We can call memset and not
977 * zeroize because the metadata is not particularly sensitive. */
978 memset( slot, 0, sizeof( *slot ) );
979 return( status );
980}
981
Ronald Croncf56a0a2020-08-04 09:51:30 +0200982psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100984 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +0200985 psa_status_t status; /* status of the last operation */
986 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200987#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
988 psa_se_drv_table_entry_t *driver;
989#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990
Ronald Croncf56a0a2020-08-04 09:51:30 +0200991 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200992 return( PSA_SUCCESS );
993
Ronald Cronf2911112020-10-29 17:51:10 +0100994 /*
Ronald Cron19daca92020-11-10 18:08:03 +0100995 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +0100996 * key, this will load the key description from persistent memory if not
997 * done yet. We cannot avoid this loading as without it we don't know if
998 * the key is operated by an SE or not and this information is needed by
999 * the current implementation.
1000 */
Ronald Cron5c522922020-11-14 16:35:34 +01001001 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001002 if( status != PSA_SUCCESS )
1003 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001004
Ronald Cronf2911112020-10-29 17:51:10 +01001005 /*
1006 * If the key slot containing the key description is under access by the
1007 * library (apart from the present access), the key cannot be destroyed
1008 * yet. For the time being, just return in error. Eventually (to be
1009 * implemented), the key should be destroyed when all accesses have
1010 * stopped.
1011 */
Ronald Cron5c522922020-11-14 16:35:34 +01001012 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001013 {
Ronald Cron5c522922020-11-14 16:35:34 +01001014 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001015 return( PSA_ERROR_GENERIC_ERROR );
1016 }
1017
Gilles Peskine354f7672019-07-12 23:46:38 +02001018#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001019 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001020 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001021 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001022 /* For a key in a secure element, we need to do three things:
1023 * remove the key file in internal storage, destroy the
1024 * key inside the secure element, and update the driver's
1025 * persistent data. Start a transaction that will encompass these
1026 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001027 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001028 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001029 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001030 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001031 status = psa_crypto_save_transaction( );
1032 if( status != PSA_SUCCESS )
1033 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001034 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001035 /* We should still try to destroy the key in the secure
1036 * element and the key metadata in storage. This is especially
1037 * important if the error is that the storage is full.
1038 * But how to do it exactly without risking an inconsistent
1039 * state after a reset?
1040 * https://github.com/ARMmbed/mbed-crypto/issues/215
1041 */
1042 overall_status = status;
1043 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001044 }
1045
Ronald Cronea0f8a62020-11-25 17:52:23 +01001046 status = psa_destroy_se_key( driver,
1047 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001048 if( overall_status == PSA_SUCCESS )
1049 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001050 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001051#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1052
Darryl Greend49a4992018-06-18 17:27:26 +01001053#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001054 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001055 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001056 status = psa_destroy_persistent_key( slot->attr.id );
1057 if( overall_status == PSA_SUCCESS )
1058 overall_status = status;
1059
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001060 /* TODO: other slots may have a copy of the same key. We should
1061 * invalidate them.
1062 * https://github.com/ARMmbed/mbed-crypto/issues/214
1063 */
Darryl Greend49a4992018-06-18 17:27:26 +01001064 }
1065#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001066
Gilles Peskinefc762652019-07-22 19:30:34 +02001067#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1068 if( driver != NULL )
1069 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001070 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001071 if( overall_status == PSA_SUCCESS )
1072 overall_status = status;
1073 status = psa_crypto_stop_transaction( );
1074 if( overall_status == PSA_SUCCESS )
1075 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001076 }
1077#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1078
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001079#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1080exit:
1081#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001082 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001083 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1084 if( overall_status == PSA_SUCCESS )
1085 overall_status = status;
1086 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087}
1088
John Durkop07cc04a2020-11-16 22:08:34 -08001089#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001090 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001091static psa_status_t psa_get_rsa_public_exponent(
1092 const mbedtls_rsa_context *rsa,
1093 psa_key_attributes_t *attributes )
1094{
1095 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001097 uint8_t *buffer = NULL;
1098 size_t buflen;
1099 mbedtls_mpi_init( &mpi );
1100
1101 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1102 if( ret != 0 )
1103 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001104 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1105 {
1106 /* It's the default value, which is reported as an empty string,
1107 * so there's nothing to do. */
1108 goto exit;
1109 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001110
1111 buflen = mbedtls_mpi_size( &mpi );
1112 buffer = mbedtls_calloc( 1, buflen );
1113 if( buffer == NULL )
1114 {
1115 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1116 goto exit;
1117 }
1118 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1119 if( ret != 0 )
1120 goto exit;
1121 attributes->domain_parameters = buffer;
1122 attributes->domain_parameters_size = buflen;
1123
1124exit:
1125 mbedtls_mpi_free( &mpi );
1126 if( ret != 0 )
1127 mbedtls_free( buffer );
1128 return( mbedtls_to_psa_error( ret ) );
1129}
John Durkop07cc04a2020-11-16 22:08:34 -08001130#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001131 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001132
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001133/** Retrieve all the publicly-accessible attributes of a key.
1134 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001135psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001136 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001139 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001140 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001141
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001142 psa_reset_key_attributes( attributes );
1143
Ronald Cron5c522922020-11-14 16:35:34 +01001144 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001145 if( status != PSA_SUCCESS )
1146 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001147
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001148 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001149 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1150 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1151
1152#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1153 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001154 psa_set_key_slot_number( attributes,
1155 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001156#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001157
Gilles Peskine8e338702019-07-30 20:06:31 +02001158 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001159 {
John Durkop07cc04a2020-11-16 22:08:34 -08001160#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001161 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001162 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001163 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001164#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001165 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001166 * is not yet implemented.
1167 * https://github.com/ARMmbed/mbed-crypto/issues/216
1168 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001169 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001170 break;
1171#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001172 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001173 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001174
Ronald Cron90857082020-11-25 14:58:33 +01001175 status = mbedtls_psa_rsa_load_representation(
1176 slot->attr.type,
1177 slot->key.data,
1178 slot->key.bytes,
1179 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001180 if( status != PSA_SUCCESS )
1181 break;
1182
Steven Cooremana2371e52020-07-28 14:30:39 +02001183 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001184 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001185 mbedtls_rsa_free( rsa );
1186 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001187 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001188 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001189#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001190 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001191 default:
1192 /* Nothing else to do. */
1193 break;
1194 }
1195
1196 if( status != PSA_SUCCESS )
1197 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001198
Ronald Cron5c522922020-11-14 16:35:34 +01001199 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001200
Ronald Cron5c522922020-11-14 16:35:34 +01001201 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001202}
1203
Gilles Peskinec8000c02019-08-02 20:15:51 +02001204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1205psa_status_t psa_get_key_slot_number(
1206 const psa_key_attributes_t *attributes,
1207 psa_key_slot_number_t *slot_number )
1208{
1209 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1210 {
1211 *slot_number = attributes->slot_number;
1212 return( PSA_SUCCESS );
1213 }
1214 else
1215 return( PSA_ERROR_INVALID_ARGUMENT );
1216}
1217#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1218
Ronald Crond18b5f82020-11-25 16:46:05 +01001219static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1220 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001221 uint8_t *data,
1222 size_t data_size,
1223 size_t *data_length )
1224{
Ronald Crond18b5f82020-11-25 16:46:05 +01001225 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001226 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001227 memcpy( data, key_buffer, key_buffer_size );
1228 memset( data + key_buffer_size, 0,
1229 data_size - key_buffer_size );
1230 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001231 return( PSA_SUCCESS );
1232}
1233
Ronald Cron7285cda2020-11-26 14:46:37 +01001234psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001235 const psa_key_attributes_t *attributes,
1236 const uint8_t *key_buffer, size_t key_buffer_size,
1237 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001238{
Ronald Crond18b5f82020-11-25 16:46:05 +01001239 psa_key_type_t type = attributes->core.type;
1240
Ronald Crond18b5f82020-11-25 16:46:05 +01001241 if( key_type_is_raw_bytes( type ) ||
1242 PSA_KEY_TYPE_IS_RSA( type ) ||
1243 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001244 {
1245 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001246 key_buffer, key_buffer_size,
1247 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001248 }
1249 else
1250 {
1251 /* This shouldn't happen in the reference implementation, but
1252 it is valid for a special-purpose implementation to omit
1253 support for exporting certain key types. */
1254 return( PSA_ERROR_NOT_SUPPORTED );
1255 }
1256}
1257
1258psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1259 uint8_t *data,
1260 size_t data_size,
1261 size_t *data_length )
1262{
1263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1264 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1265 psa_key_slot_t *slot;
1266
Ronald Crone907e552021-01-18 13:22:38 +01001267 /* Reject a zero-length output buffer now, since this can never be a
1268 * valid key representation. This way we know that data must be a valid
1269 * pointer and we can do things like memset(data, ..., data_size). */
1270 if( data_size == 0 )
1271 return( PSA_ERROR_BUFFER_TOO_SMALL );
1272
Ronald Cron9486f9d2020-11-26 13:36:23 +01001273 /* Set the key to empty now, so that even when there are errors, we always
1274 * set data_length to a value between 0 and data_size. On error, setting
1275 * the key to empty is a good choice because an empty key representation is
1276 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001277 *data_length = 0;
1278
Ronald Cron9486f9d2020-11-26 13:36:23 +01001279 /* Export requires the EXPORT flag. There is an exception for public keys,
1280 * which don't require any flag, but
1281 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1282 */
1283 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1284 PSA_KEY_USAGE_EXPORT, 0 );
1285 if( status != PSA_SUCCESS )
1286 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001287
Ronald Crond18b5f82020-11-25 16:46:05 +01001288 psa_key_attributes_t attributes = {
1289 .core = slot->attr
1290 };
Ronald Cron67227982020-11-26 15:16:05 +01001291 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001292 slot->key.data, slot->key.bytes,
1293 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001294
Ronald Cron9486f9d2020-11-26 13:36:23 +01001295 unlock_status = psa_unlock_key_slot( slot );
1296
1297 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1298}
1299
Ronald Cron7285cda2020-11-26 14:46:37 +01001300psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001301 const psa_key_attributes_t *attributes,
1302 const uint8_t *key_buffer,
1303 size_t key_buffer_size,
1304 uint8_t *data,
1305 size_t data_size,
1306 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001307{
Ronald Crond18b5f82020-11-25 16:46:05 +01001308 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001309
Ronald Crond18b5f82020-11-25 16:46:05 +01001310 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001311 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001312 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001313 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001314 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001315 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001316 key_buffer, key_buffer_size,
1317 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001318 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001319
Ronald Crond18b5f82020-11-25 16:46:05 +01001320 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001321 {
John Durkop0e005192020-10-31 22:06:54 -07001322#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1323 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001324 return( mbedtls_psa_rsa_export_public_key( attributes,
1325 key_buffer,
1326 key_buffer_size,
1327 data,
1328 data_size,
1329 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001330#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001331 /* We don't know how to convert a private RSA key to public. */
1332 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001333#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1334 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001335 }
1336 else
Moran Pekera998bc62018-04-16 18:16:20 +03001337 {
John Durkop6ba40d12020-11-10 08:50:04 -08001338#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1339 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001340 return( mbedtls_psa_ecp_export_public_key( attributes,
1341 key_buffer,
1342 key_buffer_size,
1343 data,
1344 data_size,
1345 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001346#else
1347 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001348 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001349#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1350 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001351 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001353 else
1354 {
1355 /* This shouldn't happen in the reference implementation, but
1356 it is valid for a special-purpose implementation to omit
1357 support for exporting certain key types. */
1358 return( PSA_ERROR_NOT_SUPPORTED );
1359 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001360}
Ronald Crond18b5f82020-11-25 16:46:05 +01001361
Ronald Croncf56a0a2020-08-04 09:51:30 +02001362psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001363 uint8_t *data,
1364 size_t data_size,
1365 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001366{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001368 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001369 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001370
Ronald Crone907e552021-01-18 13:22:38 +01001371 /* Reject a zero-length output buffer now, since this can never be a
1372 * valid key representation. This way we know that data must be a valid
1373 * pointer and we can do things like memset(data, ..., data_size). */
1374 if( data_size == 0 )
1375 return( PSA_ERROR_BUFFER_TOO_SMALL );
1376
Darryl Greendd8fb772018-11-07 16:00:44 +00001377 /* Set the key to empty now, so that even when there are errors, we always
1378 * set data_length to a value between 0 and data_size. On error, setting
1379 * the key to empty is a good choice because an empty key representation is
1380 * unlikely to be accepted anywhere. */
1381 *data_length = 0;
1382
1383 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001384 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001385 if( status != PSA_SUCCESS )
1386 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001387
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1389 {
1390 status = PSA_ERROR_INVALID_ARGUMENT;
1391 goto exit;
1392 }
1393
Ronald Crond18b5f82020-11-25 16:46:05 +01001394 psa_key_attributes_t attributes = {
1395 .core = slot->attr
1396 };
Ronald Cron67227982020-11-26 15:16:05 +01001397 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001398 &attributes, slot->key.data, slot->key.bytes,
1399 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001400
1401exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001402 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001403
Ronald Cron5c522922020-11-14 16:35:34 +01001404 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001405}
1406
Gilles Peskine91e8c332019-08-02 19:19:39 +02001407#if defined(static_assert)
1408static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1409 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001410static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001411 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001412static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001413 "One or more key attribute flag is listed as both internal-only and external-only" );
1414#endif
1415
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001416/** Validate that a key policy is internally well-formed.
1417 *
1418 * This function only rejects invalid policies. It does not validate the
1419 * consistency of the policy with respect to other attributes of the key
1420 * such as the key type.
1421 */
1422static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001423{
1424 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001425 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001426 PSA_KEY_USAGE_ENCRYPT |
1427 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001428 PSA_KEY_USAGE_SIGN_HASH |
1429 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001430 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1431 return( PSA_ERROR_INVALID_ARGUMENT );
1432
Gilles Peskine4747d192019-04-17 15:05:45 +02001433 return( PSA_SUCCESS );
1434}
1435
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001436/** Validate the internal consistency of key attributes.
1437 *
1438 * This function only rejects invalid attribute values. If does not
1439 * validate the consistency of the attributes with any key data that may
1440 * be involved in the creation of the key.
1441 *
1442 * Call this function early in the key creation process.
1443 *
1444 * \param[in] attributes Key attributes for the new key.
1445 * \param[out] p_drv On any return, the driver for the key, if any.
1446 * NULL for a transparent key.
1447 *
1448 */
1449static psa_status_t psa_validate_key_attributes(
1450 const psa_key_attributes_t *attributes,
1451 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001452{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001453 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001454 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001455 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001456
Ronald Cron54b90082020-10-29 15:26:43 +01001457 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001458 if( status != PSA_SUCCESS )
1459 return( status );
1460
Ronald Crond2ed4812020-07-17 16:11:30 +02001461 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001462 if( status != PSA_SUCCESS )
1463 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001464
Ronald Cron65f38a32020-10-23 17:11:13 +02001465 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1466 {
1467 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1468 return( PSA_ERROR_INVALID_ARGUMENT );
1469 }
1470 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001471 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001472 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001473 if( status != PSA_SUCCESS )
1474 return( status );
1475 }
1476
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001477 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001478 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001479 return( status );
1480
1481 /* Refuse to create overly large keys.
1482 * Note that this doesn't trigger on import if the attributes don't
1483 * explicitly specify a size (so psa_get_key_bits returns 0), so
1484 * psa_import_key() needs its own checks. */
1485 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1486 return( PSA_ERROR_NOT_SUPPORTED );
1487
Gilles Peskine91e8c332019-08-02 19:19:39 +02001488 /* Reject invalid flags. These should not be reachable through the API. */
1489 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1490 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1491 return( PSA_ERROR_INVALID_ARGUMENT );
1492
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001493 return( PSA_SUCCESS );
1494}
1495
Gilles Peskine4747d192019-04-17 15:05:45 +02001496/** Prepare a key slot to receive key material.
1497 *
1498 * This function allocates a key slot and sets its metadata.
1499 *
1500 * If this function fails, call psa_fail_key_creation().
1501 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001502 * This function is intended to be used as follows:
1503 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001504 * it with the specified attributes, and in case of a volatile key assign it
1505 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001506 * -# Populate the slot with the key material.
1507 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1508 * In case of failure at any step, stop the sequence and call
1509 * psa_fail_key_creation().
1510 *
Ronald Cron5c522922020-11-14 16:35:34 +01001511 * On success, the key slot is locked. It is the responsibility of the caller
1512 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001513 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001514 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001515 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001516 * \param[out] p_slot On success, a pointer to the prepared slot.
1517 * \param[out] p_drv On any return, the driver for the key, if any.
1518 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001519 *
1520 * \retval #PSA_SUCCESS
1521 * The key slot is ready to receive key material.
1522 * \return If this function fails, the key slot is an invalid state.
1523 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001524 */
1525static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001526 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001527 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001528 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001529 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001530{
1531 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001532 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001533 psa_key_slot_t *slot;
1534
Gilles Peskinedf179142019-07-15 22:02:14 +02001535 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001536 *p_drv = NULL;
1537
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001538 status = psa_validate_key_attributes( attributes, p_drv );
1539 if( status != PSA_SUCCESS )
1540 return( status );
1541
Ronald Cronc4d1b512020-07-31 11:26:37 +02001542 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001543 if( status != PSA_SUCCESS )
1544 return( status );
1545 slot = *p_slot;
1546
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001547 /* We're storing the declared bit-size of the key. It's up to each
1548 * creation mechanism to verify that this information is correct.
1549 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001550 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001551 * is optional (import, copy). In case of a volatile key, assign it the
1552 * volatile key identifier associated to the slot returned to contain its
1553 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001554
1555 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001556 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1557 {
1558#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1559 slot->attr.id = volatile_key_id;
1560#else
1561 slot->attr.id.key_id = volatile_key_id;
1562#endif
1563 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001564
Gilles Peskine91e8c332019-08-02 19:19:39 +02001565 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001566 * external-only flags, query `attributes`. Thanks to the check
1567 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001568 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001569 * may have set. */
1570 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001571
Gilles Peskinecbaff462019-07-12 23:46:04 +02001572#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001573 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001574 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001575 * create the key file in internal storage, create the
1576 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001577 * persistent data. This is done by starting a transaction that will
1578 * encompass these three actions.
1579 * For registering a volatile key, we just need to find an appropriate
1580 * slot number inside the SE. Since the key is designated volatile, creating
1581 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001582 /* The first thing to do is to find a slot number for the new key.
1583 * We save the slot number in persistent storage as part of the
1584 * transaction data. It will be needed to recover if the power
1585 * fails during the key creation process, to clean up on the secure
1586 * element side after restarting. Obtaining a slot number from the
1587 * secure element driver updates its persistent state, but we do not yet
1588 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001589 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001590 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001591 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001592 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001593 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001594 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001595 if( status != PSA_SUCCESS )
1596 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001597
1598 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001599 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001600 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1601 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001602 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001603 psa_crypto_transaction.key.id = slot->attr.id;
1604 status = psa_crypto_save_transaction( );
1605 if( status != PSA_SUCCESS )
1606 {
1607 (void) psa_crypto_stop_transaction( );
1608 return( status );
1609 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001610 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001611
1612 status = psa_copy_key_material_into_slot(
1613 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001614 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001615
1616 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1617 {
1618 /* Key registration only makes sense with a secure element. */
1619 return( PSA_ERROR_INVALID_ARGUMENT );
1620 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001621#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1622
Ronald Cronc4d1b512020-07-31 11:26:37 +02001623 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001624}
Gilles Peskine4747d192019-04-17 15:05:45 +02001625
1626/** Finalize the creation of a key once its key material has been set.
1627 *
1628 * This entails writing the key to persistent storage.
1629 *
1630 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001631 * See the documentation of psa_start_key_creation() for the intended use
1632 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001633 *
Ronald Cron5c522922020-11-14 16:35:34 +01001634 * If the finalization succeeds, the function unlocks the key slot (it was
1635 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1636 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001637 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001638 * \param[in,out] slot Pointer to the slot with key material.
1639 * \param[in] driver The secure element driver for the key,
1640 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001641 * \param[out] key On success, identifier of the key. Note that the
1642 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001643 *
1644 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001645 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001646 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1647 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1648 * \retval #PSA_ERROR_ALREADY_EXISTS
1649 * \retval #PSA_ERROR_DATA_INVALID
1650 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001651 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001652 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001653 * \return If this function fails, the key slot is an invalid state.
1654 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001656static psa_status_t psa_finish_key_creation(
1657 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001658 psa_se_drv_table_entry_t *driver,
1659 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001660{
1661 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001662 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001663 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001664
1665#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001666 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001667 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001668#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1669 if( driver != NULL )
1670 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001671 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001672 psa_key_slot_number_t slot_number =
1673 psa_key_slot_get_slot_number( slot ) ;
1674
Gilles Peskineb46bef22019-07-30 21:32:04 +02001675#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001676 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001677 sizeof( data.slot_number ),
1678 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001679#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001680 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001681 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001682 (uint8_t*) &data,
1683 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001684 }
1685 else
1686#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1687 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001688 /* Key material is saved in export representation in the slot, so
1689 * just pass the slot buffer for storage. */
1690 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001691 slot->key.data,
1692 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001693 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001694 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001695#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1696
Gilles Peskinecbaff462019-07-12 23:46:04 +02001697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001698 /* Finish the transaction for a key creation. This does not
1699 * happen when registering an existing key. Detect this case
1700 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001701 * creation of a persistent key in a secure element requires a transaction,
1702 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001703 if( driver != NULL &&
1704 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001705 {
1706 status = psa_save_se_persistent_data( driver );
1707 if( status != PSA_SUCCESS )
1708 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001709 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001710 return( status );
1711 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001712 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001713 }
1714#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1715
Ronald Cron50972942020-11-14 11:28:25 +01001716 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001717 {
1718 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001719 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001720 if( status != PSA_SUCCESS )
1721 *key = MBEDTLS_SVC_KEY_ID_INIT;
1722 }
Ronald Cron50972942020-11-14 11:28:25 +01001723
Gilles Peskine4747d192019-04-17 15:05:45 +02001724 return( status );
1725}
1726
1727/** Abort the creation of a key.
1728 *
1729 * You may call this function after calling psa_start_key_creation(),
1730 * or after psa_finish_key_creation() fails. In other circumstances, this
1731 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001732 * See the documentation of psa_start_key_creation() for the intended use
1733 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001734 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001735 * \param[in,out] slot Pointer to the slot with key material.
1736 * \param[in] driver The secure element driver for the key,
1737 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001738 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001739static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001740 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001741{
Gilles Peskine011e4282019-06-26 18:34:38 +02001742 (void) driver;
1743
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 if( slot == NULL )
1745 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001746
1747#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001748 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001749 * element, and the failure happened later (when saving metadata
1750 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001751 * element.
1752 * https://github.com/ARMmbed/mbed-crypto/issues/217
1753 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001754
Gilles Peskined7729582019-08-05 15:55:54 +02001755 /* Abort the ongoing transaction if any (there may not be one if
1756 * the creation process failed before starting one, or if the
1757 * key creation is a registration of a key in a secure element).
1758 * Earlier functions must already have done what it takes to undo any
1759 * partial creation. All that's left is to update the transaction data
1760 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001761 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001762#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1763
Gilles Peskine4747d192019-04-17 15:05:45 +02001764 psa_wipe_key_slot( slot );
1765}
1766
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001767/** Validate optional attributes during key creation.
1768 *
1769 * Some key attributes are optional during key creation. If they are
1770 * specified in the attributes structure, check that they are consistent
1771 * with the data in the slot.
1772 *
1773 * This function should be called near the end of key creation, after
1774 * the slot in memory is fully populated but before saving persistent data.
1775 */
1776static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001777 const psa_key_slot_t *slot,
1778 const psa_key_attributes_t *attributes )
1779{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001780 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001781 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001782 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001783 return( PSA_ERROR_INVALID_ARGUMENT );
1784 }
1785
1786 if( attributes->domain_parameters_size != 0 )
1787 {
John Durkop0e005192020-10-31 22:06:54 -07001788#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1789 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001790 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001791 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001792 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001793 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001795
Ronald Cron90857082020-11-25 14:58:33 +01001796 psa_status_t status = mbedtls_psa_rsa_load_representation(
1797 slot->attr.type,
1798 slot->key.data,
1799 slot->key.bytes,
1800 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001801 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001802 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001803
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001804 mbedtls_mpi_init( &actual );
1805 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001806 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001807 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001808 mbedtls_rsa_free( rsa );
1809 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001810 if( ret != 0 )
1811 goto rsa_exit;
1812 ret = mbedtls_mpi_read_binary( &required,
1813 attributes->domain_parameters,
1814 attributes->domain_parameters_size );
1815 if( ret != 0 )
1816 goto rsa_exit;
1817 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1818 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1819 rsa_exit:
1820 mbedtls_mpi_free( &actual );
1821 mbedtls_mpi_free( &required );
1822 if( ret != 0)
1823 return( mbedtls_to_psa_error( ret ) );
1824 }
1825 else
John Durkop9814fa22020-11-04 12:28:15 -08001826#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1827 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001828 {
1829 return( PSA_ERROR_INVALID_ARGUMENT );
1830 }
1831 }
1832
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001833 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001834 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001835 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001836 return( PSA_ERROR_INVALID_ARGUMENT );
1837 }
1838
1839 return( PSA_SUCCESS );
1840}
1841
Gilles Peskine4747d192019-04-17 15:05:45 +02001842psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001843 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001844 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001845 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001846{
1847 psa_status_t status;
1848 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001849 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001850 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851
Ronald Cron81709fc2020-11-14 12:10:32 +01001852 *key = MBEDTLS_SVC_KEY_ID_INIT;
1853
Gilles Peskine0f84d622019-09-12 19:03:13 +02001854 /* Reject zero-length symmetric keys (including raw data key objects).
1855 * This also rejects any key which might be encoded as an empty string,
1856 * which is never valid. */
1857 if( data_length == 0 )
1858 return( PSA_ERROR_INVALID_ARGUMENT );
1859
Gilles Peskinedf179142019-07-15 22:02:14 +02001860 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001861 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001862 if( status != PSA_SUCCESS )
1863 goto exit;
1864
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001865 /* In the case of a transparent key or an opaque key stored in local
1866 * storage (thus not in the case of generating a key in a secure element
1867 * or cryptoprocessor with storage), we have to allocate a buffer to
1868 * hold the generated key material. */
1869 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001870 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001871 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001872 if( status != PSA_SUCCESS )
1873 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001874 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001875
1876 bits = slot->attr.bits;
1877 status = psa_driver_wrapper_import_key( attributes,
1878 data, data_length,
1879 slot->key.data,
1880 slot->key.bytes,
1881 &slot->key.bytes, &bits );
1882 if( status != PSA_SUCCESS )
1883 goto exit;
1884
1885 if( slot->attr.bits == 0 )
1886 slot->attr.bits = (psa_key_bits_t) bits;
1887 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001888 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001889 status = PSA_ERROR_INVALID_ARGUMENT;
1890 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001891 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001892
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001893 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001894 if( status != PSA_SUCCESS )
1895 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896
Ronald Cron81709fc2020-11-14 12:10:32 +01001897 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001899 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001900 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001901
Gilles Peskine4747d192019-04-17 15:05:45 +02001902 return( status );
1903}
1904
Gilles Peskined7729582019-08-05 15:55:54 +02001905#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1906psa_status_t mbedtls_psa_register_se_key(
1907 const psa_key_attributes_t *attributes )
1908{
1909 psa_status_t status;
1910 psa_key_slot_t *slot = NULL;
1911 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001913
1914 /* Leaving attributes unspecified is not currently supported.
1915 * It could make sense to query the key type and size from the
1916 * secure element, but not all secure elements support this
1917 * and the driver HAL doesn't currently support it. */
1918 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1919 return( PSA_ERROR_NOT_SUPPORTED );
1920 if( psa_get_key_bits( attributes ) == 0 )
1921 return( PSA_ERROR_NOT_SUPPORTED );
1922
1923 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001924 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02001925 if( status != PSA_SUCCESS )
1926 goto exit;
1927
Ronald Cron81709fc2020-11-14 12:10:32 +01001928 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02001929
1930exit:
1931 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02001932 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001933
Gilles Peskined7729582019-08-05 15:55:54 +02001934 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001935 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02001936 return( status );
1937}
1938#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1939
Gilles Peskinef603c712019-01-19 13:40:11 +01001940static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001941 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001942{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001943 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001944 source->key.data,
1945 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01001946 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02001947 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01001948
Steven Cooreman398aee52020-10-13 14:35:45 +02001949 target->attr.type = source->attr.type;
1950 target->attr.bits = source->attr.bits;
1951
1952 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01001953}
1954
Ronald Croncf56a0a2020-08-04 09:51:30 +02001955psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001956 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001957 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01001958{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001960 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01001961 psa_key_slot_t *source_slot = NULL;
1962 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001963 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001964 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001965
Ronald Cron81709fc2020-11-14 12:10:32 +01001966 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
1967
Ronald Cron5c522922020-11-14 16:35:34 +01001968 status = psa_get_and_lock_transparent_key_slot_with_policy(
1969 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001970 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001971 goto exit;
1972
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001973 status = psa_validate_optional_attributes( source_slot,
1974 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001975 if( status != PSA_SUCCESS )
1976 goto exit;
1977
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001978 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001979 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001980 if( status != PSA_SUCCESS )
1981 goto exit;
1982
Ronald Cron81709fc2020-11-14 12:10:32 +01001983 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
1984 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001985 if( status != PSA_SUCCESS )
1986 goto exit;
1987
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001988#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1989 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001990 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001991 /* Copying to a secure element is not implemented yet. */
1992 status = PSA_ERROR_NOT_SUPPORTED;
1993 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001994 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001995#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001996
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001997 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001998 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001999 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002000
Ronald Cron81709fc2020-11-14 12:10:32 +01002001 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002002exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002003 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002004 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002005
Ronald Cron5c522922020-11-14 16:35:34 +01002006 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002007
Ronald Cron5c522922020-11-14 16:35:34 +01002008 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002009}
2010
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002011
2012
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002013/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002014/* Message digests */
2015/****************************************************************/
2016
John Durkop07cc04a2020-11-16 22:08:34 -08002017#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002018 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2019 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002020 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002021static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002022{
2023 switch( alg )
2024 {
John Durkopee4e6602020-11-27 08:48:46 -08002025#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002026 case PSA_ALG_MD2:
2027 return( &mbedtls_md2_info );
2028#endif
John Durkopee4e6602020-11-27 08:48:46 -08002029#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002030 case PSA_ALG_MD4:
2031 return( &mbedtls_md4_info );
2032#endif
John Durkopee4e6602020-11-27 08:48:46 -08002033#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002034 case PSA_ALG_MD5:
2035 return( &mbedtls_md5_info );
2036#endif
John Durkopee4e6602020-11-27 08:48:46 -08002037#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002038 case PSA_ALG_RIPEMD160:
2039 return( &mbedtls_ripemd160_info );
2040#endif
John Durkopee4e6602020-11-27 08:48:46 -08002041#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002042 case PSA_ALG_SHA_1:
2043 return( &mbedtls_sha1_info );
2044#endif
John Durkopee4e6602020-11-27 08:48:46 -08002045#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002046 case PSA_ALG_SHA_224:
2047 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002048#endif
2049#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002050 case PSA_ALG_SHA_256:
2051 return( &mbedtls_sha256_info );
2052#endif
John Durkopee4e6602020-11-27 08:48:46 -08002053#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002054 case PSA_ALG_SHA_384:
2055 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002056#endif
John Durkopee4e6602020-11-27 08:48:46 -08002057#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002058 case PSA_ALG_SHA_512:
2059 return( &mbedtls_sha512_info );
2060#endif
2061 default:
2062 return( NULL );
2063 }
2064}
John Durkop07cc04a2020-11-16 22:08:34 -08002065#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002066 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2067 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2068 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002069
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002070psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2071{
2072 switch( operation->alg )
2073 {
Gilles Peskine81736312018-06-26 15:04:31 +02002074 case 0:
2075 /* The object has (apparently) been initialized but it is not
2076 * in use. It's ok to call abort on such an object, and there's
2077 * nothing to do. */
2078 break;
John Durkopee4e6602020-11-27 08:48:46 -08002079#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002080 case PSA_ALG_MD2:
2081 mbedtls_md2_free( &operation->ctx.md2 );
2082 break;
2083#endif
John Durkopee4e6602020-11-27 08:48:46 -08002084#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002085 case PSA_ALG_MD4:
2086 mbedtls_md4_free( &operation->ctx.md4 );
2087 break;
2088#endif
John Durkopee4e6602020-11-27 08:48:46 -08002089#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002090 case PSA_ALG_MD5:
2091 mbedtls_md5_free( &operation->ctx.md5 );
2092 break;
2093#endif
John Durkopee4e6602020-11-27 08:48:46 -08002094#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002095 case PSA_ALG_RIPEMD160:
2096 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2097 break;
2098#endif
John Durkopee4e6602020-11-27 08:48:46 -08002099#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002100 case PSA_ALG_SHA_1:
2101 mbedtls_sha1_free( &operation->ctx.sha1 );
2102 break;
2103#endif
John Durkop6ca23272020-12-03 06:01:32 -08002104#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002105 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002106 mbedtls_sha256_free( &operation->ctx.sha256 );
2107 break;
2108#endif
2109#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002110 case PSA_ALG_SHA_256:
2111 mbedtls_sha256_free( &operation->ctx.sha256 );
2112 break;
2113#endif
John Durkop6ca23272020-12-03 06:01:32 -08002114#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002115 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002116 mbedtls_sha512_free( &operation->ctx.sha512 );
2117 break;
2118#endif
2119#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002120 case PSA_ALG_SHA_512:
2121 mbedtls_sha512_free( &operation->ctx.sha512 );
2122 break;
2123#endif
2124 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002125 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002126 }
2127 operation->alg = 0;
2128 return( PSA_SUCCESS );
2129}
2130
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002131psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002132 psa_algorithm_t alg )
2133{
Janos Follath24eed8d2019-11-22 13:21:35 +00002134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002135
2136 /* A context must be freshly initialized before it can be set up. */
2137 if( operation->alg != 0 )
2138 {
2139 return( PSA_ERROR_BAD_STATE );
2140 }
2141
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002142 switch( alg )
2143 {
John Durkopee4e6602020-11-27 08:48:46 -08002144#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002145 case PSA_ALG_MD2:
2146 mbedtls_md2_init( &operation->ctx.md2 );
2147 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2148 break;
2149#endif
John Durkopee4e6602020-11-27 08:48:46 -08002150#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002151 case PSA_ALG_MD4:
2152 mbedtls_md4_init( &operation->ctx.md4 );
2153 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2154 break;
2155#endif
John Durkopee4e6602020-11-27 08:48:46 -08002156#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002157 case PSA_ALG_MD5:
2158 mbedtls_md5_init( &operation->ctx.md5 );
2159 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2160 break;
2161#endif
John Durkopee4e6602020-11-27 08:48:46 -08002162#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163 case PSA_ALG_RIPEMD160:
2164 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2165 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2166 break;
2167#endif
John Durkopee4e6602020-11-27 08:48:46 -08002168#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002169 case PSA_ALG_SHA_1:
2170 mbedtls_sha1_init( &operation->ctx.sha1 );
2171 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2172 break;
2173#endif
John Durkopee4e6602020-11-27 08:48:46 -08002174#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002175 case PSA_ALG_SHA_224:
2176 mbedtls_sha256_init( &operation->ctx.sha256 );
2177 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2178 break;
John Durkopee4e6602020-11-27 08:48:46 -08002179#endif
2180#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002181 case PSA_ALG_SHA_256:
2182 mbedtls_sha256_init( &operation->ctx.sha256 );
2183 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2184 break;
2185#endif
John Durkopee4e6602020-11-27 08:48:46 -08002186#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002187 case PSA_ALG_SHA_384:
2188 mbedtls_sha512_init( &operation->ctx.sha512 );
2189 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2190 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002191#endif
John Durkopee4e6602020-11-27 08:48:46 -08002192#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193 case PSA_ALG_SHA_512:
2194 mbedtls_sha512_init( &operation->ctx.sha512 );
2195 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2196 break;
2197#endif
2198 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002199 return( PSA_ALG_IS_HASH( alg ) ?
2200 PSA_ERROR_NOT_SUPPORTED :
2201 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202 }
2203 if( ret == 0 )
2204 operation->alg = alg;
2205 else
2206 psa_hash_abort( operation );
2207 return( mbedtls_to_psa_error( ret ) );
2208}
2209
2210psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2211 const uint8_t *input,
2212 size_t input_length )
2213{
Janos Follath24eed8d2019-11-22 13:21:35 +00002214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002215
2216 /* Don't require hash implementations to behave correctly on a
2217 * zero-length input, which may have an invalid pointer. */
2218 if( input_length == 0 )
2219 return( PSA_SUCCESS );
2220
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221 switch( operation->alg )
2222 {
John Durkopee4e6602020-11-27 08:48:46 -08002223#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002224 case PSA_ALG_MD2:
2225 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2226 input, input_length );
2227 break;
2228#endif
John Durkopee4e6602020-11-27 08:48:46 -08002229#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002230 case PSA_ALG_MD4:
2231 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2232 input, input_length );
2233 break;
2234#endif
John Durkopee4e6602020-11-27 08:48:46 -08002235#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236 case PSA_ALG_MD5:
2237 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2238 input, input_length );
2239 break;
2240#endif
John Durkopee4e6602020-11-27 08:48:46 -08002241#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242 case PSA_ALG_RIPEMD160:
2243 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2244 input, input_length );
2245 break;
2246#endif
John Durkopee4e6602020-11-27 08:48:46 -08002247#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002248 case PSA_ALG_SHA_1:
2249 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2250 input, input_length );
2251 break;
2252#endif
John Durkop6ca23272020-12-03 06:01:32 -08002253#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002255 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2256 input, input_length );
2257 break;
2258#endif
2259#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 case PSA_ALG_SHA_256:
2261 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2262 input, input_length );
2263 break;
2264#endif
John Durkop6ca23272020-12-03 06:01:32 -08002265#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002267 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2268 input, input_length );
2269 break;
2270#endif
2271#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272 case PSA_ALG_SHA_512:
2273 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2274 input, input_length );
2275 break;
2276#endif
2277 default:
John Durkopee4e6602020-11-27 08:48:46 -08002278 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002279 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002281
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002282 if( ret != 0 )
2283 psa_hash_abort( operation );
2284 return( mbedtls_to_psa_error( ret ) );
2285}
2286
2287psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2288 uint8_t *hash,
2289 size_t hash_size,
2290 size_t *hash_length )
2291{
itayzafrir40835d42018-08-02 13:14:17 +03002292 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002294 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295
2296 /* Fill the output buffer with something that isn't a valid hash
2297 * (barring an attack on the hash and deliberately-crafted input),
2298 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002299 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002300 /* If hash_size is 0 then hash may be NULL and then the
2301 * call to memset would have undefined behavior. */
2302 if( hash_size != 0 )
2303 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002304
2305 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002306 {
2307 status = PSA_ERROR_BUFFER_TOO_SMALL;
2308 goto exit;
2309 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002310
2311 switch( operation->alg )
2312 {
John Durkopee4e6602020-11-27 08:48:46 -08002313#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314 case PSA_ALG_MD2:
2315 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2316 break;
2317#endif
John Durkopee4e6602020-11-27 08:48:46 -08002318#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319 case PSA_ALG_MD4:
2320 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2321 break;
2322#endif
John Durkopee4e6602020-11-27 08:48:46 -08002323#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 case PSA_ALG_MD5:
2325 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2326 break;
2327#endif
John Durkopee4e6602020-11-27 08:48:46 -08002328#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329 case PSA_ALG_RIPEMD160:
2330 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2331 break;
2332#endif
John Durkopee4e6602020-11-27 08:48:46 -08002333#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334 case PSA_ALG_SHA_1:
2335 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2336 break;
2337#endif
John Durkop6ca23272020-12-03 06:01:32 -08002338#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002340 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2341 break;
2342#endif
2343#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002344 case PSA_ALG_SHA_256:
2345 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2346 break;
2347#endif
John Durkop6ca23272020-12-03 06:01:32 -08002348#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002350 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2351 break;
2352#endif
2353#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002354 case PSA_ALG_SHA_512:
2355 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2356 break;
2357#endif
2358 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002359 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360 }
itayzafrir40835d42018-08-02 13:14:17 +03002361 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002362
itayzafrir40835d42018-08-02 13:14:17 +03002363exit:
2364 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002366 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002367 return( psa_hash_abort( operation ) );
2368 }
2369 else
2370 {
2371 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002372 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002373 }
2374}
2375
Gilles Peskine2d277862018-06-18 15:41:12 +02002376psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2377 const uint8_t *hash,
2378 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002379{
2380 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2381 size_t actual_hash_length;
2382 psa_status_t status = psa_hash_finish( operation,
2383 actual_hash, sizeof( actual_hash ),
2384 &actual_hash_length );
2385 if( status != PSA_SUCCESS )
2386 return( status );
2387 if( actual_hash_length != hash_length )
2388 return( PSA_ERROR_INVALID_SIGNATURE );
2389 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2390 return( PSA_ERROR_INVALID_SIGNATURE );
2391 return( PSA_SUCCESS );
2392}
2393
Gilles Peskine0a749c82019-11-28 19:33:58 +01002394psa_status_t psa_hash_compute( psa_algorithm_t alg,
2395 const uint8_t *input, size_t input_length,
2396 uint8_t *hash, size_t hash_size,
2397 size_t *hash_length )
2398{
2399 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2400 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2401
2402 *hash_length = hash_size;
2403 status = psa_hash_setup( &operation, alg );
2404 if( status != PSA_SUCCESS )
2405 goto exit;
2406 status = psa_hash_update( &operation, input, input_length );
2407 if( status != PSA_SUCCESS )
2408 goto exit;
2409 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2410 if( status != PSA_SUCCESS )
2411 goto exit;
2412
2413exit:
2414 if( status == PSA_SUCCESS )
2415 status = psa_hash_abort( &operation );
2416 else
2417 psa_hash_abort( &operation );
2418 return( status );
2419}
2420
2421psa_status_t psa_hash_compare( psa_algorithm_t alg,
2422 const uint8_t *input, size_t input_length,
2423 const uint8_t *hash, size_t hash_length )
2424{
2425 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2427
2428 status = psa_hash_setup( &operation, alg );
2429 if( status != PSA_SUCCESS )
2430 goto exit;
2431 status = psa_hash_update( &operation, input, input_length );
2432 if( status != PSA_SUCCESS )
2433 goto exit;
2434 status = psa_hash_verify( &operation, hash, hash_length );
2435 if( status != PSA_SUCCESS )
2436 goto exit;
2437
2438exit:
2439 if( status == PSA_SUCCESS )
2440 status = psa_hash_abort( &operation );
2441 else
2442 psa_hash_abort( &operation );
2443 return( status );
2444}
2445
Gilles Peskineeb35d782019-01-22 17:56:16 +01002446psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2447 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002448{
2449 if( target_operation->alg != 0 )
2450 return( PSA_ERROR_BAD_STATE );
2451
2452 switch( source_operation->alg )
2453 {
2454 case 0:
2455 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002456#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002457 case PSA_ALG_MD2:
2458 mbedtls_md2_clone( &target_operation->ctx.md2,
2459 &source_operation->ctx.md2 );
2460 break;
2461#endif
John Durkopee4e6602020-11-27 08:48:46 -08002462#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002463 case PSA_ALG_MD4:
2464 mbedtls_md4_clone( &target_operation->ctx.md4,
2465 &source_operation->ctx.md4 );
2466 break;
2467#endif
John Durkopee4e6602020-11-27 08:48:46 -08002468#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002469 case PSA_ALG_MD5:
2470 mbedtls_md5_clone( &target_operation->ctx.md5,
2471 &source_operation->ctx.md5 );
2472 break;
2473#endif
John Durkopee4e6602020-11-27 08:48:46 -08002474#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002475 case PSA_ALG_RIPEMD160:
2476 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2477 &source_operation->ctx.ripemd160 );
2478 break;
2479#endif
John Durkopee4e6602020-11-27 08:48:46 -08002480#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002481 case PSA_ALG_SHA_1:
2482 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2483 &source_operation->ctx.sha1 );
2484 break;
2485#endif
John Durkop6ca23272020-12-03 06:01:32 -08002486#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002487 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002488 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2489 &source_operation->ctx.sha256 );
2490 break;
2491#endif
2492#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002493 case PSA_ALG_SHA_256:
2494 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2495 &source_operation->ctx.sha256 );
2496 break;
2497#endif
John Durkop6ca23272020-12-03 06:01:32 -08002498#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002499 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002500 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2501 &source_operation->ctx.sha512 );
2502 break;
2503#endif
2504#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002505 case PSA_ALG_SHA_512:
2506 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2507 &source_operation->ctx.sha512 );
2508 break;
2509#endif
2510 default:
2511 return( PSA_ERROR_NOT_SUPPORTED );
2512 }
2513
2514 target_operation->alg = source_operation->alg;
2515 return( PSA_SUCCESS );
2516}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002517
2518
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002519/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520/* MAC */
2521/****************************************************************/
2522
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002523static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524 psa_algorithm_t alg,
2525 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002526 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002527 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002530 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002532 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002533 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002534
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2536 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002537 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002538 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002539 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002540 mode = MBEDTLS_MODE_STREAM;
2541 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542 case PSA_ALG_CTR:
2543 mode = MBEDTLS_MODE_CTR;
2544 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002545 case PSA_ALG_CFB:
2546 mode = MBEDTLS_MODE_CFB;
2547 break;
2548 case PSA_ALG_OFB:
2549 mode = MBEDTLS_MODE_OFB;
2550 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002551 case PSA_ALG_ECB_NO_PADDING:
2552 mode = MBEDTLS_MODE_ECB;
2553 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002554 case PSA_ALG_CBC_NO_PADDING:
2555 mode = MBEDTLS_MODE_CBC;
2556 break;
2557 case PSA_ALG_CBC_PKCS7:
2558 mode = MBEDTLS_MODE_CBC;
2559 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002560 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561 mode = MBEDTLS_MODE_CCM;
2562 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002563 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564 mode = MBEDTLS_MODE_GCM;
2565 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002566 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002567 mode = MBEDTLS_MODE_CHACHAPOLY;
2568 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569 default:
2570 return( NULL );
2571 }
2572 }
2573 else if( alg == PSA_ALG_CMAC )
2574 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575 else
2576 return( NULL );
2577
2578 switch( key_type )
2579 {
2580 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002581 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582 break;
2583 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002584 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2585 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002587 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002589 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002590 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2591 * but two-key Triple-DES is functionally three-key Triple-DES
2592 * with K1=K3, so that's how we present it to mbedtls. */
2593 if( key_bits == 128 )
2594 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595 break;
2596 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002597 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598 break;
2599 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002600 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002602 case PSA_KEY_TYPE_CHACHA20:
2603 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2604 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605 default:
2606 return( NULL );
2607 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002608 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002609 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610
Jaeden Amero23bbb752018-06-26 14:16:54 +01002611 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2612 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613}
2614
John Durkop6ba40d12020-11-10 08:50:04 -08002615#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002616static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002617{
Gilles Peskine2d277862018-06-18 15:41:12 +02002618 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002619 {
2620 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002621 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002622 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002623 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002624 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002625 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002626 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002627 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002628 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002629 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002630 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002631 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002632 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002633 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002634 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002635 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002636 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002637 return( 128 );
2638 default:
2639 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002640 }
2641}
John Durkop07cc04a2020-11-16 22:08:34 -08002642#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002643
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002644/* Initialize the MAC operation structure. Once this function has been
2645 * called, psa_mac_abort can run and will do the right thing. */
2646static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2647 psa_algorithm_t alg )
2648{
2649 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2650
2651 operation->alg = alg;
2652 operation->key_set = 0;
2653 operation->iv_set = 0;
2654 operation->iv_required = 0;
2655 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002656 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002657
2658#if defined(MBEDTLS_CMAC_C)
2659 if( alg == PSA_ALG_CMAC )
2660 {
2661 operation->iv_required = 0;
2662 mbedtls_cipher_init( &operation->ctx.cmac );
2663 status = PSA_SUCCESS;
2664 }
2665 else
2666#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002667#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002668 if( PSA_ALG_IS_HMAC( operation->alg ) )
2669 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002670 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2671 operation->ctx.hmac.hash_ctx.alg = 0;
2672 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002673 }
2674 else
John Durkopd0321952020-10-29 21:37:36 -07002675#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002676 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002677 if( ! PSA_ALG_IS_MAC( alg ) )
2678 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002679 }
2680
2681 if( status != PSA_SUCCESS )
2682 memset( operation, 0, sizeof( *operation ) );
2683 return( status );
2684}
2685
John Durkop6ba40d12020-11-10 08:50:04 -08002686#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002687static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2688{
Gilles Peskine3f108122018-12-07 18:14:53 +01002689 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002690 return( psa_hash_abort( &hmac->hash_ctx ) );
2691}
John Durkop6ba40d12020-11-10 08:50:04 -08002692#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002693
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2695{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002696 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002697 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002698 /* The object has (apparently) been initialized but it is not
2699 * in use. It's ok to call abort on such an object, and there's
2700 * nothing to do. */
2701 return( PSA_SUCCESS );
2702 }
2703 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002705 if( operation->alg == PSA_ALG_CMAC )
2706 {
2707 mbedtls_cipher_free( &operation->ctx.cmac );
2708 }
2709 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002711#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002712 if( PSA_ALG_IS_HMAC( operation->alg ) )
2713 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002714 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002715 }
2716 else
John Durkopd0321952020-10-29 21:37:36 -07002717#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002718 {
2719 /* Sanity check (shouldn't happen: operation->alg should
2720 * always have been initialized to a valid value). */
2721 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722 }
Moran Peker41deec42018-04-04 15:43:05 +03002723
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724 operation->alg = 0;
2725 operation->key_set = 0;
2726 operation->iv_set = 0;
2727 operation->iv_required = 0;
2728 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002729 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002730
Gilles Peskine8c9def32018-02-08 10:02:12 +01002731 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002732
2733bad_state:
2734 /* If abort is called on an uninitialized object, we can't trust
2735 * anything. Wipe the object in case it contains confidential data.
2736 * This may result in a memory leak if a pointer gets overwritten,
2737 * but it's too late to do anything about this. */
2738 memset( operation, 0, sizeof( *operation ) );
2739 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740}
2741
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002742#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002743static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002744 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002745 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002746 const mbedtls_cipher_info_t *cipher_info )
2747{
Janos Follath24eed8d2019-11-22 13:21:35 +00002748 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002749
2750 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002751
2752 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2753 if( ret != 0 )
2754 return( ret );
2755
2756 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002757 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002758 key_bits );
2759 return( ret );
2760}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002761#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002762
John Durkop6ba40d12020-11-10 08:50:04 -08002763#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002764static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2765 const uint8_t *key,
2766 size_t key_length,
2767 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002768{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002769 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002770 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002771 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002772 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002773 psa_status_t status;
2774
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002775 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2776 * overflow below. This should never trigger if the hash algorithm
2777 * is implemented correctly. */
2778 /* The size checks against the ipad and opad buffers cannot be written
2779 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2780 * because that triggers -Wlogical-op on GCC 7.3. */
2781 if( block_size > sizeof( ipad ) )
2782 return( PSA_ERROR_NOT_SUPPORTED );
2783 if( block_size > sizeof( hmac->opad ) )
2784 return( PSA_ERROR_NOT_SUPPORTED );
2785 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002786 return( PSA_ERROR_NOT_SUPPORTED );
2787
Gilles Peskined223b522018-06-11 18:12:58 +02002788 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002789 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002790 status = psa_hash_compute( hash_alg, key, key_length,
2791 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002792 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002793 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002794 }
Gilles Peskine96889972018-07-12 17:07:03 +02002795 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2796 * but it is permitted. It is common when HMAC is used in HKDF, for
2797 * example. Don't call `memcpy` in the 0-length because `key` could be
2798 * an invalid pointer which would make the behavior undefined. */
2799 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002800 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002801
Gilles Peskined223b522018-06-11 18:12:58 +02002802 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2803 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002804 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002805 ipad[i] ^= 0x36;
2806 memset( ipad + key_length, 0x36, block_size - key_length );
2807
2808 /* Copy the key material from ipad to opad, flipping the requisite bits,
2809 * and filling the rest of opad with the requisite constant. */
2810 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002811 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2812 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002813
Gilles Peskine01126fa2018-07-12 17:04:55 +02002814 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002815 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002816 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002817
Gilles Peskine01126fa2018-07-12 17:04:55 +02002818 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002819
2820cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002821 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002822
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002823 return( status );
2824}
John Durkop6ba40d12020-11-10 08:50:04 -08002825#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002826
Gilles Peskine89167cb2018-07-08 20:12:23 +02002827static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002828 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002829 psa_algorithm_t alg,
2830 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002833 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002834 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002836 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002837 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002838 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002839 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002841 /* A context must be freshly initialized before it can be set up. */
2842 if( operation->alg != 0 )
2843 {
2844 return( PSA_ERROR_BAD_STATE );
2845 }
2846
Gilles Peskined911eb72018-08-14 15:18:45 +02002847 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002848 if( status != PSA_SUCCESS )
2849 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002850 if( is_sign )
2851 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002852
Ronald Cron5c522922020-11-14 16:35:34 +01002853 status = psa_get_and_lock_transparent_key_slot_with_policy(
2854 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002855 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002856 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002857 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002858
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002860 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002861 {
2862 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002863 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002864 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002866 if( cipher_info == NULL )
2867 {
2868 status = PSA_ERROR_NOT_SUPPORTED;
2869 goto exit;
2870 }
2871 operation->mac_size = cipher_info->block_size;
2872 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2873 status = mbedtls_to_psa_error( ret );
2874 }
2875 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002877#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02002878 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002879 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002880 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002881 if( hash_alg == 0 )
2882 {
2883 status = PSA_ERROR_NOT_SUPPORTED;
2884 goto exit;
2885 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002886
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002887 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002888 /* Sanity check. This shouldn't fail on a valid configuration. */
2889 if( operation->mac_size == 0 ||
2890 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2891 {
2892 status = PSA_ERROR_NOT_SUPPORTED;
2893 goto exit;
2894 }
2895
Gilles Peskine8e338702019-07-30 20:06:31 +02002896 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002897 {
2898 status = PSA_ERROR_INVALID_ARGUMENT;
2899 goto exit;
2900 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002901
Gilles Peskine01126fa2018-07-12 17:04:55 +02002902 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002903 slot->key.data,
2904 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02002905 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002906 }
2907 else
John Durkopd0321952020-10-29 21:37:36 -07002908#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002909 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002910 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002911 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002912 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002913
Gilles Peskined911eb72018-08-14 15:18:45 +02002914 if( truncated == 0 )
2915 {
2916 /* The "normal" case: untruncated algorithm. Nothing to do. */
2917 }
2918 else if( truncated < 4 )
2919 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002920 /* A very short MAC is too short for security since it can be
2921 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2922 * so we make this our minimum, even though 32 bits is still
2923 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002924 status = PSA_ERROR_NOT_SUPPORTED;
2925 }
2926 else if( truncated > operation->mac_size )
2927 {
2928 /* It's impossible to "truncate" to a larger length. */
2929 status = PSA_ERROR_INVALID_ARGUMENT;
2930 }
2931 else
2932 operation->mac_size = truncated;
2933
Gilles Peskinefbfac682018-07-08 20:51:54 +02002934exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002935 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002936 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002937 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002938 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002939 else
2940 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002941 operation->key_set = 1;
2942 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002943
Ronald Cron5c522922020-11-14 16:35:34 +01002944 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002945
Ronald Cron5c522922020-11-14 16:35:34 +01002946 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002947}
2948
Gilles Peskine89167cb2018-07-08 20:12:23 +02002949psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002950 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002951 psa_algorithm_t alg )
2952{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002953 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002954}
2955
2956psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002957 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002958 psa_algorithm_t alg )
2959{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002960 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002961}
2962
Gilles Peskine8c9def32018-02-08 10:02:12 +01002963psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2964 const uint8_t *input,
2965 size_t input_length )
2966{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002967 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002968 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002969 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002970 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002971 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002972 operation->has_input = 1;
2973
Gilles Peskine8c9def32018-02-08 10:02:12 +01002974#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002975 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002976 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002977 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2978 input, input_length );
2979 status = mbedtls_to_psa_error( ret );
2980 }
2981 else
2982#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002983#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002984 if( PSA_ALG_IS_HMAC( operation->alg ) )
2985 {
2986 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2987 input_length );
2988 }
2989 else
John Durkopd0321952020-10-29 21:37:36 -07002990#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002991 {
2992 /* This shouldn't happen if `operation` was initialized by
2993 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002994 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002995 }
2996
Gilles Peskinefbfac682018-07-08 20:51:54 +02002997 if( status != PSA_SUCCESS )
2998 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002999 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003000}
3001
John Durkop6ba40d12020-11-10 08:50:04 -08003002#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003003static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3004 uint8_t *mac,
3005 size_t mac_size )
3006{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003007 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003008 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3009 size_t hash_size = 0;
3010 size_t block_size = psa_get_hash_block_size( hash_alg );
3011 psa_status_t status;
3012
Gilles Peskine01126fa2018-07-12 17:04:55 +02003013 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3014 if( status != PSA_SUCCESS )
3015 return( status );
3016 /* From here on, tmp needs to be wiped. */
3017
3018 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3019 if( status != PSA_SUCCESS )
3020 goto exit;
3021
3022 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3023 if( status != PSA_SUCCESS )
3024 goto exit;
3025
3026 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3027 if( status != PSA_SUCCESS )
3028 goto exit;
3029
Gilles Peskined911eb72018-08-14 15:18:45 +02003030 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3031 if( status != PSA_SUCCESS )
3032 goto exit;
3033
3034 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003035
3036exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003037 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003038 return( status );
3039}
John Durkop6ba40d12020-11-10 08:50:04 -08003040#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003041
mohammad16036df908f2018-04-02 08:34:15 -07003042static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003043 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003044 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003045{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003046 if( ! operation->key_set )
3047 return( PSA_ERROR_BAD_STATE );
3048 if( operation->iv_required && ! operation->iv_set )
3049 return( PSA_ERROR_BAD_STATE );
3050
Gilles Peskine8c9def32018-02-08 10:02:12 +01003051 if( mac_size < operation->mac_size )
3052 return( PSA_ERROR_BUFFER_TOO_SMALL );
3053
Gilles Peskine8c9def32018-02-08 10:02:12 +01003054#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003055 if( operation->alg == PSA_ALG_CMAC )
3056 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003057 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003058 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3059 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003060 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003061 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003062 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003063 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003064 else
3065#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003066#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003067 if( PSA_ALG_IS_HMAC( operation->alg ) )
3068 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003069 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003070 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003071 }
3072 else
John Durkopd0321952020-10-29 21:37:36 -07003073#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003074 {
3075 /* This shouldn't happen if `operation` was initialized by
3076 * a setup function. */
3077 return( PSA_ERROR_BAD_STATE );
3078 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003079}
3080
Gilles Peskineacd4be32018-07-08 19:56:25 +02003081psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3082 uint8_t *mac,
3083 size_t mac_size,
3084 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003085{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003086 psa_status_t status;
3087
Jaeden Amero252ef282019-02-15 14:05:35 +00003088 if( operation->alg == 0 )
3089 {
3090 return( PSA_ERROR_BAD_STATE );
3091 }
3092
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003093 /* Fill the output buffer with something that isn't a valid mac
3094 * (barring an attack on the mac and deliberately-crafted input),
3095 * in case the caller doesn't check the return status properly. */
3096 *mac_length = mac_size;
3097 /* If mac_size is 0 then mac may be NULL and then the
3098 * call to memset would have undefined behavior. */
3099 if( mac_size != 0 )
3100 memset( mac, '!', mac_size );
3101
Gilles Peskine89167cb2018-07-08 20:12:23 +02003102 if( ! operation->is_sign )
3103 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003104 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003105 }
mohammad16036df908f2018-04-02 08:34:15 -07003106
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003107 status = psa_mac_finish_internal( operation, mac, mac_size );
3108
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003109 if( status == PSA_SUCCESS )
3110 {
3111 status = psa_mac_abort( operation );
3112 if( status == PSA_SUCCESS )
3113 *mac_length = operation->mac_size;
3114 else
3115 memset( mac, '!', mac_size );
3116 }
3117 else
3118 psa_mac_abort( operation );
3119 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003120}
3121
Gilles Peskineacd4be32018-07-08 19:56:25 +02003122psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3123 const uint8_t *mac,
3124 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003125{
Gilles Peskine828ed142018-06-18 23:25:51 +02003126 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003127 psa_status_t status;
3128
Jaeden Amero252ef282019-02-15 14:05:35 +00003129 if( operation->alg == 0 )
3130 {
3131 return( PSA_ERROR_BAD_STATE );
3132 }
3133
Gilles Peskine89167cb2018-07-08 20:12:23 +02003134 if( operation->is_sign )
3135 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003136 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003137 }
3138 if( operation->mac_size != mac_length )
3139 {
3140 status = PSA_ERROR_INVALID_SIGNATURE;
3141 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003142 }
mohammad16036df908f2018-04-02 08:34:15 -07003143
3144 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003145 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003146 if( status != PSA_SUCCESS )
3147 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003148
3149 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3150 status = PSA_ERROR_INVALID_SIGNATURE;
3151
3152cleanup:
3153 if( status == PSA_SUCCESS )
3154 status = psa_mac_abort( operation );
3155 else
3156 psa_mac_abort( operation );
3157
Gilles Peskine3f108122018-12-07 18:14:53 +01003158 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003159
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003160 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003161}
3162
3163
Gilles Peskine20035e32018-02-03 22:44:14 +01003164
Gilles Peskine20035e32018-02-03 22:44:14 +01003165/****************************************************************/
3166/* Asymmetric cryptography */
3167/****************************************************************/
3168
John Durkop6ba40d12020-11-10 08:50:04 -08003169#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3170 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003171/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003172 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003173static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3174 size_t hash_length,
3175 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003176{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003177 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003178 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003179 *md_alg = mbedtls_md_get_type( md_info );
3180
3181 /* The Mbed TLS RSA module uses an unsigned int for hash length
3182 * parameters. Validate that it fits so that we don't risk an
3183 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003184#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003185 if( hash_length > UINT_MAX )
3186 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003187#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003188
John Durkop0e005192020-10-31 22:06:54 -07003189#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003190 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3191 * must be correct. */
3192 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3193 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003194 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003195 if( md_info == NULL )
3196 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003197 if( mbedtls_md_get_size( md_info ) != hash_length )
3198 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003199 }
John Durkop0e005192020-10-31 22:06:54 -07003200#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003201
John Durkop0e005192020-10-31 22:06:54 -07003202#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003203 /* PSS requires a hash internally. */
3204 if( PSA_ALG_IS_RSA_PSS( alg ) )
3205 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003206 if( md_info == NULL )
3207 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003208 }
John Durkop0e005192020-10-31 22:06:54 -07003209#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003210
Gilles Peskine61b91d42018-06-08 16:09:36 +02003211 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003212}
3213
Gilles Peskine2b450e32018-06-27 15:42:46 +02003214static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3215 psa_algorithm_t alg,
3216 const uint8_t *hash,
3217 size_t hash_length,
3218 uint8_t *signature,
3219 size_t signature_size,
3220 size_t *signature_length )
3221{
3222 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003224 mbedtls_md_type_t md_alg;
3225
3226 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3227 if( status != PSA_SUCCESS )
3228 return( status );
3229
Gilles Peskine630a18a2018-06-29 17:49:35 +02003230 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003231 return( PSA_ERROR_BUFFER_TOO_SMALL );
3232
John Durkop0e005192020-10-31 22:06:54 -07003233#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003234 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3235 {
3236 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3237 MBEDTLS_MD_NONE );
3238 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003239 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003240 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003241 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003242 md_alg,
3243 (unsigned int) hash_length,
3244 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003245 signature );
3246 }
3247 else
John Durkop0e005192020-10-31 22:06:54 -07003248#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3249#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003250 if( PSA_ALG_IS_RSA_PSS( alg ) )
3251 {
3252 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3253 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003254 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003255 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003256 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003257 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003258 (unsigned int) hash_length,
3259 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003260 signature );
3261 }
3262 else
John Durkop0e005192020-10-31 22:06:54 -07003263#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003264 {
3265 return( PSA_ERROR_INVALID_ARGUMENT );
3266 }
3267
3268 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003269 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003270 return( mbedtls_to_psa_error( ret ) );
3271}
3272
3273static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3274 psa_algorithm_t alg,
3275 const uint8_t *hash,
3276 size_t hash_length,
3277 const uint8_t *signature,
3278 size_t signature_length )
3279{
3280 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003281 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003282 mbedtls_md_type_t md_alg;
3283
3284 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3285 if( status != PSA_SUCCESS )
3286 return( status );
3287
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003288 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3289 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003290
John Durkop0e005192020-10-31 22:06:54 -07003291#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003292 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3293 {
3294 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3295 MBEDTLS_MD_NONE );
3296 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003297 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003298 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003299 MBEDTLS_RSA_PUBLIC,
3300 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003301 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003302 hash,
3303 signature );
3304 }
3305 else
John Durkop0e005192020-10-31 22:06:54 -07003306#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3307#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003308 if( PSA_ALG_IS_RSA_PSS( alg ) )
3309 {
3310 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3311 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003312 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003313 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003314 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003315 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003316 (unsigned int) hash_length,
3317 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003318 signature );
3319 }
3320 else
John Durkop0e005192020-10-31 22:06:54 -07003321#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003322 {
3323 return( PSA_ERROR_INVALID_ARGUMENT );
3324 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003325
3326 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3327 * the rest of the signature is invalid". This has little use in
3328 * practice and PSA doesn't report this distinction. */
3329 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3330 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003331 return( mbedtls_to_psa_error( ret ) );
3332}
John Durkop6ba40d12020-11-10 08:50:04 -08003333#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3334 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003335
John Durkop6ba40d12020-11-10 08:50:04 -08003336#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3337 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003338/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3339 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3340 * (even though these functions don't modify it). */
3341static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3342 psa_algorithm_t alg,
3343 const uint8_t *hash,
3344 size_t hash_length,
3345 uint8_t *signature,
3346 size_t signature_size,
3347 size_t *signature_length )
3348{
Janos Follath24eed8d2019-11-22 13:21:35 +00003349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003350 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003351 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003352 mbedtls_mpi_init( &r );
3353 mbedtls_mpi_init( &s );
3354
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003355 if( signature_size < 2 * curve_bytes )
3356 {
3357 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3358 goto cleanup;
3359 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003360
John Durkop0ea39e02020-10-13 19:58:20 -07003361#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003362 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3363 {
3364 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3365 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3366 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003367 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3368 &ecp->d, hash,
3369 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003370 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003371 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003372 }
3373 else
John Durkop0ea39e02020-10-13 19:58:20 -07003374#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003375 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003376 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003377 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3378 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003379 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003380 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003381 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003382
3383 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3384 signature,
3385 curve_bytes ) );
3386 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3387 signature + curve_bytes,
3388 curve_bytes ) );
3389
3390cleanup:
3391 mbedtls_mpi_free( &r );
3392 mbedtls_mpi_free( &s );
3393 if( ret == 0 )
3394 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003395 return( mbedtls_to_psa_error( ret ) );
3396}
3397
3398static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3399 const uint8_t *hash,
3400 size_t hash_length,
3401 const uint8_t *signature,
3402 size_t signature_length )
3403{
Janos Follath24eed8d2019-11-22 13:21:35 +00003404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003405 mbedtls_mpi r, s;
3406 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3407 mbedtls_mpi_init( &r );
3408 mbedtls_mpi_init( &s );
3409
3410 if( signature_length != 2 * curve_bytes )
3411 return( PSA_ERROR_INVALID_SIGNATURE );
3412
3413 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3414 signature,
3415 curve_bytes ) );
3416 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3417 signature + curve_bytes,
3418 curve_bytes ) );
3419
Steven Cooremanacda8342020-07-24 23:09:52 +02003420 /* Check whether the public part is loaded. If not, load it. */
3421 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3422 {
3423 MBEDTLS_MPI_CHK(
3424 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003425 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003426 }
3427
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003428 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3429 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003430
3431cleanup:
3432 mbedtls_mpi_free( &r );
3433 mbedtls_mpi_free( &s );
3434 return( mbedtls_to_psa_error( ret ) );
3435}
John Durkop6ba40d12020-11-10 08:50:04 -08003436#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3437 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003438
Ronald Croncf56a0a2020-08-04 09:51:30 +02003439psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003440 psa_algorithm_t alg,
3441 const uint8_t *hash,
3442 size_t hash_length,
3443 uint8_t *signature,
3444 size_t signature_size,
3445 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003446{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003447 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003448 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003449 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003450
3451 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003452 /* Immediately reject a zero-length signature buffer. This guarantees
3453 * that signature must be a valid pointer. (On the other hand, the hash
3454 * buffer can in principle be empty since it doesn't actually have
3455 * to be a hash.) */
3456 if( signature_size == 0 )
3457 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003458
Ronald Cron5c522922020-11-14 16:35:34 +01003459 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3460 PSA_KEY_USAGE_SIGN_HASH,
3461 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003462 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003463 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003464 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003465 {
3466 status = PSA_ERROR_INVALID_ARGUMENT;
3467 goto exit;
3468 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003469
Steven Cooremancd84cb42020-07-16 20:28:36 +02003470 /* Try any of the available accelerators first */
3471 status = psa_driver_wrapper_sign_hash( slot,
3472 alg,
3473 hash,
3474 hash_length,
3475 signature,
3476 signature_size,
3477 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003478 if( status != PSA_ERROR_NOT_SUPPORTED ||
3479 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003480 goto exit;
3481
Steven Cooreman7a250572020-07-17 16:43:05 +02003482 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003483#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3484 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003485 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003486 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003487 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003488
Ronald Cron90857082020-11-25 14:58:33 +01003489 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3490 slot->key.data,
3491 slot->key.bytes,
3492 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003493 if( status != PSA_SUCCESS )
3494 goto exit;
3495
Steven Cooremana2371e52020-07-28 14:30:39 +02003496 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003497 alg,
3498 hash, hash_length,
3499 signature, signature_size,
3500 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003501
Steven Cooremana2371e52020-07-28 14:30:39 +02003502 mbedtls_rsa_free( rsa );
3503 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003504 }
3505 else
John Durkop6ba40d12020-11-10 08:50:04 -08003506#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3507 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003508 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003509 {
John Durkop9814fa22020-11-04 12:28:15 -08003510#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3511 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003512 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003513#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003514 PSA_ALG_IS_ECDSA( alg )
3515#else
3516 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3517#endif
3518 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003519 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003520 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003521 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003522 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003523 slot->key.data,
3524 slot->key.bytes,
3525 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003526 if( status != PSA_SUCCESS )
3527 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003528 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003529 alg,
3530 hash, hash_length,
3531 signature, signature_size,
3532 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003533 mbedtls_ecp_keypair_free( ecp );
3534 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003535 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003536 else
John Durkop9814fa22020-11-04 12:28:15 -08003537#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3538 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003539 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003540 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003541 }
itayzafrir5c753392018-05-08 11:18:38 +03003542 }
3543 else
itayzafrir5c753392018-05-08 11:18:38 +03003544 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003545 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003546 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003547
3548exit:
3549 /* Fill the unused part of the output buffer (the whole buffer on error,
3550 * the trailing part on success) with something that isn't a valid mac
3551 * (barring an attack on the mac and deliberately-crafted input),
3552 * in case the caller doesn't check the return status properly. */
3553 if( status == PSA_SUCCESS )
3554 memset( signature + *signature_length, '!',
3555 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003556 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003557 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003558 /* If signature_size is 0 then we have nothing to do. We must not call
3559 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003560
Ronald Cron5c522922020-11-14 16:35:34 +01003561 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003562
Ronald Cron5c522922020-11-14 16:35:34 +01003563 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003564}
3565
Ronald Croncf56a0a2020-08-04 09:51:30 +02003566psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003567 psa_algorithm_t alg,
3568 const uint8_t *hash,
3569 size_t hash_length,
3570 const uint8_t *signature,
3571 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003572{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003573 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003574 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003575 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003576
Ronald Cron5c522922020-11-14 16:35:34 +01003577 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3578 PSA_KEY_USAGE_VERIFY_HASH,
3579 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003580 if( status != PSA_SUCCESS )
3581 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003582
Steven Cooreman55ae2172020-07-17 19:46:15 +02003583 /* Try any of the available accelerators first */
3584 status = psa_driver_wrapper_verify_hash( slot,
3585 alg,
3586 hash,
3587 hash_length,
3588 signature,
3589 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003590 if( status != PSA_ERROR_NOT_SUPPORTED ||
3591 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003592 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003593
John Durkop6ba40d12020-11-10 08:50:04 -08003594#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3595 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003596 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003597 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003598 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003599
Ronald Cron90857082020-11-25 14:58:33 +01003600 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3601 slot->key.data,
3602 slot->key.bytes,
3603 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003604 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003605 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003606
Steven Cooremana2371e52020-07-28 14:30:39 +02003607 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003608 alg,
3609 hash, hash_length,
3610 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003611 mbedtls_rsa_free( rsa );
3612 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003613 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003614 }
3615 else
John Durkop6ba40d12020-11-10 08:50:04 -08003616#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3617 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003618 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003619 {
John Durkop9814fa22020-11-04 12:28:15 -08003620#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3621 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003622 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003623 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003624 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003625 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003626 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003627 slot->key.data,
3628 slot->key.bytes,
3629 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003630 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003631 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003632 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003633 hash, hash_length,
3634 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003635 mbedtls_ecp_keypair_free( ecp );
3636 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003637 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003638 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003639 else
John Durkop9814fa22020-11-04 12:28:15 -08003640#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3641 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003642 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003643 status = PSA_ERROR_INVALID_ARGUMENT;
3644 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003645 }
itayzafrir5c753392018-05-08 11:18:38 +03003646 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003647 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003648 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003649 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003650 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003651
3652exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003653 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003654
Ronald Cron5c522922020-11-14 16:35:34 +01003655 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003656}
3657
John Durkop0e005192020-10-31 22:06:54 -07003658#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003659static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3660 mbedtls_rsa_context *rsa )
3661{
3662 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3663 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3664 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3665 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3666}
John Durkop0e005192020-10-31 22:06:54 -07003667#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003668
Ronald Croncf56a0a2020-08-04 09:51:30 +02003669psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003670 psa_algorithm_t alg,
3671 const uint8_t *input,
3672 size_t input_length,
3673 const uint8_t *salt,
3674 size_t salt_length,
3675 uint8_t *output,
3676 size_t output_size,
3677 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003678{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003679 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003680 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003681 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003682
Darryl Green5cc689a2018-07-24 15:34:10 +01003683 (void) input;
3684 (void) input_length;
3685 (void) salt;
3686 (void) output;
3687 (void) output_size;
3688
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003689 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003690
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003691 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3692 return( PSA_ERROR_INVALID_ARGUMENT );
3693
Ronald Cron5c522922020-11-14 16:35:34 +01003694 status = psa_get_and_lock_transparent_key_slot_with_policy(
3695 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003696 if( status != PSA_SUCCESS )
3697 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003698 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3699 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003700 {
3701 status = PSA_ERROR_INVALID_ARGUMENT;
3702 goto exit;
3703 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003704
John Durkop6ba40d12020-11-10 08:50:04 -08003705#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3706 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003707 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003708 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003709 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003710 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3711 slot->key.data,
3712 slot->key.bytes,
3713 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003714 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003715 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003716
3717 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003718 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003719 status = PSA_ERROR_BUFFER_TOO_SMALL;
3720 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003721 }
John Durkop0e005192020-10-31 22:06:54 -07003722#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003723 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003724 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003725 status = mbedtls_to_psa_error(
3726 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003727 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003728 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003729 MBEDTLS_RSA_PUBLIC,
3730 input_length,
3731 input,
3732 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003733 }
3734 else
John Durkop0e005192020-10-31 22:06:54 -07003735#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3736#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003737 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003738 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003739 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003740 status = mbedtls_to_psa_error(
3741 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003742 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003743 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003744 MBEDTLS_RSA_PUBLIC,
3745 salt, salt_length,
3746 input_length,
3747 input,
3748 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003749 }
3750 else
John Durkop0e005192020-10-31 22:06:54 -07003751#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003752 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003753 status = PSA_ERROR_INVALID_ARGUMENT;
3754 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003755 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003756rsa_exit:
3757 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003758 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003759
Steven Cooremana2371e52020-07-28 14:30:39 +02003760 mbedtls_rsa_free( rsa );
3761 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003762 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003763 else
John Durkop9814fa22020-11-04 12:28:15 -08003764#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003765 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003766 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003767 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003768 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003769
3770exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003771 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003772
Ronald Cron5c522922020-11-14 16:35:34 +01003773 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003774}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003775
Ronald Croncf56a0a2020-08-04 09:51:30 +02003776psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003777 psa_algorithm_t alg,
3778 const uint8_t *input,
3779 size_t input_length,
3780 const uint8_t *salt,
3781 size_t salt_length,
3782 uint8_t *output,
3783 size_t output_size,
3784 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003785{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003786 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003787 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003788 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003789
Darryl Green5cc689a2018-07-24 15:34:10 +01003790 (void) input;
3791 (void) input_length;
3792 (void) salt;
3793 (void) output;
3794 (void) output_size;
3795
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003796 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003797
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003798 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3799 return( PSA_ERROR_INVALID_ARGUMENT );
3800
Ronald Cron5c522922020-11-14 16:35:34 +01003801 status = psa_get_and_lock_transparent_key_slot_with_policy(
3802 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003803 if( status != PSA_SUCCESS )
3804 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003805 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003806 {
3807 status = PSA_ERROR_INVALID_ARGUMENT;
3808 goto exit;
3809 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003810
John Durkop6ba40d12020-11-10 08:50:04 -08003811#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3812 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003813 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003814 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003815 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003816 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3817 slot->key.data,
3818 slot->key.bytes,
3819 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003820 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003821 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003822
Steven Cooremana2371e52020-07-28 14:30:39 +02003823 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003824 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003825 status = PSA_ERROR_INVALID_ARGUMENT;
3826 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003827 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003828
John Durkop0e005192020-10-31 22:06:54 -07003829#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003830 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003831 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003832 status = mbedtls_to_psa_error(
3833 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003834 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003835 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003836 MBEDTLS_RSA_PRIVATE,
3837 output_length,
3838 input,
3839 output,
3840 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003841 }
3842 else
John Durkop0e005192020-10-31 22:06:54 -07003843#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3844#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003845 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003846 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003847 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003848 status = mbedtls_to_psa_error(
3849 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003850 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003851 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003852 MBEDTLS_RSA_PRIVATE,
3853 salt, salt_length,
3854 output_length,
3855 input,
3856 output,
3857 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003858 }
3859 else
John Durkop0e005192020-10-31 22:06:54 -07003860#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003861 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003862 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003863 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003864
Steven Cooreman4fed4552020-08-03 14:46:03 +02003865rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003866 mbedtls_rsa_free( rsa );
3867 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003868 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003869 else
John Durkop6ba40d12020-11-10 08:50:04 -08003870#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3871 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003873 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003874 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003875
3876exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003877 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003878
Ronald Cron5c522922020-11-14 16:35:34 +01003879 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003880}
Gilles Peskine20035e32018-02-03 22:44:14 +01003881
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003882
3883
mohammad1603503973b2018-03-12 15:59:30 +02003884/****************************************************************/
3885/* Symmetric cryptography */
3886/****************************************************************/
3887
Gilles Peskinee553c652018-06-04 16:22:46 +02003888static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003889 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02003890 psa_algorithm_t alg,
3891 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003892{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003893 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003894 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003895 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003896 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003897 size_t key_bits;
3898 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003899 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3900 PSA_KEY_USAGE_ENCRYPT :
3901 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003902
Steven Cooremand3feccd2020-09-01 15:56:14 +02003903 /* A context must be freshly initialized before it can be set up. */
3904 if( operation->alg != 0 )
3905 return( PSA_ERROR_BAD_STATE );
3906
Steven Cooremana07b9972020-09-10 14:54:14 +02003907 /* The requested algorithm must be one that can be processed by cipher. */
3908 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02003909 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003910
Steven Cooremanef8575e2020-09-11 11:44:50 +02003911 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01003912 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003913 if( status != PSA_SUCCESS )
3914 goto exit;
3915
3916 /* Initialize the operation struct members, except for alg. The alg member
3917 * is used to indicate to psa_cipher_abort that there are resources to free,
3918 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003919 operation->key_set = 0;
3920 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003921 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02003922 operation->iv_size = 0;
3923 operation->block_size = 0;
3924 if( alg == PSA_ALG_ECB_NO_PADDING )
3925 operation->iv_required = 0;
3926 else
3927 operation->iv_required = 1;
3928
Steven Cooremana07b9972020-09-10 14:54:14 +02003929 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02003930 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003931 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003932 slot,
3933 alg );
3934 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003935 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003936 slot,
3937 alg );
3938
Steven Cooremanef8575e2020-09-11 11:44:50 +02003939 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003940 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003941 /* Once the driver context is initialised, it needs to be freed using
3942 * psa_cipher_abort. Indicate this through setting alg. */
3943 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003944 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02003945
Steven Cooremand3feccd2020-09-01 15:56:14 +02003946 if( status != PSA_ERROR_NOT_SUPPORTED ||
3947 psa_key_lifetime_is_external( slot->attr.lifetime ) )
3948 goto exit;
3949
Steven Cooremana07b9972020-09-10 14:54:14 +02003950 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02003951 * available for the given algorithm & key. */
3952 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02003953
Steven Cooremana07b9972020-09-10 14:54:14 +02003954 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02003955 * psa_cipher_abort. Indicate there is something to be freed through setting
3956 * alg, and indicate the operation is being done using mbedtls crypto through
3957 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003958 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003959 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02003960
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003961 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02003962 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003963 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003964 {
3965 status = PSA_ERROR_NOT_SUPPORTED;
3966 goto exit;
3967 }
mohammad1603503973b2018-03-12 15:59:30 +02003968
mohammad1603503973b2018-03-12 15:59:30 +02003969 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003970 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003971 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003972
David Brown12ca5032021-02-11 11:02:00 -07003973#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02003974 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003975 {
3976 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003977 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01003978 memcpy( keys, slot->key.data, 16 );
3979 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003980 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3981 keys,
3982 192, cipher_operation );
3983 }
3984 else
3985#endif
3986 {
3987 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003988 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003989 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003990 }
Moran Peker41deec42018-04-04 15:43:05 +03003991 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003992 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003993
David Brown63ca2602021-01-26 11:51:12 -07003994#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
3995 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003996 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003997 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003998 case PSA_ALG_CBC_NO_PADDING:
3999 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4000 MBEDTLS_PADDING_NONE );
4001 break;
4002 case PSA_ALG_CBC_PKCS7:
4003 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4004 MBEDTLS_PADDING_PKCS7 );
4005 break;
4006 default:
4007 /* The algorithm doesn't involve padding. */
4008 ret = 0;
4009 break;
4010 }
4011 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004012 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07004013#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02004014
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004015 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004016 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004017 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4018 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004019 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004020 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004021 }
David Brown1bfe4d72021-02-16 12:54:35 -07004022#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02004023 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004024 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004025 operation->iv_size = 12;
4026#endif
mohammad1603503973b2018-03-12 15:59:30 +02004027
Steven Cooreman7df02922020-09-09 15:28:49 +02004028 status = PSA_SUCCESS;
4029
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004030exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004031 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004032 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004033 if( status == PSA_SUCCESS )
4034 {
4035 /* Update operation flags for both driver and software implementations */
4036 operation->key_set = 1;
4037 }
4038 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004039 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004040
Ronald Cron5c522922020-11-14 16:35:34 +01004041 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004042
Ronald Cron5c522922020-11-14 16:35:34 +01004043 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004044}
4045
Gilles Peskinefe119512018-07-08 21:39:34 +02004046psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004047 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004048 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004049{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004050 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004051}
4052
Gilles Peskinefe119512018-07-08 21:39:34 +02004053psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004054 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004055 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004056{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004057 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004058}
4059
Gilles Peskinefe119512018-07-08 21:39:34 +02004060psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004061 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004062 size_t iv_size,
4063 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004064{
itayzafrir534bd7c2018-08-02 13:56:32 +03004065 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004067 if( operation->iv_set || ! operation->iv_required )
4068 {
4069 return( PSA_ERROR_BAD_STATE );
4070 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004071
Steven Cooremanef8575e2020-09-11 11:44:50 +02004072 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004073 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004074 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004075 iv,
4076 iv_size,
4077 iv_length );
4078 goto exit;
4079 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004080
Moran Peker41deec42018-04-04 15:43:05 +03004081 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004082 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004083 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004084 goto exit;
4085 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004086 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004087 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004088 if( ret != 0 )
4089 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004090 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004091 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004092 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004093
mohammad16038481e742018-03-18 13:57:31 +02004094 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004095 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004096
Moran Peker395db872018-05-31 14:07:14 +03004097exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004098 if( status == PSA_SUCCESS )
4099 operation->iv_set = 1;
4100 else
Moran Peker395db872018-05-31 14:07:14 +03004101 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004102 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004103}
4104
Gilles Peskinefe119512018-07-08 21:39:34 +02004105psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004106 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004107 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004108{
itayzafrir534bd7c2018-08-02 13:56:32 +03004109 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004111 if( operation->iv_set || ! operation->iv_required )
4112 {
4113 return( PSA_ERROR_BAD_STATE );
4114 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004115
Steven Cooremanef8575e2020-09-11 11:44:50 +02004116 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004117 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004118 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004119 iv,
4120 iv_length );
4121 goto exit;
4122 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004123
Moran Pekera28258c2018-05-29 16:25:04 +03004124 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004125 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004126 status = PSA_ERROR_INVALID_ARGUMENT;
4127 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004128 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004129 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4130 status = mbedtls_to_psa_error( ret );
4131exit:
4132 if( status == PSA_SUCCESS )
4133 operation->iv_set = 1;
4134 else
mohammad1603503973b2018-03-12 15:59:30 +02004135 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004136 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004137}
4138
Steven Cooreman177deba2020-09-07 17:14:14 +02004139/* Process input for which the algorithm is set to ECB mode. This requires
4140 * manual processing, since the PSA API is defined as being able to process
4141 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4142 * underlying mbedtls_cipher_update only takes full blocks. */
4143static psa_status_t psa_cipher_update_ecb_internal(
4144 mbedtls_cipher_context_t *ctx,
4145 const uint8_t *input,
4146 size_t input_length,
4147 uint8_t *output,
4148 size_t output_size,
4149 size_t *output_length )
4150{
4151 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4152 size_t block_size = ctx->cipher_info->block_size;
4153 size_t internal_output_length = 0;
4154 *output_length = 0;
4155
4156 if( input_length == 0 )
4157 {
4158 status = PSA_SUCCESS;
4159 goto exit;
4160 }
4161
4162 if( ctx->unprocessed_len > 0 )
4163 {
4164 /* Fill up to block size, and run the block if there's a full one. */
4165 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4166
4167 if( input_length < bytes_to_copy )
4168 bytes_to_copy = input_length;
4169
4170 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4171 input, bytes_to_copy );
4172 input_length -= bytes_to_copy;
4173 input += bytes_to_copy;
4174 ctx->unprocessed_len += bytes_to_copy;
4175
4176 if( ctx->unprocessed_len == block_size )
4177 {
4178 status = mbedtls_to_psa_error(
4179 mbedtls_cipher_update( ctx,
4180 ctx->unprocessed_data,
4181 block_size,
4182 output, &internal_output_length ) );
4183
4184 if( status != PSA_SUCCESS )
4185 goto exit;
4186
4187 output += internal_output_length;
4188 output_size -= internal_output_length;
4189 *output_length += internal_output_length;
4190 ctx->unprocessed_len = 0;
4191 }
4192 }
4193
4194 while( input_length >= block_size )
4195 {
4196 /* Run all full blocks we have, one by one */
4197 status = mbedtls_to_psa_error(
4198 mbedtls_cipher_update( ctx, input,
4199 block_size,
4200 output, &internal_output_length ) );
4201
4202 if( status != PSA_SUCCESS )
4203 goto exit;
4204
4205 input_length -= block_size;
4206 input += block_size;
4207
4208 output += internal_output_length;
4209 output_size -= internal_output_length;
4210 *output_length += internal_output_length;
4211 }
4212
4213 if( input_length > 0 )
4214 {
4215 /* Save unprocessed bytes for later processing */
4216 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4217 input, input_length );
4218 ctx->unprocessed_len += input_length;
4219 }
4220
4221 status = PSA_SUCCESS;
4222
4223exit:
4224 return( status );
4225}
4226
Gilles Peskinee553c652018-06-04 16:22:46 +02004227psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4228 const uint8_t *input,
4229 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004230 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004231 size_t output_size,
4232 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004233{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004234 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004235 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004236 if( operation->alg == 0 )
4237 {
4238 return( PSA_ERROR_BAD_STATE );
4239 }
4240 if( operation->iv_required && ! operation->iv_set )
4241 {
4242 return( PSA_ERROR_BAD_STATE );
4243 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004244
Steven Cooremanef8575e2020-09-11 11:44:50 +02004245 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004246 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004247 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004248 input,
4249 input_length,
4250 output,
4251 output_size,
4252 output_length );
4253 goto exit;
4254 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004255
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004256 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004257 {
4258 /* Take the unprocessed partial block left over from previous
4259 * update calls, if any, plus the input to this call. Remove
4260 * the last partial block, if any. You get the data that will be
4261 * output in this call. */
4262 expected_output_size =
4263 ( operation->ctx.cipher.unprocessed_len + input_length )
4264 / operation->block_size * operation->block_size;
4265 }
4266 else
4267 {
4268 expected_output_size = input_length;
4269 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004270
Gilles Peskine89d789c2018-06-04 17:17:16 +02004271 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004272 {
4273 status = PSA_ERROR_BUFFER_TOO_SMALL;
4274 goto exit;
4275 }
mohammad160382759612018-03-12 18:16:40 +02004276
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004277 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4278 {
4279 /* mbedtls_cipher_update has an API inconsistency: it will only
4280 * process a single block at a time in ECB mode. Abstract away that
4281 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004282 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4283 input,
4284 input_length,
4285 output,
4286 output_size,
4287 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004288 }
4289 else
4290 {
4291 status = mbedtls_to_psa_error(
4292 mbedtls_cipher_update( &operation->ctx.cipher, input,
4293 input_length, output, output_length ) );
4294 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004295exit:
4296 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004297 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004298 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004299}
4300
Gilles Peskinee553c652018-06-04 16:22:46 +02004301psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4302 uint8_t *output,
4303 size_t output_size,
4304 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004305{
David Saadab4ecc272019-02-14 13:48:10 +02004306 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004307 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004308 if( operation->alg == 0 )
4309 {
4310 return( PSA_ERROR_BAD_STATE );
4311 }
4312 if( operation->iv_required && ! operation->iv_set )
4313 {
4314 return( PSA_ERROR_BAD_STATE );
4315 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004316
Steven Cooremanef8575e2020-09-11 11:44:50 +02004317 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004318 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004319 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004320 output,
4321 output_size,
4322 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004323 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004324 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004325
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004326 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004327 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004328 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004329 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004330 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004331 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004332 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004333 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004334 }
4335
Steven Cooremanef8575e2020-09-11 11:44:50 +02004336 status = mbedtls_to_psa_error(
4337 mbedtls_cipher_finish( &operation->ctx.cipher,
4338 temp_output_buffer,
4339 output_length ) );
4340 if( status != PSA_SUCCESS )
4341 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004342
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004343 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004344 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004345 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004346 memcpy( output, temp_output_buffer, *output_length );
4347 else
Janos Follath315b51c2018-07-09 16:04:51 +01004348 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004349
Steven Cooremanef8575e2020-09-11 11:44:50 +02004350exit:
4351 if( operation->mbedtls_in_use == 1 )
4352 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004353
Steven Cooremanef8575e2020-09-11 11:44:50 +02004354 if( status == PSA_SUCCESS )
4355 return( psa_cipher_abort( operation ) );
4356 else
4357 {
4358 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004359 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004360
Steven Cooremanef8575e2020-09-11 11:44:50 +02004361 return( status );
4362 }
mohammad1603503973b2018-03-12 15:59:30 +02004363}
4364
Gilles Peskinee553c652018-06-04 16:22:46 +02004365psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4366{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004367 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004368 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004369 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004370 * in use. It's ok to call abort on such an object, and there's
4371 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004372 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004373 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004374
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004375 /* Sanity check (shouldn't happen: operation->alg should
4376 * always have been initialized to a valid value). */
4377 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4378 return( PSA_ERROR_BAD_STATE );
4379
Steven Cooremanef8575e2020-09-11 11:44:50 +02004380 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004381 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004382 else
4383 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004384
Moran Peker41deec42018-04-04 15:43:05 +03004385 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004386 operation->key_set = 0;
4387 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004388 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004389 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004390 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004391 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004392
Moran Peker395db872018-05-31 14:07:14 +03004393 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004394}
4395
Gilles Peskinea0655c32018-04-30 17:06:50 +02004396
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004397
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004398
mohammad16035955c982018-04-26 00:53:03 +03004399/****************************************************************/
4400/* AEAD */
4401/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004402
Gilles Peskineedf9a652018-08-17 18:11:56 +02004403typedef struct
4404{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004405 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004406 const mbedtls_cipher_info_t *cipher_info;
4407 union
4408 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004409 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004410#if defined(MBEDTLS_CCM_C)
4411 mbedtls_ccm_context ccm;
4412#endif /* MBEDTLS_CCM_C */
4413#if defined(MBEDTLS_GCM_C)
4414 mbedtls_gcm_context gcm;
4415#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004416#if defined(MBEDTLS_CHACHAPOLY_C)
4417 mbedtls_chachapoly_context chachapoly;
4418#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004419 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004420 psa_algorithm_t core_alg;
4421 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004422 uint8_t tag_length;
4423} aead_operation_t;
4424
Ronald Cronf95a2b12020-10-22 15:24:49 +02004425#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4426
Gilles Peskine30a9e412019-01-14 18:36:12 +01004427static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004428{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004429 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004430 {
4431#if defined(MBEDTLS_CCM_C)
4432 case PSA_ALG_CCM:
4433 mbedtls_ccm_free( &operation->ctx.ccm );
4434 break;
4435#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004436#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004437 case PSA_ALG_GCM:
4438 mbedtls_gcm_free( &operation->ctx.gcm );
4439 break;
4440#endif /* MBEDTLS_GCM_C */
4441 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004442
Ronald Cron5c522922020-11-14 16:35:34 +01004443 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004444}
4445
4446static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004447 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004448 psa_key_usage_t usage,
4449 psa_algorithm_t alg )
4450{
4451 psa_status_t status;
4452 size_t key_bits;
4453 mbedtls_cipher_id_t cipher_id;
4454
Ronald Cron5c522922020-11-14 16:35:34 +01004455 status = psa_get_and_lock_transparent_key_slot_with_policy(
4456 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004457 if( status != PSA_SUCCESS )
4458 return( status );
4459
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004460 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004461
4462 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004463 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004464 &cipher_id );
4465 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004466 {
4467 status = PSA_ERROR_NOT_SUPPORTED;
4468 goto cleanup;
4469 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004470
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004471 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004472 {
4473#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004474 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004475 operation->core_alg = PSA_ALG_CCM;
4476 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004477 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4478 * The call to mbedtls_ccm_encrypt_and_tag or
4479 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004480 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004481 {
4482 status = PSA_ERROR_INVALID_ARGUMENT;
4483 goto cleanup;
4484 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004485 mbedtls_ccm_init( &operation->ctx.ccm );
4486 status = mbedtls_to_psa_error(
4487 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004488 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004489 (unsigned int) key_bits ) );
4490 if( status != 0 )
4491 goto cleanup;
4492 break;
4493#endif /* MBEDTLS_CCM_C */
4494
4495#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004496 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004497 operation->core_alg = PSA_ALG_GCM;
4498 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004499 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4500 * The call to mbedtls_gcm_crypt_and_tag or
4501 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004502 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004503 {
4504 status = PSA_ERROR_INVALID_ARGUMENT;
4505 goto cleanup;
4506 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004507 mbedtls_gcm_init( &operation->ctx.gcm );
4508 status = mbedtls_to_psa_error(
4509 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004510 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004511 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004512 if( status != 0 )
4513 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004514 break;
4515#endif /* MBEDTLS_GCM_C */
4516
Gilles Peskine26869f22019-05-06 15:25:00 +02004517#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004518 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004519 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4520 operation->full_tag_length = 16;
4521 /* We only support the default tag length. */
4522 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004523 {
4524 status = PSA_ERROR_NOT_SUPPORTED;
4525 goto cleanup;
4526 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004527 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4528 status = mbedtls_to_psa_error(
4529 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004530 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004531 if( status != 0 )
4532 goto cleanup;
4533 break;
4534#endif /* MBEDTLS_CHACHAPOLY_C */
4535
Gilles Peskineedf9a652018-08-17 18:11:56 +02004536 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004537 status = PSA_ERROR_NOT_SUPPORTED;
4538 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004539 }
4540
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004541 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4542 {
4543 status = PSA_ERROR_INVALID_ARGUMENT;
4544 goto cleanup;
4545 }
4546 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004547
Gilles Peskineedf9a652018-08-17 18:11:56 +02004548 return( PSA_SUCCESS );
4549
4550cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004551 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004552 return( status );
4553}
4554
Ronald Croncf56a0a2020-08-04 09:51:30 +02004555psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004556 psa_algorithm_t alg,
4557 const uint8_t *nonce,
4558 size_t nonce_length,
4559 const uint8_t *additional_data,
4560 size_t additional_data_length,
4561 const uint8_t *plaintext,
4562 size_t plaintext_length,
4563 uint8_t *ciphertext,
4564 size_t ciphertext_size,
4565 size_t *ciphertext_length )
4566{
mohammad16035955c982018-04-26 00:53:03 +03004567 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004568 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004569 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004570
mohammad1603f08a5502018-06-03 15:05:47 +03004571 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004572
Ronald Croncf56a0a2020-08-04 09:51:30 +02004573 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004574 if( status != PSA_SUCCESS )
4575 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004576
Gilles Peskineedf9a652018-08-17 18:11:56 +02004577 /* For all currently supported modes, the tag is at the end of the
4578 * ciphertext. */
4579 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4580 {
4581 status = PSA_ERROR_BUFFER_TOO_SMALL;
4582 goto exit;
4583 }
4584 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004585
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004586#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004587 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004588 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004589 status = mbedtls_to_psa_error(
4590 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4591 MBEDTLS_GCM_ENCRYPT,
4592 plaintext_length,
4593 nonce, nonce_length,
4594 additional_data, additional_data_length,
4595 plaintext, ciphertext,
4596 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004597 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004598 else
4599#endif /* MBEDTLS_GCM_C */
4600#if defined(MBEDTLS_CCM_C)
4601 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004602 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004603 status = mbedtls_to_psa_error(
4604 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4605 plaintext_length,
4606 nonce, nonce_length,
4607 additional_data,
4608 additional_data_length,
4609 plaintext, ciphertext,
4610 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004611 }
mohammad16035c8845f2018-05-09 05:40:09 -07004612 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004613#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004614#if defined(MBEDTLS_CHACHAPOLY_C)
4615 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4616 {
4617 if( nonce_length != 12 || operation.tag_length != 16 )
4618 {
4619 status = PSA_ERROR_NOT_SUPPORTED;
4620 goto exit;
4621 }
4622 status = mbedtls_to_psa_error(
4623 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4624 plaintext_length,
4625 nonce,
4626 additional_data,
4627 additional_data_length,
4628 plaintext,
4629 ciphertext,
4630 tag ) );
4631 }
4632 else
4633#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004634 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004635 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004636 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004637 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004638
Gilles Peskineedf9a652018-08-17 18:11:56 +02004639 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4640 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004641
Gilles Peskineedf9a652018-08-17 18:11:56 +02004642exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004643 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004644 if( status == PSA_SUCCESS )
4645 *ciphertext_length = plaintext_length + operation.tag_length;
4646 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004647}
4648
Gilles Peskineee652a32018-06-01 19:23:52 +02004649/* Locate the tag in a ciphertext buffer containing the encrypted data
4650 * followed by the tag. Return the length of the part preceding the tag in
4651 * *plaintext_length. This is the size of the plaintext in modes where
4652 * the encrypted data has the same size as the plaintext, such as
4653 * CCM and GCM. */
4654static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4655 const uint8_t *ciphertext,
4656 size_t ciphertext_length,
4657 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004658 const uint8_t **p_tag )
4659{
4660 size_t payload_length;
4661 if( tag_length > ciphertext_length )
4662 return( PSA_ERROR_INVALID_ARGUMENT );
4663 payload_length = ciphertext_length - tag_length;
4664 if( payload_length > plaintext_size )
4665 return( PSA_ERROR_BUFFER_TOO_SMALL );
4666 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004667 return( PSA_SUCCESS );
4668}
4669
Ronald Croncf56a0a2020-08-04 09:51:30 +02004670psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004671 psa_algorithm_t alg,
4672 const uint8_t *nonce,
4673 size_t nonce_length,
4674 const uint8_t *additional_data,
4675 size_t additional_data_length,
4676 const uint8_t *ciphertext,
4677 size_t ciphertext_length,
4678 uint8_t *plaintext,
4679 size_t plaintext_size,
4680 size_t *plaintext_length )
4681{
mohammad16035955c982018-04-26 00:53:03 +03004682 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004683 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004684 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004685
Gilles Peskineee652a32018-06-01 19:23:52 +02004686 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004687
Ronald Croncf56a0a2020-08-04 09:51:30 +02004688 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004689 if( status != PSA_SUCCESS )
4690 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004691
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004692 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4693 ciphertext, ciphertext_length,
4694 plaintext_size, &tag );
4695 if( status != PSA_SUCCESS )
4696 goto exit;
4697
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004698#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004699 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004700 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004701 status = mbedtls_to_psa_error(
4702 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4703 ciphertext_length - operation.tag_length,
4704 nonce, nonce_length,
4705 additional_data,
4706 additional_data_length,
4707 tag, operation.tag_length,
4708 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004709 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004710 else
4711#endif /* MBEDTLS_GCM_C */
4712#if defined(MBEDTLS_CCM_C)
4713 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004714 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004715 status = mbedtls_to_psa_error(
4716 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4717 ciphertext_length - operation.tag_length,
4718 nonce, nonce_length,
4719 additional_data,
4720 additional_data_length,
4721 ciphertext, plaintext,
4722 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004723 }
mohammad160339574652018-06-01 04:39:53 -07004724 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004725#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004726#if defined(MBEDTLS_CHACHAPOLY_C)
4727 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4728 {
4729 if( nonce_length != 12 || operation.tag_length != 16 )
4730 {
4731 status = PSA_ERROR_NOT_SUPPORTED;
4732 goto exit;
4733 }
4734 status = mbedtls_to_psa_error(
4735 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4736 ciphertext_length - operation.tag_length,
4737 nonce,
4738 additional_data,
4739 additional_data_length,
4740 tag,
4741 ciphertext,
4742 plaintext ) );
4743 }
4744 else
4745#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004746 {
mohammad1603554faad2018-06-03 15:07:38 +03004747 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004748 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004749
Gilles Peskineedf9a652018-08-17 18:11:56 +02004750 if( status != PSA_SUCCESS && plaintext_size != 0 )
4751 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004752
Gilles Peskineedf9a652018-08-17 18:11:56 +02004753exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004754 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004755 if( status == PSA_SUCCESS )
4756 *plaintext_length = ciphertext_length - operation.tag_length;
4757 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004758}
4759
Gilles Peskinea0655c32018-04-30 17:06:50 +02004760
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004761
Gilles Peskine20035e32018-02-03 22:44:14 +01004762/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004763/* Generators */
4764/****************************************************************/
4765
John Durkop07cc04a2020-11-16 22:08:34 -08004766#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4767 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4768 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4769#define AT_LEAST_ONE_BUILTIN_KDF
4770#endif
4771
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004772#define HKDF_STATE_INIT 0 /* no input yet */
4773#define HKDF_STATE_STARTED 1 /* got salt */
4774#define HKDF_STATE_KEYED 2 /* got key */
4775#define HKDF_STATE_OUTPUT 3 /* output started */
4776
Gilles Peskinecbe66502019-05-16 16:59:18 +02004777static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004779{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004780 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4781 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004782 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004783 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004784}
4785
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004786psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004787{
4788 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004790 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004791 {
4792 /* The object has (apparently) been initialized but it is not
4793 * in use. It's ok to call abort on such an object, and there's
4794 * nothing to do. */
4795 }
4796 else
John Durkopd0321952020-10-29 21:37:36 -07004797#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004798 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004799 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004800 mbedtls_free( operation->ctx.hkdf.info );
4801 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004802 }
John Durkop07cc04a2020-11-16 22:08:34 -08004803 else
4804#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4805#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4806 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4807 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004809 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004810 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004811 if( operation->ctx.tls12_prf.seed != NULL )
4812 {
4813 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4814 operation->ctx.tls12_prf.seed_length );
4815 mbedtls_free( operation->ctx.tls12_prf.seed );
4816 }
4817
4818 if( operation->ctx.tls12_prf.label != NULL )
4819 {
4820 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4821 operation->ctx.tls12_prf.label_length );
4822 mbedtls_free( operation->ctx.tls12_prf.label );
4823 }
4824
4825 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4826
4827 /* We leave the fields Ai and output_block to be erased safely by the
4828 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004829 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004830 else
John Durkop07cc04a2020-11-16 22:08:34 -08004831#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4832 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004833 {
4834 status = PSA_ERROR_BAD_STATE;
4835 }
Janos Follath083036a2019-06-11 10:22:26 +01004836 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004837 return( status );
4838}
4839
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004840psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004841 size_t *capacity)
4842{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004844 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004846 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004847 }
4848
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004850 return( PSA_SUCCESS );
4851}
4852
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004854 size_t capacity )
4855{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004857 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004859 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004860 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004861 return( PSA_SUCCESS );
4862}
4863
John Durkopd0321952020-10-29 21:37:36 -07004864#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004866 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004867static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004868 psa_algorithm_t hash_alg,
4869 uint8_t *output,
4870 size_t output_length )
4871{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004872 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004873 psa_status_t status;
4874
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004875 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4876 return( PSA_ERROR_BAD_STATE );
4877 hkdf->state = HKDF_STATE_OUTPUT;
4878
Gilles Peskinebef7f142018-07-12 17:22:21 +02004879 while( output_length != 0 )
4880 {
4881 /* Copy what remains of the current block */
4882 uint8_t n = hash_length - hkdf->offset_in_block;
4883 if( n > output_length )
4884 n = (uint8_t) output_length;
4885 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4886 output += n;
4887 output_length -= n;
4888 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004889 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004890 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004891 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004892 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004893 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004894 * object was corrupted or if this function is called directly
4895 * inside the library. */
4896 if( hkdf->block_number == 0xff )
4897 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004898
4899 /* We need a new block */
4900 ++hkdf->block_number;
4901 hkdf->offset_in_block = 0;
4902 status = psa_hmac_setup_internal( &hkdf->hmac,
4903 hkdf->prk, hash_length,
4904 hash_alg );
4905 if( status != PSA_SUCCESS )
4906 return( status );
4907 if( hkdf->block_number != 1 )
4908 {
4909 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4910 hkdf->output_block,
4911 hash_length );
4912 if( status != PSA_SUCCESS )
4913 return( status );
4914 }
4915 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4916 hkdf->info,
4917 hkdf->info_length );
4918 if( status != PSA_SUCCESS )
4919 return( status );
4920 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4921 &hkdf->block_number, 1 );
4922 if( status != PSA_SUCCESS )
4923 return( status );
4924 status = psa_hmac_finish_internal( &hkdf->hmac,
4925 hkdf->output_block,
4926 sizeof( hkdf->output_block ) );
4927 if( status != PSA_SUCCESS )
4928 return( status );
4929 }
4930
4931 return( PSA_SUCCESS );
4932}
John Durkop07cc04a2020-11-16 22:08:34 -08004933#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004934
John Durkop07cc04a2020-11-16 22:08:34 -08004935#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4936 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004937static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4938 psa_tls12_prf_key_derivation_t *tls12_prf,
4939 psa_algorithm_t alg )
4940{
4941 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004942 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004943 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4944 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004945
Janos Follath7742fee2019-06-17 12:58:10 +01004946 /* We can't be wanting more output after block 0xff, otherwise
4947 * the capacity check in psa_key_derivation_output_bytes() would have
4948 * prevented this call. It could happen only if the operation
4949 * object was corrupted or if this function is called directly
4950 * inside the library. */
4951 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004952 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004953
4954 /* We need a new block */
4955 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004956 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004957
4958 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4959 *
4960 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4961 *
4962 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4963 * HMAC_hash(secret, A(2) + seed) +
4964 * HMAC_hash(secret, A(3) + seed) + ...
4965 *
4966 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004967 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004968 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004969 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004970 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004971 * is currently extracted as `output_block` and where i is
4972 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004973 */
4974
Janos Follathea29bfb2019-06-19 12:21:20 +01004975 /* Save the hash context before using it, to preserve the hash state with
4976 * only the inner padding in it. We need this, because inner padding depends
4977 * on the key (secret in the RFC's terminology). */
4978 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4979 if( status != PSA_SUCCESS )
4980 goto cleanup;
4981
4982 /* Calculate A(i) where i = tls12_prf->block_number. */
4983 if( tls12_prf->block_number == 1 )
4984 {
4985 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4986 * the variable seed and in this instance means it in the context of the
4987 * P_hash function, where seed = label + seed.) */
4988 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4989 tls12_prf->label, tls12_prf->label_length );
4990 if( status != PSA_SUCCESS )
4991 goto cleanup;
4992 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4993 tls12_prf->seed, tls12_prf->seed_length );
4994 if( status != PSA_SUCCESS )
4995 goto cleanup;
4996 }
4997 else
4998 {
4999 /* A(i) = HMAC_hash(secret, A(i-1)) */
5000 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5001 tls12_prf->Ai, hash_length );
5002 if( status != PSA_SUCCESS )
5003 goto cleanup;
5004 }
5005
5006 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5007 tls12_prf->Ai, hash_length );
5008 if( status != PSA_SUCCESS )
5009 goto cleanup;
5010 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5011 if( status != PSA_SUCCESS )
5012 goto cleanup;
5013
5014 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5015 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5016 tls12_prf->Ai, hash_length );
5017 if( status != PSA_SUCCESS )
5018 goto cleanup;
5019 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5020 tls12_prf->label, tls12_prf->label_length );
5021 if( status != PSA_SUCCESS )
5022 goto cleanup;
5023 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5024 tls12_prf->seed, tls12_prf->seed_length );
5025 if( status != PSA_SUCCESS )
5026 goto cleanup;
5027 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5028 tls12_prf->output_block, hash_length );
5029 if( status != PSA_SUCCESS )
5030 goto cleanup;
5031 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5032 if( status != PSA_SUCCESS )
5033 goto cleanup;
5034
Janos Follath7742fee2019-06-17 12:58:10 +01005035
5036cleanup:
5037
Janos Follathea29bfb2019-06-19 12:21:20 +01005038 cleanup_status = psa_hash_abort( &backup );
5039 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5040 status = cleanup_status;
5041
5042 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005043}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005044
Janos Follath844eb0e2019-06-19 12:10:49 +01005045static psa_status_t psa_key_derivation_tls12_prf_read(
5046 psa_tls12_prf_key_derivation_t *tls12_prf,
5047 psa_algorithm_t alg,
5048 uint8_t *output,
5049 size_t output_length )
5050{
5051 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005052 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005053 psa_status_t status;
5054 uint8_t offset, length;
5055
5056 while( output_length != 0 )
5057 {
5058 /* Check if we have fully processed the current block. */
5059 if( tls12_prf->left_in_block == 0 )
5060 {
5061 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5062 alg );
5063 if( status != PSA_SUCCESS )
5064 return( status );
5065
5066 continue;
5067 }
5068
5069 if( tls12_prf->left_in_block > output_length )
5070 length = (uint8_t) output_length;
5071 else
5072 length = tls12_prf->left_in_block;
5073
5074 offset = hash_length - tls12_prf->left_in_block;
5075 memcpy( output, tls12_prf->output_block + offset, length );
5076 output += length;
5077 output_length -= length;
5078 tls12_prf->left_in_block -= length;
5079 }
5080
5081 return( PSA_SUCCESS );
5082}
John Durkop07cc04a2020-11-16 22:08:34 -08005083#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5084 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005085
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005086psa_status_t psa_key_derivation_output_bytes(
5087 psa_key_derivation_operation_t *operation,
5088 uint8_t *output,
5089 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005090{
5091 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005092 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005093
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005095 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005096 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005097 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005098 }
5099
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005100 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005101 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005102 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005103 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005104 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005105 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005106 goto exit;
5107 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005109 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005111 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005112 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5113 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005114 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005115 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005116 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005117 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005118 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005119
John Durkopd0321952020-10-29 21:37:36 -07005120#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005121 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005122 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005123 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005124 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005125 output, output_length );
5126 }
Janos Follathadbec812019-06-14 11:05:39 +01005127 else
John Durkop07cc04a2020-11-16 22:08:34 -08005128#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5129#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5130 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005131 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005132 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005133 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005135 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005136 output_length );
5137 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005138 else
John Durkop07cc04a2020-11-16 22:08:34 -08005139#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5140 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005141 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005142 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005143 return( PSA_ERROR_BAD_STATE );
5144 }
5145
5146exit:
5147 if( status != PSA_SUCCESS )
5148 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005149 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005150 * This allows us to differentiate between exhausted operations and
5151 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5152 * operations. */
5153 psa_algorithm_t alg = operation->alg;
5154 psa_key_derivation_abort( operation );
5155 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005156 memset( output, '!', output_length );
5157 }
5158 return( status );
5159}
5160
David Brown7807bf72021-02-09 16:28:23 -07005161#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02005162static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5163{
5164 if( data_size >= 8 )
5165 mbedtls_des_key_set_parity( data );
5166 if( data_size >= 16 )
5167 mbedtls_des_key_set_parity( data + 8 );
5168 if( data_size >= 24 )
5169 mbedtls_des_key_set_parity( data + 16 );
5170}
David Brown7807bf72021-02-09 16:28:23 -07005171#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005172
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005173static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005174 psa_key_slot_t *slot,
5175 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005176 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005177{
5178 uint8_t *data = NULL;
5179 size_t bytes = PSA_BITS_TO_BYTES( bits );
5180 psa_status_t status;
5181
Gilles Peskine8e338702019-07-30 20:06:31 +02005182 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005183 return( PSA_ERROR_INVALID_ARGUMENT );
5184 if( bits % 8 != 0 )
5185 return( PSA_ERROR_INVALID_ARGUMENT );
5186 data = mbedtls_calloc( 1, bytes );
5187 if( data == NULL )
5188 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5189
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005191 if( status != PSA_SUCCESS )
5192 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005193#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02005194 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005195 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07005196#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01005197
5198 status = psa_allocate_buffer_to_slot( slot, bytes );
5199 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00005200 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01005201
Ronald Cronbf33c932020-11-28 18:06:53 +01005202 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005203 psa_key_attributes_t attributes = {
5204 .core = slot->attr
5205 };
5206
Ronald Cronbf33c932020-11-28 18:06:53 +01005207 status = psa_driver_wrapper_import_key( &attributes,
5208 data, bytes,
5209 slot->key.data,
5210 slot->key.bytes,
5211 &slot->key.bytes, &bits );
5212 if( bits != slot->attr.bits )
5213 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005214
5215exit:
5216 mbedtls_free( data );
5217 return( status );
5218}
5219
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005220psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005222 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005223{
5224 psa_status_t status;
5225 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005226 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005227
Ronald Cron81709fc2020-11-14 12:10:32 +01005228 *key = MBEDTLS_SVC_KEY_ID_INIT;
5229
Gilles Peskine0f84d622019-09-12 19:03:13 +02005230 /* Reject any attempt to create a zero-length key so that we don't
5231 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5232 if( psa_get_key_bits( attributes ) == 0 )
5233 return( PSA_ERROR_INVALID_ARGUMENT );
5234
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005235 if( ! operation->can_output_key )
5236 return( PSA_ERROR_NOT_PERMITTED );
5237
Ronald Cron81709fc2020-11-14 12:10:32 +01005238 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5239 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005240#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5241 if( driver != NULL )
5242 {
5243 /* Deriving a key in a secure element is not implemented yet. */
5244 status = PSA_ERROR_NOT_SUPPORTED;
5245 }
5246#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005247 if( status == PSA_SUCCESS )
5248 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005249 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005250 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005252 }
5253 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005254 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005255 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005256 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005257
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005258 return( status );
5259}
5260
Gilles Peskineeab56e42018-07-12 17:12:33 +02005261
5262
5263/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005264/* Key derivation */
5265/****************************************************************/
5266
Steven Cooreman932ffb72021-02-15 12:14:32 +01005267#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005268static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005270 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005271{
John Durkop07cc04a2020-11-16 22:08:34 -08005272 int is_kdf_alg_supported;
5273
Janos Follath5fe19732019-06-20 15:09:30 +01005274 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5275 * initialisers for this union leave some bytes unspecified.) */
5276 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5277
Gilles Peskine969c5d62019-01-16 15:53:06 +01005278 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005279#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005280 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5281 is_kdf_alg_supported = 1;
5282 else
5283#endif
5284#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5285 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5286 is_kdf_alg_supported = 1;
5287 else
5288#endif
5289#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5290 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5291 is_kdf_alg_supported = 1;
5292 else
5293#endif
5294 is_kdf_alg_supported = 0;
5295
5296 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005297 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005298 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005299 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005300 if( hash_size == 0 )
5301 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005302 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5303 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005304 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005305 {
5306 return( PSA_ERROR_NOT_SUPPORTED );
5307 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005308 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005309 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005310 }
John Durkop07cc04a2020-11-16 22:08:34 -08005311
5312 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005313}
John Durkop07cc04a2020-11-16 22:08:34 -08005314#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005315
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005316psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005317 psa_algorithm_t alg )
5318{
5319 psa_status_t status;
5320
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005321 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005322 return( PSA_ERROR_BAD_STATE );
5323
Gilles Peskine6843c292019-01-18 16:44:49 +01005324 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5325 return( PSA_ERROR_INVALID_ARGUMENT );
5326 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005327 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005328#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005329 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005330 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005331#else
5332 return( PSA_ERROR_NOT_SUPPORTED );
5333#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005334 }
5335 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5336 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005337#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005339#else
5340 return( PSA_ERROR_NOT_SUPPORTED );
5341#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005342 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005343 else
5344 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005345
5346 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005348 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005349}
5350
John Durkopd0321952020-10-29 21:37:36 -07005351#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005352static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005353 psa_algorithm_t hash_alg,
5354 psa_key_derivation_step_t step,
5355 const uint8_t *data,
5356 size_t data_length )
5357{
5358 psa_status_t status;
5359 switch( step )
5360 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005361 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005362 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005363 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005364 status = psa_hmac_setup_internal( &hkdf->hmac,
5365 data, data_length,
5366 hash_alg );
5367 if( status != PSA_SUCCESS )
5368 return( status );
5369 hkdf->state = HKDF_STATE_STARTED;
5370 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005371 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005372 /* If no salt was provided, use an empty salt. */
5373 if( hkdf->state == HKDF_STATE_INIT )
5374 {
5375 status = psa_hmac_setup_internal( &hkdf->hmac,
5376 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005377 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005378 if( status != PSA_SUCCESS )
5379 return( status );
5380 hkdf->state = HKDF_STATE_STARTED;
5381 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005382 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005383 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005384 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5385 data, data_length );
5386 if( status != PSA_SUCCESS )
5387 return( status );
5388 status = psa_hmac_finish_internal( &hkdf->hmac,
5389 hkdf->prk,
5390 sizeof( hkdf->prk ) );
5391 if( status != PSA_SUCCESS )
5392 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005393 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005394 hkdf->block_number = 0;
5395 hkdf->state = HKDF_STATE_KEYED;
5396 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005397 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005398 if( hkdf->state == HKDF_STATE_OUTPUT )
5399 return( PSA_ERROR_BAD_STATE );
5400 if( hkdf->info_set )
5401 return( PSA_ERROR_BAD_STATE );
5402 hkdf->info_length = data_length;
5403 if( data_length != 0 )
5404 {
5405 hkdf->info = mbedtls_calloc( 1, data_length );
5406 if( hkdf->info == NULL )
5407 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5408 memcpy( hkdf->info, data, data_length );
5409 }
5410 hkdf->info_set = 1;
5411 return( PSA_SUCCESS );
5412 default:
5413 return( PSA_ERROR_INVALID_ARGUMENT );
5414 }
5415}
John Durkop07cc04a2020-11-16 22:08:34 -08005416#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005417
John Durkop07cc04a2020-11-16 22:08:34 -08005418#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5419 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005420static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5421 const uint8_t *data,
5422 size_t data_length )
5423{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005424 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005425 return( PSA_ERROR_BAD_STATE );
5426
Janos Follathd6dce9f2019-07-04 09:11:38 +01005427 if( data_length != 0 )
5428 {
5429 prf->seed = mbedtls_calloc( 1, data_length );
5430 if( prf->seed == NULL )
5431 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005432
Janos Follathd6dce9f2019-07-04 09:11:38 +01005433 memcpy( prf->seed, data, data_length );
5434 prf->seed_length = data_length;
5435 }
Janos Follathf08e2652019-06-13 09:05:41 +01005436
Gilles Peskine43f958b2020-12-13 14:55:14 +01005437 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005438
5439 return( PSA_SUCCESS );
5440}
5441
Janos Follath81550542019-06-13 14:26:34 +01005442static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5443 psa_algorithm_t hash_alg,
5444 const uint8_t *data,
5445 size_t data_length )
5446{
5447 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005448 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005449 return( PSA_ERROR_BAD_STATE );
5450
5451 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5452 if( status != PSA_SUCCESS )
5453 return( status );
5454
Gilles Peskine43f958b2020-12-13 14:55:14 +01005455 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005456
5457 return( PSA_SUCCESS );
5458}
5459
Janos Follath63028dd2019-06-13 09:15:47 +01005460static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5461 const uint8_t *data,
5462 size_t data_length )
5463{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005464 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005465 return( PSA_ERROR_BAD_STATE );
5466
Janos Follathd6dce9f2019-07-04 09:11:38 +01005467 if( data_length != 0 )
5468 {
5469 prf->label = mbedtls_calloc( 1, data_length );
5470 if( prf->label == NULL )
5471 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005472
Janos Follathd6dce9f2019-07-04 09:11:38 +01005473 memcpy( prf->label, data, data_length );
5474 prf->label_length = data_length;
5475 }
Janos Follath63028dd2019-06-13 09:15:47 +01005476
Gilles Peskine43f958b2020-12-13 14:55:14 +01005477 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005478
5479 return( PSA_SUCCESS );
5480}
5481
Janos Follathb03233e2019-06-11 15:30:30 +01005482static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5483 psa_algorithm_t hash_alg,
5484 psa_key_derivation_step_t step,
5485 const uint8_t *data,
5486 size_t data_length )
5487{
Janos Follathb03233e2019-06-11 15:30:30 +01005488 switch( step )
5489 {
Janos Follathf08e2652019-06-13 09:05:41 +01005490 case PSA_KEY_DERIVATION_INPUT_SEED:
5491 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005492 case PSA_KEY_DERIVATION_INPUT_SECRET:
5493 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005494 case PSA_KEY_DERIVATION_INPUT_LABEL:
5495 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005496 default:
5497 return( PSA_ERROR_INVALID_ARGUMENT );
5498 }
5499}
John Durkop07cc04a2020-11-16 22:08:34 -08005500#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5501 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5502
5503#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5504static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5505 psa_tls12_prf_key_derivation_t *prf,
5506 psa_algorithm_t hash_alg,
5507 const uint8_t *data,
5508 size_t data_length )
5509{
5510 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005511 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005512 uint8_t *cur = pms;
5513
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005514 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005515 return( PSA_ERROR_INVALID_ARGUMENT );
5516
5517 /* Quoting RFC 4279, Section 2:
5518 *
5519 * The premaster secret is formed as follows: if the PSK is N octets
5520 * long, concatenate a uint16 with the value N, N zero octets, a second
5521 * uint16 with the value N, and the PSK itself.
5522 */
5523
5524 *cur++ = ( data_length >> 8 ) & 0xff;
5525 *cur++ = ( data_length >> 0 ) & 0xff;
5526 memset( cur, 0, data_length );
5527 cur += data_length;
5528 *cur++ = pms[0];
5529 *cur++ = pms[1];
5530 memcpy( cur, data, data_length );
5531 cur += data_length;
5532
5533 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5534
5535 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5536 return( status );
5537}
Janos Follath6660f0e2019-06-17 08:44:03 +01005538
5539static psa_status_t psa_tls12_prf_psk_to_ms_input(
5540 psa_tls12_prf_key_derivation_t *prf,
5541 psa_algorithm_t hash_alg,
5542 psa_key_derivation_step_t step,
5543 const uint8_t *data,
5544 size_t data_length )
5545{
5546 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005547 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005548 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5549 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005550 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005551
5552 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5553}
John Durkop07cc04a2020-11-16 22:08:34 -08005554#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005555
Gilles Peskineb8965192019-09-24 16:21:10 +02005556/** Check whether the given key type is acceptable for the given
5557 * input step of a key derivation.
5558 *
5559 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5560 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5561 * Both secret and non-secret inputs can alternatively have the type
5562 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5563 * that the input was passed as a buffer rather than via a key object.
5564 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005565static int psa_key_derivation_check_input_type(
5566 psa_key_derivation_step_t step,
5567 psa_key_type_t key_type )
5568{
5569 switch( step )
5570 {
5571 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005572 if( key_type == PSA_KEY_TYPE_DERIVE )
5573 return( PSA_SUCCESS );
5574 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005575 return( PSA_SUCCESS );
5576 break;
5577 case PSA_KEY_DERIVATION_INPUT_LABEL:
5578 case PSA_KEY_DERIVATION_INPUT_SALT:
5579 case PSA_KEY_DERIVATION_INPUT_INFO:
5580 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005581 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5582 return( PSA_SUCCESS );
5583 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005584 return( PSA_SUCCESS );
5585 break;
5586 }
5587 return( PSA_ERROR_INVALID_ARGUMENT );
5588}
5589
Janos Follathb80a94e2019-06-12 15:54:46 +01005590static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005591 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005592 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005593 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005594 const uint8_t *data,
5595 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005596{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005597 psa_status_t status;
5598 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5599
5600 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005601 if( status != PSA_SUCCESS )
5602 goto exit;
5603
John Durkopd0321952020-10-29 21:37:36 -07005604#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005605 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005606 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005607 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005608 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005609 step, data, data_length );
5610 }
John Durkop07cc04a2020-11-16 22:08:34 -08005611 else
5612#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5613#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5614 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005615 {
Janos Follathb03233e2019-06-11 15:30:30 +01005616 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5617 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5618 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005619 }
John Durkop07cc04a2020-11-16 22:08:34 -08005620 else
5621#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5622#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5623 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005624 {
5625 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5626 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5627 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005628 }
5629 else
John Durkop07cc04a2020-11-16 22:08:34 -08005630#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005631 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005632 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005633 (void) data;
5634 (void) data_length;
5635 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005636 return( PSA_ERROR_BAD_STATE );
5637 }
5638
Gilles Peskine224b0d62019-09-23 18:13:17 +02005639exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005640 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005641 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005642 return( status );
5643}
5644
Janos Follath51f4a0f2019-06-14 11:35:55 +01005645psa_status_t psa_key_derivation_input_bytes(
5646 psa_key_derivation_operation_t *operation,
5647 psa_key_derivation_step_t step,
5648 const uint8_t *data,
5649 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005650{
Gilles Peskineb8965192019-09-24 16:21:10 +02005651 return( psa_key_derivation_input_internal( operation, step,
5652 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005653 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005654}
5655
Janos Follath51f4a0f2019-06-14 11:35:55 +01005656psa_status_t psa_key_derivation_input_key(
5657 psa_key_derivation_operation_t *operation,
5658 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005659 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005660{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005661 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005662 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005663 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005664
Ronald Cron5c522922020-11-14 16:35:34 +01005665 status = psa_get_and_lock_transparent_key_slot_with_policy(
5666 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005667 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005668 {
5669 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005670 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005671 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005672
5673 /* Passing a key object as a SECRET input unlocks the permission
5674 * to output to a key object. */
5675 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5676 operation->can_output_key = 1;
5677
Ronald Cronf95a2b12020-10-22 15:24:49 +02005678 status = psa_key_derivation_input_internal( operation,
5679 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005680 slot->key.data,
5681 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005682
Ronald Cron5c522922020-11-14 16:35:34 +01005683 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005684
Ronald Cron5c522922020-11-14 16:35:34 +01005685 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005686}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005687
5688
5689
5690/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005691/* Key agreement */
5692/****************************************************************/
5693
John Durkop6ba40d12020-11-10 08:50:04 -08005694#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005695static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5696 size_t peer_key_length,
5697 const mbedtls_ecp_keypair *our_key,
5698 uint8_t *shared_secret,
5699 size_t shared_secret_size,
5700 size_t *shared_secret_length )
5701{
Steven Cooremand4867872020-08-05 16:31:39 +02005702 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005703 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005704 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005705 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005706 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005707 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005708
Ronald Cron90857082020-11-25 14:58:33 +01005709 status = mbedtls_psa_ecp_load_representation(
5710 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005711 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005712 peer_key,
5713 peer_key_length,
5714 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005715 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005716 goto exit;
5717
Jaeden Amero97271b32019-01-10 19:38:51 +00005718 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005719 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005720 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005721 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005722 status = mbedtls_to_psa_error(
5723 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5724 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005725 goto exit;
5726
Jaeden Amero97271b32019-01-10 19:38:51 +00005727 status = mbedtls_to_psa_error(
5728 mbedtls_ecdh_calc_secret( &ecdh,
5729 shared_secret_length,
5730 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005731 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005732 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005733 if( status != PSA_SUCCESS )
5734 goto exit;
5735 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5736 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005737
5738exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005739 if( status != PSA_SUCCESS )
5740 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005741 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005742 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005743 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005744
Jaeden Amero97271b32019-01-10 19:38:51 +00005745 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005746}
John Durkop6ba40d12020-11-10 08:50:04 -08005747#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005748
Gilles Peskine01d718c2018-09-18 12:01:02 +02005749#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5750
Gilles Peskine0216fe12019-04-11 21:23:21 +02005751static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5752 psa_key_slot_t *private_key,
5753 const uint8_t *peer_key,
5754 size_t peer_key_length,
5755 uint8_t *shared_secret,
5756 size_t shared_secret_size,
5757 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005758{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005759 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005760 {
John Durkop6ba40d12020-11-10 08:50:04 -08005761#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005762 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005763 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005764 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005765 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005766 psa_status_t status = mbedtls_psa_ecp_load_representation(
5767 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005768 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005769 private_key->key.data,
5770 private_key->key.bytes,
5771 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005772 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005773 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005774 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005775 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005776 shared_secret, shared_secret_size,
5777 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005778 mbedtls_ecp_keypair_free( ecp );
5779 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005780 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005781#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005782 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005783 (void) private_key;
5784 (void) peer_key;
5785 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005786 (void) shared_secret;
5787 (void) shared_secret_size;
5788 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005789 return( PSA_ERROR_NOT_SUPPORTED );
5790 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005791}
5792
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005793/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005794 * to potentially free embedded data structures and wipe confidential data.
5795 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005796static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005797 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005798 psa_key_slot_t *private_key,
5799 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005800 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005801{
5802 psa_status_t status;
5803 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5804 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005805 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005806
5807 /* Step 1: run the secret agreement algorithm to generate the shared
5808 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005809 status = psa_key_agreement_raw_internal( ka_alg,
5810 private_key,
5811 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005812 shared_secret,
5813 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005814 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005815 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005816 goto exit;
5817
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005818 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005819 * the shared secret. A shared secret is permitted wherever a key
5820 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005821 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005822 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005823 shared_secret,
5824 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005825exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005826 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005827 return( status );
5828}
5829
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005830psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005831 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005832 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005833 const uint8_t *peer_key,
5834 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005835{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005836 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005837 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005838 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005839
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005840 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005841 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005842 status = psa_get_and_lock_transparent_key_slot_with_policy(
5843 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005844 if( status != PSA_SUCCESS )
5845 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005846 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005847 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005848 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005849 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005850 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005851 else
5852 {
5853 /* If a private key has been added as SECRET, we allow the derived
5854 * key material to be used as a key in PSA Crypto. */
5855 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5856 operation->can_output_key = 1;
5857 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005858
Ronald Cron5c522922020-11-14 16:35:34 +01005859 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005860
Ronald Cron5c522922020-11-14 16:35:34 +01005861 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005862}
5863
Gilles Peskinebe697d82019-05-16 18:00:41 +02005864psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005865 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005866 const uint8_t *peer_key,
5867 size_t peer_key_length,
5868 uint8_t *output,
5869 size_t output_size,
5870 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005871{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005872 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005873 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005874 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005875
5876 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5877 {
5878 status = PSA_ERROR_INVALID_ARGUMENT;
5879 goto exit;
5880 }
Ronald Cron5c522922020-11-14 16:35:34 +01005881 status = psa_get_and_lock_transparent_key_slot_with_policy(
5882 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005883 if( status != PSA_SUCCESS )
5884 goto exit;
5885
5886 status = psa_key_agreement_raw_internal( alg, slot,
5887 peer_key, peer_key_length,
5888 output, output_size,
5889 output_length );
5890
5891exit:
5892 if( status != PSA_SUCCESS )
5893 {
5894 /* If an error happens and is not handled properly, the output
5895 * may be used as a key to protect sensitive data. Arrange for such
5896 * a key to be random, which is likely to result in decryption or
5897 * verification errors. This is better than filling the buffer with
5898 * some constant data such as zeros, which would result in the data
5899 * being protected with a reproducible, easily knowable key.
5900 */
5901 psa_generate_random( output, output_size );
5902 *output_length = output_size;
5903 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005904
Ronald Cron5c522922020-11-14 16:35:34 +01005905 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005906
Ronald Cron5c522922020-11-14 16:35:34 +01005907 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005908}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005909
5910
Gilles Peskine30524eb2020-11-13 17:02:26 +01005911
Gilles Peskine86a440b2018-11-12 18:39:40 +01005912/****************************************************************/
5913/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005914/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005915
Gilles Peskine30524eb2020-11-13 17:02:26 +01005916/** Initialize the PSA random generator.
5917 */
5918static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5919{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005920#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5921 memset( rng, 0, sizeof( *rng ) );
5922#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5923
Gilles Peskine30524eb2020-11-13 17:02:26 +01005924 /* Set default configuration if
5925 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5926 if( rng->entropy_init == NULL )
5927 rng->entropy_init = mbedtls_entropy_init;
5928 if( rng->entropy_free == NULL )
5929 rng->entropy_free = mbedtls_entropy_free;
5930
5931 rng->entropy_init( &rng->entropy );
5932#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5933 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5934 /* The PSA entropy injection feature depends on using NV seed as an entropy
5935 * source. Add NV seed as an entropy source for PSA entropy injection. */
5936 mbedtls_entropy_add_source( &rng->entropy,
5937 mbedtls_nv_seed_poll, NULL,
5938 MBEDTLS_ENTROPY_BLOCK_SIZE,
5939 MBEDTLS_ENTROPY_SOURCE_STRONG );
5940#endif
5941
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005942 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005943#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005944}
5945
5946/** Deinitialize the PSA random generator.
5947 */
5948static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5949{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005950#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5951 memset( rng, 0, sizeof( *rng ) );
5952#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005953 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005954 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005955#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005956}
5957
5958/** Seed the PSA random generator.
5959 */
5960static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5961{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005962#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5963 /* Do nothing: the external RNG seeds itself. */
5964 (void) rng;
5965 return( PSA_SUCCESS );
5966#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005967 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005968 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5969 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005970 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005971#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005972}
5973
Gilles Peskinee59236f2018-01-27 23:32:46 +01005974psa_status_t psa_generate_random( uint8_t *output,
5975 size_t output_size )
5976{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005977 GUARD_MODULE_INITIALIZED;
5978
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005979#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5980
5981 size_t output_length = 0;
5982 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5983 output, output_size,
5984 &output_length );
5985 if( status != PSA_SUCCESS )
5986 return( status );
5987 /* Breaking up a request into smaller chunks is currently not supported
5988 * for the extrernal RNG interface. */
5989 if( output_length != output_size )
5990 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5991 return( PSA_SUCCESS );
5992
5993#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5994
Gilles Peskine71ddab92021-01-04 21:01:07 +01005995 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005996 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005997 size_t request_size =
5998 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5999 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6000 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006001 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6002 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006003 if( ret != 0 )
6004 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006005 output_size -= request_size;
6006 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006007 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006008 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006009#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006010}
6011
Gilles Peskine8814fc42020-12-14 15:33:44 +01006012/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006013 *
6014 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6015 * `psa_generate_random(...)`. The state parameter is ignored since the
6016 * PSA API doesn't support passing an explicit state.
6017 *
6018 * In the non-external case, psa_generate_random() calls an
6019 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6020 * and semantics as mbedtls_psa_get_random(). As an optimization,
6021 * instead of doing this back-and-forth between the PSA API and the
6022 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6023 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006024 */
6025#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6026int mbedtls_psa_get_random( void *p_rng,
6027 unsigned char *output,
6028 size_t output_size )
6029{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006030 /* This function takes a pointer to the RNG state because that's what
6031 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6032 * its own state internally and doesn't let the caller access that state.
6033 * So we just ignore the state parameter, and in practice we'll pass
6034 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006035 (void) p_rng;
6036 psa_status_t status = psa_generate_random( output, output_size );
6037 if( status == PSA_SUCCESS )
6038 return( 0 );
6039 else
6040 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6041}
6042#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6043
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006044#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6045#include "mbedtls/entropy_poll.h"
6046
Gilles Peskine7228da22019-07-15 11:06:15 +02006047psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006048 size_t seed_size )
6049{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006050 if( global_data.initialized )
6051 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006052
6053 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6054 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6055 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6056 return( PSA_ERROR_INVALID_ARGUMENT );
6057
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006058 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006059}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006060#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006061
Ronald Cron3772afe2021-02-08 16:10:05 +01006062/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006063 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006064 * \param type The key type
6065 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006066 *
6067 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006068 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006069 * \retval #PSA_ERROR_INVALID_ARGUMENT
6070 * The size in bits of the key is not valid.
6071 * \retval #PSA_ERROR_NOT_SUPPORTED
6072 * The type and/or the size in bits of the key or the combination of
6073 * the two is not supported.
6074 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006075static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6076 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006077{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006078 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006079
Gilles Peskinee59236f2018-01-27 23:32:46 +01006080 if( key_type_is_raw_bytes( type ) )
6081 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02006082 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006083 if( status != PSA_SUCCESS )
6084 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006085 }
6086 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006087#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006088 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6089 {
6090 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6091 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006092
Ronald Cron01b2aba2020-10-05 09:42:02 +02006093 /* Accept only byte-aligned keys, for the same reasons as
6094 * in psa_import_rsa_key(). */
6095 if( bits % 8 != 0 )
6096 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006097 }
6098 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006099#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006100
Ronald Cron5c4d3862020-12-07 11:07:24 +01006101#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006102 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6103 {
Ronald Crond81ab562021-02-16 09:01:16 +01006104 /* To avoid empty block, return successfully here. */
6105 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006106 }
6107 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006108#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006109 {
6110 return( PSA_ERROR_NOT_SUPPORTED );
6111 }
6112
6113 return( PSA_SUCCESS );
6114}
6115
Ronald Cron55ed0592020-10-05 10:30:40 +02006116psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006117 const psa_key_attributes_t *attributes,
6118 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006119{
6120 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006121 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006122
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006123 if( ( attributes->domain_parameters == NULL ) &&
6124 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006125 return( PSA_ERROR_INVALID_ARGUMENT );
6126
Ronald Cron01b2aba2020-10-05 09:42:02 +02006127 if( key_type_is_raw_bytes( type ) )
6128 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006129 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006130 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006131 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006132
David Brown12ca5032021-02-11 11:02:00 -07006133#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006134 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006135 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006136#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006137 }
6138 else
6139
John Durkop07cc04a2020-11-16 22:08:34 -08006140#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006141 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006142 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006143 return( mbedtls_psa_rsa_generate_key( attributes,
6144 key_buffer,
6145 key_buffer_size,
6146 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006147 }
6148 else
John Durkop07cc04a2020-11-16 22:08:34 -08006149#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006150
John Durkop9814fa22020-11-04 12:28:15 -08006151#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006152 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006153 {
Ronald Cron7023db52020-11-20 18:17:42 +01006154 return( mbedtls_psa_ecp_generate_key( attributes,
6155 key_buffer,
6156 key_buffer_size,
6157 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006158 }
6159 else
John Durkop9814fa22020-11-04 12:28:15 -08006160#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006161 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006162 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006163 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006164 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006165
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006166 return( PSA_SUCCESS );
6167}
Darryl Green0c6575a2018-11-07 16:05:30 +00006168
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006169psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006170 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006171{
6172 psa_status_t status;
6173 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006174 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006175 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006176
Ronald Cron81709fc2020-11-14 12:10:32 +01006177 *key = MBEDTLS_SVC_KEY_ID_INIT;
6178
Gilles Peskine0f84d622019-09-12 19:03:13 +02006179 /* Reject any attempt to create a zero-length key so that we don't
6180 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6181 if( psa_get_key_bits( attributes ) == 0 )
6182 return( PSA_ERROR_INVALID_ARGUMENT );
6183
Ronald Cron81709fc2020-11-14 12:10:32 +01006184 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6185 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006186 if( status != PSA_SUCCESS )
6187 goto exit;
6188
Ronald Cron977c2472020-10-13 08:32:21 +02006189 /* In the case of a transparent key or an opaque key stored in local
6190 * storage (thus not in the case of generating a key in a secure element
6191 * or cryptoprocessor with storage), we have to allocate a buffer to
6192 * hold the generated key material. */
6193 if( slot->key.data == NULL )
6194 {
6195 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6196 PSA_KEY_LOCATION_LOCAL_STORAGE )
6197 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006198 status = psa_validate_key_type_and_size_for_key_generation(
6199 attributes->core.type, attributes->core.bits );
6200 if( status != PSA_SUCCESS )
6201 goto exit;
6202
6203 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6204 attributes->core.type,
6205 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006206 }
6207 else
6208 {
6209 status = psa_driver_wrapper_get_key_buffer_size(
6210 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006211 if( status != PSA_SUCCESS )
6212 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006213 }
Ronald Cron977c2472020-10-13 08:32:21 +02006214
6215 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6216 if( status != PSA_SUCCESS )
6217 goto exit;
6218 }
6219
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006220 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006221 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006222
Ronald Cron2b56bc82020-10-05 10:02:26 +02006223 if( status != PSA_SUCCESS )
6224 psa_remove_key_data_from_memory( slot );
6225
Gilles Peskine11792082019-08-06 18:36:36 +02006226exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006227 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006228 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006229 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006230 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006231
Darryl Green0c6575a2018-11-07 16:05:30 +00006232 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006233}
6234
Gilles Peskinee59236f2018-01-27 23:32:46 +01006235/****************************************************************/
6236/* Module setup */
6237/****************************************************************/
6238
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006239#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006240psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6241 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6242 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6243{
6244 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6245 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006246 global_data.rng.entropy_init = entropy_init;
6247 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006248 return( PSA_SUCCESS );
6249}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006250#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006251
Gilles Peskinee59236f2018-01-27 23:32:46 +01006252void mbedtls_psa_crypto_free( void )
6253{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006254 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006255 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6256 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006257 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006258 }
6259 /* Wipe all remaining data, including configuration.
6260 * In particular, this sets all state indicator to the value
6261 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006262 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006263#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006264 /* Unregister all secure element drivers, so that we restart from
6265 * a pristine state. */
6266 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006268}
6269
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006270#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6271/** Recover a transaction that was interrupted by a power failure.
6272 *
6273 * This function is called during initialization, before psa_crypto_init()
6274 * returns. If this function returns a failure status, the initialization
6275 * fails.
6276 */
6277static psa_status_t psa_crypto_recover_transaction(
6278 const psa_crypto_transaction_t *transaction )
6279{
6280 switch( transaction->unknown.type )
6281 {
6282 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6283 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006284 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006285 * is implemented.
6286 * https://github.com/ARMmbed/mbed-crypto/issues/218
6287 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006288 default:
6289 /* We found an unsupported transaction in the storage.
6290 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006291 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006292 }
6293}
6294#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6295
Gilles Peskinee59236f2018-01-27 23:32:46 +01006296psa_status_t psa_crypto_init( void )
6297{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006298 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006299
Gilles Peskinec6b69072018-11-20 21:42:52 +01006300 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006301 if( global_data.initialized != 0 )
6302 return( PSA_SUCCESS );
6303
Gilles Peskine30524eb2020-11-13 17:02:26 +01006304 /* Initialize and seed the random generator. */
6305 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006306 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006307 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006308 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006309 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006310 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006311
Gilles Peskine66fb1262018-12-10 16:29:04 +01006312 status = psa_initialize_key_slots( );
6313 if( status != PSA_SUCCESS )
6314 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006315
Gilles Peskined9348f22019-10-01 15:22:29 +02006316#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6317 status = psa_init_all_se_drivers( );
6318 if( status != PSA_SUCCESS )
6319 goto exit;
6320#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6321
Gilles Peskine4b734222019-07-24 15:56:31 +02006322#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006323 status = psa_crypto_load_transaction( );
6324 if( status == PSA_SUCCESS )
6325 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006326 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6327 if( status != PSA_SUCCESS )
6328 goto exit;
6329 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006330 }
6331 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6332 {
6333 /* There's no transaction to complete. It's all good. */
6334 status = PSA_SUCCESS;
6335 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006336#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006337
Gilles Peskinec6b69072018-11-20 21:42:52 +01006338 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006339 global_data.initialized = 1;
6340
6341exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006342 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006343 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006344 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006345}
6346
6347#endif /* MBEDTLS_PSA_CRYPTO_C */