blob: b6e26b3f0adafd0cc11d1e1eaa50bc57a6ded724 [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 Cooremanb3ce8152021-02-18 12:03:50 +0100666 /* Special case: one or both sides contain a wildcard. */
667 if( PSA_ALG_IS_WILDCARD( alg1 ) || PSA_ALG_IS_WILDCARD( alg2 ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100668 {
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100669 /* If the policies are from the same hash-and-sign family, check
670 * if one is a wildcard. If so the other has the specific algorithm. */
671 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
672 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
673 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
674 {
675 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
676 return( alg2 );
677 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
678 return( alg1 );
679 }
680 /* If the policies are from the same AEAD family, then at least one
681 * of them is a minimum-tag-length policy. Calculate the most
682 * restrictive tag length. */
683 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
684 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
685 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
686 {
687 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
688 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
689 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
690
691 /* If both are wildcards, return most restricitve wildcard */
692 if( PSA_ALG_IS_WILDCARD( alg1 ) && PSA_ALG_IS_WILDCARD( alg2 ) )
693 {
694 return( PSA_ALG_AEAD_WITH_MINIMUM_LENGTH_TAG( alg1, max_len ) );
695 }
696 /* If only one is a wildcard, return specific algorithm if compatible. */
697 if( PSA_ALG_IS_WILDCARD( alg1 ) && (alg1_len <= alg2_len) )
698 {
699 return( alg2 );
700 }
701 if( PSA_ALG_IS_WILDCARD( alg2 ) && (alg2_len <= alg1_len) )
702 {
703 return( alg1 );
704 }
705 }
706 /* If the policies are from the same MAC family, then at least one
707 * of them is a minimum-tag-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;
716
717 /* If both are wildcards, return most restricitve wildcard */
718 if( PSA_ALG_IS_WILDCARD( alg1 ) && PSA_ALG_IS_WILDCARD( alg2 ) )
719 {
720 return( PSA_ALG_MAC_WITH_MINIMUM_LENGTH_TAG( alg1, max_len ) );
721 }
722 /* If only one is a wildcard, return specific algorithm if compatible.
723 * Special case: specific MAC algorithm with '0' as length means full-
724 * length MAC, which is always allowed by a wildcard with the same
725 * base algorithm. */
726 if( PSA_ALG_IS_WILDCARD( alg1 ) &&
727 ( ( alg1_len <= alg2_len) ||
728 ( alg2 == PSA_ALG_FULL_LENGTH_MAC( alg1 ) ) ) )
729 {
730 return( alg2 );
731 }
732 if( PSA_ALG_IS_WILDCARD( alg2 ) &&
733 ( (alg2_len <= alg1_len) ||
734 ( alg1 == PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) )
735 {
736 return( alg1 );
737 }
738 }
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 Cooremanb3ce8152021-02-18 12:03:50 +0100753 /* Special cases: the policy is an algorithm collection. */
754 if( PSA_ALG_IS_WILDCARD( policy_alg ) )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200755 {
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100756 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
757 * and requested_alg is the same hash-and-sign family with any hash,
758 * then requested_alg is compliant with policy_alg. */
759 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
760 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
761 {
762 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
763 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
764 }
765 /* If policy_alg is a wildcard AEAD algorithm of the same base as
766 * the requested algorithm, check the requested tag length to be
767 * equal-length or longer than the wildcard-specified length. */
768 if( PSA_ALG_IS_AEAD( policy_alg ) &&
769 PSA_ALG_IS_AEAD( requested_alg ) &&
770 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
771 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) )
772 {
773 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
774 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
775 }
776 /* If policy_alg is a wildcard MAC algorithm of the same base as
777 * the requested algorithm, check the requested tag length to be
778 * equal-length or longer than the wildcard-specified length. */
779 if( PSA_ALG_IS_MAC( policy_alg ) &&
780 PSA_ALG_IS_MAC( requested_alg ) &&
781 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
782 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
783 {
784 /* Special case: full-length MAC is encoded with 0-length.
785 * A minimum-length policy will always allow a full-length MAC. */
786 if( PSA_ALG_FULL_LENGTH_MAC( requested_alg ) == requested_alg )
787 return( 1 );
788
789 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
790 PSA_MAC_TRUNCATED_LENGTH( requested_alg ) );
791 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200792 }
Steven Cooremance48e852020-10-05 16:02:45 +0200793 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200794 * a key derivation with that key agreement should also be allowed. This
795 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200796 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
797 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
798 {
799 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
800 policy_alg );
801 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100802 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200803 return( 0 );
804}
805
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100806/** Test whether a policy permits an algorithm.
807 *
808 * The caller must test usage flags separately.
809 */
810static int psa_key_policy_permits( const psa_key_policy_t *policy,
811 psa_algorithm_t alg )
812{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200813 return( psa_key_algorithm_permits( policy->alg, alg ) ||
814 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100815}
816
Gilles Peskinef603c712019-01-19 13:40:11 +0100817/** Restrict a key policy based on a constraint.
818 *
819 * \param[in,out] policy The policy to restrict.
820 * \param[in] constraint The policy constraint to apply.
821 *
822 * \retval #PSA_SUCCESS
823 * \c *policy contains the intersection of the original value of
824 * \c *policy and \c *constraint.
825 * \retval #PSA_ERROR_INVALID_ARGUMENT
826 * \c *policy and \c *constraint are incompatible.
827 * \c *policy is unchanged.
828 */
829static psa_status_t psa_restrict_key_policy(
830 psa_key_policy_t *policy,
831 const psa_key_policy_t *constraint )
832{
833 psa_algorithm_t intersection_alg =
834 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200835 psa_algorithm_t intersection_alg2 =
836 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100837 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
838 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200839 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
840 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100841 policy->usage &= constraint->usage;
842 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200843 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100844 return( PSA_SUCCESS );
845}
846
Ronald Cron5c522922020-11-14 16:35:34 +0100847/** Get the description of a key given its identifier and policy constraints
848 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200849 *
Ronald Cron5c522922020-11-14 16:35:34 +0100850 * The key must have allow all the usage flags set in \p usage. If \p alg is
851 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +0100852 *
Ronald Cron5c522922020-11-14 16:35:34 +0100853 * In case of a persistent key, the function loads the description of the key
854 * into a key slot if not already done.
855 *
856 * On success, the returned key slot is locked. It is the responsibility of
857 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200858 */
Ronald Cron5c522922020-11-14 16:35:34 +0100859static psa_status_t psa_get_and_lock_key_slot_with_policy(
860 mbedtls_svc_key_id_t key,
861 psa_key_slot_t **p_slot,
862 psa_key_usage_t usage,
863 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100864{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200865 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
866 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100867
Ronald Cron5c522922020-11-14 16:35:34 +0100868 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100869 if( status != PSA_SUCCESS )
870 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200871 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100872
873 /* Enforce that usage policy for the key slot contains all the flags
874 * required by the usage parameter. There is one exception: public
875 * keys can always be exported, so we treat public key objects as
876 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200877 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100878 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200879
880 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +0200881 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200882 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100883
884 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200885 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200886 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +0100887
Darryl Green06fd18d2018-07-16 11:21:11 +0100888 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200889
890error:
891 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100892 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200893
894 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100895}
Darryl Green940d72c2018-07-13 13:18:51 +0100896
Ronald Cron5c522922020-11-14 16:35:34 +0100897/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200898 *
899 * A transparent key is a key for which the key material is directly
900 * available, as opposed to a key in a secure element.
901 *
Ronald Cron5c522922020-11-14 16:35:34 +0100902 * This is a temporary function to use instead of
903 * psa_get_and_lock_key_slot_with_policy() until secure element support is
904 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200905 *
Ronald Cron5c522922020-11-14 16:35:34 +0100906 * On success, the returned key slot is locked. It is the responsibility of the
907 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200908 */
909#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +0100910static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
911 mbedtls_svc_key_id_t key,
912 psa_key_slot_t **p_slot,
913 psa_key_usage_t usage,
914 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200915{
Ronald Cron5c522922020-11-14 16:35:34 +0100916 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
917 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200918 if( status != PSA_SUCCESS )
919 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200920
Gilles Peskineadad8132019-07-25 11:31:23 +0200921 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200922 {
Ronald Cron5c522922020-11-14 16:35:34 +0100923 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200924 *p_slot = NULL;
925 return( PSA_ERROR_NOT_SUPPORTED );
926 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200927
Gilles Peskine28f8f302019-07-24 13:30:31 +0200928 return( PSA_SUCCESS );
929}
930#else /* MBEDTLS_PSA_CRYPTO_SE_C */
931/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +0100932#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
933 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
935
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100936/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100937static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000938{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100939 /* Data pointer will always be either a valid pointer or NULL in an
940 * initialized slot, so we can just free it. */
941 if( slot->key.data != NULL )
942 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
943
944 mbedtls_free( slot->key.data );
945 slot->key.data = NULL;
946 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000947
948 return( PSA_SUCCESS );
949}
950
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100951/** Completely wipe a slot in memory, including its policy.
952 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100953psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100954{
955 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +0100956
957 /*
958 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100959 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +0100960 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
961 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +0100962 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +0100963 */
Ronald Cron5c522922020-11-14 16:35:34 +0100964 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +0100965 {
966#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +0100967 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +0100968#endif
Ronald Cronddd3d052020-10-30 14:07:07 +0100969 status = PSA_ERROR_CORRUPTION_DETECTED;
970 }
971
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200972 /* Multipart operations may still be using the key. This is safe
973 * because all multipart operation objects are independent from
974 * the key slot: if they need to access the key after the setup
975 * phase, they have a copy of the key. Note that this means that
976 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100977 /* At this point, key material and other type-specific content has
978 * been wiped. Clear remaining metadata. We can call memset and not
979 * zeroize because the metadata is not particularly sensitive. */
980 memset( slot, 0, sizeof( *slot ) );
981 return( status );
982}
983
Ronald Croncf56a0a2020-08-04 09:51:30 +0200984psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100986 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +0200987 psa_status_t status; /* status of the last operation */
988 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200989#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
990 psa_se_drv_table_entry_t *driver;
991#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992
Ronald Croncf56a0a2020-08-04 09:51:30 +0200993 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200994 return( PSA_SUCCESS );
995
Ronald Cronf2911112020-10-29 17:51:10 +0100996 /*
Ronald Cron19daca92020-11-10 18:08:03 +0100997 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +0100998 * key, this will load the key description from persistent memory if not
999 * done yet. We cannot avoid this loading as without it we don't know if
1000 * the key is operated by an SE or not and this information is needed by
1001 * the current implementation.
1002 */
Ronald Cron5c522922020-11-14 16:35:34 +01001003 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001004 if( status != PSA_SUCCESS )
1005 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001006
Ronald Cronf2911112020-10-29 17:51:10 +01001007 /*
1008 * If the key slot containing the key description is under access by the
1009 * library (apart from the present access), the key cannot be destroyed
1010 * yet. For the time being, just return in error. Eventually (to be
1011 * implemented), the key should be destroyed when all accesses have
1012 * stopped.
1013 */
Ronald Cron5c522922020-11-14 16:35:34 +01001014 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001015 {
Ronald Cron5c522922020-11-14 16:35:34 +01001016 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001017 return( PSA_ERROR_GENERIC_ERROR );
1018 }
1019
Gilles Peskine354f7672019-07-12 23:46:38 +02001020#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001021 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001022 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001023 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001024 /* For a key in a secure element, we need to do three things:
1025 * remove the key file in internal storage, destroy the
1026 * key inside the secure element, and update the driver's
1027 * persistent data. Start a transaction that will encompass these
1028 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001029 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001030 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001031 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001032 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001033 status = psa_crypto_save_transaction( );
1034 if( status != PSA_SUCCESS )
1035 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001036 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001037 /* We should still try to destroy the key in the secure
1038 * element and the key metadata in storage. This is especially
1039 * important if the error is that the storage is full.
1040 * But how to do it exactly without risking an inconsistent
1041 * state after a reset?
1042 * https://github.com/ARMmbed/mbed-crypto/issues/215
1043 */
1044 overall_status = status;
1045 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 }
1047
Ronald Cronea0f8a62020-11-25 17:52:23 +01001048 status = psa_destroy_se_key( driver,
1049 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001050 if( overall_status == PSA_SUCCESS )
1051 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001052 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001053#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1054
Darryl Greend49a4992018-06-18 17:27:26 +01001055#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001056 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001057 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001058 status = psa_destroy_persistent_key( slot->attr.id );
1059 if( overall_status == PSA_SUCCESS )
1060 overall_status = status;
1061
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001062 /* TODO: other slots may have a copy of the same key. We should
1063 * invalidate them.
1064 * https://github.com/ARMmbed/mbed-crypto/issues/214
1065 */
Darryl Greend49a4992018-06-18 17:27:26 +01001066 }
1067#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001068
Gilles Peskinefc762652019-07-22 19:30:34 +02001069#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1070 if( driver != NULL )
1071 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001072 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001073 if( overall_status == PSA_SUCCESS )
1074 overall_status = status;
1075 status = psa_crypto_stop_transaction( );
1076 if( overall_status == PSA_SUCCESS )
1077 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001078 }
1079#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1080
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001081#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1082exit:
1083#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001084 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001085 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1086 if( overall_status == PSA_SUCCESS )
1087 overall_status = status;
1088 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001089}
1090
John Durkop07cc04a2020-11-16 22:08:34 -08001091#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001092 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001093static psa_status_t psa_get_rsa_public_exponent(
1094 const mbedtls_rsa_context *rsa,
1095 psa_key_attributes_t *attributes )
1096{
1097 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001099 uint8_t *buffer = NULL;
1100 size_t buflen;
1101 mbedtls_mpi_init( &mpi );
1102
1103 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1104 if( ret != 0 )
1105 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001106 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1107 {
1108 /* It's the default value, which is reported as an empty string,
1109 * so there's nothing to do. */
1110 goto exit;
1111 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001112
1113 buflen = mbedtls_mpi_size( &mpi );
1114 buffer = mbedtls_calloc( 1, buflen );
1115 if( buffer == NULL )
1116 {
1117 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1118 goto exit;
1119 }
1120 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1121 if( ret != 0 )
1122 goto exit;
1123 attributes->domain_parameters = buffer;
1124 attributes->domain_parameters_size = buflen;
1125
1126exit:
1127 mbedtls_mpi_free( &mpi );
1128 if( ret != 0 )
1129 mbedtls_free( buffer );
1130 return( mbedtls_to_psa_error( ret ) );
1131}
John Durkop07cc04a2020-11-16 22:08:34 -08001132#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001133 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001134
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001135/** Retrieve all the publicly-accessible attributes of a key.
1136 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001137psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001138 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001139{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001140 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001141 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001142 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001143
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001144 psa_reset_key_attributes( attributes );
1145
Ronald Cron5c522922020-11-14 16:35:34 +01001146 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001147 if( status != PSA_SUCCESS )
1148 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001149
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001150 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001151 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1152 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1153
1154#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1155 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001156 psa_set_key_slot_number( attributes,
1157 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001158#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001159
Gilles Peskine8e338702019-07-30 20:06:31 +02001160 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001161 {
John Durkop07cc04a2020-11-16 22:08:34 -08001162#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001163 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001164 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001165 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001166#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001167 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001168 * is not yet implemented.
1169 * https://github.com/ARMmbed/mbed-crypto/issues/216
1170 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001171 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001172 break;
1173#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001174 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001175 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001176
Ronald Cron90857082020-11-25 14:58:33 +01001177 status = mbedtls_psa_rsa_load_representation(
1178 slot->attr.type,
1179 slot->key.data,
1180 slot->key.bytes,
1181 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001182 if( status != PSA_SUCCESS )
1183 break;
1184
Steven Cooremana2371e52020-07-28 14:30:39 +02001185 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001186 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001187 mbedtls_rsa_free( rsa );
1188 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001189 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001190 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001191#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001192 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001193 default:
1194 /* Nothing else to do. */
1195 break;
1196 }
1197
1198 if( status != PSA_SUCCESS )
1199 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001200
Ronald Cron5c522922020-11-14 16:35:34 +01001201 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001202
Ronald Cron5c522922020-11-14 16:35:34 +01001203 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001204}
1205
Gilles Peskinec8000c02019-08-02 20:15:51 +02001206#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1207psa_status_t psa_get_key_slot_number(
1208 const psa_key_attributes_t *attributes,
1209 psa_key_slot_number_t *slot_number )
1210{
1211 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1212 {
1213 *slot_number = attributes->slot_number;
1214 return( PSA_SUCCESS );
1215 }
1216 else
1217 return( PSA_ERROR_INVALID_ARGUMENT );
1218}
1219#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1220
Ronald Crond18b5f82020-11-25 16:46:05 +01001221static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1222 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001223 uint8_t *data,
1224 size_t data_size,
1225 size_t *data_length )
1226{
Ronald Crond18b5f82020-11-25 16:46:05 +01001227 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001228 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001229 memcpy( data, key_buffer, key_buffer_size );
1230 memset( data + key_buffer_size, 0,
1231 data_size - key_buffer_size );
1232 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001233 return( PSA_SUCCESS );
1234}
1235
Ronald Cron7285cda2020-11-26 14:46:37 +01001236psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001237 const psa_key_attributes_t *attributes,
1238 const uint8_t *key_buffer, size_t key_buffer_size,
1239 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001240{
Ronald Crond18b5f82020-11-25 16:46:05 +01001241 psa_key_type_t type = attributes->core.type;
1242
Ronald Crond18b5f82020-11-25 16:46:05 +01001243 if( key_type_is_raw_bytes( type ) ||
1244 PSA_KEY_TYPE_IS_RSA( type ) ||
1245 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001246 {
1247 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001248 key_buffer, key_buffer_size,
1249 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001250 }
1251 else
1252 {
1253 /* This shouldn't happen in the reference implementation, but
1254 it is valid for a special-purpose implementation to omit
1255 support for exporting certain key types. */
1256 return( PSA_ERROR_NOT_SUPPORTED );
1257 }
1258}
1259
1260psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1261 uint8_t *data,
1262 size_t data_size,
1263 size_t *data_length )
1264{
1265 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1266 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1267 psa_key_slot_t *slot;
1268
Ronald Crone907e552021-01-18 13:22:38 +01001269 /* Reject a zero-length output buffer now, since this can never be a
1270 * valid key representation. This way we know that data must be a valid
1271 * pointer and we can do things like memset(data, ..., data_size). */
1272 if( data_size == 0 )
1273 return( PSA_ERROR_BUFFER_TOO_SMALL );
1274
Ronald Cron9486f9d2020-11-26 13:36:23 +01001275 /* Set the key to empty now, so that even when there are errors, we always
1276 * set data_length to a value between 0 and data_size. On error, setting
1277 * the key to empty is a good choice because an empty key representation is
1278 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001279 *data_length = 0;
1280
Ronald Cron9486f9d2020-11-26 13:36:23 +01001281 /* Export requires the EXPORT flag. There is an exception for public keys,
1282 * which don't require any flag, but
1283 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1284 */
1285 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1286 PSA_KEY_USAGE_EXPORT, 0 );
1287 if( status != PSA_SUCCESS )
1288 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001289
Ronald Crond18b5f82020-11-25 16:46:05 +01001290 psa_key_attributes_t attributes = {
1291 .core = slot->attr
1292 };
Ronald Cron67227982020-11-26 15:16:05 +01001293 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001294 slot->key.data, slot->key.bytes,
1295 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001296
Ronald Cron9486f9d2020-11-26 13:36:23 +01001297 unlock_status = psa_unlock_key_slot( slot );
1298
1299 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1300}
1301
Ronald Cron7285cda2020-11-26 14:46:37 +01001302psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001303 const psa_key_attributes_t *attributes,
1304 const uint8_t *key_buffer,
1305 size_t key_buffer_size,
1306 uint8_t *data,
1307 size_t data_size,
1308 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001309{
Ronald Crond18b5f82020-11-25 16:46:05 +01001310 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001311
Ronald Crond18b5f82020-11-25 16:46:05 +01001312 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001313 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001314 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001315 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001316 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001317 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001318 key_buffer, key_buffer_size,
1319 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001320 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001321
Ronald Crond18b5f82020-11-25 16:46:05 +01001322 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001323 {
John Durkop0e005192020-10-31 22:06:54 -07001324#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1325 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001326 return( mbedtls_psa_rsa_export_public_key( attributes,
1327 key_buffer,
1328 key_buffer_size,
1329 data,
1330 data_size,
1331 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001332#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001333 /* We don't know how to convert a private RSA key to public. */
1334 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001335#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1336 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001337 }
1338 else
Moran Pekera998bc62018-04-16 18:16:20 +03001339 {
John Durkop6ba40d12020-11-10 08:50:04 -08001340#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1341 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001342 return( mbedtls_psa_ecp_export_public_key( attributes,
1343 key_buffer,
1344 key_buffer_size,
1345 data,
1346 data_size,
1347 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001348#else
1349 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001350 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001351#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1352 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001353 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001354 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001355 else
1356 {
1357 /* This shouldn't happen in the reference implementation, but
1358 it is valid for a special-purpose implementation to omit
1359 support for exporting certain key types. */
1360 return( PSA_ERROR_NOT_SUPPORTED );
1361 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001362}
Ronald Crond18b5f82020-11-25 16:46:05 +01001363
Ronald Croncf56a0a2020-08-04 09:51:30 +02001364psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001365 uint8_t *data,
1366 size_t data_size,
1367 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001368{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001369 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001370 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001371 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001372
Ronald Crone907e552021-01-18 13:22:38 +01001373 /* Reject a zero-length output buffer now, since this can never be a
1374 * valid key representation. This way we know that data must be a valid
1375 * pointer and we can do things like memset(data, ..., data_size). */
1376 if( data_size == 0 )
1377 return( PSA_ERROR_BUFFER_TOO_SMALL );
1378
Darryl Greendd8fb772018-11-07 16:00:44 +00001379 /* Set the key to empty now, so that even when there are errors, we always
1380 * set data_length to a value between 0 and data_size. On error, setting
1381 * the key to empty is a good choice because an empty key representation is
1382 * unlikely to be accepted anywhere. */
1383 *data_length = 0;
1384
1385 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001386 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001387 if( status != PSA_SUCCESS )
1388 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001389
Ronald Cron9486f9d2020-11-26 13:36:23 +01001390 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1391 {
1392 status = PSA_ERROR_INVALID_ARGUMENT;
1393 goto exit;
1394 }
1395
Ronald Crond18b5f82020-11-25 16:46:05 +01001396 psa_key_attributes_t attributes = {
1397 .core = slot->attr
1398 };
Ronald Cron67227982020-11-26 15:16:05 +01001399 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001400 &attributes, slot->key.data, slot->key.bytes,
1401 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001402
1403exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001404 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001405
Ronald Cron5c522922020-11-14 16:35:34 +01001406 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001407}
1408
Gilles Peskine91e8c332019-08-02 19:19:39 +02001409#if defined(static_assert)
1410static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1411 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001412static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001413 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001414static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001415 "One or more key attribute flag is listed as both internal-only and external-only" );
1416#endif
1417
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001418/** Validate that a key policy is internally well-formed.
1419 *
1420 * This function only rejects invalid policies. It does not validate the
1421 * consistency of the policy with respect to other attributes of the key
1422 * such as the key type.
1423 */
1424static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001425{
1426 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001427 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001428 PSA_KEY_USAGE_ENCRYPT |
1429 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001430 PSA_KEY_USAGE_SIGN_HASH |
1431 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001432 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1433 return( PSA_ERROR_INVALID_ARGUMENT );
1434
Gilles Peskine4747d192019-04-17 15:05:45 +02001435 return( PSA_SUCCESS );
1436}
1437
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001438/** Validate the internal consistency of key attributes.
1439 *
1440 * This function only rejects invalid attribute values. If does not
1441 * validate the consistency of the attributes with any key data that may
1442 * be involved in the creation of the key.
1443 *
1444 * Call this function early in the key creation process.
1445 *
1446 * \param[in] attributes Key attributes for the new key.
1447 * \param[out] p_drv On any return, the driver for the key, if any.
1448 * NULL for a transparent key.
1449 *
1450 */
1451static psa_status_t psa_validate_key_attributes(
1452 const psa_key_attributes_t *attributes,
1453 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001454{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001455 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001456 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001457 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001458
Ronald Cron54b90082020-10-29 15:26:43 +01001459 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001460 if( status != PSA_SUCCESS )
1461 return( status );
1462
Ronald Crond2ed4812020-07-17 16:11:30 +02001463 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001464 if( status != PSA_SUCCESS )
1465 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001466
Ronald Cron65f38a32020-10-23 17:11:13 +02001467 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1468 {
1469 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1470 return( PSA_ERROR_INVALID_ARGUMENT );
1471 }
1472 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001473 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001474 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001475 if( status != PSA_SUCCESS )
1476 return( status );
1477 }
1478
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001479 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001480 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001481 return( status );
1482
1483 /* Refuse to create overly large keys.
1484 * Note that this doesn't trigger on import if the attributes don't
1485 * explicitly specify a size (so psa_get_key_bits returns 0), so
1486 * psa_import_key() needs its own checks. */
1487 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1488 return( PSA_ERROR_NOT_SUPPORTED );
1489
Gilles Peskine91e8c332019-08-02 19:19:39 +02001490 /* Reject invalid flags. These should not be reachable through the API. */
1491 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1492 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1493 return( PSA_ERROR_INVALID_ARGUMENT );
1494
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001495 return( PSA_SUCCESS );
1496}
1497
Gilles Peskine4747d192019-04-17 15:05:45 +02001498/** Prepare a key slot to receive key material.
1499 *
1500 * This function allocates a key slot and sets its metadata.
1501 *
1502 * If this function fails, call psa_fail_key_creation().
1503 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001504 * This function is intended to be used as follows:
1505 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001506 * it with the specified attributes, and in case of a volatile key assign it
1507 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001508 * -# Populate the slot with the key material.
1509 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1510 * In case of failure at any step, stop the sequence and call
1511 * psa_fail_key_creation().
1512 *
Ronald Cron5c522922020-11-14 16:35:34 +01001513 * On success, the key slot is locked. It is the responsibility of the caller
1514 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001515 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001516 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001517 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001518 * \param[out] p_slot On success, a pointer to the prepared slot.
1519 * \param[out] p_drv On any return, the driver for the key, if any.
1520 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001521 *
1522 * \retval #PSA_SUCCESS
1523 * The key slot is ready to receive key material.
1524 * \return If this function fails, the key slot is an invalid state.
1525 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001526 */
1527static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001528 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001529 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001530 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001531 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001532{
1533 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001534 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001535 psa_key_slot_t *slot;
1536
Gilles Peskinedf179142019-07-15 22:02:14 +02001537 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001538 *p_drv = NULL;
1539
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001540 status = psa_validate_key_attributes( attributes, p_drv );
1541 if( status != PSA_SUCCESS )
1542 return( status );
1543
Ronald Cronc4d1b512020-07-31 11:26:37 +02001544 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001545 if( status != PSA_SUCCESS )
1546 return( status );
1547 slot = *p_slot;
1548
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001549 /* We're storing the declared bit-size of the key. It's up to each
1550 * creation mechanism to verify that this information is correct.
1551 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001552 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001553 * is optional (import, copy). In case of a volatile key, assign it the
1554 * volatile key identifier associated to the slot returned to contain its
1555 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001556
1557 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001558 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1559 {
1560#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1561 slot->attr.id = volatile_key_id;
1562#else
1563 slot->attr.id.key_id = volatile_key_id;
1564#endif
1565 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001566
Gilles Peskine91e8c332019-08-02 19:19:39 +02001567 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001568 * external-only flags, query `attributes`. Thanks to the check
1569 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001570 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001571 * may have set. */
1572 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001573
Gilles Peskinecbaff462019-07-12 23:46:04 +02001574#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001575 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001576 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001577 * create the key file in internal storage, create the
1578 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001579 * persistent data. This is done by starting a transaction that will
1580 * encompass these three actions.
1581 * For registering a volatile key, we just need to find an appropriate
1582 * slot number inside the SE. Since the key is designated volatile, creating
1583 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001584 /* The first thing to do is to find a slot number for the new key.
1585 * We save the slot number in persistent storage as part of the
1586 * transaction data. It will be needed to recover if the power
1587 * fails during the key creation process, to clean up on the secure
1588 * element side after restarting. Obtaining a slot number from the
1589 * secure element driver updates its persistent state, but we do not yet
1590 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001591 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001592 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001593 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001594 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001595 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001596 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001597 if( status != PSA_SUCCESS )
1598 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001599
1600 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001601 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001602 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1603 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001604 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001605 psa_crypto_transaction.key.id = slot->attr.id;
1606 status = psa_crypto_save_transaction( );
1607 if( status != PSA_SUCCESS )
1608 {
1609 (void) psa_crypto_stop_transaction( );
1610 return( status );
1611 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001612 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001613
1614 status = psa_copy_key_material_into_slot(
1615 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001616 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001617
1618 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1619 {
1620 /* Key registration only makes sense with a secure element. */
1621 return( PSA_ERROR_INVALID_ARGUMENT );
1622 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001623#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1624
Ronald Cronc4d1b512020-07-31 11:26:37 +02001625 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001626}
Gilles Peskine4747d192019-04-17 15:05:45 +02001627
1628/** Finalize the creation of a key once its key material has been set.
1629 *
1630 * This entails writing the key to persistent storage.
1631 *
1632 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001633 * See the documentation of psa_start_key_creation() for the intended use
1634 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001635 *
Ronald Cron5c522922020-11-14 16:35:34 +01001636 * If the finalization succeeds, the function unlocks the key slot (it was
1637 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1638 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001639 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001640 * \param[in,out] slot Pointer to the slot with key material.
1641 * \param[in] driver The secure element driver for the key,
1642 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001643 * \param[out] key On success, identifier of the key. Note that the
1644 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001645 *
1646 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001647 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001648 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1649 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1650 * \retval #PSA_ERROR_ALREADY_EXISTS
1651 * \retval #PSA_ERROR_DATA_INVALID
1652 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001653 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001654 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001655 * \return If this function fails, the key slot is an invalid state.
1656 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001657 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001658static psa_status_t psa_finish_key_creation(
1659 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001660 psa_se_drv_table_entry_t *driver,
1661 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001662{
1663 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001664 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001665 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001666
1667#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001668 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001669 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001670#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1671 if( driver != NULL )
1672 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001673 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001674 psa_key_slot_number_t slot_number =
1675 psa_key_slot_get_slot_number( slot ) ;
1676
Gilles Peskineb46bef22019-07-30 21:32:04 +02001677#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001678 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001679 sizeof( data.slot_number ),
1680 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001681#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001682 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001683 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001684 (uint8_t*) &data,
1685 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001686 }
1687 else
1688#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1689 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001690 /* Key material is saved in export representation in the slot, so
1691 * just pass the slot buffer for storage. */
1692 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001693 slot->key.data,
1694 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001695 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001696 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001697#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1698
Gilles Peskinecbaff462019-07-12 23:46:04 +02001699#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001700 /* Finish the transaction for a key creation. This does not
1701 * happen when registering an existing key. Detect this case
1702 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001703 * creation of a persistent key in a secure element requires a transaction,
1704 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001705 if( driver != NULL &&
1706 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001707 {
1708 status = psa_save_se_persistent_data( driver );
1709 if( status != PSA_SUCCESS )
1710 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001711 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001712 return( status );
1713 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001714 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001715 }
1716#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1717
Ronald Cron50972942020-11-14 11:28:25 +01001718 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001719 {
1720 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001721 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001722 if( status != PSA_SUCCESS )
1723 *key = MBEDTLS_SVC_KEY_ID_INIT;
1724 }
Ronald Cron50972942020-11-14 11:28:25 +01001725
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 return( status );
1727}
1728
1729/** Abort the creation of a key.
1730 *
1731 * You may call this function after calling psa_start_key_creation(),
1732 * or after psa_finish_key_creation() fails. In other circumstances, this
1733 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001734 * See the documentation of psa_start_key_creation() for the intended use
1735 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001736 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001737 * \param[in,out] slot Pointer to the slot with key material.
1738 * \param[in] driver The secure element driver for the key,
1739 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001740 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001741static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001742 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001743{
Gilles Peskine011e4282019-06-26 18:34:38 +02001744 (void) driver;
1745
Gilles Peskine4747d192019-04-17 15:05:45 +02001746 if( slot == NULL )
1747 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001748
1749#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001750 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001751 * element, and the failure happened later (when saving metadata
1752 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001753 * element.
1754 * https://github.com/ARMmbed/mbed-crypto/issues/217
1755 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001756
Gilles Peskined7729582019-08-05 15:55:54 +02001757 /* Abort the ongoing transaction if any (there may not be one if
1758 * the creation process failed before starting one, or if the
1759 * key creation is a registration of a key in a secure element).
1760 * Earlier functions must already have done what it takes to undo any
1761 * partial creation. All that's left is to update the transaction data
1762 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001763 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001764#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1765
Gilles Peskine4747d192019-04-17 15:05:45 +02001766 psa_wipe_key_slot( slot );
1767}
1768
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001769/** Validate optional attributes during key creation.
1770 *
1771 * Some key attributes are optional during key creation. If they are
1772 * specified in the attributes structure, check that they are consistent
1773 * with the data in the slot.
1774 *
1775 * This function should be called near the end of key creation, after
1776 * the slot in memory is fully populated but before saving persistent data.
1777 */
1778static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001779 const psa_key_slot_t *slot,
1780 const psa_key_attributes_t *attributes )
1781{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001782 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001783 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001784 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001785 return( PSA_ERROR_INVALID_ARGUMENT );
1786 }
1787
1788 if( attributes->domain_parameters_size != 0 )
1789 {
John Durkop0e005192020-10-31 22:06:54 -07001790#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1791 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001792 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001793 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001794 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001795 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001796 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001797
Ronald Cron90857082020-11-25 14:58:33 +01001798 psa_status_t status = mbedtls_psa_rsa_load_representation(
1799 slot->attr.type,
1800 slot->key.data,
1801 slot->key.bytes,
1802 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001803 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001804 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001805
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001806 mbedtls_mpi_init( &actual );
1807 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001808 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001809 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001810 mbedtls_rsa_free( rsa );
1811 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001812 if( ret != 0 )
1813 goto rsa_exit;
1814 ret = mbedtls_mpi_read_binary( &required,
1815 attributes->domain_parameters,
1816 attributes->domain_parameters_size );
1817 if( ret != 0 )
1818 goto rsa_exit;
1819 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1820 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1821 rsa_exit:
1822 mbedtls_mpi_free( &actual );
1823 mbedtls_mpi_free( &required );
1824 if( ret != 0)
1825 return( mbedtls_to_psa_error( ret ) );
1826 }
1827 else
John Durkop9814fa22020-11-04 12:28:15 -08001828#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1829 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001830 {
1831 return( PSA_ERROR_INVALID_ARGUMENT );
1832 }
1833 }
1834
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001835 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001836 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001837 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001838 return( PSA_ERROR_INVALID_ARGUMENT );
1839 }
1840
1841 return( PSA_SUCCESS );
1842}
1843
Gilles Peskine4747d192019-04-17 15:05:45 +02001844psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001845 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001846 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001847 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001848{
1849 psa_status_t status;
1850 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001851 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001852 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001853
Ronald Cron81709fc2020-11-14 12:10:32 +01001854 *key = MBEDTLS_SVC_KEY_ID_INIT;
1855
Gilles Peskine0f84d622019-09-12 19:03:13 +02001856 /* Reject zero-length symmetric keys (including raw data key objects).
1857 * This also rejects any key which might be encoded as an empty string,
1858 * which is never valid. */
1859 if( data_length == 0 )
1860 return( PSA_ERROR_INVALID_ARGUMENT );
1861
Gilles Peskinedf179142019-07-15 22:02:14 +02001862 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001863 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001864 if( status != PSA_SUCCESS )
1865 goto exit;
1866
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001867 /* In the case of a transparent key or an opaque key stored in local
1868 * storage (thus not in the case of generating a key in a secure element
1869 * or cryptoprocessor with storage), we have to allocate a buffer to
1870 * hold the generated key material. */
1871 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001872 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001873 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001874 if( status != PSA_SUCCESS )
1875 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001876 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001877
1878 bits = slot->attr.bits;
1879 status = psa_driver_wrapper_import_key( attributes,
1880 data, data_length,
1881 slot->key.data,
1882 slot->key.bytes,
1883 &slot->key.bytes, &bits );
1884 if( status != PSA_SUCCESS )
1885 goto exit;
1886
1887 if( slot->attr.bits == 0 )
1888 slot->attr.bits = (psa_key_bits_t) bits;
1889 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001890 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001891 status = PSA_ERROR_INVALID_ARGUMENT;
1892 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001893 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001894
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001895 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001896 if( status != PSA_SUCCESS )
1897 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898
Ronald Cron81709fc2020-11-14 12:10:32 +01001899 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001900exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001901 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001902 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001903
Gilles Peskine4747d192019-04-17 15:05:45 +02001904 return( status );
1905}
1906
Gilles Peskined7729582019-08-05 15:55:54 +02001907#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1908psa_status_t mbedtls_psa_register_se_key(
1909 const psa_key_attributes_t *attributes )
1910{
1911 psa_status_t status;
1912 psa_key_slot_t *slot = NULL;
1913 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001915
1916 /* Leaving attributes unspecified is not currently supported.
1917 * It could make sense to query the key type and size from the
1918 * secure element, but not all secure elements support this
1919 * and the driver HAL doesn't currently support it. */
1920 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1921 return( PSA_ERROR_NOT_SUPPORTED );
1922 if( psa_get_key_bits( attributes ) == 0 )
1923 return( PSA_ERROR_NOT_SUPPORTED );
1924
1925 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001926 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02001927 if( status != PSA_SUCCESS )
1928 goto exit;
1929
Ronald Cron81709fc2020-11-14 12:10:32 +01001930 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02001931
1932exit:
1933 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02001934 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001935
Gilles Peskined7729582019-08-05 15:55:54 +02001936 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001937 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02001938 return( status );
1939}
1940#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1941
Gilles Peskinef603c712019-01-19 13:40:11 +01001942static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001943 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001944{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001945 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001946 source->key.data,
1947 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01001948 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02001949 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01001950
Steven Cooreman398aee52020-10-13 14:35:45 +02001951 target->attr.type = source->attr.type;
1952 target->attr.bits = source->attr.bits;
1953
1954 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01001955}
1956
Ronald Croncf56a0a2020-08-04 09:51:30 +02001957psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001958 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001959 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01001960{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001962 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01001963 psa_key_slot_t *source_slot = NULL;
1964 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001965 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001966 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001967
Ronald Cron81709fc2020-11-14 12:10:32 +01001968 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
1969
Ronald Cron5c522922020-11-14 16:35:34 +01001970 status = psa_get_and_lock_transparent_key_slot_with_policy(
1971 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001972 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001973 goto exit;
1974
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001975 status = psa_validate_optional_attributes( source_slot,
1976 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001977 if( status != PSA_SUCCESS )
1978 goto exit;
1979
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001980 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001981 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001982 if( status != PSA_SUCCESS )
1983 goto exit;
1984
Ronald Cron81709fc2020-11-14 12:10:32 +01001985 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
1986 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001987 if( status != PSA_SUCCESS )
1988 goto exit;
1989
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001990#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1991 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001992 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001993 /* Copying to a secure element is not implemented yet. */
1994 status = PSA_ERROR_NOT_SUPPORTED;
1995 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001996 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001997#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001998
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001999 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002000 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002001 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002002
Ronald Cron81709fc2020-11-14 12:10:32 +01002003 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002004exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002005 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002006 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002007
Ronald Cron5c522922020-11-14 16:35:34 +01002008 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002009
Ronald Cron5c522922020-11-14 16:35:34 +01002010 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002011}
2012
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002013
2014
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002015/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002016/* Message digests */
2017/****************************************************************/
2018
John Durkop07cc04a2020-11-16 22:08:34 -08002019#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002020 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2021 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002022 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002023static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002024{
2025 switch( alg )
2026 {
John Durkopee4e6602020-11-27 08:48:46 -08002027#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002028 case PSA_ALG_MD2:
2029 return( &mbedtls_md2_info );
2030#endif
John Durkopee4e6602020-11-27 08:48:46 -08002031#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002032 case PSA_ALG_MD4:
2033 return( &mbedtls_md4_info );
2034#endif
John Durkopee4e6602020-11-27 08:48:46 -08002035#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002036 case PSA_ALG_MD5:
2037 return( &mbedtls_md5_info );
2038#endif
John Durkopee4e6602020-11-27 08:48:46 -08002039#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002040 case PSA_ALG_RIPEMD160:
2041 return( &mbedtls_ripemd160_info );
2042#endif
John Durkopee4e6602020-11-27 08:48:46 -08002043#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002044 case PSA_ALG_SHA_1:
2045 return( &mbedtls_sha1_info );
2046#endif
John Durkopee4e6602020-11-27 08:48:46 -08002047#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002048 case PSA_ALG_SHA_224:
2049 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002050#endif
2051#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002052 case PSA_ALG_SHA_256:
2053 return( &mbedtls_sha256_info );
2054#endif
John Durkopee4e6602020-11-27 08:48:46 -08002055#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002056 case PSA_ALG_SHA_384:
2057 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002058#endif
John Durkopee4e6602020-11-27 08:48:46 -08002059#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002060 case PSA_ALG_SHA_512:
2061 return( &mbedtls_sha512_info );
2062#endif
2063 default:
2064 return( NULL );
2065 }
2066}
John Durkop07cc04a2020-11-16 22:08:34 -08002067#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002068 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2069 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2070 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002071
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002072psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2073{
2074 switch( operation->alg )
2075 {
Gilles Peskine81736312018-06-26 15:04:31 +02002076 case 0:
2077 /* The object has (apparently) been initialized but it is not
2078 * in use. It's ok to call abort on such an object, and there's
2079 * nothing to do. */
2080 break;
John Durkopee4e6602020-11-27 08:48:46 -08002081#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002082 case PSA_ALG_MD2:
2083 mbedtls_md2_free( &operation->ctx.md2 );
2084 break;
2085#endif
John Durkopee4e6602020-11-27 08:48:46 -08002086#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002087 case PSA_ALG_MD4:
2088 mbedtls_md4_free( &operation->ctx.md4 );
2089 break;
2090#endif
John Durkopee4e6602020-11-27 08:48:46 -08002091#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002092 case PSA_ALG_MD5:
2093 mbedtls_md5_free( &operation->ctx.md5 );
2094 break;
2095#endif
John Durkopee4e6602020-11-27 08:48:46 -08002096#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002097 case PSA_ALG_RIPEMD160:
2098 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2099 break;
2100#endif
John Durkopee4e6602020-11-27 08:48:46 -08002101#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002102 case PSA_ALG_SHA_1:
2103 mbedtls_sha1_free( &operation->ctx.sha1 );
2104 break;
2105#endif
John Durkop6ca23272020-12-03 06:01:32 -08002106#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002107 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002108 mbedtls_sha256_free( &operation->ctx.sha256 );
2109 break;
2110#endif
2111#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002112 case PSA_ALG_SHA_256:
2113 mbedtls_sha256_free( &operation->ctx.sha256 );
2114 break;
2115#endif
John Durkop6ca23272020-12-03 06:01:32 -08002116#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002117 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002118 mbedtls_sha512_free( &operation->ctx.sha512 );
2119 break;
2120#endif
2121#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122 case PSA_ALG_SHA_512:
2123 mbedtls_sha512_free( &operation->ctx.sha512 );
2124 break;
2125#endif
2126 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002127 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002128 }
2129 operation->alg = 0;
2130 return( PSA_SUCCESS );
2131}
2132
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002133psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002134 psa_algorithm_t alg )
2135{
Janos Follath24eed8d2019-11-22 13:21:35 +00002136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002137
2138 /* A context must be freshly initialized before it can be set up. */
2139 if( operation->alg != 0 )
2140 {
2141 return( PSA_ERROR_BAD_STATE );
2142 }
2143
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002144 switch( alg )
2145 {
John Durkopee4e6602020-11-27 08:48:46 -08002146#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002147 case PSA_ALG_MD2:
2148 mbedtls_md2_init( &operation->ctx.md2 );
2149 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2150 break;
2151#endif
John Durkopee4e6602020-11-27 08:48:46 -08002152#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002153 case PSA_ALG_MD4:
2154 mbedtls_md4_init( &operation->ctx.md4 );
2155 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2156 break;
2157#endif
John Durkopee4e6602020-11-27 08:48:46 -08002158#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002159 case PSA_ALG_MD5:
2160 mbedtls_md5_init( &operation->ctx.md5 );
2161 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2162 break;
2163#endif
John Durkopee4e6602020-11-27 08:48:46 -08002164#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002165 case PSA_ALG_RIPEMD160:
2166 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2167 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2168 break;
2169#endif
John Durkopee4e6602020-11-27 08:48:46 -08002170#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002171 case PSA_ALG_SHA_1:
2172 mbedtls_sha1_init( &operation->ctx.sha1 );
2173 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2174 break;
2175#endif
John Durkopee4e6602020-11-27 08:48:46 -08002176#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002177 case PSA_ALG_SHA_224:
2178 mbedtls_sha256_init( &operation->ctx.sha256 );
2179 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2180 break;
John Durkopee4e6602020-11-27 08:48:46 -08002181#endif
2182#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002183 case PSA_ALG_SHA_256:
2184 mbedtls_sha256_init( &operation->ctx.sha256 );
2185 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2186 break;
2187#endif
John Durkopee4e6602020-11-27 08:48:46 -08002188#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189 case PSA_ALG_SHA_384:
2190 mbedtls_sha512_init( &operation->ctx.sha512 );
2191 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2192 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002193#endif
John Durkopee4e6602020-11-27 08:48:46 -08002194#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195 case PSA_ALG_SHA_512:
2196 mbedtls_sha512_init( &operation->ctx.sha512 );
2197 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2198 break;
2199#endif
2200 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002201 return( PSA_ALG_IS_HASH( alg ) ?
2202 PSA_ERROR_NOT_SUPPORTED :
2203 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002204 }
2205 if( ret == 0 )
2206 operation->alg = alg;
2207 else
2208 psa_hash_abort( operation );
2209 return( mbedtls_to_psa_error( ret ) );
2210}
2211
2212psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2213 const uint8_t *input,
2214 size_t input_length )
2215{
Janos Follath24eed8d2019-11-22 13:21:35 +00002216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002217
2218 /* Don't require hash implementations to behave correctly on a
2219 * zero-length input, which may have an invalid pointer. */
2220 if( input_length == 0 )
2221 return( PSA_SUCCESS );
2222
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002223 switch( operation->alg )
2224 {
John Durkopee4e6602020-11-27 08:48:46 -08002225#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002226 case PSA_ALG_MD2:
2227 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2228 input, input_length );
2229 break;
2230#endif
John Durkopee4e6602020-11-27 08:48:46 -08002231#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002232 case PSA_ALG_MD4:
2233 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2234 input, input_length );
2235 break;
2236#endif
John Durkopee4e6602020-11-27 08:48:46 -08002237#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238 case PSA_ALG_MD5:
2239 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2240 input, input_length );
2241 break;
2242#endif
John Durkopee4e6602020-11-27 08:48:46 -08002243#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244 case PSA_ALG_RIPEMD160:
2245 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2246 input, input_length );
2247 break;
2248#endif
John Durkopee4e6602020-11-27 08:48:46 -08002249#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002250 case PSA_ALG_SHA_1:
2251 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2252 input, input_length );
2253 break;
2254#endif
John Durkop6ca23272020-12-03 06:01:32 -08002255#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002257 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2258 input, input_length );
2259 break;
2260#endif
2261#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002262 case PSA_ALG_SHA_256:
2263 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2264 input, input_length );
2265 break;
2266#endif
John Durkop6ca23272020-12-03 06:01:32 -08002267#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002268 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002269 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2270 input, input_length );
2271 break;
2272#endif
2273#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002274 case PSA_ALG_SHA_512:
2275 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2276 input, input_length );
2277 break;
2278#endif
2279 default:
John Durkopee4e6602020-11-27 08:48:46 -08002280 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002281 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002282 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002283
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284 if( ret != 0 )
2285 psa_hash_abort( operation );
2286 return( mbedtls_to_psa_error( ret ) );
2287}
2288
2289psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2290 uint8_t *hash,
2291 size_t hash_size,
2292 size_t *hash_length )
2293{
itayzafrir40835d42018-08-02 13:14:17 +03002294 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002296 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002297
2298 /* Fill the output buffer with something that isn't a valid hash
2299 * (barring an attack on the hash and deliberately-crafted input),
2300 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002301 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002302 /* If hash_size is 0 then hash may be NULL and then the
2303 * call to memset would have undefined behavior. */
2304 if( hash_size != 0 )
2305 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002306
2307 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002308 {
2309 status = PSA_ERROR_BUFFER_TOO_SMALL;
2310 goto exit;
2311 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002312
2313 switch( operation->alg )
2314 {
John Durkopee4e6602020-11-27 08:48:46 -08002315#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316 case PSA_ALG_MD2:
2317 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2318 break;
2319#endif
John Durkopee4e6602020-11-27 08:48:46 -08002320#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321 case PSA_ALG_MD4:
2322 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2323 break;
2324#endif
John Durkopee4e6602020-11-27 08:48:46 -08002325#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326 case PSA_ALG_MD5:
2327 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2328 break;
2329#endif
John Durkopee4e6602020-11-27 08:48:46 -08002330#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331 case PSA_ALG_RIPEMD160:
2332 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2333 break;
2334#endif
John Durkopee4e6602020-11-27 08:48:46 -08002335#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336 case PSA_ALG_SHA_1:
2337 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2338 break;
2339#endif
John Durkop6ca23272020-12-03 06:01:32 -08002340#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002342 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2343 break;
2344#endif
2345#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 case PSA_ALG_SHA_256:
2347 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2348 break;
2349#endif
John Durkop6ca23272020-12-03 06:01:32 -08002350#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002352 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2353 break;
2354#endif
2355#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356 case PSA_ALG_SHA_512:
2357 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2358 break;
2359#endif
2360 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002361 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002362 }
itayzafrir40835d42018-08-02 13:14:17 +03002363 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364
itayzafrir40835d42018-08-02 13:14:17 +03002365exit:
2366 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002367 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002368 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002369 return( psa_hash_abort( operation ) );
2370 }
2371 else
2372 {
2373 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002374 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375 }
2376}
2377
Gilles Peskine2d277862018-06-18 15:41:12 +02002378psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2379 const uint8_t *hash,
2380 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381{
2382 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2383 size_t actual_hash_length;
2384 psa_status_t status = psa_hash_finish( operation,
2385 actual_hash, sizeof( actual_hash ),
2386 &actual_hash_length );
2387 if( status != PSA_SUCCESS )
2388 return( status );
2389 if( actual_hash_length != hash_length )
2390 return( PSA_ERROR_INVALID_SIGNATURE );
2391 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2392 return( PSA_ERROR_INVALID_SIGNATURE );
2393 return( PSA_SUCCESS );
2394}
2395
Gilles Peskine0a749c82019-11-28 19:33:58 +01002396psa_status_t psa_hash_compute( psa_algorithm_t alg,
2397 const uint8_t *input, size_t input_length,
2398 uint8_t *hash, size_t hash_size,
2399 size_t *hash_length )
2400{
2401 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2402 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2403
2404 *hash_length = hash_size;
2405 status = psa_hash_setup( &operation, alg );
2406 if( status != PSA_SUCCESS )
2407 goto exit;
2408 status = psa_hash_update( &operation, input, input_length );
2409 if( status != PSA_SUCCESS )
2410 goto exit;
2411 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2412 if( status != PSA_SUCCESS )
2413 goto exit;
2414
2415exit:
2416 if( status == PSA_SUCCESS )
2417 status = psa_hash_abort( &operation );
2418 else
2419 psa_hash_abort( &operation );
2420 return( status );
2421}
2422
2423psa_status_t psa_hash_compare( psa_algorithm_t alg,
2424 const uint8_t *input, size_t input_length,
2425 const uint8_t *hash, size_t hash_length )
2426{
2427 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2429
2430 status = psa_hash_setup( &operation, alg );
2431 if( status != PSA_SUCCESS )
2432 goto exit;
2433 status = psa_hash_update( &operation, input, input_length );
2434 if( status != PSA_SUCCESS )
2435 goto exit;
2436 status = psa_hash_verify( &operation, hash, hash_length );
2437 if( status != PSA_SUCCESS )
2438 goto exit;
2439
2440exit:
2441 if( status == PSA_SUCCESS )
2442 status = psa_hash_abort( &operation );
2443 else
2444 psa_hash_abort( &operation );
2445 return( status );
2446}
2447
Gilles Peskineeb35d782019-01-22 17:56:16 +01002448psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2449 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002450{
2451 if( target_operation->alg != 0 )
2452 return( PSA_ERROR_BAD_STATE );
2453
2454 switch( source_operation->alg )
2455 {
2456 case 0:
2457 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002458#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002459 case PSA_ALG_MD2:
2460 mbedtls_md2_clone( &target_operation->ctx.md2,
2461 &source_operation->ctx.md2 );
2462 break;
2463#endif
John Durkopee4e6602020-11-27 08:48:46 -08002464#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002465 case PSA_ALG_MD4:
2466 mbedtls_md4_clone( &target_operation->ctx.md4,
2467 &source_operation->ctx.md4 );
2468 break;
2469#endif
John Durkopee4e6602020-11-27 08:48:46 -08002470#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002471 case PSA_ALG_MD5:
2472 mbedtls_md5_clone( &target_operation->ctx.md5,
2473 &source_operation->ctx.md5 );
2474 break;
2475#endif
John Durkopee4e6602020-11-27 08:48:46 -08002476#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002477 case PSA_ALG_RIPEMD160:
2478 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2479 &source_operation->ctx.ripemd160 );
2480 break;
2481#endif
John Durkopee4e6602020-11-27 08:48:46 -08002482#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002483 case PSA_ALG_SHA_1:
2484 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2485 &source_operation->ctx.sha1 );
2486 break;
2487#endif
John Durkop6ca23272020-12-03 06:01:32 -08002488#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002489 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002490 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2491 &source_operation->ctx.sha256 );
2492 break;
2493#endif
2494#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002495 case PSA_ALG_SHA_256:
2496 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2497 &source_operation->ctx.sha256 );
2498 break;
2499#endif
John Durkop6ca23272020-12-03 06:01:32 -08002500#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002501 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002502 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2503 &source_operation->ctx.sha512 );
2504 break;
2505#endif
2506#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002507 case PSA_ALG_SHA_512:
2508 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2509 &source_operation->ctx.sha512 );
2510 break;
2511#endif
2512 default:
2513 return( PSA_ERROR_NOT_SUPPORTED );
2514 }
2515
2516 target_operation->alg = source_operation->alg;
2517 return( PSA_SUCCESS );
2518}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002519
2520
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002521/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002522/* MAC */
2523/****************************************************************/
2524
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002525static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526 psa_algorithm_t alg,
2527 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002528 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002529 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002530{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002532 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002533
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002534 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002535 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002536
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2538 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002539 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002540 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002541 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542 mode = MBEDTLS_MODE_STREAM;
2543 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544 case PSA_ALG_CTR:
2545 mode = MBEDTLS_MODE_CTR;
2546 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002547 case PSA_ALG_CFB:
2548 mode = MBEDTLS_MODE_CFB;
2549 break;
2550 case PSA_ALG_OFB:
2551 mode = MBEDTLS_MODE_OFB;
2552 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002553 case PSA_ALG_ECB_NO_PADDING:
2554 mode = MBEDTLS_MODE_ECB;
2555 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002556 case PSA_ALG_CBC_NO_PADDING:
2557 mode = MBEDTLS_MODE_CBC;
2558 break;
2559 case PSA_ALG_CBC_PKCS7:
2560 mode = MBEDTLS_MODE_CBC;
2561 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002562 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563 mode = MBEDTLS_MODE_CCM;
2564 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002565 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566 mode = MBEDTLS_MODE_GCM;
2567 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002568 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002569 mode = MBEDTLS_MODE_CHACHAPOLY;
2570 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571 default:
2572 return( NULL );
2573 }
2574 }
2575 else if( alg == PSA_ALG_CMAC )
2576 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002577 else
2578 return( NULL );
2579
2580 switch( key_type )
2581 {
2582 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002583 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002584 break;
2585 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002586 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2587 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002589 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002590 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002591 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002592 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2593 * but two-key Triple-DES is functionally three-key Triple-DES
2594 * with K1=K3, so that's how we present it to mbedtls. */
2595 if( key_bits == 128 )
2596 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597 break;
2598 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002599 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600 break;
2601 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002602 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002604 case PSA_KEY_TYPE_CHACHA20:
2605 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2606 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607 default:
2608 return( NULL );
2609 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002610 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002611 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612
Jaeden Amero23bbb752018-06-26 14:16:54 +01002613 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2614 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615}
2616
John Durkop6ba40d12020-11-10 08:50:04 -08002617#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002618static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002619{
Gilles Peskine2d277862018-06-18 15:41:12 +02002620 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002621 {
2622 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002623 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002624 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002625 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002626 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002627 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002628 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002629 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002630 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002631 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002632 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002633 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002634 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002635 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002636 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002637 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002638 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002639 return( 128 );
2640 default:
2641 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002642 }
2643}
John Durkop07cc04a2020-11-16 22:08:34 -08002644#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002645
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002646/* Initialize the MAC operation structure. Once this function has been
2647 * called, psa_mac_abort can run and will do the right thing. */
2648static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2649 psa_algorithm_t alg )
2650{
2651 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2652
2653 operation->alg = alg;
2654 operation->key_set = 0;
2655 operation->iv_set = 0;
2656 operation->iv_required = 0;
2657 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002658 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002659
2660#if defined(MBEDTLS_CMAC_C)
2661 if( alg == PSA_ALG_CMAC )
2662 {
2663 operation->iv_required = 0;
2664 mbedtls_cipher_init( &operation->ctx.cmac );
2665 status = PSA_SUCCESS;
2666 }
2667 else
2668#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002669#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002670 if( PSA_ALG_IS_HMAC( operation->alg ) )
2671 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002672 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2673 operation->ctx.hmac.hash_ctx.alg = 0;
2674 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002675 }
2676 else
John Durkopd0321952020-10-29 21:37:36 -07002677#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002678 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002679 if( ! PSA_ALG_IS_MAC( alg ) )
2680 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002681 }
2682
2683 if( status != PSA_SUCCESS )
2684 memset( operation, 0, sizeof( *operation ) );
2685 return( status );
2686}
2687
John Durkop6ba40d12020-11-10 08:50:04 -08002688#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002689static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2690{
Gilles Peskine3f108122018-12-07 18:14:53 +01002691 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002692 return( psa_hash_abort( &hmac->hash_ctx ) );
2693}
John Durkop6ba40d12020-11-10 08:50:04 -08002694#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002695
Gilles Peskine8c9def32018-02-08 10:02:12 +01002696psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2697{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002698 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002699 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002700 /* The object has (apparently) been initialized but it is not
2701 * in use. It's ok to call abort on such an object, and there's
2702 * nothing to do. */
2703 return( PSA_SUCCESS );
2704 }
2705 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002706#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002707 if( operation->alg == PSA_ALG_CMAC )
2708 {
2709 mbedtls_cipher_free( &operation->ctx.cmac );
2710 }
2711 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002712#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002713#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002714 if( PSA_ALG_IS_HMAC( operation->alg ) )
2715 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002716 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002717 }
2718 else
John Durkopd0321952020-10-29 21:37:36 -07002719#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002720 {
2721 /* Sanity check (shouldn't happen: operation->alg should
2722 * always have been initialized to a valid value). */
2723 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724 }
Moran Peker41deec42018-04-04 15:43:05 +03002725
Gilles Peskine8c9def32018-02-08 10:02:12 +01002726 operation->alg = 0;
2727 operation->key_set = 0;
2728 operation->iv_set = 0;
2729 operation->iv_required = 0;
2730 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002731 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002732
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002734
2735bad_state:
2736 /* If abort is called on an uninitialized object, we can't trust
2737 * anything. Wipe the object in case it contains confidential data.
2738 * This may result in a memory leak if a pointer gets overwritten,
2739 * but it's too late to do anything about this. */
2740 memset( operation, 0, sizeof( *operation ) );
2741 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742}
2743
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002744#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002745static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002746 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002747 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002748 const mbedtls_cipher_info_t *cipher_info )
2749{
Janos Follath24eed8d2019-11-22 13:21:35 +00002750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002751
2752 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002753
2754 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2755 if( ret != 0 )
2756 return( ret );
2757
2758 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002759 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002760 key_bits );
2761 return( ret );
2762}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002763#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002764
John Durkop6ba40d12020-11-10 08:50:04 -08002765#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002766static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2767 const uint8_t *key,
2768 size_t key_length,
2769 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002770{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002771 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002772 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002773 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002774 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002775 psa_status_t status;
2776
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002777 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2778 * overflow below. This should never trigger if the hash algorithm
2779 * is implemented correctly. */
2780 /* The size checks against the ipad and opad buffers cannot be written
2781 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2782 * because that triggers -Wlogical-op on GCC 7.3. */
2783 if( block_size > sizeof( ipad ) )
2784 return( PSA_ERROR_NOT_SUPPORTED );
2785 if( block_size > sizeof( hmac->opad ) )
2786 return( PSA_ERROR_NOT_SUPPORTED );
2787 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002788 return( PSA_ERROR_NOT_SUPPORTED );
2789
Gilles Peskined223b522018-06-11 18:12:58 +02002790 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002791 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002792 status = psa_hash_compute( hash_alg, key, key_length,
2793 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002794 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002795 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002796 }
Gilles Peskine96889972018-07-12 17:07:03 +02002797 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2798 * but it is permitted. It is common when HMAC is used in HKDF, for
2799 * example. Don't call `memcpy` in the 0-length because `key` could be
2800 * an invalid pointer which would make the behavior undefined. */
2801 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002802 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002803
Gilles Peskined223b522018-06-11 18:12:58 +02002804 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2805 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002806 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002807 ipad[i] ^= 0x36;
2808 memset( ipad + key_length, 0x36, block_size - key_length );
2809
2810 /* Copy the key material from ipad to opad, flipping the requisite bits,
2811 * and filling the rest of opad with the requisite constant. */
2812 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002813 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2814 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002815
Gilles Peskine01126fa2018-07-12 17:04:55 +02002816 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002817 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002818 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002819
Gilles Peskine01126fa2018-07-12 17:04:55 +02002820 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002821
2822cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002823 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002824
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002825 return( status );
2826}
John Durkop6ba40d12020-11-10 08:50:04 -08002827#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002828
Gilles Peskine89167cb2018-07-08 20:12:23 +02002829static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002830 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002831 psa_algorithm_t alg,
2832 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002833{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002834 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002835 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002836 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002837 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002838 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002839 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002840 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002841 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002842
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002843 /* A context must be freshly initialized before it can be set up. */
2844 if( operation->alg != 0 )
2845 {
2846 return( PSA_ERROR_BAD_STATE );
2847 }
2848
Gilles Peskined911eb72018-08-14 15:18:45 +02002849 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002850 if( status != PSA_SUCCESS )
2851 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002852 if( is_sign )
2853 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002854
Ronald Cron5c522922020-11-14 16:35:34 +01002855 status = psa_get_and_lock_transparent_key_slot_with_policy(
2856 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002857 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002858 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002859 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002860
Gilles Peskine8c9def32018-02-08 10:02:12 +01002861#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002862 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002863 {
2864 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002865 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002866 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002868 if( cipher_info == NULL )
2869 {
2870 status = PSA_ERROR_NOT_SUPPORTED;
2871 goto exit;
2872 }
2873 operation->mac_size = cipher_info->block_size;
2874 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2875 status = mbedtls_to_psa_error( ret );
2876 }
2877 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002878#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002879#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02002880 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002881 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002882 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002883 if( hash_alg == 0 )
2884 {
2885 status = PSA_ERROR_NOT_SUPPORTED;
2886 goto exit;
2887 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002888
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002889 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002890 /* Sanity check. This shouldn't fail on a valid configuration. */
2891 if( operation->mac_size == 0 ||
2892 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2893 {
2894 status = PSA_ERROR_NOT_SUPPORTED;
2895 goto exit;
2896 }
2897
Gilles Peskine8e338702019-07-30 20:06:31 +02002898 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002899 {
2900 status = PSA_ERROR_INVALID_ARGUMENT;
2901 goto exit;
2902 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002903
Gilles Peskine01126fa2018-07-12 17:04:55 +02002904 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002905 slot->key.data,
2906 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02002907 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002908 }
2909 else
John Durkopd0321952020-10-29 21:37:36 -07002910#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002911 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002912 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002913 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002914 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002915
Gilles Peskined911eb72018-08-14 15:18:45 +02002916 if( truncated == 0 )
2917 {
2918 /* The "normal" case: untruncated algorithm. Nothing to do. */
2919 }
2920 else if( truncated < 4 )
2921 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002922 /* A very short MAC is too short for security since it can be
2923 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2924 * so we make this our minimum, even though 32 bits is still
2925 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002926 status = PSA_ERROR_NOT_SUPPORTED;
2927 }
2928 else if( truncated > operation->mac_size )
2929 {
2930 /* It's impossible to "truncate" to a larger length. */
2931 status = PSA_ERROR_INVALID_ARGUMENT;
2932 }
2933 else
2934 operation->mac_size = truncated;
2935
Gilles Peskinefbfac682018-07-08 20:51:54 +02002936exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002937 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002938 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002939 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002940 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002941 else
2942 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002943 operation->key_set = 1;
2944 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002945
Ronald Cron5c522922020-11-14 16:35:34 +01002946 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002947
Ronald Cron5c522922020-11-14 16:35:34 +01002948 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002949}
2950
Gilles Peskine89167cb2018-07-08 20:12:23 +02002951psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002952 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002953 psa_algorithm_t alg )
2954{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002955 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002956}
2957
2958psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002959 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002960 psa_algorithm_t alg )
2961{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002962 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002963}
2964
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2966 const uint8_t *input,
2967 size_t input_length )
2968{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002969 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002970 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002971 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002972 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002973 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002974 operation->has_input = 1;
2975
Gilles Peskine8c9def32018-02-08 10:02:12 +01002976#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002977 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002978 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002979 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2980 input, input_length );
2981 status = mbedtls_to_psa_error( ret );
2982 }
2983 else
2984#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002985#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002986 if( PSA_ALG_IS_HMAC( operation->alg ) )
2987 {
2988 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2989 input_length );
2990 }
2991 else
John Durkopd0321952020-10-29 21:37:36 -07002992#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002993 {
2994 /* This shouldn't happen if `operation` was initialized by
2995 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002996 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002997 }
2998
Gilles Peskinefbfac682018-07-08 20:51:54 +02002999 if( status != PSA_SUCCESS )
3000 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003001 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002}
3003
John Durkop6ba40d12020-11-10 08:50:04 -08003004#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003005static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3006 uint8_t *mac,
3007 size_t mac_size )
3008{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003009 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003010 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3011 size_t hash_size = 0;
3012 size_t block_size = psa_get_hash_block_size( hash_alg );
3013 psa_status_t status;
3014
Gilles Peskine01126fa2018-07-12 17:04:55 +02003015 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3016 if( status != PSA_SUCCESS )
3017 return( status );
3018 /* From here on, tmp needs to be wiped. */
3019
3020 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3021 if( status != PSA_SUCCESS )
3022 goto exit;
3023
3024 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3025 if( status != PSA_SUCCESS )
3026 goto exit;
3027
3028 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3029 if( status != PSA_SUCCESS )
3030 goto exit;
3031
Gilles Peskined911eb72018-08-14 15:18:45 +02003032 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3033 if( status != PSA_SUCCESS )
3034 goto exit;
3035
3036 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003037
3038exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003039 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003040 return( status );
3041}
John Durkop6ba40d12020-11-10 08:50:04 -08003042#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003043
mohammad16036df908f2018-04-02 08:34:15 -07003044static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003045 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003046 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003047{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003048 if( ! operation->key_set )
3049 return( PSA_ERROR_BAD_STATE );
3050 if( operation->iv_required && ! operation->iv_set )
3051 return( PSA_ERROR_BAD_STATE );
3052
Gilles Peskine8c9def32018-02-08 10:02:12 +01003053 if( mac_size < operation->mac_size )
3054 return( PSA_ERROR_BUFFER_TOO_SMALL );
3055
Gilles Peskine8c9def32018-02-08 10:02:12 +01003056#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003057 if( operation->alg == PSA_ALG_CMAC )
3058 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003059 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003060 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3061 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003062 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003063 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003064 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003065 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003066 else
3067#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003068#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003069 if( PSA_ALG_IS_HMAC( operation->alg ) )
3070 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003071 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003072 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003073 }
3074 else
John Durkopd0321952020-10-29 21:37:36 -07003075#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003076 {
3077 /* This shouldn't happen if `operation` was initialized by
3078 * a setup function. */
3079 return( PSA_ERROR_BAD_STATE );
3080 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003081}
3082
Gilles Peskineacd4be32018-07-08 19:56:25 +02003083psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3084 uint8_t *mac,
3085 size_t mac_size,
3086 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003087{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003088 psa_status_t status;
3089
Jaeden Amero252ef282019-02-15 14:05:35 +00003090 if( operation->alg == 0 )
3091 {
3092 return( PSA_ERROR_BAD_STATE );
3093 }
3094
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003095 /* Fill the output buffer with something that isn't a valid mac
3096 * (barring an attack on the mac and deliberately-crafted input),
3097 * in case the caller doesn't check the return status properly. */
3098 *mac_length = mac_size;
3099 /* If mac_size is 0 then mac may be NULL and then the
3100 * call to memset would have undefined behavior. */
3101 if( mac_size != 0 )
3102 memset( mac, '!', mac_size );
3103
Gilles Peskine89167cb2018-07-08 20:12:23 +02003104 if( ! operation->is_sign )
3105 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003106 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003107 }
mohammad16036df908f2018-04-02 08:34:15 -07003108
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003109 status = psa_mac_finish_internal( operation, mac, mac_size );
3110
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003111 if( status == PSA_SUCCESS )
3112 {
3113 status = psa_mac_abort( operation );
3114 if( status == PSA_SUCCESS )
3115 *mac_length = operation->mac_size;
3116 else
3117 memset( mac, '!', mac_size );
3118 }
3119 else
3120 psa_mac_abort( operation );
3121 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003122}
3123
Gilles Peskineacd4be32018-07-08 19:56:25 +02003124psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3125 const uint8_t *mac,
3126 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127{
Gilles Peskine828ed142018-06-18 23:25:51 +02003128 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003129 psa_status_t status;
3130
Jaeden Amero252ef282019-02-15 14:05:35 +00003131 if( operation->alg == 0 )
3132 {
3133 return( PSA_ERROR_BAD_STATE );
3134 }
3135
Gilles Peskine89167cb2018-07-08 20:12:23 +02003136 if( operation->is_sign )
3137 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003138 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003139 }
3140 if( operation->mac_size != mac_length )
3141 {
3142 status = PSA_ERROR_INVALID_SIGNATURE;
3143 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003144 }
mohammad16036df908f2018-04-02 08:34:15 -07003145
3146 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003147 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003148 if( status != PSA_SUCCESS )
3149 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003150
3151 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3152 status = PSA_ERROR_INVALID_SIGNATURE;
3153
3154cleanup:
3155 if( status == PSA_SUCCESS )
3156 status = psa_mac_abort( operation );
3157 else
3158 psa_mac_abort( operation );
3159
Gilles Peskine3f108122018-12-07 18:14:53 +01003160 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003161
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003162 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003163}
3164
3165
Gilles Peskine20035e32018-02-03 22:44:14 +01003166
Gilles Peskine20035e32018-02-03 22:44:14 +01003167/****************************************************************/
3168/* Asymmetric cryptography */
3169/****************************************************************/
3170
John Durkop6ba40d12020-11-10 08:50:04 -08003171#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3172 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003173/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003174 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003175static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3176 size_t hash_length,
3177 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003178{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003179 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003180 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003181 *md_alg = mbedtls_md_get_type( md_info );
3182
3183 /* The Mbed TLS RSA module uses an unsigned int for hash length
3184 * parameters. Validate that it fits so that we don't risk an
3185 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003186#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003187 if( hash_length > UINT_MAX )
3188 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003189#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003190
John Durkop0e005192020-10-31 22:06:54 -07003191#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003192 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3193 * must be correct. */
3194 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3195 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003196 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003197 if( md_info == NULL )
3198 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003199 if( mbedtls_md_get_size( md_info ) != hash_length )
3200 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003201 }
John Durkop0e005192020-10-31 22:06:54 -07003202#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003203
John Durkop0e005192020-10-31 22:06:54 -07003204#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003205 /* PSS requires a hash internally. */
3206 if( PSA_ALG_IS_RSA_PSS( alg ) )
3207 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003208 if( md_info == NULL )
3209 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003210 }
John Durkop0e005192020-10-31 22:06:54 -07003211#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003212
Gilles Peskine61b91d42018-06-08 16:09:36 +02003213 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003214}
3215
Gilles Peskine2b450e32018-06-27 15:42:46 +02003216static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3217 psa_algorithm_t alg,
3218 const uint8_t *hash,
3219 size_t hash_length,
3220 uint8_t *signature,
3221 size_t signature_size,
3222 size_t *signature_length )
3223{
3224 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003226 mbedtls_md_type_t md_alg;
3227
3228 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3229 if( status != PSA_SUCCESS )
3230 return( status );
3231
Gilles Peskine630a18a2018-06-29 17:49:35 +02003232 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003233 return( PSA_ERROR_BUFFER_TOO_SMALL );
3234
John Durkop0e005192020-10-31 22:06:54 -07003235#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003236 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3237 {
3238 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3239 MBEDTLS_MD_NONE );
3240 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003241 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003242 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003243 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003244 md_alg,
3245 (unsigned int) hash_length,
3246 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003247 signature );
3248 }
3249 else
John Durkop0e005192020-10-31 22:06:54 -07003250#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3251#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003252 if( PSA_ALG_IS_RSA_PSS( alg ) )
3253 {
3254 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3255 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003256 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003257 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003258 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003259 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003260 (unsigned int) hash_length,
3261 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003262 signature );
3263 }
3264 else
John Durkop0e005192020-10-31 22:06:54 -07003265#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003266 {
3267 return( PSA_ERROR_INVALID_ARGUMENT );
3268 }
3269
3270 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003271 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003272 return( mbedtls_to_psa_error( ret ) );
3273}
3274
3275static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3276 psa_algorithm_t alg,
3277 const uint8_t *hash,
3278 size_t hash_length,
3279 const uint8_t *signature,
3280 size_t signature_length )
3281{
3282 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003284 mbedtls_md_type_t md_alg;
3285
3286 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3287 if( status != PSA_SUCCESS )
3288 return( status );
3289
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003290 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3291 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003292
John Durkop0e005192020-10-31 22:06:54 -07003293#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003294 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3295 {
3296 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3297 MBEDTLS_MD_NONE );
3298 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003299 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003300 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003301 MBEDTLS_RSA_PUBLIC,
3302 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003303 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003304 hash,
3305 signature );
3306 }
3307 else
John Durkop0e005192020-10-31 22:06:54 -07003308#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3309#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003310 if( PSA_ALG_IS_RSA_PSS( alg ) )
3311 {
3312 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3313 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003314 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003315 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003316 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003317 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003318 (unsigned int) hash_length,
3319 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003320 signature );
3321 }
3322 else
John Durkop0e005192020-10-31 22:06:54 -07003323#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003324 {
3325 return( PSA_ERROR_INVALID_ARGUMENT );
3326 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003327
3328 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3329 * the rest of the signature is invalid". This has little use in
3330 * practice and PSA doesn't report this distinction. */
3331 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3332 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003333 return( mbedtls_to_psa_error( ret ) );
3334}
John Durkop6ba40d12020-11-10 08:50:04 -08003335#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3336 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003337
John Durkop6ba40d12020-11-10 08:50:04 -08003338#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3339 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003340/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3341 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3342 * (even though these functions don't modify it). */
3343static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3344 psa_algorithm_t alg,
3345 const uint8_t *hash,
3346 size_t hash_length,
3347 uint8_t *signature,
3348 size_t signature_size,
3349 size_t *signature_length )
3350{
Janos Follath24eed8d2019-11-22 13:21:35 +00003351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003352 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003353 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003354 mbedtls_mpi_init( &r );
3355 mbedtls_mpi_init( &s );
3356
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003357 if( signature_size < 2 * curve_bytes )
3358 {
3359 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3360 goto cleanup;
3361 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003362
John Durkop0ea39e02020-10-13 19:58:20 -07003363#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003364 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3365 {
3366 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3367 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3368 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003369 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3370 &ecp->d, hash,
3371 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003372 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003373 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003374 }
3375 else
John Durkop0ea39e02020-10-13 19:58:20 -07003376#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003377 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003378 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003379 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3380 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003381 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003382 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003383 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003384
3385 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3386 signature,
3387 curve_bytes ) );
3388 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3389 signature + curve_bytes,
3390 curve_bytes ) );
3391
3392cleanup:
3393 mbedtls_mpi_free( &r );
3394 mbedtls_mpi_free( &s );
3395 if( ret == 0 )
3396 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003397 return( mbedtls_to_psa_error( ret ) );
3398}
3399
3400static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3401 const uint8_t *hash,
3402 size_t hash_length,
3403 const uint8_t *signature,
3404 size_t signature_length )
3405{
Janos Follath24eed8d2019-11-22 13:21:35 +00003406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003407 mbedtls_mpi r, s;
3408 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3409 mbedtls_mpi_init( &r );
3410 mbedtls_mpi_init( &s );
3411
3412 if( signature_length != 2 * curve_bytes )
3413 return( PSA_ERROR_INVALID_SIGNATURE );
3414
3415 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3416 signature,
3417 curve_bytes ) );
3418 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3419 signature + curve_bytes,
3420 curve_bytes ) );
3421
Steven Cooremanacda8342020-07-24 23:09:52 +02003422 /* Check whether the public part is loaded. If not, load it. */
3423 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3424 {
3425 MBEDTLS_MPI_CHK(
3426 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003427 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003428 }
3429
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003430 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3431 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003432
3433cleanup:
3434 mbedtls_mpi_free( &r );
3435 mbedtls_mpi_free( &s );
3436 return( mbedtls_to_psa_error( ret ) );
3437}
John Durkop6ba40d12020-11-10 08:50:04 -08003438#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3439 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003440
Ronald Croncf56a0a2020-08-04 09:51:30 +02003441psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003442 psa_algorithm_t alg,
3443 const uint8_t *hash,
3444 size_t hash_length,
3445 uint8_t *signature,
3446 size_t signature_size,
3447 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003448{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003450 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003451 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003452
3453 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003454 /* Immediately reject a zero-length signature buffer. This guarantees
3455 * that signature must be a valid pointer. (On the other hand, the hash
3456 * buffer can in principle be empty since it doesn't actually have
3457 * to be a hash.) */
3458 if( signature_size == 0 )
3459 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003460
Ronald Cron5c522922020-11-14 16:35:34 +01003461 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3462 PSA_KEY_USAGE_SIGN_HASH,
3463 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003464 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003465 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003466 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003467 {
3468 status = PSA_ERROR_INVALID_ARGUMENT;
3469 goto exit;
3470 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003471
Steven Cooremancd84cb42020-07-16 20:28:36 +02003472 /* Try any of the available accelerators first */
3473 status = psa_driver_wrapper_sign_hash( slot,
3474 alg,
3475 hash,
3476 hash_length,
3477 signature,
3478 signature_size,
3479 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003480 if( status != PSA_ERROR_NOT_SUPPORTED ||
3481 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003482 goto exit;
3483
Steven Cooreman7a250572020-07-17 16:43:05 +02003484 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003485#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3486 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003487 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003488 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003489 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003490
Ronald Cron90857082020-11-25 14:58:33 +01003491 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3492 slot->key.data,
3493 slot->key.bytes,
3494 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003495 if( status != PSA_SUCCESS )
3496 goto exit;
3497
Steven Cooremana2371e52020-07-28 14:30:39 +02003498 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003499 alg,
3500 hash, hash_length,
3501 signature, signature_size,
3502 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003503
Steven Cooremana2371e52020-07-28 14:30:39 +02003504 mbedtls_rsa_free( rsa );
3505 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003506 }
3507 else
John Durkop6ba40d12020-11-10 08:50:04 -08003508#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3509 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003510 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003511 {
John Durkop9814fa22020-11-04 12:28:15 -08003512#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3513 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003514 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003515#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003516 PSA_ALG_IS_ECDSA( alg )
3517#else
3518 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3519#endif
3520 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003521 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003522 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003523 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003524 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003525 slot->key.data,
3526 slot->key.bytes,
3527 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003528 if( status != PSA_SUCCESS )
3529 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003530 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003531 alg,
3532 hash, hash_length,
3533 signature, signature_size,
3534 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003535 mbedtls_ecp_keypair_free( ecp );
3536 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003537 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003538 else
John Durkop9814fa22020-11-04 12:28:15 -08003539#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3540 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003541 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003542 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003543 }
itayzafrir5c753392018-05-08 11:18:38 +03003544 }
3545 else
itayzafrir5c753392018-05-08 11:18:38 +03003546 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003547 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003548 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003549
3550exit:
3551 /* Fill the unused part of the output buffer (the whole buffer on error,
3552 * the trailing part on success) with something that isn't a valid mac
3553 * (barring an attack on the mac and deliberately-crafted input),
3554 * in case the caller doesn't check the return status properly. */
3555 if( status == PSA_SUCCESS )
3556 memset( signature + *signature_length, '!',
3557 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003558 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003559 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003560 /* If signature_size is 0 then we have nothing to do. We must not call
3561 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003562
Ronald Cron5c522922020-11-14 16:35:34 +01003563 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003564
Ronald Cron5c522922020-11-14 16:35:34 +01003565 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003566}
3567
Ronald Croncf56a0a2020-08-04 09:51:30 +02003568psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003569 psa_algorithm_t alg,
3570 const uint8_t *hash,
3571 size_t hash_length,
3572 const uint8_t *signature,
3573 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003574{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003576 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003577 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003578
Ronald Cron5c522922020-11-14 16:35:34 +01003579 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3580 PSA_KEY_USAGE_VERIFY_HASH,
3581 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003582 if( status != PSA_SUCCESS )
3583 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003584
Steven Cooreman55ae2172020-07-17 19:46:15 +02003585 /* Try any of the available accelerators first */
3586 status = psa_driver_wrapper_verify_hash( slot,
3587 alg,
3588 hash,
3589 hash_length,
3590 signature,
3591 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003592 if( status != PSA_ERROR_NOT_SUPPORTED ||
3593 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003594 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003595
John Durkop6ba40d12020-11-10 08:50:04 -08003596#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3597 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003598 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003599 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003600 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003601
Ronald Cron90857082020-11-25 14:58:33 +01003602 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3603 slot->key.data,
3604 slot->key.bytes,
3605 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003606 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003607 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003608
Steven Cooremana2371e52020-07-28 14:30:39 +02003609 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003610 alg,
3611 hash, hash_length,
3612 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003613 mbedtls_rsa_free( rsa );
3614 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003615 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003616 }
3617 else
John Durkop6ba40d12020-11-10 08:50:04 -08003618#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3619 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003620 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003621 {
John Durkop9814fa22020-11-04 12:28:15 -08003622#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3623 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003624 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003625 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003626 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003627 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003628 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003629 slot->key.data,
3630 slot->key.bytes,
3631 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003632 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003633 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003634 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003635 hash, hash_length,
3636 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003637 mbedtls_ecp_keypair_free( ecp );
3638 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003639 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003640 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003641 else
John Durkop9814fa22020-11-04 12:28:15 -08003642#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3643 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003644 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003645 status = PSA_ERROR_INVALID_ARGUMENT;
3646 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003647 }
itayzafrir5c753392018-05-08 11:18:38 +03003648 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003649 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003650 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003651 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003652 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003653
3654exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003655 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003656
Ronald Cron5c522922020-11-14 16:35:34 +01003657 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003658}
3659
John Durkop0e005192020-10-31 22:06:54 -07003660#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003661static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3662 mbedtls_rsa_context *rsa )
3663{
3664 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3665 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3666 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3667 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3668}
John Durkop0e005192020-10-31 22:06:54 -07003669#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003670
Ronald Croncf56a0a2020-08-04 09:51:30 +02003671psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003672 psa_algorithm_t alg,
3673 const uint8_t *input,
3674 size_t input_length,
3675 const uint8_t *salt,
3676 size_t salt_length,
3677 uint8_t *output,
3678 size_t output_size,
3679 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003680{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003681 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003682 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003683 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003684
Darryl Green5cc689a2018-07-24 15:34:10 +01003685 (void) input;
3686 (void) input_length;
3687 (void) salt;
3688 (void) output;
3689 (void) output_size;
3690
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003691 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003692
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003693 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3694 return( PSA_ERROR_INVALID_ARGUMENT );
3695
Ronald Cron5c522922020-11-14 16:35:34 +01003696 status = psa_get_and_lock_transparent_key_slot_with_policy(
3697 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003698 if( status != PSA_SUCCESS )
3699 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003700 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3701 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003702 {
3703 status = PSA_ERROR_INVALID_ARGUMENT;
3704 goto exit;
3705 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003706
John Durkop6ba40d12020-11-10 08:50:04 -08003707#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3708 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003709 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003710 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003711 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003712 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3713 slot->key.data,
3714 slot->key.bytes,
3715 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003716 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003717 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003718
3719 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003720 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003721 status = PSA_ERROR_BUFFER_TOO_SMALL;
3722 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003723 }
John Durkop0e005192020-10-31 22:06:54 -07003724#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003725 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003726 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003727 status = mbedtls_to_psa_error(
3728 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003729 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003730 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003731 MBEDTLS_RSA_PUBLIC,
3732 input_length,
3733 input,
3734 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003735 }
3736 else
John Durkop0e005192020-10-31 22:06:54 -07003737#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3738#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003739 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003740 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003741 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003742 status = mbedtls_to_psa_error(
3743 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003744 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003745 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003746 MBEDTLS_RSA_PUBLIC,
3747 salt, salt_length,
3748 input_length,
3749 input,
3750 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003751 }
3752 else
John Durkop0e005192020-10-31 22:06:54 -07003753#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003754 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003755 status = PSA_ERROR_INVALID_ARGUMENT;
3756 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003757 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003758rsa_exit:
3759 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003760 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003761
Steven Cooremana2371e52020-07-28 14:30:39 +02003762 mbedtls_rsa_free( rsa );
3763 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003764 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003765 else
John Durkop9814fa22020-11-04 12:28:15 -08003766#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003767 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003768 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003769 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003770 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003771
3772exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003773 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003774
Ronald Cron5c522922020-11-14 16:35:34 +01003775 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003776}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003777
Ronald Croncf56a0a2020-08-04 09:51:30 +02003778psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003779 psa_algorithm_t alg,
3780 const uint8_t *input,
3781 size_t input_length,
3782 const uint8_t *salt,
3783 size_t salt_length,
3784 uint8_t *output,
3785 size_t output_size,
3786 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003787{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003788 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003789 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003790 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003791
Darryl Green5cc689a2018-07-24 15:34:10 +01003792 (void) input;
3793 (void) input_length;
3794 (void) salt;
3795 (void) output;
3796 (void) output_size;
3797
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003798 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003799
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003800 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3801 return( PSA_ERROR_INVALID_ARGUMENT );
3802
Ronald Cron5c522922020-11-14 16:35:34 +01003803 status = psa_get_and_lock_transparent_key_slot_with_policy(
3804 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003805 if( status != PSA_SUCCESS )
3806 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003807 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003808 {
3809 status = PSA_ERROR_INVALID_ARGUMENT;
3810 goto exit;
3811 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003812
John Durkop6ba40d12020-11-10 08:50:04 -08003813#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3814 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003815 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003816 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003817 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003818 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3819 slot->key.data,
3820 slot->key.bytes,
3821 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003822 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003823 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003824
Steven Cooremana2371e52020-07-28 14:30:39 +02003825 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003826 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003827 status = PSA_ERROR_INVALID_ARGUMENT;
3828 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003829 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003830
John Durkop0e005192020-10-31 22:06:54 -07003831#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003832 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003833 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003834 status = mbedtls_to_psa_error(
3835 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003836 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003837 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003838 MBEDTLS_RSA_PRIVATE,
3839 output_length,
3840 input,
3841 output,
3842 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003843 }
3844 else
John Durkop0e005192020-10-31 22:06:54 -07003845#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3846#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003847 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003848 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003849 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003850 status = mbedtls_to_psa_error(
3851 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003852 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003853 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003854 MBEDTLS_RSA_PRIVATE,
3855 salt, salt_length,
3856 output_length,
3857 input,
3858 output,
3859 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003860 }
3861 else
John Durkop0e005192020-10-31 22:06:54 -07003862#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003863 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003864 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003865 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003866
Steven Cooreman4fed4552020-08-03 14:46:03 +02003867rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003868 mbedtls_rsa_free( rsa );
3869 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003870 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871 else
John Durkop6ba40d12020-11-10 08:50:04 -08003872#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3873 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003874 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003875 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003877
3878exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003879 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003880
Ronald Cron5c522922020-11-14 16:35:34 +01003881 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003882}
Gilles Peskine20035e32018-02-03 22:44:14 +01003883
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003884
3885
mohammad1603503973b2018-03-12 15:59:30 +02003886/****************************************************************/
3887/* Symmetric cryptography */
3888/****************************************************************/
3889
Gilles Peskinee553c652018-06-04 16:22:46 +02003890static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003891 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02003892 psa_algorithm_t alg,
3893 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003894{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003895 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003896 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003897 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003898 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003899 size_t key_bits;
3900 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003901 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3902 PSA_KEY_USAGE_ENCRYPT :
3903 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003904
Steven Cooremand3feccd2020-09-01 15:56:14 +02003905 /* A context must be freshly initialized before it can be set up. */
3906 if( operation->alg != 0 )
3907 return( PSA_ERROR_BAD_STATE );
3908
Steven Cooremana07b9972020-09-10 14:54:14 +02003909 /* The requested algorithm must be one that can be processed by cipher. */
3910 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02003911 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003912
Steven Cooremanef8575e2020-09-11 11:44:50 +02003913 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01003914 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003915 if( status != PSA_SUCCESS )
3916 goto exit;
3917
3918 /* Initialize the operation struct members, except for alg. The alg member
3919 * is used to indicate to psa_cipher_abort that there are resources to free,
3920 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003921 operation->key_set = 0;
3922 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003923 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02003924 operation->iv_size = 0;
3925 operation->block_size = 0;
3926 if( alg == PSA_ALG_ECB_NO_PADDING )
3927 operation->iv_required = 0;
3928 else
3929 operation->iv_required = 1;
3930
Steven Cooremana07b9972020-09-10 14:54:14 +02003931 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02003932 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003933 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003934 slot,
3935 alg );
3936 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003937 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003938 slot,
3939 alg );
3940
Steven Cooremanef8575e2020-09-11 11:44:50 +02003941 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003942 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003943 /* Once the driver context is initialised, it needs to be freed using
3944 * psa_cipher_abort. Indicate this through setting alg. */
3945 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003946 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02003947
Steven Cooremand3feccd2020-09-01 15:56:14 +02003948 if( status != PSA_ERROR_NOT_SUPPORTED ||
3949 psa_key_lifetime_is_external( slot->attr.lifetime ) )
3950 goto exit;
3951
Steven Cooremana07b9972020-09-10 14:54:14 +02003952 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02003953 * available for the given algorithm & key. */
3954 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02003955
Steven Cooremana07b9972020-09-10 14:54:14 +02003956 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02003957 * psa_cipher_abort. Indicate there is something to be freed through setting
3958 * alg, and indicate the operation is being done using mbedtls crypto through
3959 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003960 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003961 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02003962
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003963 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02003964 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003965 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003966 {
3967 status = PSA_ERROR_NOT_SUPPORTED;
3968 goto exit;
3969 }
mohammad1603503973b2018-03-12 15:59:30 +02003970
mohammad1603503973b2018-03-12 15:59:30 +02003971 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003972 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003973 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003974
David Brown12ca5032021-02-11 11:02:00 -07003975#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02003976 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003977 {
3978 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003979 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01003980 memcpy( keys, slot->key.data, 16 );
3981 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003982 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3983 keys,
3984 192, cipher_operation );
3985 }
3986 else
3987#endif
3988 {
3989 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003990 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003991 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003992 }
Moran Peker41deec42018-04-04 15:43:05 +03003993 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003994 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003995
David Brown63ca2602021-01-26 11:51:12 -07003996#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
3997 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003998 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003999 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004000 case PSA_ALG_CBC_NO_PADDING:
4001 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4002 MBEDTLS_PADDING_NONE );
4003 break;
4004 case PSA_ALG_CBC_PKCS7:
4005 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4006 MBEDTLS_PADDING_PKCS7 );
4007 break;
4008 default:
4009 /* The algorithm doesn't involve padding. */
4010 ret = 0;
4011 break;
4012 }
4013 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004014 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07004015#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02004016
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004017 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004018 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004019 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4020 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004021 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004022 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004023 }
David Brown1bfe4d72021-02-16 12:54:35 -07004024#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02004025 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004026 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004027 operation->iv_size = 12;
4028#endif
mohammad1603503973b2018-03-12 15:59:30 +02004029
Steven Cooreman7df02922020-09-09 15:28:49 +02004030 status = PSA_SUCCESS;
4031
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004032exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004033 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004034 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004035 if( status == PSA_SUCCESS )
4036 {
4037 /* Update operation flags for both driver and software implementations */
4038 operation->key_set = 1;
4039 }
4040 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004041 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004042
Ronald Cron5c522922020-11-14 16:35:34 +01004043 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004044
Ronald Cron5c522922020-11-14 16:35:34 +01004045 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004046}
4047
Gilles Peskinefe119512018-07-08 21:39:34 +02004048psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004049 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004050 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004051{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004052 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004053}
4054
Gilles Peskinefe119512018-07-08 21:39:34 +02004055psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004056 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004057 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004058{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004059 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004060}
4061
Gilles Peskinefe119512018-07-08 21:39:34 +02004062psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004063 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004064 size_t iv_size,
4065 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004066{
itayzafrir534bd7c2018-08-02 13:56:32 +03004067 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004069 if( operation->iv_set || ! operation->iv_required )
4070 {
4071 return( PSA_ERROR_BAD_STATE );
4072 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004073
Steven Cooremanef8575e2020-09-11 11:44:50 +02004074 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004075 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004076 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004077 iv,
4078 iv_size,
4079 iv_length );
4080 goto exit;
4081 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004082
Moran Peker41deec42018-04-04 15:43:05 +03004083 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004084 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004085 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004086 goto exit;
4087 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004088 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004089 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004090 if( ret != 0 )
4091 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004092 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004093 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004094 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004095
mohammad16038481e742018-03-18 13:57:31 +02004096 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004097 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004098
Moran Peker395db872018-05-31 14:07:14 +03004099exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004100 if( status == PSA_SUCCESS )
4101 operation->iv_set = 1;
4102 else
Moran Peker395db872018-05-31 14:07:14 +03004103 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004104 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004105}
4106
Gilles Peskinefe119512018-07-08 21:39:34 +02004107psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004108 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004109 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004110{
itayzafrir534bd7c2018-08-02 13:56:32 +03004111 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004113 if( operation->iv_set || ! operation->iv_required )
4114 {
4115 return( PSA_ERROR_BAD_STATE );
4116 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004117
Steven Cooremanef8575e2020-09-11 11:44:50 +02004118 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004119 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004120 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004121 iv,
4122 iv_length );
4123 goto exit;
4124 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004125
Moran Pekera28258c2018-05-29 16:25:04 +03004126 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004127 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004128 status = PSA_ERROR_INVALID_ARGUMENT;
4129 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004130 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004131 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4132 status = mbedtls_to_psa_error( ret );
4133exit:
4134 if( status == PSA_SUCCESS )
4135 operation->iv_set = 1;
4136 else
mohammad1603503973b2018-03-12 15:59:30 +02004137 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004138 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004139}
4140
Steven Cooreman177deba2020-09-07 17:14:14 +02004141/* Process input for which the algorithm is set to ECB mode. This requires
4142 * manual processing, since the PSA API is defined as being able to process
4143 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4144 * underlying mbedtls_cipher_update only takes full blocks. */
4145static psa_status_t psa_cipher_update_ecb_internal(
4146 mbedtls_cipher_context_t *ctx,
4147 const uint8_t *input,
4148 size_t input_length,
4149 uint8_t *output,
4150 size_t output_size,
4151 size_t *output_length )
4152{
4153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4154 size_t block_size = ctx->cipher_info->block_size;
4155 size_t internal_output_length = 0;
4156 *output_length = 0;
4157
4158 if( input_length == 0 )
4159 {
4160 status = PSA_SUCCESS;
4161 goto exit;
4162 }
4163
4164 if( ctx->unprocessed_len > 0 )
4165 {
4166 /* Fill up to block size, and run the block if there's a full one. */
4167 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4168
4169 if( input_length < bytes_to_copy )
4170 bytes_to_copy = input_length;
4171
4172 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4173 input, bytes_to_copy );
4174 input_length -= bytes_to_copy;
4175 input += bytes_to_copy;
4176 ctx->unprocessed_len += bytes_to_copy;
4177
4178 if( ctx->unprocessed_len == block_size )
4179 {
4180 status = mbedtls_to_psa_error(
4181 mbedtls_cipher_update( ctx,
4182 ctx->unprocessed_data,
4183 block_size,
4184 output, &internal_output_length ) );
4185
4186 if( status != PSA_SUCCESS )
4187 goto exit;
4188
4189 output += internal_output_length;
4190 output_size -= internal_output_length;
4191 *output_length += internal_output_length;
4192 ctx->unprocessed_len = 0;
4193 }
4194 }
4195
4196 while( input_length >= block_size )
4197 {
4198 /* Run all full blocks we have, one by one */
4199 status = mbedtls_to_psa_error(
4200 mbedtls_cipher_update( ctx, input,
4201 block_size,
4202 output, &internal_output_length ) );
4203
4204 if( status != PSA_SUCCESS )
4205 goto exit;
4206
4207 input_length -= block_size;
4208 input += block_size;
4209
4210 output += internal_output_length;
4211 output_size -= internal_output_length;
4212 *output_length += internal_output_length;
4213 }
4214
4215 if( input_length > 0 )
4216 {
4217 /* Save unprocessed bytes for later processing */
4218 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4219 input, input_length );
4220 ctx->unprocessed_len += input_length;
4221 }
4222
4223 status = PSA_SUCCESS;
4224
4225exit:
4226 return( status );
4227}
4228
Gilles Peskinee553c652018-06-04 16:22:46 +02004229psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4230 const uint8_t *input,
4231 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004232 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004233 size_t output_size,
4234 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004235{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004236 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004237 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004238 if( operation->alg == 0 )
4239 {
4240 return( PSA_ERROR_BAD_STATE );
4241 }
4242 if( operation->iv_required && ! operation->iv_set )
4243 {
4244 return( PSA_ERROR_BAD_STATE );
4245 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004246
Steven Cooremanef8575e2020-09-11 11:44:50 +02004247 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004248 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004249 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004250 input,
4251 input_length,
4252 output,
4253 output_size,
4254 output_length );
4255 goto exit;
4256 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004257
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004258 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004259 {
4260 /* Take the unprocessed partial block left over from previous
4261 * update calls, if any, plus the input to this call. Remove
4262 * the last partial block, if any. You get the data that will be
4263 * output in this call. */
4264 expected_output_size =
4265 ( operation->ctx.cipher.unprocessed_len + input_length )
4266 / operation->block_size * operation->block_size;
4267 }
4268 else
4269 {
4270 expected_output_size = input_length;
4271 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004272
Gilles Peskine89d789c2018-06-04 17:17:16 +02004273 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004274 {
4275 status = PSA_ERROR_BUFFER_TOO_SMALL;
4276 goto exit;
4277 }
mohammad160382759612018-03-12 18:16:40 +02004278
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004279 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4280 {
4281 /* mbedtls_cipher_update has an API inconsistency: it will only
4282 * process a single block at a time in ECB mode. Abstract away that
4283 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004284 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4285 input,
4286 input_length,
4287 output,
4288 output_size,
4289 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004290 }
4291 else
4292 {
4293 status = mbedtls_to_psa_error(
4294 mbedtls_cipher_update( &operation->ctx.cipher, input,
4295 input_length, output, output_length ) );
4296 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004297exit:
4298 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004299 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004300 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004301}
4302
Gilles Peskinee553c652018-06-04 16:22:46 +02004303psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4304 uint8_t *output,
4305 size_t output_size,
4306 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004307{
David Saadab4ecc272019-02-14 13:48:10 +02004308 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004309 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004310 if( operation->alg == 0 )
4311 {
4312 return( PSA_ERROR_BAD_STATE );
4313 }
4314 if( operation->iv_required && ! operation->iv_set )
4315 {
4316 return( PSA_ERROR_BAD_STATE );
4317 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004318
Steven Cooremanef8575e2020-09-11 11:44:50 +02004319 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004320 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004321 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004322 output,
4323 output_size,
4324 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004325 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004326 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004327
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004328 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004329 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004330 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004331 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004332 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004333 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004334 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004335 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004336 }
4337
Steven Cooremanef8575e2020-09-11 11:44:50 +02004338 status = mbedtls_to_psa_error(
4339 mbedtls_cipher_finish( &operation->ctx.cipher,
4340 temp_output_buffer,
4341 output_length ) );
4342 if( status != PSA_SUCCESS )
4343 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004344
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004345 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004346 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004347 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004348 memcpy( output, temp_output_buffer, *output_length );
4349 else
Janos Follath315b51c2018-07-09 16:04:51 +01004350 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004351
Steven Cooremanef8575e2020-09-11 11:44:50 +02004352exit:
4353 if( operation->mbedtls_in_use == 1 )
4354 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004355
Steven Cooremanef8575e2020-09-11 11:44:50 +02004356 if( status == PSA_SUCCESS )
4357 return( psa_cipher_abort( operation ) );
4358 else
4359 {
4360 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004361 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004362
Steven Cooremanef8575e2020-09-11 11:44:50 +02004363 return( status );
4364 }
mohammad1603503973b2018-03-12 15:59:30 +02004365}
4366
Gilles Peskinee553c652018-06-04 16:22:46 +02004367psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4368{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004369 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004370 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004371 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004372 * in use. It's ok to call abort on such an object, and there's
4373 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004374 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004375 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004376
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004377 /* Sanity check (shouldn't happen: operation->alg should
4378 * always have been initialized to a valid value). */
4379 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4380 return( PSA_ERROR_BAD_STATE );
4381
Steven Cooremanef8575e2020-09-11 11:44:50 +02004382 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004383 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004384 else
4385 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004386
Moran Peker41deec42018-04-04 15:43:05 +03004387 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004388 operation->key_set = 0;
4389 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004390 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004391 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004392 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004393 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004394
Moran Peker395db872018-05-31 14:07:14 +03004395 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004396}
4397
Gilles Peskinea0655c32018-04-30 17:06:50 +02004398
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004399
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004400
mohammad16035955c982018-04-26 00:53:03 +03004401/****************************************************************/
4402/* AEAD */
4403/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004404
Gilles Peskineedf9a652018-08-17 18:11:56 +02004405typedef struct
4406{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004407 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004408 const mbedtls_cipher_info_t *cipher_info;
4409 union
4410 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004411 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004412#if defined(MBEDTLS_CCM_C)
4413 mbedtls_ccm_context ccm;
4414#endif /* MBEDTLS_CCM_C */
4415#if defined(MBEDTLS_GCM_C)
4416 mbedtls_gcm_context gcm;
4417#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004418#if defined(MBEDTLS_CHACHAPOLY_C)
4419 mbedtls_chachapoly_context chachapoly;
4420#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004421 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004422 psa_algorithm_t core_alg;
4423 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004424 uint8_t tag_length;
4425} aead_operation_t;
4426
Ronald Cronf95a2b12020-10-22 15:24:49 +02004427#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4428
Gilles Peskine30a9e412019-01-14 18:36:12 +01004429static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004430{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004431 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004432 {
4433#if defined(MBEDTLS_CCM_C)
4434 case PSA_ALG_CCM:
4435 mbedtls_ccm_free( &operation->ctx.ccm );
4436 break;
4437#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004438#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004439 case PSA_ALG_GCM:
4440 mbedtls_gcm_free( &operation->ctx.gcm );
4441 break;
4442#endif /* MBEDTLS_GCM_C */
4443 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004444
Ronald Cron5c522922020-11-14 16:35:34 +01004445 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004446}
4447
4448static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004449 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004450 psa_key_usage_t usage,
4451 psa_algorithm_t alg )
4452{
4453 psa_status_t status;
4454 size_t key_bits;
4455 mbedtls_cipher_id_t cipher_id;
4456
Ronald Cron5c522922020-11-14 16:35:34 +01004457 status = psa_get_and_lock_transparent_key_slot_with_policy(
4458 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004459 if( status != PSA_SUCCESS )
4460 return( status );
4461
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004462 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004463
4464 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004465 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004466 &cipher_id );
4467 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004468 {
4469 status = PSA_ERROR_NOT_SUPPORTED;
4470 goto cleanup;
4471 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004472
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004473 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004474 {
4475#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004476 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004477 operation->core_alg = PSA_ALG_CCM;
4478 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004479 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4480 * The call to mbedtls_ccm_encrypt_and_tag or
4481 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004482 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004483 {
4484 status = PSA_ERROR_INVALID_ARGUMENT;
4485 goto cleanup;
4486 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004487 mbedtls_ccm_init( &operation->ctx.ccm );
4488 status = mbedtls_to_psa_error(
4489 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004490 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004491 (unsigned int) key_bits ) );
4492 if( status != 0 )
4493 goto cleanup;
4494 break;
4495#endif /* MBEDTLS_CCM_C */
4496
4497#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004498 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004499 operation->core_alg = PSA_ALG_GCM;
4500 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004501 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4502 * The call to mbedtls_gcm_crypt_and_tag or
4503 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004504 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004505 {
4506 status = PSA_ERROR_INVALID_ARGUMENT;
4507 goto cleanup;
4508 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004509 mbedtls_gcm_init( &operation->ctx.gcm );
4510 status = mbedtls_to_psa_error(
4511 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004512 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004513 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004514 if( status != 0 )
4515 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004516 break;
4517#endif /* MBEDTLS_GCM_C */
4518
Gilles Peskine26869f22019-05-06 15:25:00 +02004519#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004520 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004521 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4522 operation->full_tag_length = 16;
4523 /* We only support the default tag length. */
4524 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004525 {
4526 status = PSA_ERROR_NOT_SUPPORTED;
4527 goto cleanup;
4528 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004529 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4530 status = mbedtls_to_psa_error(
4531 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004532 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004533 if( status != 0 )
4534 goto cleanup;
4535 break;
4536#endif /* MBEDTLS_CHACHAPOLY_C */
4537
Gilles Peskineedf9a652018-08-17 18:11:56 +02004538 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004539 status = PSA_ERROR_NOT_SUPPORTED;
4540 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004541 }
4542
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004543 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4544 {
4545 status = PSA_ERROR_INVALID_ARGUMENT;
4546 goto cleanup;
4547 }
4548 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004549
Gilles Peskineedf9a652018-08-17 18:11:56 +02004550 return( PSA_SUCCESS );
4551
4552cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004553 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004554 return( status );
4555}
4556
Ronald Croncf56a0a2020-08-04 09:51:30 +02004557psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004558 psa_algorithm_t alg,
4559 const uint8_t *nonce,
4560 size_t nonce_length,
4561 const uint8_t *additional_data,
4562 size_t additional_data_length,
4563 const uint8_t *plaintext,
4564 size_t plaintext_length,
4565 uint8_t *ciphertext,
4566 size_t ciphertext_size,
4567 size_t *ciphertext_length )
4568{
mohammad16035955c982018-04-26 00:53:03 +03004569 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004570 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004571 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004572
mohammad1603f08a5502018-06-03 15:05:47 +03004573 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004574
Ronald Croncf56a0a2020-08-04 09:51:30 +02004575 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004576 if( status != PSA_SUCCESS )
4577 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004578
Gilles Peskineedf9a652018-08-17 18:11:56 +02004579 /* For all currently supported modes, the tag is at the end of the
4580 * ciphertext. */
4581 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4582 {
4583 status = PSA_ERROR_BUFFER_TOO_SMALL;
4584 goto exit;
4585 }
4586 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004587
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004588#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004589 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004590 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004591 status = mbedtls_to_psa_error(
4592 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4593 MBEDTLS_GCM_ENCRYPT,
4594 plaintext_length,
4595 nonce, nonce_length,
4596 additional_data, additional_data_length,
4597 plaintext, ciphertext,
4598 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004599 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004600 else
4601#endif /* MBEDTLS_GCM_C */
4602#if defined(MBEDTLS_CCM_C)
4603 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004604 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004605 status = mbedtls_to_psa_error(
4606 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4607 plaintext_length,
4608 nonce, nonce_length,
4609 additional_data,
4610 additional_data_length,
4611 plaintext, ciphertext,
4612 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004613 }
mohammad16035c8845f2018-05-09 05:40:09 -07004614 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004615#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004616#if defined(MBEDTLS_CHACHAPOLY_C)
4617 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4618 {
4619 if( nonce_length != 12 || operation.tag_length != 16 )
4620 {
4621 status = PSA_ERROR_NOT_SUPPORTED;
4622 goto exit;
4623 }
4624 status = mbedtls_to_psa_error(
4625 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4626 plaintext_length,
4627 nonce,
4628 additional_data,
4629 additional_data_length,
4630 plaintext,
4631 ciphertext,
4632 tag ) );
4633 }
4634 else
4635#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004636 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004637 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004638 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004639 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004640
Gilles Peskineedf9a652018-08-17 18:11:56 +02004641 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4642 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004643
Gilles Peskineedf9a652018-08-17 18:11:56 +02004644exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004645 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004646 if( status == PSA_SUCCESS )
4647 *ciphertext_length = plaintext_length + operation.tag_length;
4648 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004649}
4650
Gilles Peskineee652a32018-06-01 19:23:52 +02004651/* Locate the tag in a ciphertext buffer containing the encrypted data
4652 * followed by the tag. Return the length of the part preceding the tag in
4653 * *plaintext_length. This is the size of the plaintext in modes where
4654 * the encrypted data has the same size as the plaintext, such as
4655 * CCM and GCM. */
4656static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4657 const uint8_t *ciphertext,
4658 size_t ciphertext_length,
4659 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004660 const uint8_t **p_tag )
4661{
4662 size_t payload_length;
4663 if( tag_length > ciphertext_length )
4664 return( PSA_ERROR_INVALID_ARGUMENT );
4665 payload_length = ciphertext_length - tag_length;
4666 if( payload_length > plaintext_size )
4667 return( PSA_ERROR_BUFFER_TOO_SMALL );
4668 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004669 return( PSA_SUCCESS );
4670}
4671
Ronald Croncf56a0a2020-08-04 09:51:30 +02004672psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004673 psa_algorithm_t alg,
4674 const uint8_t *nonce,
4675 size_t nonce_length,
4676 const uint8_t *additional_data,
4677 size_t additional_data_length,
4678 const uint8_t *ciphertext,
4679 size_t ciphertext_length,
4680 uint8_t *plaintext,
4681 size_t plaintext_size,
4682 size_t *plaintext_length )
4683{
mohammad16035955c982018-04-26 00:53:03 +03004684 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004685 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004686 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004687
Gilles Peskineee652a32018-06-01 19:23:52 +02004688 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004689
Ronald Croncf56a0a2020-08-04 09:51:30 +02004690 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004691 if( status != PSA_SUCCESS )
4692 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004693
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004694 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4695 ciphertext, ciphertext_length,
4696 plaintext_size, &tag );
4697 if( status != PSA_SUCCESS )
4698 goto exit;
4699
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004700#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004701 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004702 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004703 status = mbedtls_to_psa_error(
4704 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4705 ciphertext_length - operation.tag_length,
4706 nonce, nonce_length,
4707 additional_data,
4708 additional_data_length,
4709 tag, operation.tag_length,
4710 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004711 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004712 else
4713#endif /* MBEDTLS_GCM_C */
4714#if defined(MBEDTLS_CCM_C)
4715 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004716 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004717 status = mbedtls_to_psa_error(
4718 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4719 ciphertext_length - operation.tag_length,
4720 nonce, nonce_length,
4721 additional_data,
4722 additional_data_length,
4723 ciphertext, plaintext,
4724 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004725 }
mohammad160339574652018-06-01 04:39:53 -07004726 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004727#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004728#if defined(MBEDTLS_CHACHAPOLY_C)
4729 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4730 {
4731 if( nonce_length != 12 || operation.tag_length != 16 )
4732 {
4733 status = PSA_ERROR_NOT_SUPPORTED;
4734 goto exit;
4735 }
4736 status = mbedtls_to_psa_error(
4737 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4738 ciphertext_length - operation.tag_length,
4739 nonce,
4740 additional_data,
4741 additional_data_length,
4742 tag,
4743 ciphertext,
4744 plaintext ) );
4745 }
4746 else
4747#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004748 {
mohammad1603554faad2018-06-03 15:07:38 +03004749 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004750 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004751
Gilles Peskineedf9a652018-08-17 18:11:56 +02004752 if( status != PSA_SUCCESS && plaintext_size != 0 )
4753 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004754
Gilles Peskineedf9a652018-08-17 18:11:56 +02004755exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004756 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004757 if( status == PSA_SUCCESS )
4758 *plaintext_length = ciphertext_length - operation.tag_length;
4759 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004760}
4761
Gilles Peskinea0655c32018-04-30 17:06:50 +02004762
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004763
Gilles Peskine20035e32018-02-03 22:44:14 +01004764/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004765/* Generators */
4766/****************************************************************/
4767
John Durkop07cc04a2020-11-16 22:08:34 -08004768#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4769 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4770 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4771#define AT_LEAST_ONE_BUILTIN_KDF
4772#endif
4773
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004774#define HKDF_STATE_INIT 0 /* no input yet */
4775#define HKDF_STATE_STARTED 1 /* got salt */
4776#define HKDF_STATE_KEYED 2 /* got key */
4777#define HKDF_STATE_OUTPUT 3 /* output started */
4778
Gilles Peskinecbe66502019-05-16 16:59:18 +02004779static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004780 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004781{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4783 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004784 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004785 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004786}
4787
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004788psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004789{
4790 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004791 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004792 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004793 {
4794 /* The object has (apparently) been initialized but it is not
4795 * in use. It's ok to call abort on such an object, and there's
4796 * nothing to do. */
4797 }
4798 else
John Durkopd0321952020-10-29 21:37:36 -07004799#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004800 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004801 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 mbedtls_free( operation->ctx.hkdf.info );
4803 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004804 }
John Durkop07cc04a2020-11-16 22:08:34 -08004805 else
4806#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4807#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4808 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4809 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004811 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004812 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004813 if( operation->ctx.tls12_prf.seed != NULL )
4814 {
4815 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4816 operation->ctx.tls12_prf.seed_length );
4817 mbedtls_free( operation->ctx.tls12_prf.seed );
4818 }
4819
4820 if( operation->ctx.tls12_prf.label != NULL )
4821 {
4822 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4823 operation->ctx.tls12_prf.label_length );
4824 mbedtls_free( operation->ctx.tls12_prf.label );
4825 }
4826
4827 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4828
4829 /* We leave the fields Ai and output_block to be erased safely by the
4830 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004831 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004832 else
John Durkop07cc04a2020-11-16 22:08:34 -08004833#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4834 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004835 {
4836 status = PSA_ERROR_BAD_STATE;
4837 }
Janos Follath083036a2019-06-11 10:22:26 +01004838 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004839 return( status );
4840}
4841
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004842psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004843 size_t *capacity)
4844{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004846 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004847 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004848 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004849 }
4850
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004852 return( PSA_SUCCESS );
4853}
4854
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004855psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004856 size_t capacity )
4857{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004859 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004860 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004861 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004863 return( PSA_SUCCESS );
4864}
4865
John Durkopd0321952020-10-29 21:37:36 -07004866#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004867/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004868 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004869static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004870 psa_algorithm_t hash_alg,
4871 uint8_t *output,
4872 size_t output_length )
4873{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004874 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004875 psa_status_t status;
4876
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004877 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4878 return( PSA_ERROR_BAD_STATE );
4879 hkdf->state = HKDF_STATE_OUTPUT;
4880
Gilles Peskinebef7f142018-07-12 17:22:21 +02004881 while( output_length != 0 )
4882 {
4883 /* Copy what remains of the current block */
4884 uint8_t n = hash_length - hkdf->offset_in_block;
4885 if( n > output_length )
4886 n = (uint8_t) output_length;
4887 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4888 output += n;
4889 output_length -= n;
4890 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004891 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004892 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004893 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004894 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004896 * object was corrupted or if this function is called directly
4897 * inside the library. */
4898 if( hkdf->block_number == 0xff )
4899 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004900
4901 /* We need a new block */
4902 ++hkdf->block_number;
4903 hkdf->offset_in_block = 0;
4904 status = psa_hmac_setup_internal( &hkdf->hmac,
4905 hkdf->prk, hash_length,
4906 hash_alg );
4907 if( status != PSA_SUCCESS )
4908 return( status );
4909 if( hkdf->block_number != 1 )
4910 {
4911 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4912 hkdf->output_block,
4913 hash_length );
4914 if( status != PSA_SUCCESS )
4915 return( status );
4916 }
4917 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4918 hkdf->info,
4919 hkdf->info_length );
4920 if( status != PSA_SUCCESS )
4921 return( status );
4922 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4923 &hkdf->block_number, 1 );
4924 if( status != PSA_SUCCESS )
4925 return( status );
4926 status = psa_hmac_finish_internal( &hkdf->hmac,
4927 hkdf->output_block,
4928 sizeof( hkdf->output_block ) );
4929 if( status != PSA_SUCCESS )
4930 return( status );
4931 }
4932
4933 return( PSA_SUCCESS );
4934}
John Durkop07cc04a2020-11-16 22:08:34 -08004935#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004936
John Durkop07cc04a2020-11-16 22:08:34 -08004937#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4938 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004939static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4940 psa_tls12_prf_key_derivation_t *tls12_prf,
4941 psa_algorithm_t alg )
4942{
4943 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004944 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004945 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4946 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004947
Janos Follath7742fee2019-06-17 12:58:10 +01004948 /* We can't be wanting more output after block 0xff, otherwise
4949 * the capacity check in psa_key_derivation_output_bytes() would have
4950 * prevented this call. It could happen only if the operation
4951 * object was corrupted or if this function is called directly
4952 * inside the library. */
4953 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004954 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004955
4956 /* We need a new block */
4957 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004958 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004959
4960 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4961 *
4962 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4963 *
4964 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4965 * HMAC_hash(secret, A(2) + seed) +
4966 * HMAC_hash(secret, A(3) + seed) + ...
4967 *
4968 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004969 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004970 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004971 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004972 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004973 * is currently extracted as `output_block` and where i is
4974 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004975 */
4976
Janos Follathea29bfb2019-06-19 12:21:20 +01004977 /* Save the hash context before using it, to preserve the hash state with
4978 * only the inner padding in it. We need this, because inner padding depends
4979 * on the key (secret in the RFC's terminology). */
4980 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4981 if( status != PSA_SUCCESS )
4982 goto cleanup;
4983
4984 /* Calculate A(i) where i = tls12_prf->block_number. */
4985 if( tls12_prf->block_number == 1 )
4986 {
4987 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4988 * the variable seed and in this instance means it in the context of the
4989 * P_hash function, where seed = label + seed.) */
4990 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4991 tls12_prf->label, tls12_prf->label_length );
4992 if( status != PSA_SUCCESS )
4993 goto cleanup;
4994 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4995 tls12_prf->seed, tls12_prf->seed_length );
4996 if( status != PSA_SUCCESS )
4997 goto cleanup;
4998 }
4999 else
5000 {
5001 /* A(i) = HMAC_hash(secret, A(i-1)) */
5002 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5003 tls12_prf->Ai, hash_length );
5004 if( status != PSA_SUCCESS )
5005 goto cleanup;
5006 }
5007
5008 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5009 tls12_prf->Ai, hash_length );
5010 if( status != PSA_SUCCESS )
5011 goto cleanup;
5012 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5013 if( status != PSA_SUCCESS )
5014 goto cleanup;
5015
5016 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5017 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5018 tls12_prf->Ai, hash_length );
5019 if( status != PSA_SUCCESS )
5020 goto cleanup;
5021 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5022 tls12_prf->label, tls12_prf->label_length );
5023 if( status != PSA_SUCCESS )
5024 goto cleanup;
5025 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5026 tls12_prf->seed, tls12_prf->seed_length );
5027 if( status != PSA_SUCCESS )
5028 goto cleanup;
5029 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5030 tls12_prf->output_block, hash_length );
5031 if( status != PSA_SUCCESS )
5032 goto cleanup;
5033 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5034 if( status != PSA_SUCCESS )
5035 goto cleanup;
5036
Janos Follath7742fee2019-06-17 12:58:10 +01005037
5038cleanup:
5039
Janos Follathea29bfb2019-06-19 12:21:20 +01005040 cleanup_status = psa_hash_abort( &backup );
5041 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5042 status = cleanup_status;
5043
5044 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005045}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005046
Janos Follath844eb0e2019-06-19 12:10:49 +01005047static psa_status_t psa_key_derivation_tls12_prf_read(
5048 psa_tls12_prf_key_derivation_t *tls12_prf,
5049 psa_algorithm_t alg,
5050 uint8_t *output,
5051 size_t output_length )
5052{
5053 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005054 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005055 psa_status_t status;
5056 uint8_t offset, length;
5057
5058 while( output_length != 0 )
5059 {
5060 /* Check if we have fully processed the current block. */
5061 if( tls12_prf->left_in_block == 0 )
5062 {
5063 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5064 alg );
5065 if( status != PSA_SUCCESS )
5066 return( status );
5067
5068 continue;
5069 }
5070
5071 if( tls12_prf->left_in_block > output_length )
5072 length = (uint8_t) output_length;
5073 else
5074 length = tls12_prf->left_in_block;
5075
5076 offset = hash_length - tls12_prf->left_in_block;
5077 memcpy( output, tls12_prf->output_block + offset, length );
5078 output += length;
5079 output_length -= length;
5080 tls12_prf->left_in_block -= length;
5081 }
5082
5083 return( PSA_SUCCESS );
5084}
John Durkop07cc04a2020-11-16 22:08:34 -08005085#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5086 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005087
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005088psa_status_t psa_key_derivation_output_bytes(
5089 psa_key_derivation_operation_t *operation,
5090 uint8_t *output,
5091 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005092{
5093 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005095
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005096 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005097 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005098 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005099 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005100 }
5101
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005102 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005103 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005104 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005105 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005106 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005107 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005108 goto exit;
5109 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005111 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005113 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005114 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5115 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005117 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005118 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005119 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005120 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005121
John Durkopd0321952020-10-29 21:37:36 -07005122#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005123 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005124 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005125 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005126 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005127 output, output_length );
5128 }
Janos Follathadbec812019-06-14 11:05:39 +01005129 else
John Durkop07cc04a2020-11-16 22:08:34 -08005130#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5131#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5132 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005133 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005134 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005135 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005136 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005137 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005138 output_length );
5139 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005140 else
John Durkop07cc04a2020-11-16 22:08:34 -08005141#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5142 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005143 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005144 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005145 return( PSA_ERROR_BAD_STATE );
5146 }
5147
5148exit:
5149 if( status != PSA_SUCCESS )
5150 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005151 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005152 * This allows us to differentiate between exhausted operations and
5153 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5154 * operations. */
5155 psa_algorithm_t alg = operation->alg;
5156 psa_key_derivation_abort( operation );
5157 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005158 memset( output, '!', output_length );
5159 }
5160 return( status );
5161}
5162
David Brown7807bf72021-02-09 16:28:23 -07005163#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02005164static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5165{
5166 if( data_size >= 8 )
5167 mbedtls_des_key_set_parity( data );
5168 if( data_size >= 16 )
5169 mbedtls_des_key_set_parity( data + 8 );
5170 if( data_size >= 24 )
5171 mbedtls_des_key_set_parity( data + 16 );
5172}
David Brown7807bf72021-02-09 16:28:23 -07005173#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005174
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005175static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005176 psa_key_slot_t *slot,
5177 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005178 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005179{
5180 uint8_t *data = NULL;
5181 size_t bytes = PSA_BITS_TO_BYTES( bits );
5182 psa_status_t status;
5183
Gilles Peskine8e338702019-07-30 20:06:31 +02005184 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005185 return( PSA_ERROR_INVALID_ARGUMENT );
5186 if( bits % 8 != 0 )
5187 return( PSA_ERROR_INVALID_ARGUMENT );
5188 data = mbedtls_calloc( 1, bytes );
5189 if( data == NULL )
5190 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5191
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005192 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005193 if( status != PSA_SUCCESS )
5194 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005195#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02005196 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005197 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07005198#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01005199
5200 status = psa_allocate_buffer_to_slot( slot, bytes );
5201 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00005202 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01005203
Ronald Cronbf33c932020-11-28 18:06:53 +01005204 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005205 psa_key_attributes_t attributes = {
5206 .core = slot->attr
5207 };
5208
Ronald Cronbf33c932020-11-28 18:06:53 +01005209 status = psa_driver_wrapper_import_key( &attributes,
5210 data, bytes,
5211 slot->key.data,
5212 slot->key.bytes,
5213 &slot->key.bytes, &bits );
5214 if( bits != slot->attr.bits )
5215 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005216
5217exit:
5218 mbedtls_free( data );
5219 return( status );
5220}
5221
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005222psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005223 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005224 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005225{
5226 psa_status_t status;
5227 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005228 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005229
Ronald Cron81709fc2020-11-14 12:10:32 +01005230 *key = MBEDTLS_SVC_KEY_ID_INIT;
5231
Gilles Peskine0f84d622019-09-12 19:03:13 +02005232 /* Reject any attempt to create a zero-length key so that we don't
5233 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5234 if( psa_get_key_bits( attributes ) == 0 )
5235 return( PSA_ERROR_INVALID_ARGUMENT );
5236
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005237 if( ! operation->can_output_key )
5238 return( PSA_ERROR_NOT_PERMITTED );
5239
Ronald Cron81709fc2020-11-14 12:10:32 +01005240 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5241 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005242#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5243 if( driver != NULL )
5244 {
5245 /* Deriving a key in a secure element is not implemented yet. */
5246 status = PSA_ERROR_NOT_SUPPORTED;
5247 }
5248#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005249 if( status == PSA_SUCCESS )
5250 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005251 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005252 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005253 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005254 }
5255 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005256 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005257 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005258 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005259
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005260 return( status );
5261}
5262
Gilles Peskineeab56e42018-07-12 17:12:33 +02005263
5264
5265/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005266/* Key derivation */
5267/****************************************************************/
5268
Steven Cooreman932ffb72021-02-15 12:14:32 +01005269#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005270static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005272 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005273{
John Durkop07cc04a2020-11-16 22:08:34 -08005274 int is_kdf_alg_supported;
5275
Janos Follath5fe19732019-06-20 15:09:30 +01005276 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5277 * initialisers for this union leave some bytes unspecified.) */
5278 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5279
Gilles Peskine969c5d62019-01-16 15:53:06 +01005280 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005281#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005282 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5283 is_kdf_alg_supported = 1;
5284 else
5285#endif
5286#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5287 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5288 is_kdf_alg_supported = 1;
5289 else
5290#endif
5291#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5292 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5293 is_kdf_alg_supported = 1;
5294 else
5295#endif
5296 is_kdf_alg_supported = 0;
5297
5298 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005299 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005300 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005301 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005302 if( hash_size == 0 )
5303 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005304 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5305 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005306 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005307 {
5308 return( PSA_ERROR_NOT_SUPPORTED );
5309 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005310 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005311 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005312 }
John Durkop07cc04a2020-11-16 22:08:34 -08005313
5314 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005315}
John Durkop07cc04a2020-11-16 22:08:34 -08005316#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005317
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005318psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005319 psa_algorithm_t alg )
5320{
5321 psa_status_t status;
5322
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005323 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005324 return( PSA_ERROR_BAD_STATE );
5325
Gilles Peskine6843c292019-01-18 16:44:49 +01005326 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5327 return( PSA_ERROR_INVALID_ARGUMENT );
5328 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005329 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005330#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005331 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005333#else
5334 return( PSA_ERROR_NOT_SUPPORTED );
5335#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005336 }
5337 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5338 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005339#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005340 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005341#else
5342 return( PSA_ERROR_NOT_SUPPORTED );
5343#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005344 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005345 else
5346 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005347
5348 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005349 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005350 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005351}
5352
John Durkopd0321952020-10-29 21:37:36 -07005353#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005354static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005355 psa_algorithm_t hash_alg,
5356 psa_key_derivation_step_t step,
5357 const uint8_t *data,
5358 size_t data_length )
5359{
5360 psa_status_t status;
5361 switch( step )
5362 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005363 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005364 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005365 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005366 status = psa_hmac_setup_internal( &hkdf->hmac,
5367 data, data_length,
5368 hash_alg );
5369 if( status != PSA_SUCCESS )
5370 return( status );
5371 hkdf->state = HKDF_STATE_STARTED;
5372 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005373 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005374 /* If no salt was provided, use an empty salt. */
5375 if( hkdf->state == HKDF_STATE_INIT )
5376 {
5377 status = psa_hmac_setup_internal( &hkdf->hmac,
5378 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005379 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005380 if( status != PSA_SUCCESS )
5381 return( status );
5382 hkdf->state = HKDF_STATE_STARTED;
5383 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005384 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005385 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005386 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5387 data, data_length );
5388 if( status != PSA_SUCCESS )
5389 return( status );
5390 status = psa_hmac_finish_internal( &hkdf->hmac,
5391 hkdf->prk,
5392 sizeof( hkdf->prk ) );
5393 if( status != PSA_SUCCESS )
5394 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005395 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005396 hkdf->block_number = 0;
5397 hkdf->state = HKDF_STATE_KEYED;
5398 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005399 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005400 if( hkdf->state == HKDF_STATE_OUTPUT )
5401 return( PSA_ERROR_BAD_STATE );
5402 if( hkdf->info_set )
5403 return( PSA_ERROR_BAD_STATE );
5404 hkdf->info_length = data_length;
5405 if( data_length != 0 )
5406 {
5407 hkdf->info = mbedtls_calloc( 1, data_length );
5408 if( hkdf->info == NULL )
5409 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5410 memcpy( hkdf->info, data, data_length );
5411 }
5412 hkdf->info_set = 1;
5413 return( PSA_SUCCESS );
5414 default:
5415 return( PSA_ERROR_INVALID_ARGUMENT );
5416 }
5417}
John Durkop07cc04a2020-11-16 22:08:34 -08005418#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005419
John Durkop07cc04a2020-11-16 22:08:34 -08005420#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5421 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005422static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5423 const uint8_t *data,
5424 size_t data_length )
5425{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005426 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005427 return( PSA_ERROR_BAD_STATE );
5428
Janos Follathd6dce9f2019-07-04 09:11:38 +01005429 if( data_length != 0 )
5430 {
5431 prf->seed = mbedtls_calloc( 1, data_length );
5432 if( prf->seed == NULL )
5433 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005434
Janos Follathd6dce9f2019-07-04 09:11:38 +01005435 memcpy( prf->seed, data, data_length );
5436 prf->seed_length = data_length;
5437 }
Janos Follathf08e2652019-06-13 09:05:41 +01005438
Gilles Peskine43f958b2020-12-13 14:55:14 +01005439 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005440
5441 return( PSA_SUCCESS );
5442}
5443
Janos Follath81550542019-06-13 14:26:34 +01005444static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5445 psa_algorithm_t hash_alg,
5446 const uint8_t *data,
5447 size_t data_length )
5448{
5449 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005450 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005451 return( PSA_ERROR_BAD_STATE );
5452
5453 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5454 if( status != PSA_SUCCESS )
5455 return( status );
5456
Gilles Peskine43f958b2020-12-13 14:55:14 +01005457 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005458
5459 return( PSA_SUCCESS );
5460}
5461
Janos Follath63028dd2019-06-13 09:15:47 +01005462static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5463 const uint8_t *data,
5464 size_t data_length )
5465{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005466 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005467 return( PSA_ERROR_BAD_STATE );
5468
Janos Follathd6dce9f2019-07-04 09:11:38 +01005469 if( data_length != 0 )
5470 {
5471 prf->label = mbedtls_calloc( 1, data_length );
5472 if( prf->label == NULL )
5473 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005474
Janos Follathd6dce9f2019-07-04 09:11:38 +01005475 memcpy( prf->label, data, data_length );
5476 prf->label_length = data_length;
5477 }
Janos Follath63028dd2019-06-13 09:15:47 +01005478
Gilles Peskine43f958b2020-12-13 14:55:14 +01005479 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005480
5481 return( PSA_SUCCESS );
5482}
5483
Janos Follathb03233e2019-06-11 15:30:30 +01005484static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5485 psa_algorithm_t hash_alg,
5486 psa_key_derivation_step_t step,
5487 const uint8_t *data,
5488 size_t data_length )
5489{
Janos Follathb03233e2019-06-11 15:30:30 +01005490 switch( step )
5491 {
Janos Follathf08e2652019-06-13 09:05:41 +01005492 case PSA_KEY_DERIVATION_INPUT_SEED:
5493 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005494 case PSA_KEY_DERIVATION_INPUT_SECRET:
5495 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005496 case PSA_KEY_DERIVATION_INPUT_LABEL:
5497 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005498 default:
5499 return( PSA_ERROR_INVALID_ARGUMENT );
5500 }
5501}
John Durkop07cc04a2020-11-16 22:08:34 -08005502#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5503 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5504
5505#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5506static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5507 psa_tls12_prf_key_derivation_t *prf,
5508 psa_algorithm_t hash_alg,
5509 const uint8_t *data,
5510 size_t data_length )
5511{
5512 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005513 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005514 uint8_t *cur = pms;
5515
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005516 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005517 return( PSA_ERROR_INVALID_ARGUMENT );
5518
5519 /* Quoting RFC 4279, Section 2:
5520 *
5521 * The premaster secret is formed as follows: if the PSK is N octets
5522 * long, concatenate a uint16 with the value N, N zero octets, a second
5523 * uint16 with the value N, and the PSK itself.
5524 */
5525
5526 *cur++ = ( data_length >> 8 ) & 0xff;
5527 *cur++ = ( data_length >> 0 ) & 0xff;
5528 memset( cur, 0, data_length );
5529 cur += data_length;
5530 *cur++ = pms[0];
5531 *cur++ = pms[1];
5532 memcpy( cur, data, data_length );
5533 cur += data_length;
5534
5535 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5536
5537 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5538 return( status );
5539}
Janos Follath6660f0e2019-06-17 08:44:03 +01005540
5541static psa_status_t psa_tls12_prf_psk_to_ms_input(
5542 psa_tls12_prf_key_derivation_t *prf,
5543 psa_algorithm_t hash_alg,
5544 psa_key_derivation_step_t step,
5545 const uint8_t *data,
5546 size_t data_length )
5547{
5548 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005549 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005550 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5551 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005552 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005553
5554 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5555}
John Durkop07cc04a2020-11-16 22:08:34 -08005556#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005557
Gilles Peskineb8965192019-09-24 16:21:10 +02005558/** Check whether the given key type is acceptable for the given
5559 * input step of a key derivation.
5560 *
5561 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5562 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5563 * Both secret and non-secret inputs can alternatively have the type
5564 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5565 * that the input was passed as a buffer rather than via a key object.
5566 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005567static int psa_key_derivation_check_input_type(
5568 psa_key_derivation_step_t step,
5569 psa_key_type_t key_type )
5570{
5571 switch( step )
5572 {
5573 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005574 if( key_type == PSA_KEY_TYPE_DERIVE )
5575 return( PSA_SUCCESS );
5576 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005577 return( PSA_SUCCESS );
5578 break;
5579 case PSA_KEY_DERIVATION_INPUT_LABEL:
5580 case PSA_KEY_DERIVATION_INPUT_SALT:
5581 case PSA_KEY_DERIVATION_INPUT_INFO:
5582 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005583 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5584 return( PSA_SUCCESS );
5585 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005586 return( PSA_SUCCESS );
5587 break;
5588 }
5589 return( PSA_ERROR_INVALID_ARGUMENT );
5590}
5591
Janos Follathb80a94e2019-06-12 15:54:46 +01005592static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005593 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005594 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005595 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005596 const uint8_t *data,
5597 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005598{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005599 psa_status_t status;
5600 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5601
5602 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005603 if( status != PSA_SUCCESS )
5604 goto exit;
5605
John Durkopd0321952020-10-29 21:37:36 -07005606#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005607 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005608 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005609 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005610 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005611 step, data, data_length );
5612 }
John Durkop07cc04a2020-11-16 22:08:34 -08005613 else
5614#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5615#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5616 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005617 {
Janos Follathb03233e2019-06-11 15:30:30 +01005618 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5619 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5620 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005621 }
John Durkop07cc04a2020-11-16 22:08:34 -08005622 else
5623#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5624#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5625 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005626 {
5627 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5628 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5629 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005630 }
5631 else
John Durkop07cc04a2020-11-16 22:08:34 -08005632#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005633 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005634 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005635 (void) data;
5636 (void) data_length;
5637 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005638 return( PSA_ERROR_BAD_STATE );
5639 }
5640
Gilles Peskine224b0d62019-09-23 18:13:17 +02005641exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005642 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005643 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005644 return( status );
5645}
5646
Janos Follath51f4a0f2019-06-14 11:35:55 +01005647psa_status_t psa_key_derivation_input_bytes(
5648 psa_key_derivation_operation_t *operation,
5649 psa_key_derivation_step_t step,
5650 const uint8_t *data,
5651 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005652{
Gilles Peskineb8965192019-09-24 16:21:10 +02005653 return( psa_key_derivation_input_internal( operation, step,
5654 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005655 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005656}
5657
Janos Follath51f4a0f2019-06-14 11:35:55 +01005658psa_status_t psa_key_derivation_input_key(
5659 psa_key_derivation_operation_t *operation,
5660 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005661 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005662{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005663 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005664 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005665 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005666
Ronald Cron5c522922020-11-14 16:35:34 +01005667 status = psa_get_and_lock_transparent_key_slot_with_policy(
5668 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005669 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005670 {
5671 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005672 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005673 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005674
5675 /* Passing a key object as a SECRET input unlocks the permission
5676 * to output to a key object. */
5677 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5678 operation->can_output_key = 1;
5679
Ronald Cronf95a2b12020-10-22 15:24:49 +02005680 status = psa_key_derivation_input_internal( operation,
5681 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005682 slot->key.data,
5683 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005684
Ronald Cron5c522922020-11-14 16:35:34 +01005685 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005686
Ronald Cron5c522922020-11-14 16:35:34 +01005687 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005688}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005689
5690
5691
5692/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005693/* Key agreement */
5694/****************************************************************/
5695
John Durkop6ba40d12020-11-10 08:50:04 -08005696#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005697static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5698 size_t peer_key_length,
5699 const mbedtls_ecp_keypair *our_key,
5700 uint8_t *shared_secret,
5701 size_t shared_secret_size,
5702 size_t *shared_secret_length )
5703{
Steven Cooremand4867872020-08-05 16:31:39 +02005704 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005705 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005706 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005707 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005708 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005709 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005710
Ronald Cron90857082020-11-25 14:58:33 +01005711 status = mbedtls_psa_ecp_load_representation(
5712 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005713 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005714 peer_key,
5715 peer_key_length,
5716 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005717 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005718 goto exit;
5719
Jaeden Amero97271b32019-01-10 19:38:51 +00005720 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005721 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005722 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005723 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005724 status = mbedtls_to_psa_error(
5725 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5726 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005727 goto exit;
5728
Jaeden Amero97271b32019-01-10 19:38:51 +00005729 status = mbedtls_to_psa_error(
5730 mbedtls_ecdh_calc_secret( &ecdh,
5731 shared_secret_length,
5732 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005733 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005734 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005735 if( status != PSA_SUCCESS )
5736 goto exit;
5737 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5738 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005739
5740exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005741 if( status != PSA_SUCCESS )
5742 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005743 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005744 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005745 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005746
Jaeden Amero97271b32019-01-10 19:38:51 +00005747 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005748}
John Durkop6ba40d12020-11-10 08:50:04 -08005749#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005750
Gilles Peskine01d718c2018-09-18 12:01:02 +02005751#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5752
Gilles Peskine0216fe12019-04-11 21:23:21 +02005753static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5754 psa_key_slot_t *private_key,
5755 const uint8_t *peer_key,
5756 size_t peer_key_length,
5757 uint8_t *shared_secret,
5758 size_t shared_secret_size,
5759 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005760{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005761 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005762 {
John Durkop6ba40d12020-11-10 08:50:04 -08005763#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005764 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005765 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005766 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005767 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005768 psa_status_t status = mbedtls_psa_ecp_load_representation(
5769 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005770 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005771 private_key->key.data,
5772 private_key->key.bytes,
5773 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005774 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005775 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005776 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005777 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005778 shared_secret, shared_secret_size,
5779 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005780 mbedtls_ecp_keypair_free( ecp );
5781 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005782 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005783#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005784 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005785 (void) private_key;
5786 (void) peer_key;
5787 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005788 (void) shared_secret;
5789 (void) shared_secret_size;
5790 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005791 return( PSA_ERROR_NOT_SUPPORTED );
5792 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005793}
5794
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005795/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005796 * to potentially free embedded data structures and wipe confidential data.
5797 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005798static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005799 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005800 psa_key_slot_t *private_key,
5801 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005802 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005803{
5804 psa_status_t status;
5805 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5806 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005807 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005808
5809 /* Step 1: run the secret agreement algorithm to generate the shared
5810 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005811 status = psa_key_agreement_raw_internal( ka_alg,
5812 private_key,
5813 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005814 shared_secret,
5815 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005816 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005817 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005818 goto exit;
5819
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005820 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005821 * the shared secret. A shared secret is permitted wherever a key
5822 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005823 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005824 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005825 shared_secret,
5826 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005827exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005828 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005829 return( status );
5830}
5831
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005832psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005833 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005834 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005835 const uint8_t *peer_key,
5836 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005837{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005838 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005839 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005840 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005841
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005842 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005843 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005844 status = psa_get_and_lock_transparent_key_slot_with_policy(
5845 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005846 if( status != PSA_SUCCESS )
5847 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005848 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005849 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005850 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005851 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005852 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005853 else
5854 {
5855 /* If a private key has been added as SECRET, we allow the derived
5856 * key material to be used as a key in PSA Crypto. */
5857 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5858 operation->can_output_key = 1;
5859 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005860
Ronald Cron5c522922020-11-14 16:35:34 +01005861 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005862
Ronald Cron5c522922020-11-14 16:35:34 +01005863 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005864}
5865
Gilles Peskinebe697d82019-05-16 18:00:41 +02005866psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005867 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005868 const uint8_t *peer_key,
5869 size_t peer_key_length,
5870 uint8_t *output,
5871 size_t output_size,
5872 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005873{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005874 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005875 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005876 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005877
5878 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5879 {
5880 status = PSA_ERROR_INVALID_ARGUMENT;
5881 goto exit;
5882 }
Ronald Cron5c522922020-11-14 16:35:34 +01005883 status = psa_get_and_lock_transparent_key_slot_with_policy(
5884 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005885 if( status != PSA_SUCCESS )
5886 goto exit;
5887
5888 status = psa_key_agreement_raw_internal( alg, slot,
5889 peer_key, peer_key_length,
5890 output, output_size,
5891 output_length );
5892
5893exit:
5894 if( status != PSA_SUCCESS )
5895 {
5896 /* If an error happens and is not handled properly, the output
5897 * may be used as a key to protect sensitive data. Arrange for such
5898 * a key to be random, which is likely to result in decryption or
5899 * verification errors. This is better than filling the buffer with
5900 * some constant data such as zeros, which would result in the data
5901 * being protected with a reproducible, easily knowable key.
5902 */
5903 psa_generate_random( output, output_size );
5904 *output_length = output_size;
5905 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005906
Ronald Cron5c522922020-11-14 16:35:34 +01005907 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005908
Ronald Cron5c522922020-11-14 16:35:34 +01005909 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005910}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005911
5912
Gilles Peskine30524eb2020-11-13 17:02:26 +01005913
Gilles Peskine86a440b2018-11-12 18:39:40 +01005914/****************************************************************/
5915/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005916/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005917
Gilles Peskine30524eb2020-11-13 17:02:26 +01005918/** Initialize the PSA random generator.
5919 */
5920static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5921{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005922#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5923 memset( rng, 0, sizeof( *rng ) );
5924#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5925
Gilles Peskine30524eb2020-11-13 17:02:26 +01005926 /* Set default configuration if
5927 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5928 if( rng->entropy_init == NULL )
5929 rng->entropy_init = mbedtls_entropy_init;
5930 if( rng->entropy_free == NULL )
5931 rng->entropy_free = mbedtls_entropy_free;
5932
5933 rng->entropy_init( &rng->entropy );
5934#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5935 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5936 /* The PSA entropy injection feature depends on using NV seed as an entropy
5937 * source. Add NV seed as an entropy source for PSA entropy injection. */
5938 mbedtls_entropy_add_source( &rng->entropy,
5939 mbedtls_nv_seed_poll, NULL,
5940 MBEDTLS_ENTROPY_BLOCK_SIZE,
5941 MBEDTLS_ENTROPY_SOURCE_STRONG );
5942#endif
5943
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005944 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005945#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005946}
5947
5948/** Deinitialize the PSA random generator.
5949 */
5950static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5951{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005952#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5953 memset( rng, 0, sizeof( *rng ) );
5954#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005955 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005956 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005957#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005958}
5959
5960/** Seed the PSA random generator.
5961 */
5962static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5963{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005964#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5965 /* Do nothing: the external RNG seeds itself. */
5966 (void) rng;
5967 return( PSA_SUCCESS );
5968#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005969 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005970 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5971 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005972 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005973#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005974}
5975
Gilles Peskinee59236f2018-01-27 23:32:46 +01005976psa_status_t psa_generate_random( uint8_t *output,
5977 size_t output_size )
5978{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005979 GUARD_MODULE_INITIALIZED;
5980
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005981#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5982
5983 size_t output_length = 0;
5984 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5985 output, output_size,
5986 &output_length );
5987 if( status != PSA_SUCCESS )
5988 return( status );
5989 /* Breaking up a request into smaller chunks is currently not supported
5990 * for the extrernal RNG interface. */
5991 if( output_length != output_size )
5992 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5993 return( PSA_SUCCESS );
5994
5995#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5996
Gilles Peskine71ddab92021-01-04 21:01:07 +01005997 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005998 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005999 size_t request_size =
6000 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6001 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6002 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006003 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6004 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006005 if( ret != 0 )
6006 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006007 output_size -= request_size;
6008 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006009 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006010 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006011#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006012}
6013
Gilles Peskine8814fc42020-12-14 15:33:44 +01006014/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006015 *
6016 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6017 * `psa_generate_random(...)`. The state parameter is ignored since the
6018 * PSA API doesn't support passing an explicit state.
6019 *
6020 * In the non-external case, psa_generate_random() calls an
6021 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6022 * and semantics as mbedtls_psa_get_random(). As an optimization,
6023 * instead of doing this back-and-forth between the PSA API and the
6024 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6025 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006026 */
6027#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6028int mbedtls_psa_get_random( void *p_rng,
6029 unsigned char *output,
6030 size_t output_size )
6031{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006032 /* This function takes a pointer to the RNG state because that's what
6033 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6034 * its own state internally and doesn't let the caller access that state.
6035 * So we just ignore the state parameter, and in practice we'll pass
6036 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006037 (void) p_rng;
6038 psa_status_t status = psa_generate_random( output, output_size );
6039 if( status == PSA_SUCCESS )
6040 return( 0 );
6041 else
6042 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6043}
6044#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6045
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006046#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6047#include "mbedtls/entropy_poll.h"
6048
Gilles Peskine7228da22019-07-15 11:06:15 +02006049psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006050 size_t seed_size )
6051{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006052 if( global_data.initialized )
6053 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006054
6055 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6056 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6057 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6058 return( PSA_ERROR_INVALID_ARGUMENT );
6059
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006060 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006061}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006062#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006063
Ronald Cron3772afe2021-02-08 16:10:05 +01006064/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006065 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006066 * \param type The key type
6067 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006068 *
6069 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006070 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006071 * \retval #PSA_ERROR_INVALID_ARGUMENT
6072 * The size in bits of the key is not valid.
6073 * \retval #PSA_ERROR_NOT_SUPPORTED
6074 * The type and/or the size in bits of the key or the combination of
6075 * the two is not supported.
6076 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006077static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6078 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006079{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006080 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006081
Gilles Peskinee59236f2018-01-27 23:32:46 +01006082 if( key_type_is_raw_bytes( type ) )
6083 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02006084 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006085 if( status != PSA_SUCCESS )
6086 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006087 }
6088 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006089#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006090 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6091 {
6092 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6093 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006094
Ronald Cron01b2aba2020-10-05 09:42:02 +02006095 /* Accept only byte-aligned keys, for the same reasons as
6096 * in psa_import_rsa_key(). */
6097 if( bits % 8 != 0 )
6098 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006099 }
6100 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006101#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006102
Ronald Cron5c4d3862020-12-07 11:07:24 +01006103#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006104 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6105 {
Ronald Crond81ab562021-02-16 09:01:16 +01006106 /* To avoid empty block, return successfully here. */
6107 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006108 }
6109 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006110#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006111 {
6112 return( PSA_ERROR_NOT_SUPPORTED );
6113 }
6114
6115 return( PSA_SUCCESS );
6116}
6117
Ronald Cron55ed0592020-10-05 10:30:40 +02006118psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006119 const psa_key_attributes_t *attributes,
6120 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006121{
6122 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006123 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006124
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006125 if( ( attributes->domain_parameters == NULL ) &&
6126 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006127 return( PSA_ERROR_INVALID_ARGUMENT );
6128
Ronald Cron01b2aba2020-10-05 09:42:02 +02006129 if( key_type_is_raw_bytes( type ) )
6130 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006131 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006132 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006133 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006134
David Brown12ca5032021-02-11 11:02:00 -07006135#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006136 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006137 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006138#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006139 }
6140 else
6141
John Durkop07cc04a2020-11-16 22:08:34 -08006142#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006143 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006144 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006145 return( mbedtls_psa_rsa_generate_key( attributes,
6146 key_buffer,
6147 key_buffer_size,
6148 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006149 }
6150 else
John Durkop07cc04a2020-11-16 22:08:34 -08006151#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006152
John Durkop9814fa22020-11-04 12:28:15 -08006153#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006154 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006155 {
Ronald Cron7023db52020-11-20 18:17:42 +01006156 return( mbedtls_psa_ecp_generate_key( attributes,
6157 key_buffer,
6158 key_buffer_size,
6159 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006160 }
6161 else
John Durkop9814fa22020-11-04 12:28:15 -08006162#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006163 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006164 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006165 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006166 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006167
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006168 return( PSA_SUCCESS );
6169}
Darryl Green0c6575a2018-11-07 16:05:30 +00006170
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006171psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006172 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006173{
6174 psa_status_t status;
6175 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006176 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006177 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006178
Ronald Cron81709fc2020-11-14 12:10:32 +01006179 *key = MBEDTLS_SVC_KEY_ID_INIT;
6180
Gilles Peskine0f84d622019-09-12 19:03:13 +02006181 /* Reject any attempt to create a zero-length key so that we don't
6182 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6183 if( psa_get_key_bits( attributes ) == 0 )
6184 return( PSA_ERROR_INVALID_ARGUMENT );
6185
Ronald Cron81709fc2020-11-14 12:10:32 +01006186 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6187 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006188 if( status != PSA_SUCCESS )
6189 goto exit;
6190
Ronald Cron977c2472020-10-13 08:32:21 +02006191 /* In the case of a transparent key or an opaque key stored in local
6192 * storage (thus not in the case of generating a key in a secure element
6193 * or cryptoprocessor with storage), we have to allocate a buffer to
6194 * hold the generated key material. */
6195 if( slot->key.data == NULL )
6196 {
6197 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6198 PSA_KEY_LOCATION_LOCAL_STORAGE )
6199 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006200 status = psa_validate_key_type_and_size_for_key_generation(
6201 attributes->core.type, attributes->core.bits );
6202 if( status != PSA_SUCCESS )
6203 goto exit;
6204
6205 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6206 attributes->core.type,
6207 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006208 }
6209 else
6210 {
6211 status = psa_driver_wrapper_get_key_buffer_size(
6212 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006213 if( status != PSA_SUCCESS )
6214 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006215 }
Ronald Cron977c2472020-10-13 08:32:21 +02006216
6217 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6218 if( status != PSA_SUCCESS )
6219 goto exit;
6220 }
6221
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006222 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006223 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006224
Ronald Cron2b56bc82020-10-05 10:02:26 +02006225 if( status != PSA_SUCCESS )
6226 psa_remove_key_data_from_memory( slot );
6227
Gilles Peskine11792082019-08-06 18:36:36 +02006228exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006229 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006230 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006231 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006232 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006233
Darryl Green0c6575a2018-11-07 16:05:30 +00006234 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006235}
6236
Gilles Peskinee59236f2018-01-27 23:32:46 +01006237/****************************************************************/
6238/* Module setup */
6239/****************************************************************/
6240
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006241#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006242psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6243 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6244 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6245{
6246 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6247 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006248 global_data.rng.entropy_init = entropy_init;
6249 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006250 return( PSA_SUCCESS );
6251}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006252#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006253
Gilles Peskinee59236f2018-01-27 23:32:46 +01006254void mbedtls_psa_crypto_free( void )
6255{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006256 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006257 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6258 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006259 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006260 }
6261 /* Wipe all remaining data, including configuration.
6262 * In particular, this sets all state indicator to the value
6263 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006264 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006265#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006266 /* Unregister all secure element drivers, so that we restart from
6267 * a pristine state. */
6268 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006269#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006270}
6271
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006272#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6273/** Recover a transaction that was interrupted by a power failure.
6274 *
6275 * This function is called during initialization, before psa_crypto_init()
6276 * returns. If this function returns a failure status, the initialization
6277 * fails.
6278 */
6279static psa_status_t psa_crypto_recover_transaction(
6280 const psa_crypto_transaction_t *transaction )
6281{
6282 switch( transaction->unknown.type )
6283 {
6284 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6285 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006286 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006287 * is implemented.
6288 * https://github.com/ARMmbed/mbed-crypto/issues/218
6289 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006290 default:
6291 /* We found an unsupported transaction in the storage.
6292 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006293 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006294 }
6295}
6296#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6297
Gilles Peskinee59236f2018-01-27 23:32:46 +01006298psa_status_t psa_crypto_init( void )
6299{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006300 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006301
Gilles Peskinec6b69072018-11-20 21:42:52 +01006302 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006303 if( global_data.initialized != 0 )
6304 return( PSA_SUCCESS );
6305
Gilles Peskine30524eb2020-11-13 17:02:26 +01006306 /* Initialize and seed the random generator. */
6307 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006308 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006309 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006310 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006311 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006312 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006313
Gilles Peskine66fb1262018-12-10 16:29:04 +01006314 status = psa_initialize_key_slots( );
6315 if( status != PSA_SUCCESS )
6316 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006317
Gilles Peskined9348f22019-10-01 15:22:29 +02006318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6319 status = psa_init_all_se_drivers( );
6320 if( status != PSA_SUCCESS )
6321 goto exit;
6322#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6323
Gilles Peskine4b734222019-07-24 15:56:31 +02006324#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006325 status = psa_crypto_load_transaction( );
6326 if( status == PSA_SUCCESS )
6327 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006328 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6329 if( status != PSA_SUCCESS )
6330 goto exit;
6331 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006332 }
6333 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6334 {
6335 /* There's no transaction to complete. It's all good. */
6336 status = PSA_SUCCESS;
6337 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006338#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006339
Gilles Peskinec6b69072018-11-20 21:42:52 +01006340 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006341 global_data.initialized = 1;
6342
6343exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006344 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006345 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006346 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006347}
6348
6349#endif /* MBEDTLS_PSA_CRYPTO_C */